commit 4ce46740492b52299407d11c2571029be8ad295e from: Stefan Sperling date: Thu Aug 08 10:44:18 2019 UTC label staged blobs with "(staged)" in got diff output commit - 19e4b90712e9a185c4562e0563aa5a34dc6f549c commit + 4ce46740492b52299407d11c2571029be8ad295e blob - c140d3621c853710a48a9125b23fc43d77e61798 blob + 79ade469edb490ca280a4a6535c55aae69bee2cd --- got/got.c +++ got/got.c @@ -1710,7 +1710,7 @@ print_diff(void *arg, unsigned char status, unsigned c 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 c } 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 c } 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); blob - 0d86e3c0ca9aac48559bab6118c9bcac82470866 blob + 927863bea9c15cfb8336083f49201f2c717f2c89 --- include/got_diff.h +++ include/got_diff.h @@ -28,10 +28,11 @@ const struct got_error *got_diff_blob(struct got_blob_ * 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 blob - 6b7e4d457bb9821c3ae3352917a57558486181b0 blob + 6d6f3dec0067739174d0bd9beae75cd22a7d7fe9 --- lib/diff.c +++ 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 * blob - f743dbf50f1eccc4c81a1a1f4775856cce86fa07 blob + 9ef34a4c6457e4c828e91308a00f248632f7ef1d --- regress/cmdline/stage.sh +++ 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 blob - 07c730e3c86b7e3438b0a1816df6c880fc5644fd blob + 9c6d712c170e967cce17f30f21f7152824868767 --- regress/cmdline/unstage.sh +++ 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 < $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 < $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