Commit b353a198c5844a2e9b078ac654b6a61de00b5a13

Stefan Sperling 2019-08-07T17:40:15

add 'q' response to 'got stage -p'

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