diff --git a/lib/gitWorkflow.js b/lib/gitWorkflow.js index ecc6502bc..c2127246f 100644 --- a/lib/gitWorkflow.js +++ b/lib/gitWorkflow.js @@ -68,19 +68,18 @@ class GitWorkflow { debug('Done backing up merge state!') } - // Get stash of entire original state, including unstaged changes + // Get diff of staged modifications. This will be applied back to the index. after stashing all changes. + // The `git stash save --keep-index` option cannot be used since it resurrects deleted files on + // git versions before v2.23.0 (https://github.com/git/git/blob/master/Documentation/RelNotes/2.23.0.txt#L322) + const stagedDiff = await this.execGit(['diff', '--cached']) + + // Save stash of entire original state, including unstaged and untracked changes. + // This should remove all changes from the index. await this.execGit(['stash', 'save', '--quiet', '--include-untracked', STASH]) - // Apply index from previous stash back to enable running tasks against - // staged files only. `git stash --keep-index` cannot be used here because of - // a bug affecting deleted files. The bug has been fixed in git v2.23.0 - // See https://github.com/git/git/blob/master/Documentation/RelNotes/2.23.0.txt#L322 - await this.execGit(['stash', 'apply', '--index', await this.getBackupStash()]) - // Clear any unstaged changes since they are saved in the stash, - // and shouldn't be affected by tasks - await this.execGit(['clean', '-df']) - await this.execGit(['checkout', '.']) - // Since only staged files are now present, get a diff of unstaged changes - // by comparing current index against original stash, but in reverse + + // Apply staged modifications back to the index + await this.execGit([...gitApplyArgs, '--index'], { input: `${stagedDiff}\n` }) + this.unstagedDiff = await this.execGit([ 'diff', '--unified=0',