Skip to content

Commit aa64c51

Browse files
committed
convert deleteBranches into deleteBranch and call multiple times
1 parent e083379 commit aa64c51

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
}
@@ -545,17 +545,17 @@ export class BranchGitCommand extends QuickCommand {
545545

546546
endSteps(state);
547547

548-
try {
549-
await state.repo.git.deleteBranches(state.references, {
550-
force: state.flags.includes('--force'),
551-
remote: state.flags.includes('--remotes'),
552-
});
553-
} catch (ex) {
554-
Logger.error(ex);
555-
// TODO likely need some better error handling here
556-
return showGenericErrorMessage(
557-
new BranchError(ex.reason, ex, state.references.map(r => r.name).join(', ')).message,
558-
);
548+
for (const ref of state.references) {
549+
try {
550+
await state.repo.git.deleteBranch(ref, {
551+
force: state.flags.includes('--force'),
552+
remote: state.flags.includes('--remotes'),
553+
});
554+
} catch (ex) {
555+
Logger.error(ex);
556+
// TODO likely need some better error handling here
557+
return showGenericErrorMessage(ex.WithBranch(ref.name));
558+
}
559559
}
560560
}
561561
}
@@ -664,7 +664,7 @@ export class BranchGitCommand extends QuickCommand {
664664
} catch (ex) {
665665
Logger.error(ex);
666666
// TODO likely need some better error handling here
667-
return showGenericErrorMessage(new BranchError(ex.reason, ex, state.name).message);
667+
return showGenericErrorMessage(ex.WithBranch(state.name));
668668
}
669669
}
670670
}

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

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

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

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

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

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

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