Commit f0b0c0cee4bcde602c12bf5a2be6bd107a7ced66

Stefan Sperling 2019-08-04T18:41:02

fix 'mv foo bar; got rm foo; got add bar' and put paths in error messages

diff --git a/lib/worktree.c b/lib/worktree.c
index 86889ab..d52d55e 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -2597,13 +2597,14 @@ schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
 	if (status != GOT_STATUS_NO_CHANGE) {
 		if (status == GOT_STATUS_DELETE)
 			return NULL;
-		if (status != GOT_STATUS_MODIFY)
-			return got_error(GOT_ERR_FILE_STATUS);
-		if (!delete_local_mods)
-			return got_error(GOT_ERR_FILE_MODIFIED);
+		if (status == GOT_STATUS_MODIFY && !delete_local_mods)
+			return got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
+		if (status != GOT_STATUS_MODIFY &&
+		    status != GOT_STATUS_MISSING)
+			return got_error_path(relpath, GOT_ERR_FILE_STATUS);
 	}
 
-	if (unlink(ondisk_path) != 0)
+	if (status != GOT_STATUS_MISSING && unlink(ondisk_path) != 0)
 		return got_error_from_errno2("unlink", ondisk_path);
 
 	got_fileindex_entry_mark_deleted_from_disk(ie);
diff --git a/regress/cmdline/rm.sh b/regress/cmdline/rm.sh
index 8d663c8..03c8d8f 100755
--- a/regress/cmdline/rm.sh
+++ b/regress/cmdline/rm.sh
@@ -70,7 +70,8 @@ function test_rm_with_local_mods {
 	fi
 
 	echo "modified beta" > $testroot/wt/beta
-	echo 'got: file contains modifications' > $testroot/stderr.expected
+	echo 'got: beta: file contains modifications' \
+		> $testroot/stderr.expected
 	(cd $testroot/wt && got rm beta 2>$testroot/stderr)
 
 	cmp -s $testroot/stderr.expected $testroot/stderr
@@ -134,6 +135,65 @@ function test_double_rm {
 	test_done "$testroot" "0"
 }
 
+function test_rm_and_add_elsewhere {
+	local testroot=`test_init rm_and_add_elsewhere`
+
+	got checkout $testroot/repo $testroot/wt > /dev/null
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	(cd $testroot/wt && mv alpha epsilon/)
+
+	(cd $testroot/wt && got status > $testroot/stdout)
+
+	echo '!  alpha' > $testroot/stdout.expected
+	echo '?  epsilon/alpha' >> $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 'D  alpha' > $testroot/stdout.expected
+	(cd $testroot/wt && got rm alpha > $testroot/stdout)
+
+	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 'A  epsilon/alpha' > $testroot/stdout.expected
+	(cd $testroot/wt && got add epsilon/alpha > $testroot/stdout)
+
+	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 'D  alpha' > $testroot/stdout.expected
+	echo 'A  epsilon/alpha' >> $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_rm_basic
 run_test test_rm_with_local_mods
 run_test test_double_rm
+run_test test_rm_and_add_elsewhere