Commit 244725f25f8754428aebce708d49d3ea834faf90

Stefan Sperling 2019-08-03T15:20:09

make 'got status' indicate changes relative to staged files

diff --git a/got/got.c b/got/got.c
index dae6672..0cde839 100644
--- a/got/got.c
+++ b/got/got.c
@@ -2368,8 +2368,7 @@ print_status(void *arg, unsigned char status, unsigned char staged_status,
     const char *path, struct got_object_id *blob_id,
     struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
 {
-	if (status == staged_status &&
-	    (status == GOT_STATUS_ADD || status == GOT_STATUS_DELETE))
+	if (status == staged_status && (status == GOT_STATUS_DELETE))
 		status = GOT_STATUS_NO_CHANGE;
 	printf("%c%c %s\n", status, staged_status, path);
 	return NULL;
diff --git a/lib/worktree.c b/lib/worktree.c
index 4b2e439..d402893 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -1084,7 +1084,8 @@ get_file_status(unsigned char *status, struct stat *sb,
 	if (!got_fileindex_entry_has_file_on_disk(ie)) {
 		*status = GOT_STATUS_DELETE;
 		return NULL;
-	} else if (!got_fileindex_entry_has_blob(ie)) {
+	} else if (!got_fileindex_entry_has_blob(ie) &&
+	    staged_status != GOT_STATUS_ADD) {
 		*status = GOT_STATUS_ADD;
 		return NULL;
 	}
diff --git a/regress/cmdline/stage.sh b/regress/cmdline/stage.sh
index 5aa5444..5bce7df 100755
--- a/regress/cmdline/stage.sh
+++ b/regress/cmdline/stage.sh
@@ -76,8 +76,52 @@ function test_stage_status {
 	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
+	echo "modified added file again" >> $testroot/wt/foo
+
+	echo 'MM alpha' > $testroot/stdout.expected
+	echo ' D beta' >> $testroot/stdout.expected
+	echo 'A  epsilon/new' >> $testroot/stdout.expected
+	echo 'M  epsilon/zeta' >> $testroot/stdout.expected
+	echo 'MA foo' >> $testroot/stdout.expected
+	echo 'D  gamma/delta' >> $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
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	# test no-op change of added file with new stat(2) timestamp
+	echo "new file" > $testroot/wt/foo
+	echo ' A foo' > $testroot/stdout.expected
+	(cd $testroot/wt && got status foo > $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
+
+	# test staged deleted file which is restored on disk
+	echo "new file" > $testroot/wt/beta
+	echo ' D beta' > $testroot/stdout.expected
+	(cd $testroot/wt && got status beta > $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