Skip to content

Commit e658126

Browse files
committed
convert deleteBranches into deleteBranch and call multiple times
1 parent 4ef5faf commit e658126

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
@@ -414,7 +414,7 @@ export class BranchGitCommand extends QuickCommand {
414414
} catch (ex) {
415415
Logger.error(ex);
416416
// TODO likely need some better error handling here
417-
return showGenericErrorMessage(new BranchError(ex.reason, ex, state.name).message);
417+
return showGenericErrorMessage(ex.WithBranch(state.name));
418418
}
419419
}
420420
}
@@ -531,17 +531,17 @@ export class BranchGitCommand extends QuickCommand {
531531

532532
endSteps(state);
533533

534-
try {
535-
await state.repo.git.deleteBranches(state.references, {
536-
force: state.flags.includes('--force'),
537-
remote: state.flags.includes('--remotes'),
538-
});
539-
} catch (ex) {
540-
Logger.error(ex);
541-
// TODO likely need some better error handling here
542-
return showGenericErrorMessage(
543-
new BranchError(ex.reason, ex, state.references.map(r => r.name).join(', ')).message,
544-
);
534+
for (const ref of state.references) {
535+
try {
536+
await state.repo.git.deleteBranch(ref, {
537+
force: state.flags.includes('--force'),
538+
remote: state.flags.includes('--remotes'),
539+
});
540+
} catch (ex) {
541+
Logger.error(ex);
542+
// TODO likely need some better error handling here
543+
return showGenericErrorMessage(ex.WithBranch(ref.name));
544+
}
545545
}
546546
}
547547
}
@@ -650,7 +650,7 @@ export class BranchGitCommand extends QuickCommand {
650650
} catch (ex) {
651651
Logger.error(ex);
652652
// TODO likely need some better error handling here
653-
return showGenericErrorMessage(new BranchError(ex.reason, ex, state.name).message);
653+
return showGenericErrorMessage(ex.WithBranch(state.name));
654654
}
655655
}
656656
}

