Skip to content

Commit 3f0de87

Browse files
committed
Add hasUpstreamBranch method and auto-pull functionality when checking out branches with upstream
1 parent b53f484 commit 3f0de87

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

src/commands/checkoutToCommand/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,9 @@ export class CheckoutToCommand extends BaseCommand {
331331
default:
332332
try {
333333
await git.checkout(newBranchName);
334+
if (await git.hasUpstreamBranch(newBranchName)) {
335+
await git.pullCurrentBranch();
336+
}
334337
} catch (e) {
335338
throw new Error('Failed to checkout the selected branch.');
336339
}
@@ -363,6 +366,9 @@ export class CheckoutToCommand extends BaseCommand {
363366

364367
try {
365368
await git.checkout(newBranch);
369+
if (await git.hasUpstreamBranch(newBranch)) {
370+
await git.pullCurrentBranch();
371+
}
366372
} catch (e) {
367373
throw new Error('Failed to checkout the selected branch.');
368374
}
@@ -417,6 +423,9 @@ export class CheckoutToCommand extends BaseCommand {
417423
// Checkout the selected branch
418424
try {
419425
await git.checkout(newBranch);
426+
if (await git.hasUpstreamBranch(newBranch)) {
427+
await git.pullCurrentBranch();
428+
}
420429
} catch (e) {
421430
throw new Error('Failed to checkout the selected branch.');
422431
}

src/common/git/gitExecutor.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,17 @@ export class GitExecutor {
464464
return await this.#checkRemoteBranchExists(branchName);
465465
}
466466

467+
async hasUpstreamBranch(branchName: string): Promise<boolean> {
468+
const command = `git rev-parse --abbrev-ref ${branchName}@{upstream}`;
469+
470+
try {
471+
await this.#execGitCommand(command);
472+
return true;
473+
} catch {
474+
return false;
475+
}
476+
}
477+
467478
async pushBranchToGitHub(branchName: string): Promise<void> {
468479
const command = `git push -u origin ${branchName}`;
469480

0 commit comments

Comments
 (0)