Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 37 additions & 13 deletions src/managers/RemoteManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@

import inquirer from 'inquirer';
import { execSync } from 'child_process';
import { GitExecutor } from '../core/GitExecutor';
import { GitExecutor } from '../core/GitExecutor';
import { Logger } from '../utils/Logger';


/**
* Helper: get list of remote names
*/
Expand Down Expand Up @@ -122,18 +121,43 @@ export const RemoteManager = {
*/
async pushChanges() {
const remotes = getRemoteList();
const { remote } = await inquirer.prompt([
{
type: 'list',
name: 'remote',
message: 'Select remote to push to:',
choices: remotes.length ? remotes : ['origin'],
},
]);
// Step 1 — If no remotes, ask user to add one
if (remotes.length === 0) {
Logger.error('❌ No remote found for this repository.');

const { url } = await inquirer.prompt([
{ type: 'input', name: 'url', message: "Enter remote URL to add as 'origin':" },
]);

Logger.info(`🔗 Adding remote origin → ${url}`);
await GitExecutor.run(`git remote add origin ${url}`);
remotes.push('origin');
}

// Step 2 — Detect current branch
const currentBranch = execSync('git rev-parse --abbrev-ref HEAD', { encoding: 'utf-8' }).trim();

// Step 3 — Check if branch has upstream
let hasUpstream = true;
try {
execSync('git rev-parse --abbrev-ref --symbolic-full-name @{u}');
} catch {
hasUpstream = false;
}

// Step 4 — If no upstream, push -u
if (!hasUpstream) {
Logger.info(`🚀 First-time push detected for branch '${currentBranch}'.`);
Logger.info(`Setting upstream to origin/${currentBranch}...`);
await GitExecutor.run(`git push -u origin ${currentBranch}`);
Logger.success('✅ Pushed & upstream tracking set!');
return;
}

Logger.info(`🚀 Pushing changes to '${remote}'...`);
await GitExecutor.run(`git push ${remote}`);
Logger.success(`✅ Changes pushed to '${remote}'!`);
// Step 5 — Normal push
Logger.info(`🚀 Pushing changes to remote...`);
await GitExecutor.run(`git push`);
Logger.success('✅ Changes pushed!');
},

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/mocks/inquirerMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,7 @@ export function mockInquirer(answers: Record<string, any>) {
// ignore - tests will fail later if mock not present
}
}

export function clearInquirerQueue() {
_answersQueue.length = 0;
}
Loading