Commit 692bdcc498e440f1604cf7ba8a5cac22966c2ece

Stefan Sperling 2022-01-25T22:48:26

make 'got rm' report an "unexpected status" error for unversioned files ok millert@

diff --git a/lib/worktree.c b/lib/worktree.c
index 7b9584f..02ace08 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -3978,7 +3978,7 @@ schedule_for_deletion(void *arg, unsigned char status,
 
 	ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
 	if (ie == NULL)
-		return got_error_path(relpath, GOT_ERR_BAD_PATH);
+		return got_error_path(relpath, GOT_ERR_FILE_STATUS);
 
 	staged_status = get_staged_status(ie);
 	if (staged_status != GOT_STATUS_NO_CHANGE) {
diff --git a/regress/cmdline/rm.sh b/regress/cmdline/rm.sh
index 8b4c659..f82d2c0 100755
--- a/regress/cmdline/rm.sh
+++ b/regress/cmdline/rm.sh
@@ -213,6 +213,66 @@ test_rm_and_add_elsewhere() {
 		return 1
 	fi
 
+	# While here, test behaviour of rm on files in unversioned status.
+	(cd $testroot/wt && got rm epsilon/alpha > $testroot/stdout \
+		2> $testroot/stderr)
+	ret="$?"
+	if [ "$ret" == "0" ]; then
+		echo "got rm command succeeded unexpectedly" >&2
+		diff -u $testroot/stderr.expected $testroot/stderr
+		test_done "$testroot" "1"
+		return 1
+	fi
+
+	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 "got: epsilon/alpha: file has unexpected status" \
+		> $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
+
+	# And test the same case with -f.
+	(cd $testroot/wt && got rm -f epsilon/alpha > $testroot/stdout \
+		2> $testroot/stderr)
+	ret="$?"
+	if [ "$ret" == "0" ]; then
+		echo "got rm command succeeded unexpectedly" >&2
+		diff -u $testroot/stderr.expected $testroot/stderr
+		test_done "$testroot" "1"
+		return 1
+	fi
+
+	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 "got: epsilon/alpha: file has unexpected status" \
+		> $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
+
 	echo 'A  epsilon/alpha' > $testroot/stdout.expected
 	(cd $testroot/wt && got add epsilon/alpha > $testroot/stdout)