Commit d3bcc3d18ab565811e1da723157c85657570b010

Stefan Sperling 2019-08-08T17:27:36

make revert of a staged deletion a no-op for better interaction with -R

diff --git a/lib/worktree.c b/lib/worktree.c
index 092546c..1fdda69 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -2707,16 +2707,15 @@ revert_file(void *arg, unsigned char status, unsigned char staged_status,
 	char *ondisk_path = NULL;
 	struct got_blob_object *blob = NULL;
 
+	/* Reverting a staged deletion is a no-op. */
+	if (status == GOT_STATUS_DELETE &&
+	    staged_status != GOT_STATUS_NO_CHANGE)
+		return NULL;
+
 	ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
 	if (ie == NULL)
 		return got_error(GOT_ERR_BAD_PATH);
 
-	if (status == GOT_STATUS_DELETE &&
-	    staged_status != GOT_STATUS_NO_CHANGE) {
-		err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
-		goto done;
-	}
-
 	/* Construct in-repository path of tree which contains this blob. */
 	err = got_path_dirname(&parent_path, ie->path);
 	if (err) {
diff --git a/regress/cmdline/stage.sh b/regress/cmdline/stage.sh
index d21ecfe..e7fb40f 100755
--- a/regress/cmdline/stage.sh
+++ b/regress/cmdline/stage.sh
@@ -708,13 +708,22 @@ function test_stage_revert {
 	(cd $testroot/wt && got revert beta > $testroot/stdout \
 		2> $testroot/stderr)
 	ret="$?"
-	if [ "$ret" == "0" ]; then
-		echo "revert command succeeded unexpectedly" >&2
-		test_done "$testroot" "1"
+	if [ "$ret" != "0" ]; then
+		echo "revert command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
 		return 1
 	fi
 
-	echo "got: beta: file is staged" > $testroot/stderr.expected
+	echo -n > $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
+
+	echo -n > $testroot/stderr.expected
 	cmp -s $testroot/stderr.expected $testroot/stderr
 	ret="$?"
 	if [ "$ret" != "0" ]; then
@@ -807,13 +816,14 @@ function test_stage_revert {
 	(cd $testroot/wt && got revert -R . > $testroot/stdout \
 		2> $testroot/stderr)
 	ret="$?"
-	if [ "$ret" == "0" ]; then
-		echo "revert command succeeded unexpectedly" >&2
+	if [ "$ret" != "0" ]; then
+		echo "revert command failed unexpectedly" >&2
 		test_done "$testroot" "$ret"
 		return 1
 	fi
 
 	echo "R  alpha" > $testroot/stdout.expected
+	echo "R  foo" >> $testroot/stdout.expected
 	cmp -s $testroot/stdout.expected $testroot/stdout
 	ret="$?"
 	if [ "$ret" != "0" ]; then
@@ -822,7 +832,7 @@ function test_stage_revert {
 		return 1
 	fi
 
-	echo "got: beta: file is staged" > $testroot/stderr.expected
+	echo -n > $testroot/stderr.expected
 	cmp -s $testroot/stderr.expected $testroot/stderr
 	ret="$?"
 	if [ "$ret" != "0" ]; then
@@ -833,7 +843,7 @@ function test_stage_revert {
 
 	echo ' M alpha' > $testroot/stdout.expected
 	echo ' D beta' >> $testroot/stdout.expected
-	echo 'MA foo' >> $testroot/stdout.expected
+	echo ' A foo' >> $testroot/stdout.expected
 	(cd $testroot/wt && got status > $testroot/stdout)
 	cmp -s $testroot/stdout.expected $testroot/stdout
 	ret="$?"