fix: duplicate add remote branch

This commit is contained in:
Cheng Liu 2024-04-12 16:15:03 -07:00
parent b25a722b85
commit 331d60520e
No known key found for this signature in database
GPG key ID: EEC8452F7DB85CD6

View file

@ -237,9 +237,8 @@ export class CheckoutCommand implements ICommand<ICheckoutCommandOptions> {
this._gitService.executeGitCommand({ this._gitService.executeGitCommand({
args: ['branch', branch, `${remote}/${branch}`] args: ['branch', branch, `${remote}/${branch}`]
}); });
this._gitService.executeGitCommand({
args: ['remote', 'set-branches', '--add', remote, branch] this._addRemoteBranchIfNotExists(remote, branch);
});
} }
const branchExistsInLocal: boolean = Boolean( const branchExistsInLocal: boolean = Boolean(
@ -279,4 +278,21 @@ export class CheckoutCommand implements ICommand<ICheckoutCommandOptions> {
); );
return tagExistsInLocal; 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]
});
}
} }