Commit 8b13ce36c3571db545dcba7fe278009d7214dace

Stefan Sperling 2019-08-08T13:02:20

fix and test stage/unstage with unversioned files

diff --git a/lib/worktree.c b/lib/worktree.c
index 070abf5..c04d60e 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -5126,6 +5126,9 @@ check_stage_ok(void *arg, unsigned char status,
 	struct got_object_id *base_commit_idp = NULL;
 	char *in_repo_path = NULL, *p;
 
+	if (status == GOT_STATUS_UNVERSIONED)
+		return NULL;
+
 	ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
 	if (ie == NULL)
 		return got_error_path(relpath, GOT_ERR_FILE_STATUS);
@@ -5477,6 +5480,9 @@ stage_path(void *arg, unsigned char status,
 	char *ondisk_path = NULL, *path_content = NULL;
 	uint32_t stage;
 
+	if (status == GOT_STATUS_UNVERSIONED)
+		return NULL;
+
 	ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
 	if (ie == NULL)
 		return got_error_path(relpath, GOT_ERR_FILE_STATUS);
@@ -5815,7 +5821,7 @@ unstage_path(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);
 
 	if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
 	    == -1)
diff --git a/regress/cmdline/stage.sh b/regress/cmdline/stage.sh
index 9ef34a4..ee91795 100755
--- a/regress/cmdline/stage.sh
+++ b/regress/cmdline/stage.sh
@@ -82,6 +82,68 @@ function test_stage_no_changes {
 	test_done "$testroot" "$ret"
 }
 
+function test_stage_unversioned {
+	local testroot=`test_init stage_unversioned`
+
+	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
+	touch $testroot/wt/unversioned-file
+
+	(cd $testroot/wt && got status > $testroot/stdout)
+	echo "M  alpha" > $testroot/stdout.expected
+	echo "?  unversioned-file" >> $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
+
+	(cd $testroot/wt && got stage > $testroot/stdout)
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		echo "got stage command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo " M 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 "modified file again" > $testroot/wt/alpha
+
+	(cd $testroot/wt && got stage unversioned-file > $testroot/stdout \
+		2> $testroot/stderr)
+	ret="$?"
+	if [ "$ret" == "0" ]; then
+		echo "got stage command succeed unexpectedly" >&2
+		test_done "$testroot" "1"
+		return 1
+	fi
+
+	echo "got: no changes to stage" > $testroot/stderr.expected
+	cmp -s $testroot/stderr.expected $testroot/stderr
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stderr.expected $testroot/stderr
+	fi
+	test_done "$testroot" "$ret"
+
+}
+
 function test_stage_list {
 	local testroot=`test_init stage_list`
 
@@ -1739,6 +1801,7 @@ EOF
 
 run_test test_stage_basic
 run_test test_stage_no_changes
+run_test test_stage_unversioned
 run_test test_stage_list
 run_test test_stage_conflict
 run_test test_stage_out_of_date
diff --git a/regress/cmdline/unstage.sh b/regress/cmdline/unstage.sh
index 9c6d712..3d3129e 100755
--- a/regress/cmdline/unstage.sh
+++ b/regress/cmdline/unstage.sh
@@ -67,6 +67,84 @@ function test_unstage_basic {
 	test_done "$testroot" "$ret"
 }
 
+function test_unstage_unversioned {
+	local testroot=`test_init unstage_unversioned`
+
+	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)
+
+	echo ' M alpha' > $testroot/stdout.expected
+	echo ' D beta' >> $testroot/stdout.expected
+	echo ' A foo' >> $testroot/stdout.expected
+	(cd $testroot/wt && got stage > /dev/null)
+
+	touch $testroot/wt/unversioned-file
+
+	(cd $testroot/wt && got status > $testroot/stdout)
+	echo ' M alpha' > $testroot/stdout.expected
+	echo ' D beta' >> $testroot/stdout.expected
+	echo ' A foo' >> $testroot/stdout.expected
+	echo "?  unversioned-file" >> $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
+
+	(cd $testroot/wt && got unstage > $testroot/stdout)
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		echo "got unstage command failed unexpectedly" >&2
+		test_done "$testroot" "1"
+		return 1
+	fi
+
+	echo 'G  alpha' > $testroot/stdout.expected
+	echo 'D  beta' >> $testroot/stdout.expected
+	echo 'G  foo' >> $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
+
+	(cd $testroot/wt && got stage > /dev/null)
+
+	# unstaging an unversioned path is a no-op
+	(cd $testroot/wt && got unstage unversioned > $testroot/stdout)
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		echo "got unstage command failed unexpectedly" >&2
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo ' M alpha' > $testroot/stdout.expected
+	echo ' D beta' >> $testroot/stdout.expected
+	echo ' A foo' >> $testroot/stdout.expected
+	echo "?  unversioned-file" >> $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"
+}
+
 function test_unstage_patch {
 	local testroot=`test_init unstage_patch`
 
@@ -838,6 +916,7 @@ EOF
 }
 
 run_test test_unstage_basic
+run_test test_unstage_unversioned
 run_test test_unstage_patch
 run_test test_unstage_patch_added
 run_test test_unstage_patch_removed