Skip to content

Commit d05d24a

Browse files
Merge pull request #5 from prakashaditya13/develop
Merge the latest remoteManager
2 parents 00e4597 + 28e7889 commit d05d24a

File tree

3 files changed

+203
-110
lines changed

3 files changed

+203
-110
lines changed

src/managers/RemoteManager.ts

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010

1111
import inquirer from 'inquirer';
1212
import { execSync } from 'child_process';
13-
import { GitExecutor } from '../core/GitExecutor';
13+
import { GitExecutor } from '../core/GitExecutor';
1414
import { Logger } from '../utils/Logger';
1515

16-
1716
/**
1817
* Helper: get list of remote names
1918
*/
@@ -122,18 +121,43 @@ export const RemoteManager = {
122121
*/
123122
async pushChanges() {
124123
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+
}
133156

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!');
137161
},
138162

139163
/**

tests/mocks/inquirerMock.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,7 @@ export function mockInquirer(answers: Record<string, any>) {
4949
// ignore - tests will fail later if mock not present
5050
}
5151
}
52+
53+
export function clearInquirerQueue() {
54+
_answersQueue.length = 0;
55+
}

0 commit comments

Comments
 (0)