Skip to content

Commit cdec8c9

Browse files
committed
convert deleteBranches into deleteBranch and call multiple times
1 parent 3cb8625 commit cdec8c9

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
@@ -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(new BranchError(ex.reason, ex, state.name).message);
431+
return showGenericErrorMessage(ex.WithBranch(state.name));
432432
}
433433
}
434434
}
@@ -560,17 +560,17 @@ export class BranchGitCommand extends QuickCommand {
560560

561561
endSteps(state);
562562

563-
try {
564-
await state.repo.git.deleteBranches(state.references, {
565-
force: state.flags.includes('--force'),
566-
remote: state.flags.includes('--remotes'),
567-
});
568-
} catch (ex) {
569-
Logger.error(ex);
570-
// TODO likely need some better error handling here
571-
return showGenericErrorMessage(
572-
new BranchError(ex.reason, ex, state.references.map(r => r.name).join(', ')).message,
573-
);
563+
for (const ref of state.references) {
564+
try {
565+
await state.repo.git.deleteBranch(ref, {
566+
force: state.flags.includes('--force'),
567+
remote: state.flags.includes('--remotes'),
568+
});
569+
} catch (ex) {
570+
Logger.error(ex);
571+
// TODO likely need some better error handling here
572+
return showGenericErrorMessage(ex.WithBranch(ref.name));
573+
}
574574
}
575575
}
576576
}
@@ -679,7 +679,7 @@ export class BranchGitCommand extends QuickCommand {
679679
} catch (ex) {
680680
Logger.error(ex);
681681
// TODO likely need some better error handling here
682-
return showGenericErrorMessage(new BranchError(ex.reason, ex, state.name).message);
682+
return showGenericErrorMessage(ex.WithBranch(state.name));
683683
}
684684
}
685685
}