src/env/node/git/git.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ export class Git {
990990
upstream?: string;
991991
delete?: {
992992
remote: string;
993-
branches: string[];
993+
branch: string;
994994
};
995995
},
996996
): Promise<void> {
@@ -1020,7 +1020,7 @@ export class Git {
10201020
} else if (options.remote) {
10211021
params.push(options.remote);
10221022
} else if (options.delete) {
1023-
params.push('-d', options.delete.remote, ...options.delete.branches);
1023+
params.push('-d', options.delete.remote, options.delete.branch);
10241024
}
10251025

10261026
try {

src/env/node/git/localGitProvider.ts

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

12741274
@log()
1275-
async deleteBranches(
1275+
async deleteBranch(
12761276
repoPath: string,
1277-
branches: GitBranchReference[],
1277+
branch: GitBranchReference,
12781278
options: { force?: boolean; remote?: boolean },
12791279
): Promise<void> {
1280-
const localBranches = branches.filter((b: GitBranchReference) => !b.remote);
1281-
if (localBranches.length !== 0) {
1282-
const args = ['--delete'];
1283-
if (options.force) {
1284-
args.push('--force');
1285-
}
1286-
1287-
if (options.remote) {
1288-
const trackingBranches = localBranches.filter(b => b.upstream != null);
1289-
if (trackingBranches.length !== 0) {
1290-
const branchesByOrigin = groupByMap(trackingBranches, b =>
1291-
getRemoteNameFromBranchName(b.upstream!.name),
1292-
);
1293-
1294-
for (const [remote, branches] of branchesByOrigin.entries()) {
1295-
const remoteCommitByBranch: Map<string, string> = {};
1296-
branches.forEach(async b => {
1297-
remoteCommit = await this.git.rev_list(repoPath, `refs/remotes/${remote}/${b.ref}`, {
1298-
maxResults: 1,
1299-
});
1300-
remoteCommitByBranch[b.ref] = remoteCommit;
1301-
});
1280+
if (branch.remote) {
1281+
return this.git.push(repoPath, {
1282+
delete: {
1283+
remote: getRemoteNameFromBranchName(branch.name),
1284+
branch: branch.remote ? getBranchNameWithoutRemote(branch.name) : branch.name,
1285+
},
1286+
});
1287+
}
13021288

1303-
await this.git.branch(
1304-
repoPath,
1305-
'--delete',
1306-
'--remotes',
1307-
...branches.map((b: GitBranchReference) => `${remote}/${b.ref}`),
1308-
);
1289+
const args = ['--delete'];
1290+
if (options.force) {
1291+
args.push('--force');
1292+
}
13091293

1310-
try {
1311-
await this.git.branch(repoPath, ...args, ...branches.map((b: GitBranchReference) => b.ref));
1312-
await this.git.push(repoPath, {
1313-
delete: {
1314-
remote: remote,
1315-
branches: branches.map(b => getBranchNameWithoutRemote(b.upstream!.name)),
1316-
},
1317-
});
1318-
} catch (ex) {
1319-
// If it fails, restore the remote branches
1320-
remoteCommitByBranch.forEach(async (branch, commit) => {
1321-
await this.git.update_ref(repoPath, `refs/remotes/${remote}/${branch}`, commit);
1322-
await this.git.branch__set_upstream(repoPath, branch, remote, branch);
1323-
});
1324-
throw ex;
1325-
}
1326-
}
1327-
}
1328-
}
1294+
if (!options.remote || !branch.upstream) {
1295+
return this.git.branch(repoPath, ...args, branch.ref);
13291296
}
13301297

1331-
const remoteBranches = branches.filter((b: GitBranchReference) => b.remote);
1332-
if (remoteBranches.length !== 0) {
1333-
const branchesByOrigin = groupByMap(remoteBranches, b => getRemoteNameFromBranchName(b.name));
1298+
const remote = getRemoteNameFromBranchName(branch.upstream.name);
1299+
remoteCommit = await this.git.rev_list(repoPath, `refs/remotes/${remote}/${branch.ref}`, {
1300+
maxResults: 1,
1301+
});
13341302

1335-
for (const [remote, branches] of branchesByOrigin.entries()) {
1336-
await this.git.push(repoPath, {
1337-
delete: {
1338-
remote: remote,
1339-
branches: branches.map((b: GitBranchReference) =>
1340-
b.remote ? getBranchNameWithoutRemote(b.name) : b.name,
1341-
),
1342-
},
1343-
});
1344-
}
1303+
await this.git.branch(repoPath, '--delete', '--remotes', `${remote}/${branch.ref}`);
1304+
1305+
try {
1306+
await this.git.branch(repoPath, ...args, branch.ref);
1307+
} catch (ex) {
1308+
// If it fails, restore the remote branch
1309+
await this.git.update_ref(repoPath, `refs/remotes/${remote}/${branch.ref}`, commit);
1310+
await this.git.branch__set_upstream(repoPath, branch, remote, branch);
1311+
throw ex;
13451312
}
1313+
1314+
await this.git.push(repoPath, {
1315+
delete: {
1316+
remote: remote,
1317+
branch: getBranchNameWithoutRemote(branch.upstream.name),
1318+
},
1319+
});
13461320
}
13471321

13481322
@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
@@ -1377,19 +1377,15 @@ export class GitProviderService implements Disposable {
13771377
}
13781378

13791379
@log()
1380-
deleteBranches(
1380+
deleteBranch(
13811381
repoPath: string,
1382-
branches: GitBranchReference | GitBranchReference[],
1382+
branch: GitBranchReference,
13831383
options?: { force?: boolean; remote?: boolean },
13841384
): Promise<void> {
13851385
const { provider, path } = this.getProvider(repoPath);
1386-
if (provider.deleteBranches == null) throw new ProviderNotSupportedError(provider.descriptor.name);
1386+
if (provider.deleteBranch == null) throw new ProviderNotSupportedError(provider.descriptor.name);
13871387

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

0 commit comments

Comments
 (0)