Skip to content

Commit d8850d4

Browse files
committed
Merge branch 'stash-p-corner-case'
This topic branch fixes a corner case that is amazingly common in this developer's workflow: in a `git stash -p`, splitting a hunk and stashing only part of it runs into a (known) bug where the partial hunk cannot be applied in reverse. It is one of those "good enough" fixes, not a full fix, though, as the full fix would require a 3-way merge between `stash^` and the *worktree* (not `HEAD`), with `stash` as merge base (i.e. a `git revert`, but on top of the current worktree). Signed-off-by: Johannes Schindelin <[email protected]>
2 parents 2125656 + b85a6f8 commit d8850d4

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed

builtin/stash.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,7 @@ static int stash_patch(struct stash_info *info, struct pathspec ps,
10191019
}
10201020

10211021
cp_diff_tree.git_cmd = 1;
1022-
argv_array_pushl(&cp_diff_tree.args, "diff-tree", "-p", "HEAD",
1022+
argv_array_pushl(&cp_diff_tree.args, "diff-tree", "-p", "-U1", "HEAD",
10231023
oid_to_hex(&info->w_tree), "--", NULL);
10241024
if (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) {
10251025
ret = -1;

git-legacy-stash.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ create_stash () {
212212
w_tree=$(GIT_INDEX_FILE="$TMP-index" git write-tree) ||
213213
die "$(gettext "Cannot save the current worktree state")"
214214

215-
git diff-tree -p HEAD $w_tree -- >"$TMP-patch" &&
215+
git diff-tree -p -U1 HEAD $w_tree -- >"$TMP-patch" &&
216216
test -s "$TMP-patch" ||
217217
die "$(gettext "No changes selected")"
218218

t/t3904-stash-patch.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ test_expect_success 'none of this moved HEAD' '
8989
verify_saved_head
9090
'
9191

92-
test_expect_failure 'stash -p with split hunk' '
92+
test_expect_success 'stash -p with split hunk' '
9393
git reset --hard &&
9494
cat >test <<-\EOF &&
9595
aaa
@@ -106,8 +106,8 @@ test_expect_failure 'stash -p with split hunk' '
106106
ccc
107107
EOF
108108
printf "%s\n" s n y q |
109-
test_might_fail git stash -p 2>error &&
110-
! test_must_be_empty error &&
109+
git stash -p 2>error &&
110+
test_must_be_empty error &&
111111
grep "added line 1" test &&
112112
! grep "added line 2" test
113113
'

0 commit comments

Comments
 (0)