Skip to content

Commit b3ee512

Browse files
committed
handle BranchError in localGitProvider
1 parent 7b8759b commit b3ee512

File tree

3 files changed

+69
-52
lines changed

3 files changed

+69
-52
lines changed

src/commands/git/branch.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ export class BranchGitCommand extends QuickCommand {
405405
} catch (ex) {
406406
Logger.error(ex);
407407
// TODO likely need some better error handling here
408-
return showGenericErrorMessage(ex.WithBranch(state.name));
408+
return showGenericErrorMessage(ex);
409409
}
410410
}
411411
}
@@ -529,9 +529,9 @@ export class BranchGitCommand extends QuickCommand {
529529
remote: state.flags.includes('--remotes'),
530530
});
531531
} catch (ex) {
532-
Logger.error(ex);
533532
// TODO likely need some better error handling here
534-
return showGenericErrorMessage(ex.WithBranch(ref.name));
533+
Logger.error(ex);
534+
return showGenericErrorMessage(ex);
535535
}
536536
}
537537
}
@@ -641,7 +641,7 @@ export class BranchGitCommand extends QuickCommand {
641641
} catch (ex) {
642642
Logger.error(ex);
643643
// TODO likely need some better error handling here
644-
return showGenericErrorMessage(ex.WithBranch(state.name));
644+
return showGenericErrorMessage(ex);
645645
}
646646
}
647647
}

src/env/node/git/localGitProvider.ts

+64-47
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
ApplyPatchCommitErrorReason,
2424
BlameIgnoreRevsFileBadRevisionError,
2525
BlameIgnoreRevsFileError,
26+
BranchError,
2627
CherryPickError,
2728
CherryPickErrorReason,
2829
FetchError,
@@ -182,17 +183,7 @@ import { countStringLength, filterMap } from '../../../system/array';
182183
import { gate } from '../../../system/decorators/gate';
183184
import { debug, log } from '../../../system/decorators/log';
184185
import { debounce } from '../../../system/function';
185-
import {
186-
filterMap as filterMapIterable,
187-
find,
188-
first,
189-
groupByMap,
190-
join,
191-
last,
192-
map,
193-
skip,
194-
some,
195-
} from '../../../system/iterable';
186+
import { filterMap as filterMapIterable, find, first, join, last, map, skip, some } from '../../../system/iterable';
196187
import { Logger } from '../../../system/logger';
197188
import type { LogScope } from '../../../system/logger.scope';
198189
import { getLogScope, setLogScopeExit } from '../../../system/logger.scope';
@@ -1241,13 +1232,29 @@ export class LocalGitProvider implements GitProvider, Disposable {
12411232
}
12421233

12431234
@log()
1244-
async createBranch(repoPath: string, name: string, ref: string): Promise<void> {
1245-
await this.git.branch(repoPath, name, ref);
1235+
createBranch(repoPath: string, name: string, ref: string): Promise<void> {
1236+
try {
1237+
return void this.git.branch(repoPath, name, ref);
1238+
} catch (ex) {
1239+
if (ex instanceof BranchError) {
1240+
throw ex.WithBranch(branch.name);
1241+
}
1242+
1243+
throw ex;
1244+
}
12461245
}
12471246

12481247
@log()
1249-
async renameBranch(repoPath: string, oldName: string, newName: string): Promise<void> {
1250-
await this.git.branch(repoPath, '-m', oldName, newName);
1248+
renameBranch(repoPath: string, oldName: string, newName: string): Promise<void> {
1249+
try {
1250+
return void this.git.branch(repoPath, '-m', oldName, newName);
1251+
} catch (ex) {
1252+
if (ex instanceof BranchError) {
1253+
throw ex.WithBranch(branch.name);
1254+
}
1255+
1256+
throw ex;
1257+
}
12511258
}
12521259

12531260
@log()
@@ -1256,46 +1263,56 @@ export class LocalGitProvider implements GitProvider, Disposable {
12561263
branch: GitBranchReference,
12571264
options: { force?: boolean; remote?: boolean },
12581265
): Promise<void> {
1259-
if (branch.remote) {
1260-
return this.git.push(repoPath, {
1261-
delete: {
1262-
remote: getRemoteNameFromBranchName(branch.name),
1263-
branch: branch.remote ? getBranchNameWithoutRemote(branch.name) : branch.name,
1264-
},
1265-
});
1266-
}
1266+
try {
1267+
if (branch.remote) {
1268+
await this.git.push(repoPath, {
1269+
delete: {
1270+
remote: getRemoteNameFromBranchName(branch.name),
1271+
branch: branch.remote ? getBranchNameWithoutRemote(branch.name) : branch.name,
1272+
},
1273+
});
1274+
return;
1275+
}
12671276

1268-
const args = ['--delete'];
1269-
if (options.force) {
1270-
args.push('--force');
1271-
}
1277+
const args = ['--delete'];
1278+
if (options.force) {
1279+
args.push('--force');
1280+
}
12721281

1273-
if (!options.remote || !branch.upstream) {
1274-
return this.git.branch(repoPath, ...args, branch.ref);
1275-
}
1282+
if (!options.remote || !branch.upstream) {
1283+
await this.git.branch(repoPath, ...args, branch.ref);
1284+
return;
1285+
}
12761286

1277-
const remote = getRemoteNameFromBranchName(branch.upstream.name);
1278-
remoteCommit = await this.git.rev_list(repoPath, `refs/remotes/${remote}/${branch.ref}`, {
1279-
maxResults: 1,
1280-
});
1287+
const remote = getRemoteNameFromBranchName(branch.upstream.name);
1288+
remoteCommit = await this.git.rev_list(repoPath, `refs/remotes/${remote}/${branch.ref}`, {
1289+
maxResults: 1,
1290+
});
12811291

1282-
await this.git.branch(repoPath, '--delete', '--remotes', `${remote}/${branch.ref}`);
1292+
await this.git.branch(repoPath, '--delete', '--remotes', `${remote}/${branch.ref}`);
12831293

1284-
try {
1285-
await this.git.branch(repoPath, ...args, branch.ref);
1294+
try {
1295+
await this.git.branch(repoPath, ...args, branch.ref);
1296+
} catch (ex) {
1297+
// If it fails, restore the remote branch
1298+
await this.git.update_ref(repoPath, `refs/remotes/${remote}/${branch.ref}`, commit);
1299+
await this.git.branch__set_upstream(repoPath, branch, remote, branch);
1300+
throw ex;
1301+
}
1302+
1303+
await this.git.push(repoPath, {
1304+
delete: {
1305+
remote: remote,
1306+
branch: getBranchNameWithoutRemote(branch.upstream.name),
1307+
},
1308+
});
12861309
} catch (ex) {
1287-
// If it fails, restore the remote branch
1288-
await this.git.update_ref(repoPath, `refs/remotes/${remote}/${branch.ref}`, commit);
1289-
await this.git.branch__set_upstream(repoPath, branch, remote, branch);
1310+
if (ex instanceof BranchError) {
1311+
throw ex.WithBranch(branch.name);
1312+
}
1313+
12901314
throw ex;
12911315
}
1292-
1293-
await this.git.push(repoPath, {
1294-
delete: {
1295-
remote: remote,
1296-
branch: getBranchNameWithoutRemote(branch.upstream.name),
1297-
},
1298-
});
12991316
}
13001317

13011318
@log()

src/git/errors.ts

+1-1
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)