Skip to content

Commit 5bab792

Browse files
committed
convert deleteBranches into deleteBranch and call multiple times
1 parent 3112149 commit 5bab792

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
@@ -1274,77 +1274,51 @@ export class LocalGitProvider implements GitProvider, Disposable {
12741274
}
12751275

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

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

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

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

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

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