Skip to content

Commit 9c36e35

Browse files
authored
Git - add option to restore staged changes when aplying/popping a stash (#278556)
1 parent 4c798ea commit 9c36e35

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

extensions/git/src/commands.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3411,9 +3411,7 @@ export class CommandCenter {
34113411
}
34123412

34133413
await repository.migrateChanges(worktreeRepository.root, {
3414-
confirmation: true,
3415-
deleteFromSource: false,
3416-
untracked: true
3414+
confirmation: true, deleteFromSource: true, untracked: true
34173415
});
34183416
}
34193417

extensions/git/src/git.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2461,13 +2461,19 @@ export class Repository {
24612461
}
24622462
}
24632463

2464-
async popStash(index?: number): Promise<void> {
2464+
async popStash(index?: number, options?: { reinstateStagedChanges?: boolean }): Promise<void> {
24652465
const args = ['stash', 'pop'];
2466+
if (options?.reinstateStagedChanges) {
2467+
args.push('--index');
2468+
}
24662469
await this.popOrApplyStash(args, index);
24672470
}
24682471

2469-
async applyStash(index?: number): Promise<void> {
2472+
async applyStash(index?: number, options?: { reinstateStagedChanges?: boolean }): Promise<void> {
24702473
const args = ['stash', 'apply'];
2474+
if (options?.reinstateStagedChanges) {
2475+
args.push('--index');
2476+
}
24712477
await this.popOrApplyStash(args, index);
24722478
}
24732479

extensions/git/src/repository.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2268,16 +2268,16 @@ export class Repository implements Disposable {
22682268
});
22692269
}
22702270

2271-
async popStash(index?: number): Promise<void> {
2272-
return await this.run(Operation.Stash, () => this.repository.popStash(index));
2271+
async popStash(index?: number, options?: { reinstateStagedChanges?: boolean }): Promise<void> {
2272+
return await this.run(Operation.Stash, () => this.repository.popStash(index, options));
22732273
}
22742274

22752275
async dropStash(index?: number): Promise<void> {
22762276
return await this.run(Operation.Stash, () => this.repository.dropStash(index));
22772277
}
22782278

2279-
async applyStash(index?: number): Promise<void> {
2280-
return await this.run(Operation.Stash, () => this.repository.applyStash(index));
2279+
async applyStash(index?: number, options?: { reinstateStagedChanges?: boolean }): Promise<void> {
2280+
return await this.run(Operation.Stash, () => this.repository.applyStash(index, options));
22812281
}
22822282

22832283
async showStash(index: number): Promise<Change[] | undefined> {
@@ -2500,17 +2500,16 @@ export class Repository implements Disposable {
25002500
}
25012501
}
25022502

2503-
const stashName = `migration-${sourceRepository.HEAD?.name ?? sourceRepository.HEAD?.commit}-${this.HEAD?.name ?? this.HEAD?.commit}`;
2503+
const stashName = `migration:${sourceRepository.HEAD?.name ?? sourceRepository.HEAD?.commit}-${this.HEAD?.name ?? this.HEAD?.commit}`;
25042504
await sourceRepository.createStash(stashName, options?.untracked);
25052505
const stashes = await sourceRepository.getStashes();
25062506

25072507
try {
2508-
await this.applyStash(stashes[0].index);
2509-
25102508
if (options?.deleteFromSource) {
2511-
await sourceRepository.dropStash(stashes[0].index);
2509+
await this.popStash(stashes[0].index);
25122510
} else {
2513-
await sourceRepository.popStash();
2511+
await this.applyStash(stashes[0].index);
2512+
await sourceRepository.popStash(stashes[0].index, { reinstateStagedChanges: true });
25142513
}
25152514
} catch (err) {
25162515
if (err.gitErrorCode === GitErrorCodes.StashConflict) {
@@ -2523,11 +2522,11 @@ export class Repository implements Disposable {
25232522
await commands.executeCommand('workbench.view.scm');
25242523
}
25252524

2526-
await sourceRepository.popStash();
2525+
await sourceRepository.popStash(stashes[0].index, { reinstateStagedChanges: true });
25272526
return;
25282527
}
25292528

2530-
await sourceRepository.popStash();
2529+
await sourceRepository.popStash(stashes[0].index, { reinstateStagedChanges: true });
25312530
throw err;
25322531
}
25332532
}
@@ -2872,7 +2871,7 @@ export class Repository implements Disposable {
28722871
const result = await runOperation();
28732872
return result;
28742873
} finally {
2875-
await this.repository.popStash();
2874+
await this.repository.popStash(undefined, { reinstateStagedChanges: true });
28762875
}
28772876
}
28782877

0 commit comments

Comments
 (0)