Commit 4ce46740492b52299407d11c2571029be8ad295e

Stefan Sperling 2019-08-08T12:44:18

label staged blobs with "(staged)" in got diff output

diff --git a/got/got.c b/got/got.c
index c140d36..79ade46 100644
--- a/got/got.c
+++ b/got/got.c
@@ -1710,7 +1710,7 @@ print_diff(void *arg, unsigned char status, unsigned char staged_status,
 	const struct got_error *err = NULL;
 	struct got_blob_object *blob1 = NULL;
 	FILE *f2 = NULL;
-	char *abspath = NULL;
+	char *abspath = NULL, *label1 = NULL;
 	struct stat sb;
 
 	if (a->diff_staged) {
@@ -1756,13 +1756,26 @@ print_diff(void *arg, unsigned char status, unsigned char staged_status,
 	}
 
 	if (staged_status == GOT_STATUS_ADD ||
-	    staged_status == GOT_STATUS_MODIFY)
+	    staged_status == GOT_STATUS_MODIFY) {
+		char *id_str;
 		err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
 		    8192);
-	else if (status != GOT_STATUS_ADD)
+		if (err)
+			goto done;
+		err = got_object_id_str(&id_str, staged_blob_id);
+		if (err)
+			goto done;
+		if (asprintf(&label1, "%s (staged)", id_str) == -1) {
+			err = got_error_from_errno("asprintf");
+			free(id_str);
+			goto done;
+		}
+		free(id_str);
+	} else if (status != GOT_STATUS_ADD) {
 		err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
-	if (err)
-		goto done;
+		if (err)
+			goto done;
+	}
 
 	if (status != GOT_STATUS_DELETE) {
 		if (asprintf(&abspath, "%s/%s",
@@ -1783,8 +1796,8 @@ print_diff(void *arg, unsigned char status, unsigned char staged_status,
 	} else
 		sb.st_size = 0;
 
-	err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
-	    stdout);
+	err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
+	    a->diff_context, stdout);
 done:
 	if (blob1)
 		got_object_blob_close(blob1);
diff --git a/include/got_diff.h b/include/got_diff.h
index 0d86e3c..927863b 100644
--- a/include/got_diff.h
+++ b/include/got_diff.h
@@ -28,10 +28,11 @@ const struct got_error *got_diff_blob(struct got_blob_object *,
  * Compute the differences between a blob and a file and write unified diff
  * text to the provided output file. The file's size must be provided, as
  * well as a const char * diff header label which identifies the file.
+ * An optional const char * diff header label for the blob may be provided, too.
  * The number of context lines to show in the diff must be specified as well.
  */
-const struct got_error *got_diff_blob_file(struct got_blob_object *, FILE *,
-    size_t, const char *, int, FILE *);
+const struct got_error *got_diff_blob_file(struct got_blob_object *,
+    const char *, FILE *, size_t, const char *, int, FILE *);
 
 /*
  * A callback function invoked to handle the differences between two blobs
diff --git a/lib/diff.c b/lib/diff.c
index 6b7e4d4..6d6f3de 100644
--- a/lib/diff.c
+++ b/lib/diff.c
@@ -153,7 +153,7 @@ alloc_changes(struct got_diff_changes **changes)
 
 static const struct got_error *
 diff_blob_file(struct got_diff_changes **changes,
-    struct got_blob_object *blob1, FILE *f2, size_t size2,
+    struct got_blob_object *blob1, const char *label1, FILE *f2, size_t size2,
     const char *label2, int diff_context, FILE *outfile)
 {
 	struct got_diff_state ds;
@@ -205,7 +205,7 @@ diff_blob_file(struct got_diff_changes **changes,
 	flags |= D_PROTOTYPE;
 
 	if (outfile) {
-		fprintf(outfile, "blob - %s\n", idstr1);
+		fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
 		fprintf(outfile, "file + %s\n",
 		    f2 == NULL ? "/dev/null" : label2);
 	}
@@ -224,11 +224,12 @@ done:
 }
 
 const struct got_error *
-got_diff_blob_file(struct got_blob_object *blob1, FILE *f2, size_t size2,
-    const char *label2, int diff_context, FILE *outfile)
+got_diff_blob_file(struct got_blob_object *blob1, const char *label1,
+    FILE *f2, size_t size2, const char *label2, int diff_context,
+    FILE *outfile)
 {
-	return diff_blob_file(NULL, blob1, f2, size2, label2, diff_context,
-	    outfile);
+	return diff_blob_file(NULL, blob1, label1, f2, size2, label2,
+	    diff_context, outfile);
 }
 
 const struct got_error *
diff --git a/regress/cmdline/stage.sh b/regress/cmdline/stage.sh
index f743dbf..9ef34a4 100755
--- a/regress/cmdline/stage.sh
+++ b/regress/cmdline/stage.sh
@@ -766,8 +766,9 @@ function test_stage_diff {
 
 	echo "diff $head_commit $testroot/wt" > $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
-	(cd $testroot/wt && got stage -l alpha) | cut -d' ' -f 1 \
+	(cd $testroot/wt && got stage -l alpha) | cut -d' ' -f 1 | tr -d '\n' \
 		>> $testroot/stdout.expected
+	echo ' (staged)' >> $testroot/stdout.expected
 	echo 'file + alpha' >> $testroot/stdout.expected
 	echo '--- alpha' >> $testroot/stdout.expected
 	echo '+++ alpha' >> $testroot/stdout.expected
@@ -775,8 +776,9 @@ function test_stage_diff {
 	echo '-modified file' >> $testroot/stdout.expected
 	echo '+modified file again' >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
-	(cd $testroot/wt && got stage -l foo) | cut -d' ' -f 1 \
+	(cd $testroot/wt && got stage -l foo) | cut -d' ' -f 1 | tr -d '\n' \
 		>> $testroot/stdout.expected
+	echo " (staged)" >> $testroot/stdout.expected
 	echo 'file + foo' >> $testroot/stdout.expected
 	echo '--- foo' >> $testroot/stdout.expected
 	echo '+++ foo' >> $testroot/stdout.expected
diff --git a/regress/cmdline/unstage.sh b/regress/cmdline/unstage.sh
index 07c730e..9c6d712 100755
--- a/regress/cmdline/unstage.sh
+++ b/regress/cmdline/unstage.sh
@@ -257,8 +257,9 @@ EOF
 	(cd $testroot/wt && got diff > $testroot/stdout)
 	echo "diff $commit_id $testroot/wt" > $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
-	(cd $testroot/wt && got stage -l numbers) | cut -d' ' -f 1 \
-		>> $testroot/stdout.expected
+	(cd $testroot/wt && got stage -l numbers) | cut -d' ' -f 1  | \
+		tr -d '\n' >> $testroot/stdout.expected
+	echo " (staged)" >> $testroot/stdout.expected
 	echo "file + numbers" >> $testroot/stdout.expected
 	cat >> $testroot/stdout.expected <<EOF
 --- numbers
@@ -398,8 +399,9 @@ EOF
 	(cd $testroot/wt && got diff > $testroot/stdout)
 	echo "diff $commit_id $testroot/wt" > $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
-	(cd $testroot/wt && got stage -l numbers) | cut -d' ' -f 1 \
-		>> $testroot/stdout.expected
+	(cd $testroot/wt && got stage -l numbers) | cut -d' ' -f 1 | \
+		tr -d '\n' >> $testroot/stdout.expected
+	echo " (staged)" >> $testroot/stdout.expected
 	echo "file + numbers" >> $testroot/stdout.expected
 	cat >> $testroot/stdout.expected <<EOF
 --- numbers
@@ -769,8 +771,9 @@ EOF
 
 	echo "diff $commit_id $testroot/wt" > $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
-	(cd $testroot/wt && got stage -l numbers) | cut -d' ' -f 1 \
-		>> $testroot/stdout.expected
+	(cd $testroot/wt && got stage -l numbers) | cut -d' ' -f 1 | \
+		tr -d '\n' >> $testroot/stdout.expected
+	echo " (staged)" >> $testroot/stdout.expected
 	echo "file + numbers" >> $testroot/stdout.expected
 	echo "--- numbers" >> $testroot/stdout.expected
 	echo "+++ numbers" >> $testroot/stdout.expected