Skip to content

Commit e884949

Browse files
committed
handle BranchError in localGitProvider
1 parent 58d68b3 commit e884949

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,
@@ -185,17 +186,7 @@ import { countStringLength, filterMap } from '../../../system/array';
185186
import { gate } from '../../../system/decorators/gate';
186187
import { debug, log } from '../../../system/decorators/log';
187188
import { debounce } from '../../../system/function';
188-
import {
189-
filterMap as filterMapIterable,
190-
find,
191-
first,
192-
groupByMap,
193-
join,
194-
last,
195-
map,
196-
skip,
197-
some,
198-
} from '../../../system/iterable';
189+
import { filterMap as filterMapIterable, find, first, join, last, map, skip, some } from '../../../system/iterable';
199190
import { Logger } from '../../../system/logger';
200191
import type { LogScope } from '../../../system/logger.scope';
201192
import { getLogScope, setLogScopeExit } from '../../../system/logger.scope';
@@ -1262,13 +1253,29 @@ export class LocalGitProvider implements GitProvider, Disposable {
12621253
}
12631254

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

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

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

1289-
const args = ['--delete'];
1290-
if (options.force) {
1291-
args.push('--force');
1292-
}
1298+
const args = ['--delete'];
1299+
if (options.force) {
1300+
args.push('--force');
1301+
}
12931302

1294-
if (!options.remote || !branch.upstream) {
1295-
return this.git.branch(repoPath, ...args, branch.ref);
1296-
}
1303+
if (!options.remote || !branch.upstream) {
1304+
await this.git.branch(repoPath, ...args, branch.ref);
1305+
return;
1306+
}
12971307

1298-
const remote = getRemoteNameFromBranchName(branch.upstream.name);
1299-
remoteCommit = await this.git.rev_list(repoPath, `refs/remotes/${remote}/${branch.ref}`, {
1300-
maxResults: 1,
1301-
});
1308+
const remote = getRemoteNameFromBranchName(branch.upstream.name);
1309+
remoteCommit = await this.git.rev_list(repoPath, `refs/remotes/${remote}/${branch.ref}`, {
1310+
maxResults: 1,
1311+
});
13021312

1303-
await this.git.branch(repoPath, '--delete', '--remotes', `${remote}/${branch.ref}`);
1313+
await this.git.branch(repoPath, '--delete', '--remotes', `${remote}/${branch.ref}`);
13041314

1305-
try {
1306-
await this.git.branch(repoPath, ...args, branch.ref);
1315+
try {
1316+
await this.git.branch(repoPath, ...args, branch.ref);
1317+
} catch (ex) {
1318+
// If it fails, restore the remote branch
1319+
await this.git.update_ref(repoPath, `refs/remotes/${remote}/${branch.ref}`, commit);
1320+
await this.git.branch__set_upstream(repoPath, branch, remote, branch);
1321+
throw ex;
1322+
}
1323+
1324+
await this.git.push(repoPath, {
1325+
delete: {
1326+
remote: remote,
1327+
branch: getBranchNameWithoutRemote(branch.upstream.name),
1328+
},
1329+
});
13071330
} catch (ex) {
1308-
// If it fails, restore the remote branch
1309-
await this.git.update_ref(repoPath, `refs/remotes/${remote}/${branch.ref}`, commit);
1310-
await this.git.branch__set_upstream(repoPath, branch, remote, branch);
1331+
if (ex instanceof BranchError) {
1332+
throw ex.WithBranch(branch.name);
1333+
}
1334+
13111335
throw ex;
13121336
}
1313-
1314-
await this.git.push(repoPath, {
1315-
delete: {
1316-
remote: remote,
1317-
branch: getBranchNameWithoutRemote(branch.upstream.name),
1318-
},
1319-
});
13201337
}
13211338

13221339
@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)