Skip to content

Commit 1d7b456

Browse files
committed
convert deleteBranches into deleteBranch and call multiple times
1 parent c55dede commit 1d7b456

File tree

5 files changed

+57
-87
lines changed

5 files changed

+57
-87
lines changed

src/commands/git/branch.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ export class BranchGitCommand extends QuickCommand {
401401
} catch (ex) {
402402
Logger.error(ex);
403403
// TODO likely need some better error handling here
404-
return showGenericErrorMessage(new BranchError(ex.reason, ex, state.name).message);
404+
return showGenericErrorMessage(ex.WithBranch(state.name));
405405
}
406406
}
407407
}
@@ -518,17 +518,17 @@ export class BranchGitCommand extends QuickCommand {
518518

519519
endSteps(state);
520520

521-
try {
522-
await state.repo.git.deleteBranches(state.references, {
523-
force: state.flags.includes('--force'),
524-
remote: state.flags.includes('--remotes'),
525-
});
526-
} catch (ex) {
527-
Logger.error(ex);
528-
// TODO likely need some better error handling here
529-
return showGenericErrorMessage(
530-
new BranchError(ex.reason, ex, state.references.map(r => r.name).join(', ')).message,
531-
);
521+
for (const ref of state.references) {
522+
try {
523+
await state.repo.git.deleteBranch(ref, {
524+
force: state.flags.includes('--force'),
525+
remote: state.flags.includes('--remotes'),
526+
});
527+
} catch (ex) {
528+
Logger.error(ex);
529+
// TODO likely need some better error handling here
530+
return showGenericErrorMessage(ex.WithBranch(ref.name));
531+
}
532532
}
533533
}
534534
}
@@ -637,7 +637,7 @@ export class BranchGitCommand extends QuickCommand {
637637
} catch (ex) {
638638
Logger.error(ex);
639639
// TODO likely need some better error handling here
640-
return showGenericErrorMessage(new BranchError(ex.reason, ex, state.name).message);
640+
return showGenericErrorMessage(ex.WithBranch(state.name));
641641
}
642642
}
643643
}

