|
10 | 10 |
|
11 | 11 | import inquirer from 'inquirer'; |
12 | 12 | import { execSync } from 'child_process'; |
13 | | -import { GitExecutor } from '../core/GitExecutor'; |
| 13 | +import { GitExecutor } from '../core/GitExecutor'; |
14 | 14 | import { Logger } from '../utils/Logger'; |
15 | 15 |
|
16 | | - |
17 | 16 | /** |
18 | 17 | * Helper: get list of remote names |
19 | 18 | */ |
@@ -122,18 +121,43 @@ export const RemoteManager = { |
122 | 121 | */ |
123 | 122 | async pushChanges() { |
124 | 123 | const remotes = getRemoteList(); |
125 | | - const { remote } = await inquirer.prompt([ |
126 | | - { |
127 | | - type: 'list', |
128 | | - name: 'remote', |
129 | | - message: 'Select remote to push to:', |
130 | | - choices: remotes.length ? remotes : ['origin'], |
131 | | - }, |
132 | | - ]); |
| 124 | + // Step 1 — If no remotes, ask user to add one |
| 125 | + if (remotes.length === 0) { |
| 126 | + Logger.error('❌ No remote found for this repository.'); |
| 127 | + |
| 128 | + const { url } = await inquirer.prompt([ |
| 129 | + { type: 'input', name: 'url', message: "Enter remote URL to add as 'origin':" }, |
| 130 | + ]); |
| 131 | + |
| 132 | + Logger.info(`🔗 Adding remote origin → ${url}`); |
| 133 | + await GitExecutor.run(`git remote add origin ${url}`); |
| 134 | + remotes.push('origin'); |
| 135 | + } |
| 136 | + |
| 137 | + // Step 2 — Detect current branch |
| 138 | + const currentBranch = execSync('git rev-parse --abbrev-ref HEAD', { encoding: 'utf-8' }).trim(); |
| 139 | + |
| 140 | + // Step 3 — Check if branch has upstream |
| 141 | + let hasUpstream = true; |
| 142 | + try { |
| 143 | + execSync('git rev-parse --abbrev-ref --symbolic-full-name @{u}'); |
| 144 | + } catch { |
| 145 | + hasUpstream = false; |
| 146 | + } |
| 147 | + |
| 148 | + // Step 4 — If no upstream, push -u |
| 149 | + if (!hasUpstream) { |
| 150 | + Logger.info(`🚀 First-time push detected for branch '${currentBranch}'.`); |
| 151 | + Logger.info(`Setting upstream to origin/${currentBranch}...`); |
| 152 | + await GitExecutor.run(`git push -u origin ${currentBranch}`); |
| 153 | + Logger.success('✅ Pushed & upstream tracking set!'); |
| 154 | + return; |
| 155 | + } |
133 | 156 |
|
134 | | - Logger.info(`🚀 Pushing changes to '${remote}'...`); |
135 | | - await GitExecutor.run(`git push ${remote}`); |
136 | | - Logger.success(`✅ Changes pushed to '${remote}'!`); |
| 157 | + // Step 5 — Normal push |
| 158 | + Logger.info(`🚀 Pushing changes to remote...`); |
| 159 | + await GitExecutor.run(`git push`); |
| 160 | + Logger.success('✅ Changes pushed!'); |
137 | 161 | }, |
138 | 162 |
|
139 | 163 | /** |
|
0 commit comments