Commit a76c42e670c040cbc63fae5cedba54de3540ce3c

Stefan Sperling 2019-08-03T22:13:28

ensure that 'got update' refuses to update staged files

diff --git a/lib/worktree.c b/lib/worktree.c
index c62f844..0115f06 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -1185,6 +1185,10 @@ update_blob(struct got_worktree *worktree,
 		return got_error_from_errno("asprintf");
 
 	if (ie) {
+		if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
+			err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
+			goto done;
+		}
 		err = get_file_status(&status, &sb, ie, ondisk_path, repo);
 		if (err)
 			goto done;
@@ -1308,6 +1312,9 @@ delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
 	struct stat sb;
 	char *ondisk_path;
 
+	if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
+		return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
+
 	if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
 	    == -1)
 		return got_error_from_errno("asprintf");
diff --git a/regress/cmdline/stage.sh b/regress/cmdline/stage.sh
index 87156b3..4d057d7 100755
--- a/regress/cmdline/stage.sh
+++ b/regress/cmdline/stage.sh
@@ -852,6 +852,48 @@ function test_stage_rebase {
 	test_done "$testroot" "$ret"
 }
 
+function test_stage_update {
+	local testroot=`test_init stage_update`
+
+	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 stage alpha > /dev/null)
+
+	echo "modified alpha" > $testroot/repo/alpha
+	git_commit $testroot/repo -m "modified alpha"
+
+	(cd $testroot/wt && got update > $testroot/stdout  \
+		2> $testroot/stderr)
+	ret="$?"
+	if [ "$ret" == "0" ]; then
+		echo "got update command succeeded unexpectedly" >&2
+		test_done "$testroot" "1"
+		return 1
+	fi
+
+	echo -n > $testroot/stdout.expected
+	echo "got: alpha: file is staged" > $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
+	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_conflict
@@ -864,3 +906,4 @@ run_test test_stage_revert
 run_test test_stage_diff
 run_test test_stage_histedit
 run_test test_stage_rebase
+run_test test_stage_update