Commit Diff


commit - 88b1b4908e8a15350682e086f24a4b77cdfed141
commit + f289c82c68db93521308bd7bc6bb7a5733d80a26
blob - 908852abb12a7d7b09284cf1c39d1140d94eb6a7
blob + 1ead68d8ef23d037e71df862ec8e81b3b66610b0
--- lib/worktree.c
+++ lib/worktree.c
@@ -8085,6 +8085,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 - 6d7a319d9ee299c535652176799ee290326537fa
blob + 8255dfd0388d7d739afccf6c36583e08ef5ca252
--- regress/cmdline/stage.sh
+++ regress/cmdline/stage.sh
@@ -2143,10 +2143,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"
 }
@@ -2984,6 +3037,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