src/env/node/git/git.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ export class Git {
987987
upstream?: string;
988988
delete?: {
989989
remote: string;
990-
branches: string[];
990+
branch: string;
991991
};
992992
},
993993
): Promise<void> {
@@ -1017,7 +1017,7 @@ export class Git {
10171017
} else if (options.remote) {
10181018
params.push(options.remote);
10191019
} else if (options.delete) {
1020-
params.push('-d', options.delete.remote, ...options.delete.branches);
1020+
params.push('-d', options.delete.remote, options.delete.branch);
10211021
}
10221022

10231023
try {

src/env/node/git/localGitProvider.ts

Lines changed: 36 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,77 +1251,51 @@ export class LocalGitProvider implements GitProvider, Disposable {
12511251
}
12521252

12531253
@log()
1254-
async deleteBranches(
1254+
async deleteBranch(
12551255
repoPath: string,
1256-
branches: GitBranchReference[],
1256+
branch: GitBranchReference,
12571257
options: { force?: boolean; remote?: boolean },
12581258
): Promise<void> {
1259-
const localBranches = branches.filter((b: GitBranchReference) => !b.remote);
1260-
if (localBranches.length !== 0) {
1261-
const args = ['--delete'];
1262-
if (options.force) {
1263-
args.push('--force');
1264-
}
1265-
1266-
if (options.remote) {
1267-
const trackingBranches = localBranches.filter(b => b.upstream != null);
1268-
if (trackingBranches.length !== 0) {
1269-
const branchesByOrigin = groupByMap(trackingBranches, b =>
1270-
getRemoteNameFromBranchName(b.upstream!.name),
1271-
);
1272-
1273-
for (const [remote, branches] of branchesByOrigin.entries()) {
1274-
const remoteCommitByBranch: Map<string, string> = {};
1275-
branches.forEach(async b => {
1276-
remoteCommit = await this.git.rev_list(repoPath, `refs/remotes/${remote}/${b.ref}`, {
1277-
maxResults: 1,
1278-
});
1279-
remoteCommitByBranch[b.ref] = remoteCommit;
1280-
});
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+
}
12811267

1282-
await this.git.branch(
1283-
repoPath,
1284-
'--delete',
1285-
'--remotes',
1286-
...branches.map((b: GitBranchReference) => `${remote}/${b.ref}`),
1287-
);
1268+
const args = ['--delete'];
1269+
if (options.force) {
1270+
args.push('--force');
1271+
}
12881272

1289-
try {
1290-
await this.git.branch(repoPath, ...args, ...branches.map((b: GitBranchReference) => b.ref));
1291-
await this.git.push(repoPath, {
1292-
delete: {
1293-
remote: remote,
1294-
branches: branches.map(b => getBranchNameWithoutRemote(b.upstream!.name)),
1295-
},
1296-
});
1297-
} catch (ex) {
1298-
// If it fails, restore the remote branches
1299-
remoteCommitByBranch.forEach(async (branch, commit) => {
1300-
await this.git.update_ref(repoPath, `refs/remotes/${remote}/${branch}`, commit);
1301-
await this.git.branch__set_upstream(repoPath, branch, remote, branch);
1302-
});
1303-
throw ex;
1304-
}
1305-
}
1306-
}
1307-
}
1273+
if (!options.remote || !branch.upstream) {
1274+
return this.git.branch(repoPath, ...args, branch.ref);
13081275
}
13091276

1310-
const remoteBranches = branches.filter((b: GitBranchReference) => b.remote);
1311-
if (remoteBranches.length !== 0) {
1312-
const branchesByOrigin = groupByMap(remoteBranches, b => getRemoteNameFromBranchName(b.name));
1277+
const remote = getRemoteNameFromBranchName(branch.upstream.name);
1278+
remoteCommit = await this.git.rev_list(repoPath, `refs/remotes/${remote}/${branch.ref}`, {
1279+
maxResults: 1,
1280+
});
13131281

1314-
for (const [remote, branches] of branchesByOrigin.entries()) {
1315-
await this.git.push(repoPath, {
1316-
delete: {
1317-
remote: remote,
1318-
branches: branches.map((b: GitBranchReference) =>
1319-
b.remote ? getBranchNameWithoutRemote(b.name) : b.name,
1320-
),
1321-
},
1322-
});
1323-
}
1282+
await this.git.branch(repoPath, '--delete', '--remotes', `${remote}/${branch.ref}`);
1283+
1284+
try {
1285+
await this.git.branch(repoPath, ...args, branch.ref);
1286+
} 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);
1290+
throw ex;
13241291
}
1292+
1293+
await this.git.push(repoPath, {
1294+
delete: {
1295+
remote: remote,
1296+
branch: getBranchNameWithoutRemote(branch.upstream.name),
1297+
},
1298+
});
13251299
}
13261300

13271301
@log()

src/git/gitProvider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ export interface RepositoryVisibilityInfo {
115115
export interface GitProviderRepository {
116116
createBranch?(repoPath: string, name: string, ref: string): Promise<void>;
117117
renameBranch?(repoPath: string, oldName: string, newName: string): Promise<void>;
118-
deleteBranches?(
118+
deleteBranch?(
119119
repoPath: string,
120-
branches: GitBranchReference | GitBranchReference[],
120+
branches: GitBranchReference,
121121
options?: { force?: boolean; remote?: boolean },
122122
): Promise<void>;
123123
addRemote?(repoPath: string, name: string, url: string, options?: { fetch?: boolean }): Promise<void>;

src/git/gitProviderService.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,19 +1355,15 @@ export class GitProviderService implements Disposable {
13551355
}
13561356

13571357
@log()
1358-
deleteBranches(
1358+
deleteBranch(
13591359
repoPath: string,
1360-
branches: GitBranchReference | GitBranchReference[],
1360+
branch: GitBranchReference,
13611361
options?: { force?: boolean; remote?: boolean },
13621362
): Promise<void> {
13631363
const { provider, path } = this.getProvider(repoPath);
1364-
if (provider.deleteBranches == null) throw new ProviderNotSupportedError(provider.descriptor.name);
1364+
if (provider.deleteBranch == null) throw new ProviderNotSupportedError(provider.descriptor.name);
13651365

1366-
if (!Array.isArray(branches)) {
1367-
branches = [branches];
1368-
}
1369-
1370-
return provider.deleteBranches(path, branches, {
1366+
return provider.deleteBranch(path, branch, {
13711367
force: options?.force,
13721368
remote: options?.remote,
13731369
});

0 commit comments

Comments
 (0)