From 331d60520eb3d293ca7c688cf45939d7dcedce04 Mon Sep 17 00:00:00 2001 From: Cheng Liu Date: Fri, 12 Apr 2024 16:15:03 -0700 Subject: [PATCH] fix: duplicate add remote branch --- apps/sparo-lib/src/cli/commands/checkout.ts | 22 ++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/apps/sparo-lib/src/cli/commands/checkout.ts b/apps/sparo-lib/src/cli/commands/checkout.ts index 5aad10d..ad3535b 100644 --- a/apps/sparo-lib/src/cli/commands/checkout.ts +++ b/apps/sparo-lib/src/cli/commands/checkout.ts @@ -237,9 +237,8 @@ export class CheckoutCommand implements ICommand { this._gitService.executeGitCommand({ args: ['branch', branch, `${remote}/${branch}`] }); - this._gitService.executeGitCommand({ - args: ['remote', 'set-branches', '--add', remote, branch] - }); + + this._addRemoteBranchIfNotExists(remote, branch); } const branchExistsInLocal: boolean = Boolean( @@ -279,4 +278,21 @@ export class CheckoutCommand implements ICommand { ); return tagExistsInLocal; } + + private _addRemoteBranchIfNotExists(remote: string, branch: string): void { + const result: string | undefined = this._gitService.getGitConfig(`remote.${remote}.fetch`, { + array: true + }); + const remoteFetchGitConfig: string[] | undefined = result?.split('\n').filter(Boolean); + + // Prevents adding the same remote branch multiple times + const targetConfig: string = `+refs/heads/${branch}:refs/remotes/${remote}/${branch}`; + if (remoteFetchGitConfig?.some((value: string) => value === targetConfig)) { + return; + } + + this._gitService.executeGitCommand({ + args: ['remote', 'set-branches', '--add', remote, branch] + }); + } }