label staged blobs with "(staged)" in got diff output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
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