Skip to content

Commit 88734e2

Browse files
committed
safe delete branch
1 parent e916511 commit 88734e2

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

src/env/node/git/git.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1609,7 +1609,11 @@ export class Git {
16091609
async rev_list(
16101610
repoPath: string,
16111611
ref: string,
1612-
options?: { all?: boolean; maxParents?: number },
1612+
options?: {
1613+
all?: boolean;
1614+
maxParents?: number;
1615+
maxResults?: number;
1616+
},
16131617
): Promise<string[] | undefined> {
16141618
const params = ['rev-list'];
16151619
if (options?.all) {
@@ -1620,6 +1624,10 @@ export class Git {
16201624
params.push(`--max-parents=${options.maxParents}`);
16211625
}
16221626

1627+
if (options?.maxResults != null) {
1628+
params.push(`-n ${options.maxResults}`);
1629+
}
1630+
16231631
const rawData = await this.git<string>(
16241632
{ cwd: repoPath, errors: GitErrorHandling.Ignore },
16251633
...params,
@@ -1885,6 +1893,10 @@ export class Git {
18851893
return data.length === 0 ? undefined : data.trim();
18861894
}
18871895

1896+
async update_ref(repoPath: string, ...args: string[]): Promise<void> {
1897+
await this.git<string>({ cwd: repoPath }, 'update-ref', ...args);
1898+
}
1899+
18881900
show(
18891901
repoPath: string,
18901902
options?: { cancellation?: CancellationToken; configs?: readonly string[] },

src/env/node/git/localGitProvider.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,8 +1286,6 @@ export class LocalGitProvider implements GitProvider, Disposable {
12861286
args.push('--force');
12871287
}
12881288

1289-
await this.git.branch(repoPath, ...args, ...branches.map((b: GitBranchReference) => b.ref));
1290-
12911289
if (options.remote) {
12921290
const trackingBranches = localBranches.filter(b => b.upstream != null);
12931291
if (trackingBranches.length !== 0) {
@@ -1296,17 +1294,43 @@ export class LocalGitProvider implements GitProvider, Disposable {
12961294
);
12971295

12981296
for (const [remote, branches] of branchesByOrigin.entries()) {
1299-
await this.git.push(repoPath, {
1300-
delete: {
1301-
remote: remote,
1302-
branches: branches.map(b => getBranchNameWithoutRemote(b.upstream!.name)),
1303-
},
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;
13041303
});
1304+
1305+
await this.git.branch(
1306+
repoPath,
1307+
'--delete',
1308+
'--remotes',
1309+
...branches.map((b: GitBranchReference) => `${remote}/${b.ref}`),
1310+
);
1311+
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+
}
13051328
}
13061329
}
13071330
}
13081331
}
13091332

1333+
const remoteBranches = branches.filter((b: GitBranchReference) => b.remote);
13101334
if (remoteBranches.length !== 0) {
13111335
const branchesByOrigin = groupByMap(remoteBranches, b => getRemoteNameFromBranchName(b.name));
13121336

0 commit comments

Comments
 (0)