Skip to content

Commit f2b40cd

Browse files
committed
handle BranchError in localGitProvider
1 parent 68fa42b commit f2b40cd

File tree

3 files changed

+69
-52
lines changed

3 files changed

+69
-52
lines changed

src/commands/git/branch.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ export class BranchGitCommand extends QuickCommand {
414414
} catch (ex) {
415415
Logger.error(ex);
416416
// TODO likely need some better error handling here
417-
return showGenericErrorMessage(ex.WithBranch(state.name));
417+
return showGenericErrorMessage(ex);
418418
}
419419
}
420420
}
@@ -538,9 +538,9 @@ export class BranchGitCommand extends QuickCommand {
538538
remote: state.flags.includes('--remotes'),
539539
});
540540
} catch (ex) {
541-
Logger.error(ex);
542541
// TODO likely need some better error handling here
543-
return showGenericErrorMessage(ex.WithBranch(ref.name));
542+
Logger.error(ex);
543+
return showGenericErrorMessage(ex);
544544
}
545545
}
546546
}
@@ -650,7 +650,7 @@ export class BranchGitCommand extends QuickCommand {
650650
} catch (ex) {
651651
Logger.error(ex);
652652
// TODO likely need some better error handling here
653-
return showGenericErrorMessage(ex.WithBranch(state.name));
653+
return showGenericErrorMessage(ex);
654654
}
655655
}
656656
}

src/env/node/git/localGitProvider.ts

Lines changed: 64 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
ApplyPatchCommitErrorReason,
2525
BlameIgnoreRevsFileBadRevisionError,
2626
BlameIgnoreRevsFileError,
27+
BranchError,
2728
CherryPickError,
2829
CherryPickErrorReason,
2930
FetchError,
@@ -187,17 +188,7 @@ import { countStringLength, filterMap } from '../../../system/array';
187188
import { gate } from '../../../system/decorators/gate';
188189
import { debug, log } from '../../../system/decorators/log';
189190
import { debounce } from '../../../system/function';
190-
import {
191-
filterMap as filterMapIterable,
192-
find,
193-
first,
194-
groupByMap,
195-
join,
196-
last,
197-
map,
198-
skip,
199-
some,
200-
} from '../../../system/iterable';
191+
import { filterMap as filterMapIterable, find, first, join, last, map, skip, some } from '../../../system/iterable';
201192
import { Logger } from '../../../system/logger';
202193
import type { LogScope } from '../../../system/logger.scope';
203194
import { getLogScope, setLogScopeExit } from '../../../system/logger.scope';
@@ -1264,13 +1255,29 @@ export class LocalGitProvider implements GitProvider, Disposable {
12641255
}
12651256

