add 'q' response to 'got stage -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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
diff --git a/got/got.1 b/got/got.1
index 601a65e..9697e4d 100644
--- a/got/got.1
+++ b/got/got.1
@@ -921,9 +921,11 @@ among the specified paths. Otherwise, show all staged paths.
Instead of staging the entire content of a changed file, interactively
select or reject changes for staging based on
.Dq y
-and
+(select),
.Dq n
-responses.
+(reject), and
+.Dq q
+(quit) responses.
If a file is in modified status, individual patches derived from the
modified file content can be staged.
Files in added or deleted status may be staged or rejected in their entirety.
diff --git a/got/got.c b/got/got.c
index 71b71a0..fefcde6 100644
--- a/got/got.c
+++ b/got/got.c
@@ -5200,10 +5200,10 @@ show_change(unsigned char status, const char *path, FILE *patch_file)
switch (status) {
case GOT_STATUS_ADD:
- printf("A %s\nstage this addition? [y/n] ", path);
+ printf("A %s\nstage this addition? [y/n/q] ", path);
break;
case GOT_STATUS_DELETE:
- printf("D %s\nstage deletion? [y/n] ", path);
+ printf("D %s\nstage deletion? [y/n/q] ", path);
break;
case GOT_STATUS_MODIFY:
if (fseek(patch_file, 0L, SEEK_SET) == -1)
@@ -5214,7 +5214,7 @@ show_change(unsigned char status, const char *path, FILE *patch_file)
if (ferror(patch_file))
return got_error_from_errno("getline");
printf(GOT_COMMIT_SEP_STR);
- printf("M %s\nstage this change? [y/n] ", path);
+ printf("M %s\nstage this change? [y/n/q] ", path);
break;
default:
return got_error_path(path, GOT_ERR_FILE_STATUS);
@@ -5258,18 +5258,22 @@ choose_patch(int *choice, void *arg, unsigned char status, const char *path,
*choice = GOT_PATCH_CHOICE_NO;
printf("n\n");
}
+ if (strcmp(line, "q") == 0) {
+ *choice = GOT_PATCH_CHOICE_QUIT;
+ printf("q\n");
+ }
free(line);
return NULL;
}
- while (resp != 'y' && resp != 'n') {
+ while (resp != 'y' && resp != 'n' && resp != 'q') {
err = show_change(status, path, patch_file);
if (err)
return err;
resp = getchar();
if (resp == '\n')
resp = getchar();
- if (resp != 'y' && resp != 'n')
+ if (resp != 'y' && resp != 'n' && resp != 'q')
printf("invalid response '%c'\n", resp);
}
@@ -5277,6 +5281,8 @@ choose_patch(int *choice, void *arg, unsigned char status, const char *path,
*choice = GOT_PATCH_CHOICE_YES;
else if (resp == 'n')
*choice = GOT_PATCH_CHOICE_NO;
+ else if (resp == 'q')
+ *choice = GOT_PATCH_CHOICE_QUIT;
return NULL;
}
diff --git a/include/got_worktree.h b/include/got_worktree.h
index 0edf139..c4b9425 100644
--- a/include/got_worktree.h
+++ b/include/got_worktree.h
@@ -381,6 +381,7 @@ typedef const struct got_error *(*got_worktree_patch_cb)(int *, void *,
#define GOT_PATCH_CHOICE_NONE 0
#define GOT_PATCH_CHOICE_YES 1
#define GOT_PATCH_CHOICE_NO 2
+#define GOT_PATCH_CHOICE_QUIT 3
/*
* Stage the specified paths for commit.
diff --git a/lib/worktree.c b/lib/worktree.c
index 3c1ffb4..3bfbe16 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -5274,6 +5274,15 @@ apply_or_reject_change(int *choice, struct got_diff_change *change,
(*line_cur2)++;
}
break;
+ case GOT_PATCH_CHOICE_QUIT:
+ /* Copy old file's lines until EOF. */
+ while (!feof(f1)) {
+ err = copy_one_line(f1, outfile);
+ if (err)
+ goto done;
+ (*line_cur1)++;
+ }
+ break;
default:
err = got_error(GOT_ERR_PATCH_CHOICE);
break;
@@ -5355,6 +5364,8 @@ create_staged_content(char **path_outfile, struct got_object_id *blob_id,
goto done;
if (choice == GOT_PATCH_CHOICE_YES)
have_content = 1;
+ else if (choice == GOT_PATCH_CHOICE_QUIT)
+ break;
}
done:
free(id_str);
diff --git a/regress/cmdline/stage.sh b/regress/cmdline/stage.sh
index 4bc1f42..7b8a5cf 100755
--- a/regress/cmdline/stage.sh
+++ b/regress/cmdline/stage.sh
@@ -1181,7 +1181,7 @@ function test_stage_patch {
5
-----------------------------------------------
M numbers
-stage this change? [y/n] n
+stage this change? [y/n/q] n
-----------------------------------------------
@@ -4,7 +4,7 @@
4
@@ -1194,7 +1194,7 @@ stage this change? [y/n] n
10
-----------------------------------------------
M numbers
-stage this change? [y/n] n
+stage this change? [y/n/q] n
-----------------------------------------------
@@ -13,4 +13,4 @@
13
@@ -1204,7 +1204,7 @@ stage this change? [y/n] n
+c
-----------------------------------------------
M numbers
-stage this change? [y/n] n
+stage this change? [y/n/q] n
EOF
cmp -s $testroot/stdout.expected $testroot/stdout
ret="$?"
@@ -1240,7 +1240,7 @@ EOF
5
-----------------------------------------------
M numbers
-stage this change? [y/n] n
+stage this change? [y/n/q] n
-----------------------------------------------
@@ -4,7 +4,7 @@
4
@@ -1253,7 +1253,7 @@ stage this change? [y/n] n
10
-----------------------------------------------
M numbers
-stage this change? [y/n] y
+stage this change? [y/n/q] y
-----------------------------------------------
@@ -13,4 +13,4 @@
13
@@ -1263,7 +1263,7 @@ stage this change? [y/n] y
+c
-----------------------------------------------
M numbers
-stage this change? [y/n] n
+stage this change? [y/n/q] n
EOF
cmp -s $testroot/stdout.expected $testroot/stdout
ret="$?"
@@ -1346,7 +1346,7 @@ EOF
5
-----------------------------------------------
M numbers
-stage this change? [y/n] n
+stage this change? [y/n/q] n
-----------------------------------------------
@@ -4,7 +4,7 @@
4
@@ -1359,7 +1359,7 @@ stage this change? [y/n] n
10
-----------------------------------------------
M numbers
-stage this change? [y/n] n
+stage this change? [y/n/q] n
-----------------------------------------------
@@ -13,4 +13,4 @@
13
@@ -1369,7 +1369,7 @@ stage this change? [y/n] n
+c
-----------------------------------------------
M numbers
-stage this change? [y/n] y
+stage this change? [y/n/q] y
EOF
cmp -s $testroot/stdout.expected $testroot/stdout
ret="$?"
@@ -1435,7 +1435,7 @@ function test_stage_patch_added {
epsilon/new > $testroot/stdout)
echo "A epsilon/new" > $testroot/stdout.expected
- echo "stage this addition? [y/n] y" >> $testroot/stdout.expected
+ echo "stage this addition? [y/n/q] y" >> $testroot/stdout.expected
cmp -s $testroot/stdout.expected $testroot/stdout
ret="$?"
if [ "$ret" != "0" ]; then
@@ -1494,7 +1494,7 @@ function test_stage_patch_removed {
echo -n > $testroot/stdout.expected
echo "D beta" > $testroot/stdout.expected
- echo "stage deletion? [y/n] y" >> $testroot/stdout.expected
+ echo "stage deletion? [y/n/q] y" >> $testroot/stdout.expected
cmp -s $testroot/stdout.expected $testroot/stdout
ret="$?"
if [ "$ret" != "0" ]; then
@@ -1533,6 +1533,108 @@ function test_stage_patch_removed {
test_done "$testroot" "$ret"
}
+function test_stage_patch_quit {
+ local testroot=`test_init stage_patch_quit`
+
+ jot 16 > $testroot/repo/numbers
+ (cd $testroot/repo && git add numbers)
+ git_commit $testroot/repo -m "added numbers file"
+ 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
+
+ # stage first hunk and quit
+ printf "y\nq\n" > $testroot/patchscript
+ (cd $testroot/wt && got stage -F $testroot/patchscript -p \
+ numbers > $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
+stage this change? [y/n/q] y
+-----------------------------------------------
+@@ -4,7 +4,7 @@
+ 4
+ 5
+ 6
+-7
++b
+ 8
+ 9
+ 10
+-----------------------------------------------
+M numbers
+stage this change? [y/n/q] q
+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
+ 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
+ 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
+ fi
+ test_done "$testroot" "$ret"
+
+}
+
run_test test_stage_basic
run_test test_stage_no_changes
run_test test_stage_list
@@ -1552,3 +1654,4 @@ run_test test_stage_commit
run_test test_stage_patch
run_test test_stage_patch_added
run_test test_stage_patch_removed
+run_test test_stage_patch_quit