Commit 9acbc4faa04684945483940044d1b32175a9471e

Stefan Sperling 2019-08-03T15:50:08

test 'got rm' behaviour on staged files

diff --git a/include/got_error.h b/include/got_error.h
index f37c253..5e0b55d 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -114,6 +114,7 @@
 #define GOT_ERR_HISTEDIT_PATH	98
 #define GOT_ERR_NO_MERGED_PATHS 99
 #define GOT_ERR_COMMIT_BRANCH	100
+#define GOT_ERR_FILE_STAGED	101
 
 static const struct got_error {
 	int code;
@@ -230,6 +231,7 @@ static const struct got_error {
 	{ GOT_ERR_NO_MERGED_PATHS, "empty list of merged paths" },
 	{ GOT_ERR_COMMIT_BRANCH, "will not commit to a branch outside the "
 	    "\"refs/heads/\" reference namespace" },
+	{ GOT_ERR_FILE_STAGED, "file has staged changes" },
 };
 
 /*
diff --git a/lib/worktree.c b/lib/worktree.c
index d402893..9b859b2 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -2550,13 +2550,20 @@ schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
 {
 	const struct got_error *err = NULL;
 	struct got_fileindex_entry *ie = NULL;
-	unsigned char status;
+	unsigned char status, staged_status;
 	struct stat sb;
 
 	ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
 	if (ie == NULL)
 		return got_error(GOT_ERR_BAD_PATH);
 
+	staged_status = get_staged_status(ie);
+	if (staged_status != GOT_STATUS_NO_CHANGE) {
+		if (staged_status == GOT_STATUS_DELETE)
+			return NULL;
+		return got_error_path(relpath, GOT_ERR_FILE_STAGED);
+	}
+
 	err = get_file_status(&status, &sb, ie, ondisk_path, repo);
 	if (err)
 		return err;
diff --git a/regress/cmdline/stage.sh b/regress/cmdline/stage.sh
index f23f8a0..0631a5b 100755
--- a/regress/cmdline/stage.sh
+++ b/regress/cmdline/stage.sh
@@ -191,6 +191,75 @@ function test_stage_add_already_staged_file {
 	test_done "$testroot" "$ret"
 }
 
+function test_stage_rm_already_staged_file {
+	local testroot=`test_init stage_rm_already_staged_file`
+
+	got checkout $testroot/repo $testroot/wt > /dev/null
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "modified file" > $testroot/wt/alpha
+	(cd $testroot/wt && got rm beta > /dev/null)
+	echo "new file" > $testroot/wt/foo
+	(cd $testroot/wt && got add foo > /dev/null)
+
+	(cd $testroot/wt && got stage alpha beta foo > $testroot/stdout)
+
+	(cd $testroot/wt && got rm beta \
+		> $testroot/stdout 2> $testroot/stderr)
+	ret="$?"
+	if [ "$ret" == "0" ]; then
+		echo "got rm command succeeded unexpectedly" >&2
+		test_done "$testroot" "1"
+		return 1
+	fi
+	echo "got: realpath: beta: No such file or directory" \
+		> $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
+
+	for f in alpha foo; do
+		echo "got: $f: file has staged changes" \
+			> $testroot/stderr.expected
+		(cd $testroot/wt && got rm $f \
+			> $testroot/stdout 2> $testroot/stderr)
+		ret="$?"
+		if [ "$ret" == "0" ]; then
+			echo "got rm command succeeded unexpectedly" >&2
+			test_done "$testroot" "1"
+			return 1
+		fi
+		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
+	done
+
+	echo ' M alpha' > $testroot/stdout.expected
+	echo ' D beta' >> $testroot/stdout.expected
+	echo ' A foo' >> $testroot/stdout.expected
+
+	(cd $testroot/wt && got status > $testroot/stdout)
+	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_stage_basic
 run_test test_stage_status
 run_test test_stage_add_already_staged_file
+run_test test_stage_rm_already_staged_file