Commit Diff


commit - 4264b841d71d67329a7bf291a2f6565fac246668
commit + 9fdde394b8229cc24073709050d2ef7a1443b0a9
blob - c8e98cc3b117b9e02d199a6a46a60f02d73c2b83
blob + 0fefe7552a676b9488741693766a6ec76dcacf60
--- lib/worktree.c
+++ lib/worktree.c
@@ -8088,6 +8088,17 @@ stage_path(void *arg, unsigned char status,
 		err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
 		    get_staged_status(ie), relpath, blob_id,
 		    new_staged_blob_id, NULL, dirfd, de_name);
+		if (err)
+			break;
+		/*
+		 * When staging the reverse of the staged diff,
+		 * implicitly unstage the file.
+		 */
+		if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
+		    sizeof(ie->blob_sha1)) == 0) {
+			got_fileindex_entry_stage_set(ie,
+			    GOT_FILEIDX_STAGE_NONE);
+		}
 		break;
 	case GOT_STATUS_DELETE:
 		if (staged_status == GOT_STATUS_DELETE)
blob - 701e5bded8367b6fc13908410c4111d0e1c82210
blob + de1799daf93b3a96d8c0045ef984aae3eab4b368
--- regress/cmdline/stage.sh
+++ regress/cmdline/stage.sh
@@ -2146,10 +2146,63 @@ test_stage_patch_removed_twice() {
 	fi
 
 	echo -n > $testroot/stdout.expected
+	cmp -s $testroot/stdout.expected $testroot/stdout
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+	fi
+	test_done "$testroot" "$ret"
+}
+
+test_stage_patch_reversed() {
+	local testroot=`test_init stage_patch_reversed`
+
+	got checkout $testroot/repo $testroot/wt > /dev/null
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo 'ALPHA' > $testroot/wt/alpha
+	(cd $testroot/wt && got stage alpha > $testroot/stdout)
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo ' M alpha' > $testroot/stdout.expected
+	cmp -s $testroot/stdout.expected $testroot/stdout
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo 'alpha' > $testroot/wt/alpha
+	(cd $testroot/wt && got stage alpha > $testroot/stdout)
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo ' M alpha' > $testroot/stdout.expected
 	cmp -s $testroot/stdout.expected $testroot/stdout
 	ret=$?
 	if [ $ret -ne 0 ]; then
 		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	(cd $testroot/wt && got status > $testroot/stdout)
+	cmp -s /dev/null $testroot/stdout
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		diff -u /dev/null $testroot/stdout
 	fi
 	test_done "$testroot" "$ret"
 }
@@ -2987,6 +3040,7 @@ run_test test_stage_patch_added
 run_test test_stage_patch_added_twice
 run_test test_stage_patch_removed
 run_test test_stage_patch_removed_twice
+run_test test_stage_patch_reversed
 run_test test_stage_patch_quit
 run_test test_stage_patch_incomplete_script
 run_test test_stage_symlink