12661257
@log()
1267-
async createBranch(repoPath: string, name: string, ref: string): Promise<void> {
1268-
await this.git.branch(repoPath, name, ref);
1258+
createBranch(repoPath: string, name: string, ref: string): Promise<void> {
1259+
try {
1260+
return void this.git.branch(repoPath, name, ref);
1261+
} catch (ex) {
1262+
if (ex instanceof BranchError) {
1263+
throw ex.WithBranch(branch.name);
1264+
}
1265+
1266+
throw ex;
1267+
}
12691268
}
12701269

12711270
@log()
1272-
async renameBranch(repoPath: string, oldName: string, newName: string): Promise<void> {
1273-
await this.git.branch(repoPath, '-m', oldName, newName);
1271+
renameBranch(repoPath: string, oldName: string, newName: string): Promise<void> {
1272+
try {
1273+
return void this.git.branch(repoPath, '-m', oldName, newName);
1274+
} catch (ex) {
1275+
if (ex instanceof BranchError) {
1276+
throw ex.WithBranch(branch.name);
1277+
}
1278+
1279+
throw ex;
1280+
}
12741281
}
12751282

12761283
@log()
@@ -1279,46 +1286,56 @@ export class LocalGitProvider implements GitProvider, Disposable {
12791286
branch: GitBranchReference,
12801287
options: { force?: boolean; remote?: boolean },
12811288
): Promise<void> {
1282-
if (branch.remote) {
1283-
return this.git.push(repoPath, {
1284-
delete: {
1285-
remote: getRemoteNameFromBranchName(branch.name),
1286-
branch: branch.remote ? getBranchNameWithoutRemote(branch.name) : branch.name,
1287-
},
1288-
});
1289-
}
1289+
try {
1290+
if (branch.remote) {
1291+
await this.git.push(repoPath, {
1292+
delete: {
1293+
remote: getRemoteNameFromBranchName(branch.name),
1294+
branch: branch.remote ? getBranchNameWithoutRemote(branch.name) : branch.name,
1295+
},
1296+
});
1297+
return;
1298+
}
12901299

1291-
const args = ['--delete'];
1292-
if (options.force) {
1293-
args.push('--force');
1294-
}
1300+
const args = ['--delete'];
1301+
if (options.force) {
1302+
args.push('--force');
1303+
}
12951304

1296-
if (!options.remote || !branch.upstream) {
1297-
return this.git.branch(repoPath, ...args, branch.ref);
1298-
}
1305+
if (!options.remote || !branch.upstream) {
1306+
await this.git.branch(repoPath, ...args, branch.ref);
1307+
return;
1308+
}
12991309

1300-
const remote = getRemoteNameFromBranchName(branch.upstream.name);
1301-
remoteCommit = await this.git.rev_list(repoPath, `refs/remotes/${remote}/${branch.ref}`, {
1302-
maxResults: 1,
1303-
});
1310+
const remote = getRemoteNameFromBranchName(branch.upstream.name);
1311+
remoteCommit = await this.git.rev_list(repoPath, `refs/remotes/${remote}/${branch.ref}`, {
1312+
maxResults: 1,
1313+
});
13041314

1305-
await this.git.branch(repoPath, '--delete', '--remotes', `${remote}/${branch.ref}`);
1315+
await this.git.branch(repoPath, '--delete', '--remotes', `${remote}/${branch.ref}`);
13061316

1307-
try {
1308-
await this.git.branch(repoPath, ...args, branch.ref);
1317+
try {
1318+
await this.git.branch(repoPath, ...args, branch.ref);
1319+
} catch (ex) {
1320+
// If it fails, restore the remote branch
1321+
await this.git.update_ref(repoPath, `refs/remotes/${remote}/${branch.ref}`, commit);
1322+
await this.git.branch__set_upstream(repoPath, branch, remote, branch);
1323+
throw ex;
1324+
}
1325+
1326+
await this.git.push(repoPath, {
1327+
delete: {
1328+
remote: remote,
1329+
branch: getBranchNameWithoutRemote(branch.upstream.name),
1330+
},
1331+
});
13091332
} catch (ex) {
1310-
// If it fails, restore the remote branch
1311-
await this.git.update_ref(repoPath, `refs/remotes/${remote}/${branch.ref}`, commit);
1312-
await this.git.branch__set_upstream(repoPath, branch, remote, branch);
1333+
if (ex instanceof BranchError) {
1334+
throw ex.WithBranch(branch.name);
1335+
}
1336+
13131337
throw ex;
13141338
}
1315-
1316-
await this.git.push(repoPath, {
1317-
delete: {
1318-
remote: remote,
1319-
branch: getBranchNameWithoutRemote(branch.upstream.name),
1320-
},
1321-
});
13221339
}
13231340

13241341
@log()

src/git/errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ export class BranchError extends Error {
252252
}
253253

254254
private static buildBranchErrorMessage(reason?: BranchErrorReason, branch?: string): string {
255-
const baseMessage = `Unable to perform action on branch${branch ? ` '${branch}'` : ''}`;
255+
const baseMessage = `Unable to perform action ${branch ? `with branch '${branch}'` : 'on branch'}`;
256256
switch (reason) {
257257
case BranchErrorReason.BranchAlreadyExists:
258258
return `${baseMessage} because it already exists`;

0 commit comments

Comments
 (0)