Git - add option to restore staged changes when aplying/popping a stash (#278556)

This commit is contained in:
Ladislau Szomoru
2025-11-20 11:13:37 +00:00
committed by GitHub
parent 4c798ea2c6
commit 9c36e3505a
3 changed files with 20 additions and 17 deletions

View File

@@ -3411,9 +3411,7 @@ export class CommandCenter {
}
await repository.migrateChanges(worktreeRepository.root, {
confirmation: true,
deleteFromSource: false,
untracked: true
confirmation: true, deleteFromSource: true, untracked: true
});
}

View File

@@ -2461,13 +2461,19 @@ export class Repository {
}
}
async popStash(index?: number): Promise<void> {
async popStash(index?: number, options?: { reinstateStagedChanges?: boolean }): Promise<void> {
const args = ['stash', 'pop'];
if (options?.reinstateStagedChanges) {
args.push('--index');
}
await this.popOrApplyStash(args, index);
}
async applyStash(index?: number): Promise<void> {
async applyStash(index?: number, options?: { reinstateStagedChanges?: boolean }): Promise<void> {
const args = ['stash', 'apply'];
if (options?.reinstateStagedChanges) {
args.push('--index');
}
await this.popOrApplyStash(args, index);
}

View File

@@ -2268,16 +2268,16 @@ export class Repository implements Disposable {
});
}
async popStash(index?: number): Promise<void> {
return await this.run(Operation.Stash, () => this.repository.popStash(index));
async popStash(index?: number, options?: { reinstateStagedChanges?: boolean }): Promise<void> {
return await this.run(Operation.Stash, () => this.repository.popStash(index, options));
}
async dropStash(index?: number): Promise<void> {
return await this.run(Operation.Stash, () => this.repository.dropStash(index));
}
async applyStash(index?: number): Promise<void> {
return await this.run(Operation.Stash, () => this.repository.applyStash(index));
async applyStash(index?: number, options?: { reinstateStagedChanges?: boolean }): Promise<void> {
return await this.run(Operation.Stash, () => this.repository.applyStash(index, options));
}
async showStash(index: number): Promise<Change[] | undefined> {
@@ -2500,17 +2500,16 @@ export class Repository implements Disposable {
}
}
const stashName = `migration-${sourceRepository.HEAD?.name ?? sourceRepository.HEAD?.commit}-${this.HEAD?.name ?? this.HEAD?.commit}`;
const stashName = `migration:${sourceRepository.HEAD?.name ?? sourceRepository.HEAD?.commit}-${this.HEAD?.name ?? this.HEAD?.commit}`;
await sourceRepository.createStash(stashName, options?.untracked);
const stashes = await sourceRepository.getStashes();
try {
await this.applyStash(stashes[0].index);
if (options?.deleteFromSource) {
await sourceRepository.dropStash(stashes[0].index);
await this.popStash(stashes[0].index);
} else {
await sourceRepository.popStash();
await this.applyStash(stashes[0].index);
await sourceRepository.popStash(stashes[0].index, { reinstateStagedChanges: true });
}
} catch (err) {
if (err.gitErrorCode === GitErrorCodes.StashConflict) {
@@ -2523,11 +2522,11 @@ export class Repository implements Disposable {
await commands.executeCommand('workbench.view.scm');
}
await sourceRepository.popStash();
await sourceRepository.popStash(stashes[0].index, { reinstateStagedChanges: true });
return;
}
await sourceRepository.popStash();
await sourceRepository.popStash(stashes[0].index, { reinstateStagedChanges: true });
throw err;
}
}
@@ -2872,7 +2871,7 @@ export class Repository implements Disposable {
const result = await runOperation();
return result;
} finally {
await this.repository.popStash();
await this.repository.popStash(undefined, { reinstateStagedChanges: true });
}
}