Skip to content

Commit 1338976

Browse files
committed
safe delete branch
1 parent bc6384f commit 1338976

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
@@ -1608,7 +1608,11 @@ export class Git {
16081608
async rev_list(
16091609
repoPath: string,
16101610
ref: string,
1611-
options?: { all?: boolean; maxParents?: number },
1611+
options?: {
1612+
all?: boolean;
1613+
maxParents?: number;
1614+
maxResults?: number;
1615+
},
16121616
): Promise<string[] | undefined> {
16131617
const params = ['rev-list'];
16141618
if (options?.all) {
@@ -1619,6 +1623,10 @@ export class Git {
16191623
params.push(`--max-parents=${options.maxParents}`);
16201624
}
16211625

1626+
if (options?.maxResults != null) {
1627+
params.push(`-n ${options.maxResults}`);
1628+
}
1629+
16221630
const rawData = await this.git<string>(
16231631
{ cwd: repoPath, errors: GitErrorHandling.Ignore },
16241632
...params,
@@ -1891,6 +1899,10 @@ export class Git {
18911899
return data.length === 0 ? undefined : data.trim();
18921900
}
18931901

1902+
async update_ref(repoPath: string, ...args: string[]): Promise<void> {
1903+
await this.git<string>({ cwd: repoPath }, 'update-ref', ...args);
1904+
}
1905+
18941906
async show<TOut extends string | Buffer>(
18951907
repoPath: string | undefined,
18961908
fileName: string,

src/env/node/git/localGitProvider.ts

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

1266-
await this.git.branch(repoPath, ...args, ...branches.map((b: GitBranchReference) => b.ref));
1267-
12681266
if (options.remote) {
12691267
const trackingBranches = localBranches.filter(b => b.upstream != null);
12701268
if (trackingBranches.length !== 0) {
@@ -1273,17 +1271,43 @@ export class LocalGitProvider implements GitProvider, Disposable {
12731271
);
12741272

12751273
for (const [remote, branches] of branchesByOrigin.entries()) {
1276-
await this.git.push(repoPath, {
1277-
delete: {
1278-
remote: remote,
1279-
branches: branches.map(b => getBranchNameWithoutRemote(b.upstream!.name)),
1280-
},
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;
12811280
});
1281+
1282+
await this.git.branch(
1283+
repoPath,
1284+
'--delete',
1285+
'--remotes',
1286+
...branches.map((b: GitBranchReference) => `${remote}/${b.ref}`),
1287+
);
1288+
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+
}
12821305
}
12831306
}
12841307
}
12851308
}
12861309

1310+
const remoteBranches = branches.filter((b: GitBranchReference) => b.remote);
12871311
if (remoteBranches.length !== 0) {
12881312
const branchesByOrigin = groupByMap(remoteBranches, b => getRemoteNameFromBranchName(b.name));
12891313

0 commit comments

Comments
 (0)