@matheusbsbs / matheus-bagliano / commits / dda1f2a5c3
Fork 0

Implement 'git stash save --patch'

This adds a hunk-based mode to git-stash. You can select hunks from the difference between HEAD and worktree, and git-stash will build a stash that reflects these changes. The index state of the stash is the same as your current index, and we also let --patch imply --keep-index. Note that because the selected hunks are rolled back from the worktree but not the index, the resulting state may appear somewhat confusing if you had also staged these changes. This is not entirely satisfactory, but due to the way stashes are applied, other solutions would require a change to the stash format. Signed-off-by: Thomas Rast <trast@student.ethz.ch> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Rast committed Aug 13, 2009 at 14:29 UTC dda1f2a5c3aca5072aada32eef159067ba16f0e9
4 files changed +145 -18
Documentation/git-stash.txt
+12 -2
@@ -13,7 +13,7 @@ SYNOPSIS
13 'git stash' drop [-q|--quiet] [<stash>]
14 'git stash' ( pop | apply ) [--index] [-q|--quiet] [<stash>]
15 'git stash' branch <branchname> [<stash>]
16 -'git stash' [save [--keep-index] [-q|--quiet] [<message>]]
16 +'git stash' [save [--patch] [--[no-]keep-index] [-q|--quiet] [<message>]]
17 'git stash' clear
18 'git stash' create
19
@@ -42,7 +42,7 @@ is also possible).
42 OPTIONS
43 -------
44
45 -save [--keep-index] [-q|--quiet] [<message>]::
45 +save [--patch] [--[no-]keep-index] [-q|--quiet] [<message>]::
46
47 Save your local modifications to a new 'stash', and run `git reset
48 --hard` to revert them. This is the default action when no
@@ -51,6 +51,16 @@ save [--keep-index] [-q|--quiet] [<message>]::
51 +
52 If the `--keep-index` option is used, all changes already added to the
53 index are left intact.
54 ++
55 +With `--patch`, you can interactively select hunks from in the diff
56 +between HEAD and the working tree to be stashed. The stash entry is
57 +constructed such that its index state is the same as the index state
58 +of your repository, and its worktree contains only the changes you
59 +selected interactively. The selected changes are then rolled back
60 +from your worktree.
61 ++
62 +The `--patch` option implies `--keep-index`. You can use
63 +`--no-keep-index` to override this.
64
65 list [<options>]::
66
git-add--interactive.perl
+12 -2
@@ -76,6 +76,7 @@ my $patch_mode_revision;
76
77 sub apply_patch;
78 sub apply_patch_for_checkout_commit;
79 +sub apply_patch_for_stash;
80
81 my %patch_modes = (
82 'stage' => {
@@ -87,6 +88,15 @@ my %patch_modes = (
88 PARTICIPLE => 'staging',
89 FILTER => 'file-only',
90 },
91 + 'stash' => {
92 + DIFF => 'diff-index -p HEAD',
93 + APPLY => sub { apply_patch 'apply --cached', @_; },
94 + APPLY_CHECK => 'apply --cached',
95 + VERB => 'Stash',
96 + TARGET => '',
97 + PARTICIPLE => 'stashing',
98 + FILTER => undef,
99 + },
100 'reset_head' => {
101 DIFF => 'diff-index -p --cached',
102 APPLY => sub { apply_patch 'apply -R --cached', @_; },
@@ -1493,8 +1503,8 @@ sub process_args {
1503 'checkout_head' : 'checkout_nothead');
1504 $arg = shift @ARGV or die "missing --";
1505 }
1496 - } elsif ($1 eq 'stage') {
1497 - $patch_mode = 'stage';
1506 + } elsif ($1 eq 'stage' or $1 eq 'stash') {
1507 + $patch_mode = $1;
1508 $arg = shift @ARGV or die "missing --";
1509 } else {
1510 die "unknown --patch mode: $1";
git-stash.sh
+66 -14
@@ -21,6 +21,14 @@ trap 'rm -f "$TMP-*"' 0
21
22 ref_stash=refs/stash
23
24 +if git config --get-colorbool color.interactive; then
25 + help_color="$(git config --get-color color.interactive.help 'red bold')"
26 + reset_color="$(git config --get-color '' reset)"
27 +else
28 + help_color=
29 + reset_color=
30 +fi
31 +
32 no_changes () {
33 git diff-index --quiet --cached HEAD --ignore-submodules -- &&
34 git diff-files --quiet --ignore-submodules
@@ -68,19 +76,44 @@ create_stash () {
76 git commit-tree $i_tree -p $b_commit) ||
77 die "Cannot save the current index state"
78
71 - # state of the working tree
72 - w_tree=$( (
79 + if test -z "$patch_mode"
80 + then
81 +
82 + # state of the working tree
83 + w_tree=$( (
84 + rm -f "$TMP-index" &&
85 + cp -p ${GIT_INDEX_FILE-"$GIT_DIR/index"} "$TMP-index" &&
86 + GIT_INDEX_FILE="$TMP-index" &&
87 + export GIT_INDEX_FILE &&
88 + git read-tree -m $i_tree &&
89 + git add -u &&
90 + git write-tree &&
91 + rm -f "$TMP-index"
92 + ) ) ||
93 + die "Cannot save the current worktree state"
94 +
95 + else
96 +
97 rm -f "$TMP-index" &&
74 - cp -p ${GIT_INDEX_FILE-"$GIT_DIR/index"} "$TMP-index" &&
75 - GIT_INDEX_FILE="$TMP-index" &&
76 - export GIT_INDEX_FILE &&
77 - git read-tree -m $i_tree &&
78 - git add -u &&
79 - git write-tree &&
80 - rm -f "$TMP-index"
81 - ) ) ||
98 + GIT_INDEX_FILE="$TMP-index" git read-tree HEAD &&
99 +
100 + # find out what the user wants
101 + GIT_INDEX_FILE="$TMP-index" \
102 + git add--interactive --patch=stash -- &&
103 +
104 + # state of the working tree
105 + w_tree=$(GIT_INDEX_FILE="$TMP-index" git write-tree) ||
106 die "Cannot save the current worktree state"
107
108 + git diff-tree -p HEAD $w_tree > "$TMP-patch" &&
109 + test -s "$TMP-patch" ||
110 + die "No changes selected"
111 +
112 + rm -f "$TMP-index" ||
113 + die "Cannot remove temporary index (can't happen)"
114 +
115 + fi
116 +
117 # create the stash
118 if test -z "$stash_msg"
119 then
@@ -95,12 +128,20 @@ create_stash () {
128
129 save_stash () {
130 keep_index=
131 + patch_mode=
132 while test $# != 0
133 do
134 case "$1" in
135 --keep-index)
136 keep_index=t
137 ;;
138 + --no-keep-index)
139 + keep_index=
140 + ;;
141 + -p|--patch)
142 + patch_mode=t
143 + keep_index=t
144 + ;;
145 -q|--quiet)
146 GIT_QUIET=t
147 ;;
@@ -131,11 +172,22 @@ save_stash () {
172 die "Cannot save the current status"
173 say Saved working directory and index state "$stash_msg"
174
134 - git reset --hard ${GIT_QUIET:+-q}
135 -
136 - if test -n "$keep_index" && test -n $i_tree
175 + if test -z "$patch_mode"
176 then
138 - git read-tree --reset -u $i_tree
177 + git reset --hard ${GIT_QUIET:+-q}
178 +
179 + if test -n "$keep_index" && test -n $i_tree
180 + then
181 + git read-tree --reset -u $i_tree
182 + fi
183 + else
184 + git apply -R < "$TMP-patch" ||
185 + die "Cannot remove worktree changes"
186 +
187 + if test -z "$keep_index"
188 + then
189 + git reset
190 + fi
191 fi
192 }
193
t/t3904-stash-patch.sh new
+55
@@ -0,0 +1,55 @@
1 +#!/bin/sh
2 +
3 +test_description='git checkout --patch'
4 +. ./lib-patch-mode.sh
5 +
6 +test_expect_success 'setup' '
7 + mkdir dir &&
8 + echo parent > dir/foo &&
9 + echo dummy > bar &&
10 + git add bar dir/foo &&
11 + git commit -m initial &&
12 + test_tick &&
13 + test_commit second dir/foo head &&
14 + echo index > dir/foo &&
15 + git add dir/foo &&
16 + set_and_save_state bar bar_work bar_index &&
17 + save_head
18 +'
19 +
20 +# note: bar sorts before dir, so the first 'n' is always to skip 'bar'
21 +
22 +test_expect_success 'saying "n" does nothing' '
23 + set_state dir/foo work index
24 + (echo n; echo n) | test_must_fail git stash save -p &&
25 + verify_state dir/foo work index &&
26 + verify_saved_state bar
27 +'
28 +
29 +test_expect_success 'git stash -p' '
30 + (echo n; echo y) | git stash save -p &&
31 + verify_state dir/foo head index &&
32 + verify_saved_state bar &&
33 + git reset --hard &&
34 + git stash apply &&
35 + verify_state dir/foo work head &&
36 + verify_state bar dummy dummy
37 +'
38 +
39 +test_expect_success 'git stash -p --no-keep-index' '
40 + set_state dir/foo work index &&
41 + set_state bar bar_work bar_index &&
42 + (echo n; echo y) | git stash save -p --no-keep-index &&
43 + verify_state dir/foo head head &&
44 + verify_state bar bar_work dummy &&
45 + git reset --hard &&
46 + git stash apply --index &&
47 + verify_state dir/foo work index &&
48 + verify_state bar dummy bar_index
49 +'
50 +
51 +test_expect_success 'none of this moved HEAD' '
52 + verify_saved_head
53 +'
54 +
55 +test_done