Skip to content

Commit 0d3796c

Browse files
committed
handle BranchError in localGitProvider
1 parent a5905d5 commit 0d3796c

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
@@ -428,7 +428,7 @@ export class BranchGitCommand extends QuickCommand {
428428
} catch (ex) {
429429
Logger.error(ex);
430430
// TODO likely need some better error handling here
431-
return showGenericErrorMessage(ex.WithBranch(state.name));
431+
return showGenericErrorMessage(ex);
432432
}
433433
}
434434
}
@@ -552,9 +552,9 @@ export class BranchGitCommand extends QuickCommand {
552552
remote: state.flags.includes('--remotes'),
553553
});
554554
} catch (ex) {
555-
Logger.error(ex);
556555
// TODO likely need some better error handling here
557-
return showGenericErrorMessage(ex.WithBranch(ref.name));
556+
Logger.error(ex);
557+
return showGenericErrorMessage(ex);
558558
}
559559
}
560560
}
@@ -664,7 +664,7 @@ export class BranchGitCommand extends QuickCommand {
664664
} catch (ex) {
665665
Logger.error(ex);
666666
// TODO likely need some better error handling here
667-
return showGenericErrorMessage(ex.WithBranch(state.name));
667+
return showGenericErrorMessage(ex);
668668
}
669669
}
670670
}

src/env/node/git/localGitProvider.ts

+64-47
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,
@@ -188,17 +189,7 @@ import { countStringLength, filterMap } from '../../../system/array';
188189
import { gate } from '../../../system/decorators/gate';
189190
import { debug, log } from '../../../system/decorators/log';
190191
import { debounce } from '../../../system/function';
191-
import {
192-
filterMap as filterMapIterable,
193-
find,
194-
first,
195-
groupByMap,
196-
join,
197-
last,
198-
map,
199-
skip,
200-
some,
201-
} from '../../../system/iterable';
192+
import { filterMap as filterMapIterable, find, first, join, last, map, skip, some } from '../../../system/iterable';
202193
import { Logger } from '../../../system/logger';
203194
import type { LogScope } from '../../../system/logger.scope';
204195
import { getLogScope, setLogScopeExit } from '../../../system/logger.scope';
@@ -1265,13 +1256,29 @@ export class LocalGitProvider implements GitProvider, Disposable {
12651256
}
12661257

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

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

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

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

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

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

1306-
await this.git.branch(repoPath, '--delete', '--remotes', `${remote}/${branch.ref}`);
1316+
await this.git.branch(repoPath, '--delete', '--remotes', `${remote}/${branch.ref}`);
13071317

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

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