2 c672cd52 2020-10-30 neels * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
4 c672cd52 2020-10-30 neels * Permission to use, copy, modify, and distribute this software for any
5 c672cd52 2020-10-30 neels * purpose with or without fee is hereby granted, provided that the above
6 c672cd52 2020-10-30 neels * copyright notice and this permission notice appear in all copies.
8 c672cd52 2020-10-30 neels * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 c672cd52 2020-10-30 neels * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 c672cd52 2020-10-30 neels * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 c672cd52 2020-10-30 neels * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 c672cd52 2020-10-30 neels * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 c672cd52 2020-10-30 neels * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 c672cd52 2020-10-30 neels * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 c672cd52 2020-10-30 neels #include <sys/queue.h>
18 c672cd52 2020-10-30 neels #include <sys/stat.h>
20 c672cd52 2020-10-30 neels #include <stdbool.h>
21 c672cd52 2020-10-30 neels #include <stdio.h>
22 c672cd52 2020-10-30 neels #include <stdlib.h>
23 c672cd52 2020-10-30 neels #include <string.h>
24 c672cd52 2020-10-30 neels #include <limits.h>
25 c672cd52 2020-10-30 neels #include <sha1.h>
26 c672cd52 2020-10-30 neels #include <zlib.h>
28 c672cd52 2020-10-30 neels #include "got_object.h"
29 c672cd52 2020-10-30 neels #include "got_repository.h"
30 c672cd52 2020-10-30 neels #include "got_error.h"
31 c672cd52 2020-10-30 neels #include "got_diff.h"
32 c672cd52 2020-10-30 neels #include "got_opentemp.h"
33 c672cd52 2020-10-30 neels #include "got_path.h"
34 c672cd52 2020-10-30 neels #include "got_cancel.h"
35 c672cd52 2020-10-30 neels #include "got_worktree.h"
37 c672cd52 2020-10-30 neels #include "got_lib_diff.h"
38 c672cd52 2020-10-30 neels #include "got_lib_delta.h"
39 c672cd52 2020-10-30 neels #include "got_lib_inflate.h"
40 c672cd52 2020-10-30 neels #include "got_lib_object.h"
42 c672cd52 2020-10-30 neels static const struct got_error *
43 c672cd52 2020-10-30 neels add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
47 c672cd52 2020-10-30 neels p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
48 c672cd52 2020-10-30 neels if (p == NULL)
49 c672cd52 2020-10-30 neels return got_error_from_errno("reallocarray");
50 c672cd52 2020-10-30 neels *line_offsets = p;
51 c672cd52 2020-10-30 neels (*line_offsets)[*nlines] = off;
52 c672cd52 2020-10-30 neels (*nlines)++;
53 c672cd52 2020-10-30 neels return NULL;
56 c672cd52 2020-10-30 neels static const struct got_error *
57 c672cd52 2020-10-30 neels diff_blobs(off_t **line_offsets, size_t *nlines,
58 c672cd52 2020-10-30 neels struct got_diffreg_result **resultp, struct got_blob_object *blob1,
59 c672cd52 2020-10-30 neels struct got_blob_object *blob2,
60 c672cd52 2020-10-30 neels const char *label1, const char *label2, mode_t mode1, mode_t mode2,
61 c672cd52 2020-10-30 neels int diff_context, int ignore_whitespace, FILE *outfile)
63 c672cd52 2020-10-30 neels const struct got_error *err = NULL, *free_err;
64 c672cd52 2020-10-30 neels FILE *f1 = NULL, *f2 = NULL;
65 c672cd52 2020-10-30 neels char hex1[SHA1_DIGEST_STRING_LENGTH];
66 c672cd52 2020-10-30 neels char hex2[SHA1_DIGEST_STRING_LENGTH];
67 c672cd52 2020-10-30 neels char *idstr1 = NULL, *idstr2 = NULL;
68 c672cd52 2020-10-30 neels size_t size1, size2;
69 c672cd52 2020-10-30 neels struct got_diffreg_result *result;
70 c672cd52 2020-10-30 neels off_t outoff = 0;
73 c672cd52 2020-10-30 neels if (line_offsets && *line_offsets && *nlines > 0)
74 c672cd52 2020-10-30 neels outoff = (*line_offsets)[*nlines - 1];
76 c672cd52 2020-10-30 neels if (resultp)
77 c672cd52 2020-10-30 neels *resultp = NULL;
79 c672cd52 2020-10-30 neels if (blob1) {
80 c672cd52 2020-10-30 neels f1 = got_opentemp();
81 c672cd52 2020-10-30 neels if (f1 == NULL)
82 c672cd52 2020-10-30 neels return got_error_from_errno("got_opentemp");
85 c672cd52 2020-10-30 neels if (blob2) {
86 c672cd52 2020-10-30 neels f2 = got_opentemp();
87 c672cd52 2020-10-30 neels if (f2 == NULL) {
88 c672cd52 2020-10-30 neels err = got_error_from_errno("got_opentemp");
89 c672cd52 2020-10-30 neels fclose(f1);
90 c672cd52 2020-10-30 neels return err;
95 c672cd52 2020-10-30 neels if (blob1) {
96 c672cd52 2020-10-30 neels idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
97 c672cd52 2020-10-30 neels err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
100 c672cd52 2020-10-30 neels goto done;
102 c672cd52 2020-10-30 neels idstr1 = "/dev/null";
104 c672cd52 2020-10-30 neels size2 = 0;
105 c672cd52 2020-10-30 neels if (blob2) {
106 c672cd52 2020-10-30 neels idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
107 c672cd52 2020-10-30 neels err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
110 c672cd52 2020-10-30 neels goto done;
112 c672cd52 2020-10-30 neels idstr2 = "/dev/null";
114 c672cd52 2020-10-30 neels if (outfile) {
115 c672cd52 2020-10-30 neels char *modestr1 = NULL, *modestr2 = NULL;
116 c672cd52 2020-10-30 neels int modebits;
117 c672cd52 2020-10-30 neels if (mode1 && mode1 != mode2) {
118 c672cd52 2020-10-30 neels if (S_ISLNK(mode1))
119 c672cd52 2020-10-30 neels modebits = S_IFLNK;
121 c672cd52 2020-10-30 neels modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
122 c672cd52 2020-10-30 neels if (asprintf(&modestr1, " (mode %o)",
123 c672cd52 2020-10-30 neels mode1 & modebits) == -1) {
124 c672cd52 2020-10-30 neels err = got_error_from_errno("asprintf");
125 c672cd52 2020-10-30 neels goto done;
128 c672cd52 2020-10-30 neels if (mode2 && mode1 != mode2) {
129 c672cd52 2020-10-30 neels if (S_ISLNK(mode2))
130 c672cd52 2020-10-30 neels modebits = S_IFLNK;
132 c672cd52 2020-10-30 neels modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
133 c672cd52 2020-10-30 neels if (asprintf(&modestr2, " (mode %o)",
134 c672cd52 2020-10-30 neels mode2 & modebits) == -1) {
135 c672cd52 2020-10-30 neels err = got_error_from_errno("asprintf");
136 c672cd52 2020-10-30 neels goto done;
139 c672cd52 2020-10-30 neels n = fprintf(outfile, "blob - %s%s\n", idstr1,
140 c672cd52 2020-10-30 neels modestr1 ? modestr1 : "");
141 c672cd52 2020-10-30 neels if (n < 0) {
142 c672cd52 2020-10-30 neels err = got_error_from_errno("fprintf");
143 c672cd52 2020-10-30 neels goto done;
145 c672cd52 2020-10-30 neels outoff += n;
146 c672cd52 2020-10-30 neels if (line_offsets) {
147 c672cd52 2020-10-30 neels err = add_line_offset(line_offsets, nlines, outoff);
149 c672cd52 2020-10-30 neels goto done;
152 c672cd52 2020-10-30 neels n = fprintf(outfile, "blob + %s%s\n", idstr2,
153 c672cd52 2020-10-30 neels modestr2 ? modestr2 : "");
154 c672cd52 2020-10-30 neels if (n < 0) {
155 c672cd52 2020-10-30 neels err = got_error_from_errno("fprintf");
156 c672cd52 2020-10-30 neels goto done;
158 c672cd52 2020-10-30 neels outoff += n;
159 c672cd52 2020-10-30 neels if (line_offsets) {
160 c672cd52 2020-10-30 neels err = add_line_offset(line_offsets, nlines, outoff);
162 c672cd52 2020-10-30 neels goto done;
165 c672cd52 2020-10-30 neels free(modestr1);
166 c672cd52 2020-10-30 neels free(modestr2);
168 c672cd52 2020-10-30 neels err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_MYERS,
169 c672cd52 2020-10-30 neels ignore_whitespace);
171 c672cd52 2020-10-30 neels goto done;
173 c672cd52 2020-10-30 neels if (outfile) {
174 c672cd52 2020-10-30 neels err = got_diffreg_output(line_offsets, nlines, result, f1, f2,
175 c672cd52 2020-10-30 neels label1 ? label1 : idstr1,
176 c672cd52 2020-10-30 neels label2 ? label2 : idstr2,
177 c672cd52 2020-10-30 neels GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
179 c672cd52 2020-10-30 neels goto done;
182 c672cd52 2020-10-30 neels if (resultp && err == NULL)
183 c672cd52 2020-10-30 neels *resultp = result;
185 c672cd52 2020-10-30 neels free_err = got_diffreg_result_free(result);
186 c672cd52 2020-10-30 neels if (free_err && err == NULL)
187 c672cd52 2020-10-30 neels err = free_err;
190 c672cd52 2020-10-30 neels if (f1 && fclose(f1) != 0 && err == NULL)
191 c672cd52 2020-10-30 neels err = got_error_from_errno("fclose");
192 c672cd52 2020-10-30 neels if (f2 && fclose(f2) != 0 && err == NULL)
193 c672cd52 2020-10-30 neels err = got_error_from_errno("fclose");
194 c672cd52 2020-10-30 neels return err;
197 c672cd52 2020-10-30 neels const struct got_error *
198 c672cd52 2020-10-30 neels got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
199 c672cd52 2020-10-30 neels struct got_blob_object *blob2, struct got_object_id *id1,
200 c672cd52 2020-10-30 neels struct got_object_id *id2, const char *label1, const char *label2,
201 c672cd52 2020-10-30 neels mode_t mode1, mode_t mode2, struct got_repository *repo)
203 c672cd52 2020-10-30 neels struct got_diff_blob_output_unidiff_arg *a = arg;
205 c672cd52 2020-10-30 neels return diff_blobs(&a->line_offsets, &a->nlines, NULL,
206 c672cd52 2020-10-30 neels blob1, blob2, label1, label2, mode1, mode2, a->diff_context,
207 c672cd52 2020-10-30 neels a->ignore_whitespace, a->outfile);
210 c672cd52 2020-10-30 neels const struct got_error *
211 c672cd52 2020-10-30 neels got_diff_blob(off_t **line_offsets, size_t *nlines,
212 c672cd52 2020-10-30 neels struct got_blob_object *blob1, struct got_blob_object *blob2,
213 c672cd52 2020-10-30 neels const char *label1, const char *label2, int diff_context,
214 c672cd52 2020-10-30 neels int ignore_whitespace, FILE *outfile)
216 c672cd52 2020-10-30 neels return diff_blobs(line_offsets, nlines, NULL, blob1, blob2,
217 c672cd52 2020-10-30 neels label1, label2, 0, 0, diff_context, ignore_whitespace, outfile);
220 c672cd52 2020-10-30 neels static const struct got_error *
221 c672cd52 2020-10-30 neels diff_blob_file(struct got_diffreg_result **resultp,
222 c672cd52 2020-10-30 neels struct got_blob_object *blob1, const char *label1, FILE *f2, size_t size2,
223 c672cd52 2020-10-30 neels const char *label2, int diff_context, int ignore_whitespace, FILE *outfile)
225 c672cd52 2020-10-30 neels const struct got_error *err = NULL, *free_err;
226 c672cd52 2020-10-30 neels FILE *f1 = NULL;
227 c672cd52 2020-10-30 neels char hex1[SHA1_DIGEST_STRING_LENGTH];
228 c672cd52 2020-10-30 neels char *idstr1 = NULL;
229 c672cd52 2020-10-30 neels size_t size1;
230 c672cd52 2020-10-30 neels struct got_diffreg_result *result = NULL;
232 c672cd52 2020-10-30 neels if (resultp)
233 c672cd52 2020-10-30 neels *resultp = NULL;
235 c672cd52 2020-10-30 neels size1 = 0;
236 c672cd52 2020-10-30 neels if (blob1) {
237 c672cd52 2020-10-30 neels f1 = got_opentemp();
238 c672cd52 2020-10-30 neels if (f1 == NULL)
239 c672cd52 2020-10-30 neels return got_error_from_errno("got_opentemp");
240 c672cd52 2020-10-30 neels idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
241 c672cd52 2020-10-30 neels err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
244 c672cd52 2020-10-30 neels goto done;
246 c672cd52 2020-10-30 neels idstr1 = "/dev/null";
249 c672cd52 2020-10-30 neels if (outfile) {
250 c672cd52 2020-10-30 neels fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
251 c672cd52 2020-10-30 neels fprintf(outfile, "file + %s\n",
252 c672cd52 2020-10-30 neels f2 == NULL ? "/dev/null" : label2);
255 c672cd52 2020-10-30 neels err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_MYERS,
256 c672cd52 2020-10-30 neels ignore_whitespace);
258 c672cd52 2020-10-30 neels goto done;
260 c672cd52 2020-10-30 neels if (outfile) {
261 c672cd52 2020-10-30 neels err = got_diffreg_output(NULL, NULL, result, f1, f2,
262 c672cd52 2020-10-30 neels label2, label2, GOT_DIFF_OUTPUT_UNIDIFF, diff_context,
265 c672cd52 2020-10-30 neels goto done;
268 c672cd52 2020-10-30 neels if (resultp && err == NULL)
269 c672cd52 2020-10-30 neels *resultp = result;
270 c672cd52 2020-10-30 neels else if (result) {
271 c672cd52 2020-10-30 neels free_err = got_diffreg_result_free(result);
272 c672cd52 2020-10-30 neels if (free_err && err == NULL)
273 c672cd52 2020-10-30 neels err = free_err;
276 c672cd52 2020-10-30 neels if (f1 && fclose(f1) != 0 && err == NULL)
277 c672cd52 2020-10-30 neels err = got_error_from_errno("fclose");
278 c672cd52 2020-10-30 neels return err;
281 c672cd52 2020-10-30 neels const struct got_error *
282 c672cd52 2020-10-30 neels got_diff_blob_file(struct got_blob_object *blob1, const char *label1,
283 c672cd52 2020-10-30 neels FILE *f2, size_t size2, const char *label2, int diff_context,
284 c672cd52 2020-10-30 neels int ignore_whitespace, FILE *outfile)
286 c672cd52 2020-10-30 neels return diff_blob_file(NULL, blob1, label1, f2, size2, label2,
287 c672cd52 2020-10-30 neels diff_context, ignore_whitespace, outfile);
290 c672cd52 2020-10-30 neels const struct got_error *
291 c672cd52 2020-10-30 neels got_diff_blob_prepared_file(struct got_diffreg_result **resultp,
292 c672cd52 2020-10-30 neels struct diff_data *data1, struct got_blob_object *blob1,
293 c672cd52 2020-10-30 neels struct diff_data *data2, FILE *f2, char *p2, size_t size2,
294 c672cd52 2020-10-30 neels const struct diff_config *cfg, int ignore_whitespace)
296 c672cd52 2020-10-30 neels const struct got_error *err = NULL, *free_err;
297 c672cd52 2020-10-30 neels FILE *f1 = NULL;
298 c672cd52 2020-10-30 neels char hex1[SHA1_DIGEST_STRING_LENGTH];
299 c672cd52 2020-10-30 neels char *idstr1 = NULL, *p1 = NULL;
300 c672cd52 2020-10-30 neels size_t size1, size;
301 c672cd52 2020-10-30 neels struct got_diffreg_result *result = NULL;
302 c672cd52 2020-10-30 neels int f1_created = 0;
304 c672cd52 2020-10-30 neels *resultp = NULL;
306 c672cd52 2020-10-30 neels size1 = 0;
307 c672cd52 2020-10-30 neels if (blob1) {
308 c672cd52 2020-10-30 neels f1 = got_opentemp();
309 c672cd52 2020-10-30 neels if (f1 == NULL)
310 c672cd52 2020-10-30 neels return got_error_from_errno("got_opentemp");
311 c672cd52 2020-10-30 neels idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
312 c672cd52 2020-10-30 neels err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
315 c672cd52 2020-10-30 neels goto done;
317 c672cd52 2020-10-30 neels idstr1 = "/dev/null";
320 c672cd52 2020-10-30 neels err = got_diff_prepare_file(&f1, &p1, &f1_created, &size,
321 c672cd52 2020-10-30 neels data1, cfg, ignore_whitespace);
323 c672cd52 2020-10-30 neels goto done;
325 c672cd52 2020-10-30 neels err = got_diffreg_prepared_files(&result, cfg, data1, f1,
326 c672cd52 2020-10-30 neels p1, size1, data2, f2, p2, size2);
328 c672cd52 2020-10-30 neels goto done;
330 c672cd52 2020-10-30 neels *resultp = result;
332 c672cd52 2020-10-30 neels if (err) {
333 c672cd52 2020-10-30 neels if (result)
334 c672cd52 2020-10-30 neels free_err = got_diffreg_result_free_left(result);
336 c672cd52 2020-10-30 neels free_err = got_diffreg_close(f1, p1, size1, NULL,
338 c672cd52 2020-10-30 neels if (free_err && err == NULL)
339 c672cd52 2020-10-30 neels err = free_err;
341 c672cd52 2020-10-30 neels return err;
344 c672cd52 2020-10-30 neels static const struct got_error *
345 c672cd52 2020-10-30 neels diff_added_blob(struct got_object_id *id, const char *label, mode_t mode,
346 c672cd52 2020-10-30 neels struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
348 c672cd52 2020-10-30 neels const struct got_error *err;
349 c672cd52 2020-10-30 neels struct got_blob_object *blob = NULL;
350 c672cd52 2020-10-30 neels struct got_object *obj = NULL;
352 c672cd52 2020-10-30 neels err = got_object_open(&obj, repo, id);
354 c672cd52 2020-10-30 neels return err;
356 c672cd52 2020-10-30 neels err = got_object_blob_open(&blob, repo, obj, 8192);
358 c672cd52 2020-10-30 neels goto done;
359 c672cd52 2020-10-30 neels err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, 0, mode, repo);
361 c672cd52 2020-10-30 neels got_object_close(obj);
363 c672cd52 2020-10-30 neels got_object_blob_close(blob);
364 c672cd52 2020-10-30 neels return err;
367 c672cd52 2020-10-30 neels static const struct got_error *
368 c672cd52 2020-10-30 neels diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
369 c672cd52 2020-10-30 neels const char *label1, const char *label2, mode_t mode1, mode_t mode2,
370 c672cd52 2020-10-30 neels struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
372 c672cd52 2020-10-30 neels const struct got_error *err;
373 c672cd52 2020-10-30 neels struct got_object *obj1 = NULL;
374 c672cd52 2020-10-30 neels struct got_object *obj2 = NULL;
375 c672cd52 2020-10-30 neels struct got_blob_object *blob1 = NULL;
376 c672cd52 2020-10-30 neels struct got_blob_object *blob2 = NULL;
378 c672cd52 2020-10-30 neels err = got_object_open(&obj1, repo, id1);
380 c672cd52 2020-10-30 neels return err;
381 c672cd52 2020-10-30 neels if (obj1->type != GOT_OBJ_TYPE_BLOB) {
382 c672cd52 2020-10-30 neels err = got_error(GOT_ERR_OBJ_TYPE);
383 c672cd52 2020-10-30 neels goto done;
386 c672cd52 2020-10-30 neels err = got_object_open(&obj2, repo, id2);
388 c672cd52 2020-10-30 neels goto done;
389 c672cd52 2020-10-30 neels if (obj2->type != GOT_OBJ_TYPE_BLOB) {
390 c672cd52 2020-10-30 neels err = got_error(GOT_ERR_BAD_OBJ_DATA);
391 c672cd52 2020-10-30 neels goto done;
394 c672cd52 2020-10-30 neels err = got_object_blob_open(&blob1, repo, obj1, 8192);
396 c672cd52 2020-10-30 neels goto done;
398 c672cd52 2020-10-30 neels err = got_object_blob_open(&blob2, repo, obj2, 8192);
400 c672cd52 2020-10-30 neels goto done;
402 c672cd52 2020-10-30 neels err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, mode1, mode2,
406 c672cd52 2020-10-30 neels got_object_close(obj1);
408 c672cd52 2020-10-30 neels got_object_close(obj2);
409 c672cd52 2020-10-30 neels if (blob1)
410 c672cd52 2020-10-30 neels got_object_blob_close(blob1);
411 c672cd52 2020-10-30 neels if (blob2)
412 c672cd52 2020-10-30 neels got_object_blob_close(blob2);
413 c672cd52 2020-10-30 neels return err;
416 c672cd52 2020-10-30 neels static const struct got_error *
417 c672cd52 2020-10-30 neels diff_deleted_blob(struct got_object_id *id, const char *label, mode_t mode,
418 c672cd52 2020-10-30 neels struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
420 c672cd52 2020-10-30 neels const struct got_error *err;
421 c672cd52 2020-10-30 neels struct got_blob_object *blob = NULL;
422 c672cd52 2020-10-30 neels struct got_object *obj = NULL;
424 c672cd52 2020-10-30 neels err = got_object_open(&obj, repo, id);
426 c672cd52 2020-10-30 neels return err;
428 c672cd52 2020-10-30 neels err = got_object_blob_open(&blob, repo, obj, 8192);
430 c672cd52 2020-10-30 neels goto done;
431 c672cd52 2020-10-30 neels err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, mode, 0, repo);
433 c672cd52 2020-10-30 neels got_object_close(obj);
435 c672cd52 2020-10-30 neels got_object_blob_close(blob);
436 c672cd52 2020-10-30 neels return err;
439 c672cd52 2020-10-30 neels static const struct got_error *
440 c672cd52 2020-10-30 neels diff_added_tree(struct got_object_id *id, const char *label,
441 c672cd52 2020-10-30 neels struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
442 c672cd52 2020-10-30 neels int diff_content)
444 c672cd52 2020-10-30 neels const struct got_error *err = NULL;
445 c672cd52 2020-10-30 neels struct got_object *treeobj = NULL;
446 c672cd52 2020-10-30 neels struct got_tree_object *tree = NULL;
448 c672cd52 2020-10-30 neels err = got_object_open(&treeobj, repo, id);
450 c672cd52 2020-10-30 neels goto done;
452 c672cd52 2020-10-30 neels if (treeobj->type != GOT_OBJ_TYPE_TREE) {
453 c672cd52 2020-10-30 neels err = got_error(GOT_ERR_OBJ_TYPE);
454 c672cd52 2020-10-30 neels goto done;
457 c672cd52 2020-10-30 neels err = got_object_tree_open(&tree, repo, treeobj);
459 c672cd52 2020-10-30 neels goto done;
461 c672cd52 2020-10-30 neels err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
462 c672cd52 2020-10-30 neels diff_content);
465 c672cd52 2020-10-30 neels got_object_tree_close(tree);
466 c672cd52 2020-10-30 neels if (treeobj)
467 c672cd52 2020-10-30 neels got_object_close(treeobj);
468 c672cd52 2020-10-30 neels return err;
471 c672cd52 2020-10-30 neels static const struct got_error *
472 c672cd52 2020-10-30 neels diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
473 c672cd52 2020-10-30 neels const char *label1, const char *label2, struct got_repository *repo,
474 c672cd52 2020-10-30 neels got_diff_blob_cb cb, void *cb_arg, int diff_content)
476 c672cd52 2020-10-30 neels const struct got_error *err;
477 c672cd52 2020-10-30 neels struct got_object *treeobj1 = NULL;
478 c672cd52 2020-10-30 neels struct got_object *treeobj2 = NULL;
479 c672cd52 2020-10-30 neels struct got_tree_object *tree1 = NULL;
480 c672cd52 2020-10-30 neels struct got_tree_object *tree2 = NULL;
482 c672cd52 2020-10-30 neels err = got_object_open(&treeobj1, repo, id1);
484 c672cd52 2020-10-30 neels goto done;
486 c672cd52 2020-10-30 neels if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
487 c672cd52 2020-10-30 neels err = got_error(GOT_ERR_OBJ_TYPE);
488 c672cd52 2020-10-30 neels goto done;
491 c672cd52 2020-10-30 neels err = got_object_open(&treeobj2, repo, id2);
493 c672cd52 2020-10-30 neels goto done;
495 c672cd52 2020-10-30 neels if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
496 c672cd52 2020-10-30 neels err = got_error(GOT_ERR_OBJ_TYPE);
497 c672cd52 2020-10-30 neels goto done;
500 c672cd52 2020-10-30 neels err = got_object_tree_open(&tree1, repo, treeobj1);
502 c672cd52 2020-10-30 neels goto done;
504 c672cd52 2020-10-30 neels err = got_object_tree_open(&tree2, repo, treeobj2);
506 c672cd52 2020-10-30 neels goto done;
508 c672cd52 2020-10-30 neels err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
509 c672cd52 2020-10-30 neels diff_content);
512 c672cd52 2020-10-30 neels if (tree1)
513 c672cd52 2020-10-30 neels got_object_tree_close(tree1);
514 c672cd52 2020-10-30 neels if (tree2)
515 c672cd52 2020-10-30 neels got_object_tree_close(tree2);
516 c672cd52 2020-10-30 neels if (treeobj1)
517 c672cd52 2020-10-30 neels got_object_close(treeobj1);
518 c672cd52 2020-10-30 neels if (treeobj2)
519 c672cd52 2020-10-30 neels got_object_close(treeobj2);
520 c672cd52 2020-10-30 neels return err;
523 c672cd52 2020-10-30 neels static const struct got_error *
524 c672cd52 2020-10-30 neels diff_deleted_tree(struct got_object_id *id, const char *label,
525 c672cd52 2020-10-30 neels struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
526 c672cd52 2020-10-30 neels int diff_content)
528 c672cd52 2020-10-30 neels const struct got_error *err;
529 c672cd52 2020-10-30 neels struct got_object *treeobj = NULL;
530 c672cd52 2020-10-30 neels struct got_tree_object *tree = NULL;
532 c672cd52 2020-10-30 neels err = got_object_open(&treeobj, repo, id);
534 c672cd52 2020-10-30 neels goto done;
536 c672cd52 2020-10-30 neels if (treeobj->type != GOT_OBJ_TYPE_TREE) {
537 c672cd52 2020-10-30 neels err = got_error(GOT_ERR_OBJ_TYPE);
538 c672cd52 2020-10-30 neels goto done;
541 c672cd52 2020-10-30 neels err = got_object_tree_open(&tree, repo, treeobj);
543 c672cd52 2020-10-30 neels goto done;
545 c672cd52 2020-10-30 neels err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
546 c672cd52 2020-10-30 neels diff_content);
549 c672cd52 2020-10-30 neels got_object_tree_close(tree);
550 c672cd52 2020-10-30 neels if (treeobj)
551 c672cd52 2020-10-30 neels got_object_close(treeobj);
552 c672cd52 2020-10-30 neels return err;
555 c672cd52 2020-10-30 neels static const struct got_error *
556 c672cd52 2020-10-30 neels diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
557 c672cd52 2020-10-30 neels const char *label1, const char *label2, struct got_repository *repo,
558 c672cd52 2020-10-30 neels got_diff_blob_cb cb, void *cb_arg)
560 c672cd52 2020-10-30 neels /* XXX TODO */
561 c672cd52 2020-10-30 neels return NULL;
564 c672cd52 2020-10-30 neels static const struct got_error *
565 c672cd52 2020-10-30 neels diff_entry_old_new(struct got_tree_entry *te1,
566 c672cd52 2020-10-30 neels struct got_tree_entry *te2, const char *label1, const char *label2,
567 c672cd52 2020-10-30 neels struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
568 c672cd52 2020-10-30 neels int diff_content)
570 c672cd52 2020-10-30 neels const struct got_error *err = NULL;
571 c672cd52 2020-10-30 neels int id_match;
573 c672cd52 2020-10-30 neels if (got_object_tree_entry_is_submodule(te1))
574 c672cd52 2020-10-30 neels return NULL;
576 c672cd52 2020-10-30 neels if (te2 == NULL) {
577 c672cd52 2020-10-30 neels if (S_ISDIR(te1->mode))
578 c672cd52 2020-10-30 neels err = diff_deleted_tree(&te1->id, label1, repo,
579 c672cd52 2020-10-30 neels cb, cb_arg, diff_content);
581 c672cd52 2020-10-30 neels if (diff_content)
582 c672cd52 2020-10-30 neels err = diff_deleted_blob(&te1->id, label1,
583 c672cd52 2020-10-30 neels te1->mode, repo, cb, cb_arg);
585 c672cd52 2020-10-30 neels err = cb(cb_arg, NULL, NULL, &te1->id, NULL,
586 c672cd52 2020-10-30 neels label1, NULL, te1->mode, 0, repo);
588 c672cd52 2020-10-30 neels return err;
589 c672cd52 2020-10-30 neels } else if (got_object_tree_entry_is_submodule(te2))
590 c672cd52 2020-10-30 neels return NULL;
592 c672cd52 2020-10-30 neels id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
593 c672cd52 2020-10-30 neels if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
594 c672cd52 2020-10-30 neels if (!id_match)
595 c672cd52 2020-10-30 neels return diff_modified_tree(&te1->id, &te2->id,
596 c672cd52 2020-10-30 neels label1, label2, repo, cb, cb_arg, diff_content);
597 c672cd52 2020-10-30 neels } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
598 c672cd52 2020-10-30 neels (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
599 c672cd52 2020-10-30 neels if (!id_match ||
600 c672cd52 2020-10-30 neels ((te1->mode & (S_IFLNK | S_IXUSR))) !=
601 c672cd52 2020-10-30 neels (te2->mode & (S_IFLNK | S_IXUSR))) {
602 c672cd52 2020-10-30 neels if (diff_content)
603 c672cd52 2020-10-30 neels return diff_modified_blob(&te1->id, &te2->id,
604 c672cd52 2020-10-30 neels label1, label2, te1->mode, te2->mode,
605 c672cd52 2020-10-30 neels repo, cb, cb_arg);
607 c672cd52 2020-10-30 neels return cb(cb_arg, NULL, NULL, &te1->id,
608 c672cd52 2020-10-30 neels &te2->id, label1, label2, te1->mode,
609 c672cd52 2020-10-30 neels te2->mode, repo);
613 c672cd52 2020-10-30 neels if (id_match)
614 c672cd52 2020-10-30 neels return NULL;
616 c672cd52 2020-10-30 neels return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
617 c672cd52 2020-10-30 neels cb, cb_arg);
620 c672cd52 2020-10-30 neels static const struct got_error *
621 c672cd52 2020-10-30 neels diff_entry_new_old(struct got_tree_entry *te2,
622 c672cd52 2020-10-30 neels struct got_tree_entry *te1, const char *label2,
623 c672cd52 2020-10-30 neels struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
624 c672cd52 2020-10-30 neels int diff_content)
626 c672cd52 2020-10-30 neels if (te1 != NULL) /* handled by diff_entry_old_new() */
627 c672cd52 2020-10-30 neels return NULL;
629 c672cd52 2020-10-30 neels if (got_object_tree_entry_is_submodule(te2))
630 c672cd52 2020-10-30 neels return NULL;
632 c672cd52 2020-10-30 neels if (S_ISDIR(te2->mode))
633 c672cd52 2020-10-30 neels return diff_added_tree(&te2->id, label2, repo, cb, cb_arg,
634 c672cd52 2020-10-30 neels diff_content);
636 c672cd52 2020-10-30 neels if (diff_content)
637 c672cd52 2020-10-30 neels return diff_added_blob(&te2->id, label2, te2->mode, repo, cb,
640 c672cd52 2020-10-30 neels return cb(cb_arg, NULL, NULL, NULL, &te2->id, NULL, label2, 0,
641 c672cd52 2020-10-30 neels te2->mode, repo);
644 c672cd52 2020-10-30 neels const struct got_error *
645 c672cd52 2020-10-30 neels got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
646 c672cd52 2020-10-30 neels struct got_blob_object *blob2, struct got_object_id *id1,
647 c672cd52 2020-10-30 neels struct got_object_id *id2, const char *label1, const char *label2,
648 c672cd52 2020-10-30 neels mode_t mode1, mode_t mode2, struct got_repository *repo)
650 c672cd52 2020-10-30 neels const struct got_error *err = NULL;
651 c672cd52 2020-10-30 neels struct got_pathlist_head *paths = arg;
652 c672cd52 2020-10-30 neels struct got_diff_changed_path *change = NULL;
653 c672cd52 2020-10-30 neels char *path = NULL;
655 c672cd52 2020-10-30 neels path = strdup(label2 ? label2 : label1);
656 c672cd52 2020-10-30 neels if (path == NULL)
657 c672cd52 2020-10-30 neels return got_error_from_errno("malloc");
659 c672cd52 2020-10-30 neels change = malloc(sizeof(*change));
660 c672cd52 2020-10-30 neels if (change == NULL) {
661 c672cd52 2020-10-30 neels err = got_error_from_errno("malloc");
662 c672cd52 2020-10-30 neels goto done;
665 c672cd52 2020-10-30 neels change->status = GOT_STATUS_NO_CHANGE;
666 c672cd52 2020-10-30 neels if (id1 == NULL)
667 c672cd52 2020-10-30 neels change->status = GOT_STATUS_ADD;
668 c672cd52 2020-10-30 neels else if (id2 == NULL)
669 c672cd52 2020-10-30 neels change->status = GOT_STATUS_DELETE;
671 c672cd52 2020-10-30 neels if (got_object_id_cmp(id1, id2) != 0)
672 c672cd52 2020-10-30 neels change->status = GOT_STATUS_MODIFY;
673 c672cd52 2020-10-30 neels else if (mode1 != mode2)
674 c672cd52 2020-10-30 neels change->status = GOT_STATUS_MODE_CHANGE;
677 c672cd52 2020-10-30 neels err = got_pathlist_insert(NULL, paths, path, change);
679 c672cd52 2020-10-30 neels if (err) {
680 c672cd52 2020-10-30 neels free(path);
681 c672cd52 2020-10-30 neels free(change);
683 c672cd52 2020-10-30 neels return err;
686 c672cd52 2020-10-30 neels const struct got_error *
687 c672cd52 2020-10-30 neels got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
688 c672cd52 2020-10-30 neels const char *label1, const char *label2, struct got_repository *repo,
689 c672cd52 2020-10-30 neels got_diff_blob_cb cb, void *cb_arg, int diff_content)
691 c672cd52 2020-10-30 neels const struct got_error *err = NULL;
692 c672cd52 2020-10-30 neels struct got_tree_entry *te1 = NULL;
693 c672cd52 2020-10-30 neels struct got_tree_entry *te2 = NULL;
694 c672cd52 2020-10-30 neels char *l1 = NULL, *l2 = NULL;
695 c672cd52 2020-10-30 neels int tidx1 = 0, tidx2 = 0;
697 c672cd52 2020-10-30 neels if (tree1) {
698 c672cd52 2020-10-30 neels te1 = got_object_tree_get_entry(tree1, 0);
699 c672cd52 2020-10-30 neels if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
700 c672cd52 2020-10-30 neels te1->name) == -1)
701 c672cd52 2020-10-30 neels return got_error_from_errno("asprintf");
703 c672cd52 2020-10-30 neels if (tree2) {
704 c672cd52 2020-10-30 neels te2 = got_object_tree_get_entry(tree2, 0);
705 c672cd52 2020-10-30 neels if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
706 c672cd52 2020-10-30 neels te2->name) == -1)
707 c672cd52 2020-10-30 neels return got_error_from_errno("asprintf");
711 c672cd52 2020-10-30 neels if (te1) {
712 c672cd52 2020-10-30 neels struct got_tree_entry *te = NULL;
713 c672cd52 2020-10-30 neels if (tree2)
714 c672cd52 2020-10-30 neels te = got_object_tree_find_entry(tree2,
715 c672cd52 2020-10-30 neels te1->name);
718 c672cd52 2020-10-30 neels l2 = NULL;
719 c672cd52 2020-10-30 neels if (te && asprintf(&l2, "%s%s%s", label2,
720 c672cd52 2020-10-30 neels label2[0] ? "/" : "", te->name) == -1)
722 c672cd52 2020-10-30 neels got_error_from_errno("asprintf");
724 c672cd52 2020-10-30 neels err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
725 c672cd52 2020-10-30 neels cb_arg, diff_content);
730 c672cd52 2020-10-30 neels if (te2) {
731 c672cd52 2020-10-30 neels struct got_tree_entry *te = NULL;
732 c672cd52 2020-10-30 neels if (tree1)
733 c672cd52 2020-10-30 neels te = got_object_tree_find_entry(tree1,
734 c672cd52 2020-10-30 neels te2->name);
737 c672cd52 2020-10-30 neels if (asprintf(&l2, "%s%s%s", label2,
738 c672cd52 2020-10-30 neels label2[0] ? "/" : "", te->name) == -1)
740 c672cd52 2020-10-30 neels got_error_from_errno("asprintf");
742 c672cd52 2020-10-30 neels if (asprintf(&l2, "%s%s%s", label2,
743 c672cd52 2020-10-30 neels label2[0] ? "/" : "", te2->name) == -1)
745 c672cd52 2020-10-30 neels got_error_from_errno("asprintf");
747 c672cd52 2020-10-30 neels err = diff_entry_new_old(te2, te, l2, repo,
748 c672cd52 2020-10-30 neels cb, cb_arg, diff_content);
754 c672cd52 2020-10-30 neels l1 = NULL;
755 c672cd52 2020-10-30 neels if (te1) {
757 c672cd52 2020-10-30 neels te1 = got_object_tree_get_entry(tree1, tidx1);
758 c672cd52 2020-10-30 neels if (te1 &&
759 c672cd52 2020-10-30 neels asprintf(&l1, "%s%s%s", label1,
760 c672cd52 2020-10-30 neels label1[0] ? "/" : "", te1->name) == -1)
761 c672cd52 2020-10-30 neels return got_error_from_errno("asprintf");
764 c672cd52 2020-10-30 neels l2 = NULL;
765 c672cd52 2020-10-30 neels if (te2) {
767 c672cd52 2020-10-30 neels te2 = got_object_tree_get_entry(tree2, tidx2);
768 c672cd52 2020-10-30 neels if (te2 &&
769 c672cd52 2020-10-30 neels asprintf(&l2, "%s%s%s", label2,
770 c672cd52 2020-10-30 neels label2[0] ? "/" : "", te2->name) == -1)
771 c672cd52 2020-10-30 neels return got_error_from_errno("asprintf");
773 c672cd52 2020-10-30 neels } while (te1 || te2);
775 c672cd52 2020-10-30 neels return err;
778 c672cd52 2020-10-30 neels const struct got_error *
779 c672cd52 2020-10-30 neels got_diff_objects_as_blobs(off_t **line_offsets, size_t *nlines,
780 c672cd52 2020-10-30 neels struct got_object_id *id1, struct got_object_id *id2,
781 c672cd52 2020-10-30 neels const char *label1, const char *label2, int diff_context,
782 c672cd52 2020-10-30 neels int ignore_whitespace, struct got_repository *repo, FILE *outfile)
784 c672cd52 2020-10-30 neels const struct got_error *err;
785 c672cd52 2020-10-30 neels struct got_blob_object *blob1 = NULL, *blob2 = NULL;
787 c672cd52 2020-10-30 neels if (id1 == NULL && id2 == NULL)
788 c672cd52 2020-10-30 neels return got_error(GOT_ERR_NO_OBJ);
790 c672cd52 2020-10-30 neels if (id1) {
791 c672cd52 2020-10-30 neels err = got_object_open_as_blob(&blob1, repo, id1, 8192);
793 c672cd52 2020-10-30 neels goto done;
795 c672cd52 2020-10-30 neels if (id2) {
796 c672cd52 2020-10-30 neels err = got_object_open_as_blob(&blob2, repo, id2, 8192);
798 c672cd52 2020-10-30 neels goto done;
800 c672cd52 2020-10-30 neels err = got_diff_blob(line_offsets, nlines, blob1, blob2,
801 c672cd52 2020-10-30 neels label1, label2, diff_context, ignore_whitespace, outfile);
803 c672cd52 2020-10-30 neels if (blob1)
804 c672cd52 2020-10-30 neels got_object_blob_close(blob1);
805 c672cd52 2020-10-30 neels if (blob2)
806 c672cd52 2020-10-30 neels got_object_blob_close(blob2);
807 c672cd52 2020-10-30 neels return err;
810 c672cd52 2020-10-30 neels const struct got_error *
811 c672cd52 2020-10-30 neels got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
812 c672cd52 2020-10-30 neels struct got_object_id *id1, struct got_object_id *id2,
813 c672cd52 2020-10-30 neels char *label1, char *label2, int diff_context, int ignore_whitespace,
814 c672cd52 2020-10-30 neels struct got_repository *repo, FILE *outfile)
816 c672cd52 2020-10-30 neels const struct got_error *err;
817 c672cd52 2020-10-30 neels struct got_tree_object *tree1 = NULL, *tree2 = NULL;
818 c672cd52 2020-10-30 neels struct got_diff_blob_output_unidiff_arg arg;
819 c672cd52 2020-10-30 neels int want_lineoffsets = (line_offsets != NULL && *line_offsets != NULL);
821 c672cd52 2020-10-30 neels if (id1 == NULL && id2 == NULL)
822 c672cd52 2020-10-30 neels return got_error(GOT_ERR_NO_OBJ);
824 c672cd52 2020-10-30 neels if (id1) {
825 c672cd52 2020-10-30 neels err = got_object_open_as_tree(&tree1, repo, id1);
827 c672cd52 2020-10-30 neels goto done;
829 c672cd52 2020-10-30 neels if (id2) {
830 c672cd52 2020-10-30 neels err = got_object_open_as_tree(&tree2, repo, id2);
832 c672cd52 2020-10-30 neels goto done;
834 c672cd52 2020-10-30 neels arg.diff_context = diff_context;
835 c672cd52 2020-10-30 neels arg.ignore_whitespace = ignore_whitespace;
836 c672cd52 2020-10-30 neels arg.outfile = outfile;
837 c672cd52 2020-10-30 neels if (want_lineoffsets) {
838 c672cd52 2020-10-30 neels arg.line_offsets = *line_offsets;
839 c672cd52 2020-10-30 neels arg.nlines = *nlines;
841 c672cd52 2020-10-30 neels arg.line_offsets = NULL;
842 c672cd52 2020-10-30 neels arg.nlines = 0;
844 c672cd52 2020-10-30 neels err = got_diff_tree(tree1, tree2, label1, label2, repo,
845 c672cd52 2020-10-30 neels got_diff_blob_output_unidiff, &arg, 1);
847 c672cd52 2020-10-30 neels if (want_lineoffsets) {
848 c672cd52 2020-10-30 neels *line_offsets = arg.line_offsets; /* was likely re-allocated */
849 c672cd52 2020-10-30 neels *nlines = arg.nlines;
852 c672cd52 2020-10-30 neels if (tree1)
853 c672cd52 2020-10-30 neels got_object_tree_close(tree1);
854 c672cd52 2020-10-30 neels if (tree2)
855 c672cd52 2020-10-30 neels got_object_tree_close(tree2);
856 c672cd52 2020-10-30 neels return err;
859 c672cd52 2020-10-30 neels const struct got_error *
860 c672cd52 2020-10-30 neels got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
861 c672cd52 2020-10-30 neels struct got_object_id *id1, struct got_object_id *id2,
862 c672cd52 2020-10-30 neels int diff_context, int ignore_whitespace,
863 c672cd52 2020-10-30 neels struct got_repository *repo, FILE *outfile)
865 c672cd52 2020-10-30 neels const struct got_error *err;
866 c672cd52 2020-10-30 neels struct got_commit_object *commit1 = NULL, *commit2 = NULL;
868 c672cd52 2020-10-30 neels if (id2 == NULL)
869 c672cd52 2020-10-30 neels return got_error(GOT_ERR_NO_OBJ);
871 c672cd52 2020-10-30 neels if (id1) {
872 c672cd52 2020-10-30 neels err = got_object_open_as_commit(&commit1, repo, id1);
874 c672cd52 2020-10-30 neels goto done;
877 c672cd52 2020-10-30 neels err = got_object_open_as_commit(&commit2, repo, id2);
879 c672cd52 2020-10-30 neels goto done;
881 c672cd52 2020-10-30 neels err = got_diff_objects_as_trees(line_offsets, nlines,
882 c672cd52 2020-10-30 neels commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
883 c672cd52 2020-10-30 neels got_object_commit_get_tree_id(commit2), "", "", diff_context,
884 c672cd52 2020-10-30 neels ignore_whitespace, repo, outfile);
886 c672cd52 2020-10-30 neels if (commit1)
887 c672cd52 2020-10-30 neels got_object_commit_close(commit1);
888 c672cd52 2020-10-30 neels if (commit2)
889 c672cd52 2020-10-30 neels got_object_commit_close(commit2);
890 c672cd52 2020-10-30 neels return err;
893 c672cd52 2020-10-30 neels const struct got_error *
894 c672cd52 2020-10-30 neels got_diff_files(struct got_diffreg_result **resultp,
895 c672cd52 2020-10-30 neels FILE *f1, const char *label1, FILE *f2, const char *label2,
896 c672cd52 2020-10-30 neels int diff_context, int ignore_whitespace, FILE *outfile)
898 c672cd52 2020-10-30 neels const struct got_error *err = NULL;
899 c672cd52 2020-10-30 neels struct got_diffreg_result *diffreg_result = NULL;
901 c672cd52 2020-10-30 neels if (resultp)
902 c672cd52 2020-10-30 neels *resultp = NULL;
904 c672cd52 2020-10-30 neels if (outfile) {
905 c672cd52 2020-10-30 neels fprintf(outfile, "file - %s\n",
906 c672cd52 2020-10-30 neels f1 == NULL ? "/dev/null" : label1);
907 c672cd52 2020-10-30 neels fprintf(outfile, "file + %s\n",
908 c672cd52 2020-10-30 neels f2 == NULL ? "/dev/null" : label2);
911 c672cd52 2020-10-30 neels err = got_diffreg(&diffreg_result, f1, f2, GOT_DIFF_ALGORITHM_MYERS,
912 c672cd52 2020-10-30 neels ignore_whitespace);
914 c672cd52 2020-10-30 neels goto done;
916 c672cd52 2020-10-30 neels if (outfile) {
917 c672cd52 2020-10-30 neels err = got_diffreg_output(NULL, NULL, diffreg_result,
918 c672cd52 2020-10-30 neels f1, f2, label1, label2, GOT_DIFF_OUTPUT_UNIDIFF,
919 c672cd52 2020-10-30 neels diff_context, outfile);
921 c672cd52 2020-10-30 neels goto done;
925 c672cd52 2020-10-30 neels if (resultp && err == NULL)
926 c672cd52 2020-10-30 neels *resultp = diffreg_result;
927 c672cd52 2020-10-30 neels else if (diffreg_result) {
928 c672cd52 2020-10-30 neels const struct got_error *free_err;
929 c672cd52 2020-10-30 neels free_err = got_diffreg_result_free(diffreg_result);
930 c672cd52 2020-10-30 neels if (free_err && err == NULL)
931 c672cd52 2020-10-30 neels err = free_err;
934 c672cd52 2020-10-30 neels return err;