Path: blob/main/contrib/libdiff/test/expect019.diff
35066 views
--- test019.left.txt1+++ test019.right.txt2@@ -40,8 +40,23 @@3#include "got_lib_object.h"45static const struct got_error *6-diff_blobs(struct got_diffreg_result **resultp,7-struct got_blob_object *blob1, struct got_blob_object *blob2,8+add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)9+{10+ off_t *p;11+12+ p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));13+ if (p == NULL)14+ return got_error_from_errno("reallocarray");15+ *line_offsets = p;16+ (*line_offsets)[*nlines] = off;17+ (*nlines)++;18+ return NULL;19+}20+21+static const struct got_error *22+diff_blobs(off_t **line_offsets, size_t *nlines,23+ struct got_diffreg_result **resultp, struct got_blob_object *blob1,24+ struct got_blob_object *blob2,25const char *label1, const char *label2, mode_t mode1, mode_t mode2,26int diff_context, int ignore_whitespace, FILE *outfile)27{28@@ -52,7 +67,12 @@29char *idstr1 = NULL, *idstr2 = NULL;30size_t size1, size2;31struct got_diffreg_result *result;32+ off_t outoff = 0;33+ int n;3435+ if (line_offsets && *line_offsets && *nlines > 0)36+ outoff = (*line_offsets)[*nlines - 1];37+38if (resultp)39*resultp = NULL;4041@@ -116,10 +136,32 @@42goto done;43}44}45- fprintf(outfile, "blob - %s%s\n", idstr1,46+ n = fprintf(outfile, "blob - %s%s\n", idstr1,47modestr1 ? modestr1 : "");48- fprintf(outfile, "blob + %s%s\n", idstr2,49+ if (n < 0) {50+ err = got_error_from_errno("fprintf");51+ goto done;52+ }53+ outoff += n;54+ if (line_offsets) {55+ err = add_line_offset(line_offsets, nlines, outoff);56+ if (err)57+ goto done;58+ }59+60+ n = fprintf(outfile, "blob + %s%s\n", idstr2,61modestr2 ? modestr2 : "");62+ if (n < 0) {63+ err = got_error_from_errno("fprintf");64+ goto done;65+ }66+ outoff += n;67+ if (line_offsets) {68+ err = add_line_offset(line_offsets, nlines, outoff);69+ if (err)70+ goto done;71+ }72+73free(modestr1);74free(modestr2);75}76@@ -129,7 +171,7 @@77goto done;7879if (outfile) {80- err = got_diffreg_output(NULL, NULL, result, f1, f2,81+ err = got_diffreg_output(line_offsets, nlines, result, f1, f2,82label1 ? label1 : idstr1,83label2 ? label2 : idstr2,84GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);85@@ -158,21 +200,21 @@86struct got_object_id *id2, const char *label1, const char *label2,87mode_t mode1, mode_t mode2, struct got_repository *repo)88{89- const struct got_error *err;90struct got_diff_blob_output_unidiff_arg *a = arg;9192- err = diff_blobs(NULL, blob1, blob2, label1, label2, mode1, mode2,93- a->diff_context, a->ignore_whitespace, a->outfile);94- return err;95+ return diff_blobs(&a->line_offsets, &a->nlines, NULL,96+ blob1, blob2, label1, label2, mode1, mode2, a->diff_context,97+ a->ignore_whitespace, a->outfile);98}99100const struct got_error *101-got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,102+got_diff_blob(off_t **line_offsets, size_t *nlines,103+ struct got_blob_object *blob1, struct got_blob_object *blob2,104const char *label1, const char *label2, int diff_context,105int ignore_whitespace, FILE *outfile)106{107- return diff_blobs(NULL, blob1, blob2, label1, label2, 0, 0, diff_context,108- ignore_whitespace, outfile);109+ return diff_blobs(line_offsets, nlines, NULL, blob1, blob2,110+ label1, label2, 0, 0, diff_context, ignore_whitespace, outfile);111}112113static const struct got_error *114@@ -259,7 +301,8 @@115{116const struct got_error *err = NULL;117118- err = diff_blobs(result, blob1, blob2, NULL, NULL, 0, 0, 3, 0, NULL);119+ err = diff_blobs(NULL, NULL, result, blob1, blob2,120+ NULL, NULL, 0, 0, 3, 0, NULL);121if (err) {122got_diffreg_result_free(*result);123*result = NULL;124@@ -702,7 +745,8 @@125}126127const struct got_error *128-got_diff_objects_as_blobs(struct got_object_id *id1, struct got_object_id *id2,129+got_diff_objects_as_blobs(off_t **line_offsets, size_t *nlines,130+ struct got_object_id *id1, struct got_object_id *id2,131const char *label1, const char *label2, int diff_context,132int ignore_whitespace, struct got_repository *repo, FILE *outfile)133{134@@ -722,8 +766,8 @@135if (err)136goto done;137}138- err = got_diff_blob(blob1, blob2, label1, label2, diff_context,139- ignore_whitespace, outfile);140+ err = got_diff_blob(line_offsets, nlines, blob1, blob2,141+ label1, label2, diff_context, ignore_whitespace, outfile);142done:143if (blob1)144got_object_blob_close(blob1);145@@ -733,13 +777,15 @@146}147148const struct got_error *149-got_diff_objects_as_trees(struct got_object_id *id1, struct got_object_id *id2,150+got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,151+ struct got_object_id *id1, struct got_object_id *id2,152char *label1, char *label2, int diff_context, int ignore_whitespace,153struct got_repository *repo, FILE *outfile)154{155const struct got_error *err;156struct got_tree_object *tree1 = NULL, *tree2 = NULL;157struct got_diff_blob_output_unidiff_arg arg;158+ int want_lineoffsets = (line_offsets != NULL && *line_offsets != NULL);159160if (id1 == NULL && id2 == NULL)161return got_error(GOT_ERR_NO_OBJ);162@@ -757,8 +803,20 @@163arg.diff_context = diff_context;164arg.ignore_whitespace = ignore_whitespace;165arg.outfile = outfile;166+ if (want_lineoffsets) {167+ arg.line_offsets = *line_offsets;168+ arg.nlines = *nlines;169+ } else {170+ arg.line_offsets = NULL;171+ arg.nlines = 0;172+ }173err = got_diff_tree(tree1, tree2, label1, label2, repo,174got_diff_blob_output_unidiff, &arg, 1);175+176+ if (want_lineoffsets) {177+ *line_offsets = arg.line_offsets; /* was likely re-allocated */178+ *nlines = arg.nlines;179+ }180done:181if (tree1)182got_object_tree_close(tree1);183@@ -768,8 +826,9 @@184}185186const struct got_error *187-got_diff_objects_as_commits(struct got_object_id *id1,188- struct got_object_id *id2, int diff_context, int ignore_whitespace,189+got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,190+ struct got_object_id *id1, struct got_object_id *id2,191+ int diff_context, int ignore_whitespace,192struct got_repository *repo, FILE *outfile)193{194const struct got_error *err;195@@ -788,7 +847,7 @@196if (err)197goto done;198199- err = got_diff_objects_as_trees(200+ err = got_diff_objects_as_trees(line_offsets, nlines,201commit1 ? got_object_commit_get_tree_id(commit1) : NULL,202got_object_commit_get_tree_id(commit2), "", "", diff_context,203ignore_whitespace, repo, outfile);204205206