src/env/node/git/git.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,7 @@ export class Git {
10031003
upstream?: string;
10041004
delete?: {
10051005
remote: string;
1006-
branches: string[];
1006+
branch: string;
10071007
};
10081008
},
10091009
): Promise<void> {
@@ -1033,7 +1033,7 @@ export class Git {
10331033
} else if (options.remote) {
10341034
params.push(options.remote);
10351035
} else if (options.delete) {
1036-
params.push('-d', options.delete.remote, ...options.delete.branches);
1036+
params.push('-d', options.delete.remote, options.delete.branch);
10371037
}
10381038

10391039
try {

src/env/node/git/localGitProvider.ts

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

12881288
@log()
1289-
async deleteBranches(
1289+
async deleteBranch(
12901290
repoPath: string,
1291-
branches: GitBranchReference[],
1291+
branch: GitBranchReference,
12921292
options: { force?: boolean; remote?: boolean },
12931293
): Promise<void> {
1294-
const localBranches = branches.filter((b: GitBranchReference) => !b.remote);
1295-
if (localBranches.length !== 0) {
1296-
const args = ['--delete'];
1297-
if (options.force) {
1298-
args.push('--force');
1299-
}
1300-
1301-
if (options.remote) {
1302-
const trackingBranches = localBranches.filter(b => b.upstream != null);
1303-
if (trackingBranches.length !== 0) {
1304-
const branchesByOrigin = groupByMap(trackingBranches, b =>
1305-
getRemoteNameFromBranchName(b.upstream!.name),
1306-
);
1307-
1308-
for (const [remote, branches] of branchesByOrigin.entries()) {
1309-
const remoteCommitByBranch: Map<string, string> = {};
1310-
branches.forEach(async b => {
1311-
remoteCommit = await this.git.rev_list(repoPath, `refs/remotes/${remote}/${b.ref}`, {
1312-
maxResults: 1,
1313-
});
1314-
remoteCommitByBranch[b.ref] = remoteCommit;
1315-
});
1294+
if (branch.remote) {
1295+
return this.git.push(repoPath, {
1296+
delete: {
1297+
remote: getRemoteNameFromBranchName(branch.name),
1298+
branch: branch.remote ? getBranchNameWithoutRemote(branch.name) : branch.name,
1299+
},
1300+
});
1301+
}
13161302

1317-
await this.git.branch(
1318-
repoPath,
1319-
'--delete',
1320-
'--remotes',
1321-
...branches.map((b: GitBranchReference) => `${remote}/${b.ref}`),
1322-
);
1303+
const args = ['--delete'];
1304+
if (options.force) {
1305+
args.push('--force');
1306+
}
13231307

1324-
try {
1325-
await this.git.branch(repoPath, ...args, ...branches.map((b: GitBranchReference) => b.ref));
1326-
await this.git.push(repoPath, {
1327-
delete: {
1328-
remote: remote,
1329-
branches: branches.map(b => getBranchNameWithoutRemote(b.upstream!.name)),
1330-
},
1331-
});
1332-
} catch (ex) {
1333-
// If it fails, restore the remote branches
1334-
remoteCommitByBranch.forEach(async (branch, commit) => {
1335-
await this.git.update_ref(repoPath, `refs/remotes/${remote}/${branch}`, commit);
1336-
await this.git.branch__set_upstream(repoPath, branch, remote, branch);
1337-
});
1338-
throw ex;
1339-
}
1340-
}
1341-
}
1342-
}
1308+
if (!options.remote || !branch.upstream) {
1309+
return this.git.branch(repoPath, ...args, branch.ref);
13431310
}
13441311

1345-
const remoteBranches = branches.filter((b: GitBranchReference) => b.remote);
1346-
if (remoteBranches.length !== 0) {
1347-
const branchesByOrigin = groupByMap(remoteBranches, b => getRemoteNameFromBranchName(b.name));
1312+
const remote = getRemoteNameFromBranchName(branch.upstream.name);
1313+
remoteCommit = await this.git.rev_list(repoPath, `refs/remotes/${remote}/${branch.ref}`, {
1314+
maxResults: 1,
1315+
});
13481316

1349-
for (const [remote, branches] of branchesByOrigin.entries()) {
1350-
await this.git.push(repoPath, {
1351-
delete: {
1352-
remote: remote,
1353-
branches: branches.map((b: GitBranchReference) =>
1354-
b.remote ? getBranchNameWithoutRemote(b.name) : b.name,
1355-
),
1356-
},
1357-
});
1358-
}
1317+
await this.git.branch(repoPath, '--delete', '--remotes', `${remote}/${branch.ref}`);
1318+
1319+
try {
1320+
await this.git.branch(repoPath, ...args, branch.ref);
1321+
} catch (ex) {
1322+
// If it fails, restore the remote branch
1323+
await this.git.update_ref(repoPath, `refs/remotes/${remote}/${branch.ref}`, commit);
1324+
await this.git.branch__set_upstream(repoPath, branch, remote, branch);
1325+
throw ex;
13591326
}
1327+
1328+
await this.git.push(repoPath, {
1329+
delete: {
1330+
remote: remote,
1331+
branch: getBranchNameWithoutRemote(branch.upstream.name),
1332+
},
1333+
});
13601334
}
13611335

13621336
@log()

src/git/gitProvider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ export interface BranchContributorOverview {
120120
export interface GitProviderRepository {
121121
createBranch?(repoPath: string, name: string, ref: string): Promise<void>;
122122
renameBranch?(repoPath: string, oldName: string, newName: string): Promise<void>;
123-
deleteBranches?(
123+
deleteBranch?(
124124
repoPath: string,
125-
branches: GitBranchReference | GitBranchReference[],
125+
branches: GitBranchReference,
126126
options?: { force?: boolean; remote?: boolean },
127127
): Promise<void>;
128128
createTag?(repoPath: string, name: string, ref: string, message?: string): Promise<void>;

src/git/gitProviderService.ts

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

13801380
@log()
1381-
deleteBranches(
1381+
deleteBranch(
13821382
repoPath: string,
1383-
branches: GitBranchReference | GitBranchReference[],
1383+
branch: GitBranchReference,
13841384
options?: { force?: boolean; remote?: boolean },
13851385
): Promise<void> {
13861386
const { provider, path } = this.getProvider(repoPath);
1387-
if (provider.deleteBranches == null) throw new ProviderNotSupportedError(provider.descriptor.name);
1387+
if (provider.deleteBranch == null) throw new ProviderNotSupportedError(provider.descriptor.name);
13881388

1389-
if (!Array.isArray(branches)) {
1390-
branches = [branches];
1391-
}
1392-
1393-
return provider.deleteBranches(path, branches, {
1389+
return provider.deleteBranch(path, branch, {
13941390
force: options?.force,
13951391
remote: options?.remote,
13961392
});

0 commit comments

Comments
 (0)