Commit eba70f3832e0457ba487bbe50a3d8e30dca1f15f

Stefan Sperling 2019-08-08T09:01:40

verify stage -p behaviour with incomplete response script

diff --git a/got/got.c b/got/got.c
index 022dc36..39818d9 100644
--- a/got/got.c
+++ b/got/got.c
@@ -5241,6 +5241,9 @@ choose_patch(int *choice, void *arg, unsigned char status, const char *path,
 
 	if (patch_script_file) {
 		char *nl;
+		err = show_change(status, path, patch_file, n, nchanges);
+		if (err)
+			return err;
 		linelen = getline(&line, &linesize, patch_script_file);
 		if (linelen == -1) {
 			if (ferror(patch_script_file))
@@ -5250,9 +5253,6 @@ choose_patch(int *choice, void *arg, unsigned char status, const char *path,
 		nl = strchr(line, '\n');
 		if (nl)
 			*nl = '\0';
-		err = show_change(status, path, patch_file, n, nchanges);
-		if (err)
-			return err;
 		if (strcmp(line, "y") == 0) {
 			*choice = GOT_PATCH_CHOICE_YES;
 			printf("y\n");
diff --git a/regress/cmdline/stage.sh b/regress/cmdline/stage.sh
index 595696a..8c98d12 100755
--- a/regress/cmdline/stage.sh
+++ b/regress/cmdline/stage.sh
@@ -1641,6 +1641,100 @@ EOF
 
 }
 
+function test_stage_patch_incomplete_script {
+	local testroot=`test_init stage_incomplete_script`
+
+	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
+
+	# stage first hunk and then stop responding; got should error out
+	printf "y\n" > $testroot/patchscript
+	(cd $testroot/wt && got stage -F $testroot/patchscript -p \
+		> $testroot/stdout 2> $testroot/stderr)
+	ret="$?"
+	if [ "$ret" == "0" ]; then
+		echo "got stage command succeeded 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)
+stage this change? [y/n/q] y
+-----------------------------------------------
+@@ -4,7 +4,7 @@
+ 4
+ 5
+ 6
+-7
++b
+ 8
+ 9
+ 10
+-----------------------------------------------
+M  numbers (change 2 of 3)
+EOF
+	echo -n "stage this change? [y/n/q] " >> $testroot/stdout.expected
+	echo "got: invalid patch choice" > $testroot/stderr.expected
+	cmp -s $testroot/stderr.expected $testroot/stderr
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stderr.expected $testroot/stderr
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	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 "M  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 -n > $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
@@ -1661,3 +1755,4 @@ run_test test_stage_patch
 run_test test_stage_patch_added
 run_test test_stage_patch_removed
 run_test test_stage_patch_quit
+run_test test_stage_patch_incomplete_script