Skip to content

Commit 65d86ab

Browse files
committed
convert deleteBranches into deleteBranch and call multiple times
1 parent 9cd79f4 commit 65d86ab

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
@@ -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(new BranchError(ex.reason, ex, state.name).message);
408+
return showGenericErrorMessage(ex.WithBranch(state.name));
409409
}
410410
}
411411
}
@@ -522,17 +522,17 @@ export class BranchGitCommand extends QuickCommand {
522522

523523
endSteps(state);
524524

525-
try {
526-
await state.repo.git.deleteBranches(state.references, {
527-
force: state.flags.includes('--force'),
528-
remote: state.flags.includes('--remotes'),
529-
});
530-
} catch (ex) {
531-
Logger.error(ex);
532-
// TODO likely need some better error handling here
533-
return showGenericErrorMessage(
534-
new BranchError(ex.reason, ex, state.references.map(r => r.name).join(', ')).message,
535-
);
525+
for (const ref of state.references) {
526+
try {
527+
await state.repo.git.deleteBranch(ref, {
528+
force: state.flags.includes('--force'),
529+
remote: state.flags.includes('--remotes'),
530+
});
531+
} catch (ex) {
532+
Logger.error(ex);
533+
// TODO likely need some better error handling here
534+
return showGenericErrorMessage(ex.WithBranch(ref.name));
535+
}
536536
}
537537
}
538538
}
@@ -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(new BranchError(ex.reason, ex, state.name).message);
644+
return showGenericErrorMessage(ex.WithBranch(state.name));
645645
}
646646
}
647647
}

src/env/node/git/git.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,7 @@ export class Git {
989989
upstream?: string;
990990
delete?: {
991991
remote: string;
992-
branches: string[];
992+
branch: string;
993993
};
994994
},
995995
): Promise<void> {
@@ -1019,7 +1019,7 @@ export class Git {
10191019
} else if (options.remote) {
10201020
params.push(options.remote);
10211021
} else if (options.delete) {
1022-
params.push('-d', options.delete.remote, ...options.delete.branches);
1022+
params.push('-d', options.delete.remote, options.delete.branch);
10231023
}
10241024

10251025
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
@@ -1358,19 +1358,15 @@ export class GitProviderService implements Disposable {
13581358
}
13591359

13601360
@log()
1361-
deleteBranches(
1361+
deleteBranch(
13621362
repoPath: string,
1363-
branches: GitBranchReference | GitBranchReference[],
1363+
branch: GitBranchReference,
13641364
options?: { force?: boolean; remote?: boolean },
13651365
): Promise<void> {
13661366
const { provider, path } = this.getProvider(repoPath);
1367-
if (provider.deleteBranches == null) throw new ProviderNotSupportedError(provider.descriptor.name);
1367+
if (provider.deleteBranch == null) throw new ProviderNotSupportedError(provider.descriptor.name);
13681368

1369-
if (!Array.isArray(branches)) {
1370-
branches = [branches];
1371-
}
1372-
1373-
return provider.deleteBranches(path, branches, {
1369+
return provider.deleteBranch(path, branch, {
13741370
force: options?.force,
13751371
remote: options?.remote,
13761372
});

0 commit comments

Comments
 (0)