fix and test 'q' command for unstage -p
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
diff --git a/lib/worktree.c b/lib/worktree.c
index bd84650..070abf5 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -5321,12 +5321,23 @@ apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
end_old, start_new, end_new, rejectfile, outfile);
break;
case GOT_PATCH_CHOICE_QUIT:
- /* Copy old file's lines until EOF. */
- while (!feof(f1)) {
- err = copy_one_line(f1, outfile, rejectfile);
- if (err)
- goto done;
- (*line_cur1)++;
+ if (outfile) {
+ /* Copy old file's lines until EOF. */
+ while (!feof(f1)) {
+ err = copy_one_line(f1, outfile, NULL);
+ if (err)
+ goto done;
+ (*line_cur1)++;
+ }
+ }
+ if (rejectfile) {
+ /* Copy new file's lines until EOF. */
+ while (!feof(f2)) {
+ err = copy_one_line(f2, NULL, rejectfile);
+ if (err)
+ goto done;
+ (*line_cur2)++;
+ }
}
break;
default:
@@ -5731,7 +5742,7 @@ create_unstaged_content(char **path_unstaged_content,
goto done;
if (choice == GOT_PATCH_CHOICE_YES)
have_content = 1;
- if (choice == GOT_PATCH_CHOICE_NO)
+ else
have_rejected_content = 1;
if (choice == GOT_PATCH_CHOICE_QUIT)
break;
diff --git a/regress/cmdline/unstage.sh b/regress/cmdline/unstage.sh
index aa8c840..07c730e 100755
--- a/regress/cmdline/unstage.sh
+++ b/regress/cmdline/unstage.sh
@@ -684,7 +684,158 @@ function test_unstage_patch_removed {
test_done "$testroot" "$ret"
}
+function test_unstage_patch_quit {
+ local testroot=`test_init unstage_patch_quit`
+
+ jot 16 > $testroot/repo/numbers
+ echo zzz > $testroot/repo/zzz
+ (cd $testroot/repo && git add numbers zzz)
+ git_commit $testroot/repo -m "added files"
+ local commit_id=`git_show_head $testroot/repo`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ sed -i -e 's/^2$/a/' $testroot/wt/numbers
+ sed -i -e 's/^7$/b/' $testroot/wt/numbers
+ sed -i -e 's/^16$/c/' $testroot/wt/numbers
+ (cd $testroot/wt && got rm zzz > /dev/null)
+ (cd $testroot/wt && got stage > /dev/null)
+
+ # unstage first hunk and quit; and don't pass a path argument to
+ # ensure that we don't skip asking about the 'zzz' file after 'quit'
+ printf "y\nq\nn\n" > $testroot/patchscript
+ (cd $testroot/wt && got unstage -F $testroot/patchscript -p \
+ > $testroot/stdout)
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ echo "got stage command failed unexpectedly" >&2
+ test_done "$testroot" "1"
+ return 1
+ fi
+ cat > $testroot/stdout.expected <<EOF
+-----------------------------------------------
+@@ -1,5 +1,5 @@
+ 1
+-2
++a
+ 3
+ 4
+ 5
+-----------------------------------------------
+M numbers (change 1 of 3)
+unstage this change? [y/n/q] y
+-----------------------------------------------
+@@ -4,7 +4,7 @@
+ 4
+ 5
+ 6
+-7
++b
+ 8
+ 9
+ 10
+-----------------------------------------------
+M numbers (change 2 of 3)
+unstage this change? [y/n/q] q
+G numbers
+D zzz
+unstage this deletion? [y/n] n
+EOF
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ (cd $testroot/wt && got status > $testroot/stdout)
+ echo "MM numbers" > $testroot/stdout.expected
+ echo " D zzz" >> $testroot/stdout.expected
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ (cd $testroot/wt && got diff > $testroot/stdout)
+
+ echo "diff $commit_id $testroot/wt" > $testroot/stdout.expected
+ echo -n 'blob - ' >> $testroot/stdout.expected
+ (cd $testroot/wt && got stage -l numbers) | cut -d' ' -f 1 \
+ >> $testroot/stdout.expected
+ echo "file + numbers" >> $testroot/stdout.expected
+ echo "--- numbers" >> $testroot/stdout.expected
+ echo "+++ numbers" >> $testroot/stdout.expected
+ echo "@@ -1,5 +1,5 @@" >> $testroot/stdout.expected
+ echo " 1" >> $testroot/stdout.expected
+ echo "-2" >> $testroot/stdout.expected
+ echo "+a" >> $testroot/stdout.expected
+ echo " 3" >> $testroot/stdout.expected
+ echo " 4" >> $testroot/stdout.expected
+ echo " 5" >> $testroot/stdout.expected
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ (cd $testroot/wt && got diff -s > $testroot/stdout)
+ echo "diff $commit_id $testroot/wt (staged changes)" \
+ > $testroot/stdout.expected
+ echo -n 'blob - ' >> $testroot/stdout.expected
+ got tree -r $testroot/repo -i -c $commit_id \
+ | grep 'numbers$' | cut -d' ' -f 1 \
+ >> $testroot/stdout.expected
+ echo -n 'blob + ' >> $testroot/stdout.expected
+ (cd $testroot/wt && got stage -l numbers) | cut -d' ' -f 1 \
+ >> $testroot/stdout.expected
+ cat >> $testroot/stdout.expected <<EOF
+--- numbers
++++ numbers
+@@ -4,7 +4,7 @@
+ 4
+ 5
+ 6
+-7
++b
+ 8
+ 9
+ 10
+@@ -13,4 +13,4 @@
+ 13
+ 14
+ 15
+-16
++c
+EOF
+ echo -n 'blob - ' >> $testroot/stdout.expected
+ got tree -r $testroot/repo -i | grep 'zzz$' | cut -d' ' -f 1 \
+ >> $testroot/stdout.expected
+ echo 'blob + /dev/null' >> $testroot/stdout.expected
+ echo "--- zzz" >> $testroot/stdout.expected
+ echo "+++ /dev/null" >> $testroot/stdout.expected
+ echo "@@ -1 +0,0 @@" >> $testroot/stdout.expected
+ echo "-zzz" >> $testroot/stdout.expected
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ fi
+ test_done "$testroot" "$ret"
+}
+
run_test test_unstage_basic
run_test test_unstage_patch
run_test test_unstage_patch_added
run_test test_unstage_patch_removed
+run_test test_unstage_patch_quit