2 7d283eee 2017-11-29 stsp * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
4 7d283eee 2017-11-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 7d283eee 2017-11-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 7d283eee 2017-11-29 stsp * copyright notice and this permission notice appear in all copies.
8 7d283eee 2017-11-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 7d283eee 2017-11-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 7d283eee 2017-11-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 7d283eee 2017-11-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 7d283eee 2017-11-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 7d283eee 2017-11-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 7d283eee 2017-11-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
18 1c7f0520 2017-11-29 stsp #include <sys/stat.h>
20 7d283eee 2017-11-29 stsp #include <stdio.h>
21 7d283eee 2017-11-29 stsp #include <stdlib.h>
22 7d283eee 2017-11-29 stsp #include <string.h>
23 f9d67749 2017-11-30 stsp #include <limits.h>
24 588a8092 2023-02-23 thomas #include <sha1.h>
25 588a8092 2023-02-23 thomas #include <sha2.h>
26 7d283eee 2017-11-29 stsp #include <zlib.h>
28 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
30 7d283eee 2017-11-29 stsp #include "got_object.h"
31 e09a504c 2019-06-28 stsp #include "got_repository.h"
32 7d283eee 2017-11-29 stsp #include "got_error.h"
33 789689b5 2017-11-30 stsp #include "got_diff.h"
34 324d37e7 2019-05-11 stsp #include "got_path.h"
35 0208f208 2020-05-05 stsp #include "got_cancel.h"
36 0208f208 2020-05-05 stsp #include "got_worktree.h"
37 1758cce7 2022-06-13 thomas #include "got_opentemp.h"
39 718b3ab0 2018-03-17 stsp #include "got_lib_diff.h"
40 15a94983 2018-12-23 stsp #include "got_lib_delta.h"
41 15a94983 2018-12-23 stsp #include "got_lib_inflate.h"
42 15a94983 2018-12-23 stsp #include "got_lib_object.h"
44 772fcad5 2023-01-07 thomas #ifndef MAX
45 772fcad5 2023-01-07 thomas #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
48 404c43c4 2018-06-21 stsp static const struct got_error *
49 82c78e96 2022-08-06 thomas add_line_metadata(struct got_diff_line **lines, size_t *nlines,
50 82c78e96 2022-08-06 thomas off_t off, uint8_t type)
52 82c78e96 2022-08-06 thomas struct got_diff_line *p;
54 82c78e96 2022-08-06 thomas p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
55 fe621944 2020-11-10 stsp if (p == NULL)
56 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
57 82c78e96 2022-08-06 thomas *lines = p;
58 82c78e96 2022-08-06 thomas (*lines)[*nlines].offset = off;
59 82c78e96 2022-08-06 thomas (*lines)[*nlines].type = type;
60 fe621944 2020-11-10 stsp (*nlines)++;
62 fe621944 2020-11-10 stsp return NULL;
65 53d03f97 2023-01-10 thomas static void
66 53d03f97 2023-01-10 thomas diffstat_field_width(size_t *maxlen, int *add_cols, int *rm_cols, size_t len,
67 53d03f97 2023-01-10 thomas uint32_t add, uint32_t rm)
69 53d03f97 2023-01-10 thomas int d1 = 1, d2 = 1;
71 53d03f97 2023-01-10 thomas if (maxlen)
72 53d03f97 2023-01-10 thomas *maxlen = MAX(*maxlen, len);
74 53d03f97 2023-01-10 thomas while (add /= 10)
76 53d03f97 2023-01-10 thomas *add_cols = MAX(*add_cols, d1);
78 53d03f97 2023-01-10 thomas while (rm /= 10)
80 53d03f97 2023-01-10 thomas *rm_cols = MAX(*rm_cols, d2);
83 fe621944 2020-11-10 stsp static const struct got_error *
84 53d03f97 2023-01-10 thomas get_diffstat(struct got_diffstat_cb_arg *ds, const char *path,
85 53d03f97 2023-01-10 thomas struct diff_result *r, int force_text, int status)
87 53d03f97 2023-01-10 thomas const struct got_error *err;
88 53d03f97 2023-01-10 thomas struct got_pathlist_entry *pe;
89 53d03f97 2023-01-10 thomas struct got_diff_changed_path *change = NULL;
90 53d03f97 2023-01-10 thomas int flags = (r->left->atomizer_flags | r->right->atomizer_flags);
91 53d03f97 2023-01-10 thomas int isbin = (flags & DIFF_ATOMIZER_FOUND_BINARY_DATA);
94 53d03f97 2023-01-10 thomas change = calloc(1, sizeof(*change));
95 53d03f97 2023-01-10 thomas if (change == NULL)
96 e80ef83e 2023-01-10 thomas return got_error_from_errno("calloc");
98 53d03f97 2023-01-10 thomas if (!isbin || force_text) {
99 53d03f97 2023-01-10 thomas for (i = 0; i < r->chunks.len; ++i) {
100 53d03f97 2023-01-10 thomas struct diff_chunk *c;
101 53d03f97 2023-01-10 thomas int clc, crc;
103 53d03f97 2023-01-10 thomas c = diff_chunk_get(r, i);
104 53d03f97 2023-01-10 thomas clc = diff_chunk_get_left_count(c);
105 53d03f97 2023-01-10 thomas crc = diff_chunk_get_right_count(c);
107 53d03f97 2023-01-10 thomas if (crc && !clc)
108 53d03f97 2023-01-10 thomas change->add += crc;
109 53d03f97 2023-01-10 thomas if (clc && !crc)
110 53d03f97 2023-01-10 thomas change->rm += clc;
114 53d03f97 2023-01-10 thomas change->status = status;
115 53d03f97 2023-01-10 thomas ds->ins += change->add;
116 53d03f97 2023-01-10 thomas ds->del += change->rm;
117 53d03f97 2023-01-10 thomas ++ds->nfiles;
119 53d03f97 2023-01-10 thomas err = got_pathlist_append(ds->paths, path, change);
120 92a56907 2023-01-14 thomas if (err) {
121 92a56907 2023-01-14 thomas free(change);
122 53d03f97 2023-01-10 thomas return err;
125 53d03f97 2023-01-10 thomas pe = TAILQ_LAST(ds->paths, got_pathlist_head);
126 53d03f97 2023-01-10 thomas diffstat_field_width(&ds->max_path_len, &ds->add_cols, &ds->rm_cols,
127 53d03f97 2023-01-10 thomas pe->path_len, change->add, change->rm);
129 53d03f97 2023-01-10 thomas return NULL;
132 53d03f97 2023-01-10 thomas static const struct got_error *
133 82c78e96 2022-08-06 thomas diff_blobs(struct got_diff_line **lines, size_t *nlines,
134 fe621944 2020-11-10 stsp struct got_diffreg_result **resultp, struct got_blob_object *blob1,
135 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
136 46f68b20 2019-10-19 stsp const char *label1, const char *label2, mode_t mode1, mode_t mode2,
137 53d03f97 2023-01-10 thomas int diff_context, int ignore_whitespace, int force_text_diff,
138 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg *diffstat, FILE *outfile,
139 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo)
141 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
142 01986ce9 2023-02-07 thomas char hex1[GOT_OBJECT_ID_HEX_MAXLEN];
143 01986ce9 2023-02-07 thomas char hex2[GOT_OBJECT_ID_HEX_MAXLEN];
144 0c6f49ba 2022-07-01 thomas const char *idstr1 = NULL, *idstr2 = NULL;
145 92a56907 2023-01-14 thomas char *modestr1 = NULL, *modestr2 = NULL;
146 be659d10 2020-11-18 stsp off_t size1, size2;
147 a6d62598 2023-01-07 thomas struct got_diffreg_result *result = NULL;
148 fe621944 2020-11-10 stsp off_t outoff = 0;
151 3387ec82 2022-08-30 thomas if (lines && *lines && *nlines > 0)
152 82c78e96 2022-08-06 thomas outoff = (*lines)[*nlines - 1].offset;
153 82c78e96 2022-08-06 thomas else if (lines) {
154 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
159 fe621944 2020-11-10 stsp if (resultp)
160 fe621944 2020-11-10 stsp *resultp = NULL;
162 a0f32f33 2022-06-13 thomas if (f1) {
163 1758cce7 2022-06-13 thomas err = got_opentemp_truncate(f1);
165 a0f32f33 2022-06-13 thomas goto done;
167 a0f32f33 2022-06-13 thomas if (f2) {
168 1758cce7 2022-06-13 thomas err = got_opentemp_truncate(f2);
170 a0f32f33 2022-06-13 thomas goto done;
174 98abbc84 2017-11-30 stsp if (blob1) {
175 f934cf2c 2018-02-12 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
176 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
181 98abbc84 2017-11-30 stsp idstr1 = "/dev/null";
184 98abbc84 2017-11-30 stsp if (blob2) {
185 f934cf2c 2018-02-12 stsp idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
186 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
191 98abbc84 2017-11-30 stsp idstr2 = "/dev/null";
193 09de383e 2018-12-24 stsp if (outfile) {
194 40dde666 2020-07-23 stsp int modebits;
196 46f68b20 2019-10-19 stsp if (mode1 && mode1 != mode2) {
197 40dde666 2020-07-23 stsp if (S_ISLNK(mode1))
198 40dde666 2020-07-23 stsp modebits = S_IFLNK;
200 40dde666 2020-07-23 stsp modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
201 46f68b20 2019-10-19 stsp if (asprintf(&modestr1, " (mode %o)",
202 40dde666 2020-07-23 stsp mode1 & modebits) == -1) {
203 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
207 46f68b20 2019-10-19 stsp if (mode2 && mode1 != mode2) {
208 40dde666 2020-07-23 stsp if (S_ISLNK(mode2))
209 40dde666 2020-07-23 stsp modebits = S_IFLNK;
211 40dde666 2020-07-23 stsp modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
212 46f68b20 2019-10-19 stsp if (asprintf(&modestr2, " (mode %o)",
213 40dde666 2020-07-23 stsp mode2 & modebits) == -1) {
214 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
218 fe621944 2020-11-10 stsp n = fprintf(outfile, "blob - %s%s\n", idstr1,
219 46f68b20 2019-10-19 stsp modestr1 ? modestr1 : "");
222 fe621944 2020-11-10 stsp outoff += n;
223 82c78e96 2022-08-06 thomas if (lines) {
224 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
225 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_BLOB_MIN);
230 fe621944 2020-11-10 stsp n = fprintf(outfile, "blob + %s%s\n", idstr2,
231 46f68b20 2019-10-19 stsp modestr2 ? modestr2 : "");
234 fe621944 2020-11-10 stsp outoff += n;
235 82c78e96 2022-08-06 thomas if (lines) {
236 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
237 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_BLOB_PLUS);
243 25ec7006 2022-07-01 thomas err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
244 25ec7006 2022-07-01 thomas force_text_diff);
248 be97ab03 2023-01-19 thomas if (diffstat) {
249 53d03f97 2023-01-10 thomas char *path = NULL;
250 53d03f97 2023-01-10 thomas int status = GOT_STATUS_NO_CHANGE;
252 e80ef83e 2023-01-10 thomas if (blob1 == NULL)
253 e80ef83e 2023-01-10 thomas status = GOT_STATUS_ADD;
254 e80ef83e 2023-01-10 thomas else if (blob2 == NULL)
255 e80ef83e 2023-01-10 thomas status = GOT_STATUS_DELETE;
257 be97ab03 2023-01-19 thomas if (strcmp(idstr1, idstr2) != 0)
258 be97ab03 2023-01-19 thomas status = GOT_STATUS_MODIFY;
259 be97ab03 2023-01-19 thomas else if (mode1 != mode2)
260 be97ab03 2023-01-19 thomas status = GOT_STATUS_MODE_CHANGE;
263 53d03f97 2023-01-10 thomas if (label1 == NULL && label2 == NULL) {
264 53d03f97 2023-01-10 thomas /* diffstat of blobs, show hash instead of path */
265 53d03f97 2023-01-10 thomas if (asprintf(&path, "%.10s -> %.10s",
266 53d03f97 2023-01-10 thomas idstr1, idstr2) == -1) {
267 53d03f97 2023-01-10 thomas err = got_error_from_errno("asprintf");
268 53d03f97 2023-01-10 thomas goto done;
271 e80ef83e 2023-01-10 thomas if (label2 != NULL &&
272 e80ef83e 2023-01-10 thomas (status != GOT_STATUS_DELETE || label1 == NULL))
273 e80ef83e 2023-01-10 thomas path = strdup(label2);
275 e80ef83e 2023-01-10 thomas path = strdup(label1);
276 53d03f97 2023-01-10 thomas if (path == NULL) {
277 e80ef83e 2023-01-10 thomas err = got_error_from_errno("strdup");
278 53d03f97 2023-01-10 thomas goto done;
282 be97ab03 2023-01-19 thomas err = get_diffstat(diffstat, path, result->result,
283 be97ab03 2023-01-19 thomas force_text_diff, status);
284 53d03f97 2023-01-10 thomas if (err) {
285 53d03f97 2023-01-10 thomas free(path);
286 53d03f97 2023-01-10 thomas goto done;
290 fe621944 2020-11-10 stsp if (outfile) {
291 82c78e96 2022-08-06 thomas err = got_diffreg_output(lines, nlines, result,
292 1cb46f00 2020-11-21 stsp blob1 != NULL, blob2 != NULL,
293 fe621944 2020-11-10 stsp label1 ? label1 : idstr1,
294 fe621944 2020-11-10 stsp label2 ? label2 : idstr2,
295 fe621944 2020-11-10 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
301 92a56907 2023-01-14 thomas free(modestr1);
302 92a56907 2023-01-14 thomas free(modestr2);
303 fe621944 2020-11-10 stsp if (resultp && err == NULL)
304 fe621944 2020-11-10 stsp *resultp = result;
305 a6d62598 2023-01-07 thomas else if (result) {
306 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
307 fe621944 2020-11-10 stsp if (free_err && err == NULL)
308 fe621944 2020-11-10 stsp err = free_err;
311 7d283eee 2017-11-29 stsp return err;
314 aaa13589 2019-06-01 stsp const struct got_error *
315 aaa13589 2019-06-01 stsp got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
316 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
317 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
318 a0f32f33 2022-06-13 thomas const char *label1, const char *label2, mode_t mode1, mode_t mode2,
319 a0f32f33 2022-06-13 thomas struct got_repository *repo)
321 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg *a = arg;
323 82c78e96 2022-08-06 thomas return diff_blobs(&a->lines, &a->nlines, NULL,
324 a0f32f33 2022-06-13 thomas blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
325 be97ab03 2023-01-19 thomas a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile,
326 be97ab03 2023-01-19 thomas a->diff_algo);
329 404c43c4 2018-06-21 stsp const struct got_error *
330 82c78e96 2022-08-06 thomas got_diff_blob(struct got_diff_line **lines, size_t*nlines,
331 fe621944 2020-11-10 stsp struct got_blob_object *blob1, struct got_blob_object *blob2,
332 a0f32f33 2022-06-13 thomas FILE *f1, FILE *f2, const char *label1, const char *label2,
333 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo, int diff_context,
334 be97ab03 2023-01-19 thomas int ignore_whitespace, int force_text_diff,
335 53d03f97 2023-01-10 thomas struct got_diffstat_cb_arg *ds, FILE *outfile)
337 82c78e96 2022-08-06 thomas return diff_blobs(lines, nlines, NULL, blob1, blob2, f1, f2,
338 64453f7e 2020-11-21 stsp label1, label2, 0, 0, diff_context, ignore_whitespace,
339 be97ab03 2023-01-19 thomas force_text_diff, ds, outfile, diff_algo);
342 7f1f93af 2019-08-06 stsp static const struct got_error *
343 fe621944 2020-11-10 stsp diff_blob_file(struct got_diffreg_result **resultp,
344 a0f32f33 2022-06-13 thomas struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
345 6d054bb9 2022-09-23 thomas FILE *f2, int f2_exists, struct stat *sb2, const char *label2,
346 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
347 be97ab03 2023-01-19 thomas int force_text_diff, struct got_diffstat_cb_arg *diffstat, FILE *outfile)
349 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
350 01986ce9 2023-02-07 thomas char hex1[GOT_OBJECT_ID_HEX_MAXLEN];
351 0c6f49ba 2022-07-01 thomas const char *idstr1 = NULL;
352 fe621944 2020-11-10 stsp struct got_diffreg_result *result = NULL;
354 fe621944 2020-11-10 stsp if (resultp)
355 fe621944 2020-11-10 stsp *resultp = NULL;
357 a0f32f33 2022-06-13 thomas if (blob1)
358 b72f483a 2019-02-05 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
360 b72f483a 2019-02-05 stsp idstr1 = "/dev/null";
362 7f1f93af 2019-08-06 stsp if (outfile) {
363 6d054bb9 2022-09-23 thomas char *mode = NULL;
365 6d054bb9 2022-09-23 thomas /* display file mode for new added files only */
366 6d054bb9 2022-09-23 thomas if (f2_exists && blob1 == NULL) {
367 6d054bb9 2022-09-23 thomas int mmask = (S_IRWXU | S_IRWXG | S_IRWXO);
369 6d054bb9 2022-09-23 thomas if (S_ISLNK(sb2->st_mode))
370 6d054bb9 2022-09-23 thomas mmask = S_IFLNK;
371 6d054bb9 2022-09-23 thomas if (asprintf(&mode, " (mode %o)",
372 6d054bb9 2022-09-23 thomas sb2->st_mode & mmask) == -1)
373 6d054bb9 2022-09-23 thomas return got_error_from_errno("asprintf");
375 4ce46740 2019-08-08 stsp fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
376 6d054bb9 2022-09-23 thomas fprintf(outfile, "file + %s%s\n",
377 6d054bb9 2022-09-23 thomas f2_exists ? label2 : "/dev/null", mode ? mode : "");
378 6d054bb9 2022-09-23 thomas free(mode);
381 25ec7006 2022-07-01 thomas err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
382 25ec7006 2022-07-01 thomas force_text_diff);
386 fe621944 2020-11-10 stsp if (outfile) {
387 1cb46f00 2020-11-21 stsp err = got_diffreg_output(NULL, NULL, result,
388 dd2e2f52 2022-07-01 thomas blob1 != NULL, f2_exists,
389 1cb46f00 2020-11-21 stsp label2, /* show local file's path, not a blob ID */
390 1cb46f00 2020-11-21 stsp label2, GOT_DIFF_OUTPUT_UNIDIFF,
391 1cb46f00 2020-11-21 stsp diff_context, outfile);
393 53d03f97 2023-01-10 thomas goto done;
396 be97ab03 2023-01-19 thomas if (diffstat) {
397 53d03f97 2023-01-10 thomas char *path = NULL;
398 53d03f97 2023-01-10 thomas int status = GOT_STATUS_NO_CHANGE;
401 53d03f97 2023-01-10 thomas * Ignore 'm'ode status change: if there's no accompanying
402 53d03f97 2023-01-10 thomas * content change, there'll be no diffstat, and if there
403 53d03f97 2023-01-10 thomas * are actual changes, 'M'odified takes precedence.
405 53d03f97 2023-01-10 thomas if (blob1 == NULL)
406 53d03f97 2023-01-10 thomas status = GOT_STATUS_ADD;
407 53d03f97 2023-01-10 thomas else if (!f2_exists)
408 53d03f97 2023-01-10 thomas status = GOT_STATUS_DELETE;
410 53d03f97 2023-01-10 thomas status = GOT_STATUS_MODIFY;
412 e80ef83e 2023-01-10 thomas if (label2 != NULL &&
413 e80ef83e 2023-01-10 thomas (status != GOT_STATUS_DELETE || label1 == NULL))
414 e80ef83e 2023-01-10 thomas path = strdup(label2);
416 e80ef83e 2023-01-10 thomas path = strdup(label1);
417 e80ef83e 2023-01-10 thomas if (path == NULL) {
418 e80ef83e 2023-01-10 thomas err = got_error_from_errno("strdup");
419 e80ef83e 2023-01-10 thomas goto done;
422 be97ab03 2023-01-19 thomas err = get_diffstat(diffstat, path, result->result,
423 be97ab03 2023-01-19 thomas force_text_diff, status);
424 53d03f97 2023-01-10 thomas if (err) {
425 53d03f97 2023-01-10 thomas free(path);
431 fe621944 2020-11-10 stsp if (resultp && err == NULL)
432 fe621944 2020-11-10 stsp *resultp = result;
433 fe621944 2020-11-10 stsp else if (result) {
434 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
435 fe621944 2020-11-10 stsp if (free_err && err == NULL)
436 fe621944 2020-11-10 stsp err = free_err;
438 b72f483a 2019-02-05 stsp return err;
441 7f1f93af 2019-08-06 stsp const struct got_error *
442 a0f32f33 2022-06-13 thomas got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
443 6d054bb9 2022-09-23 thomas const char *label1, FILE *f2, int f2_exists, struct stat *sb2,
444 25ec7006 2022-07-01 thomas const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
445 be97ab03 2023-01-19 thomas int ignore_whitespace, int force_text_diff,
446 53d03f97 2023-01-10 thomas struct got_diffstat_cb_arg *ds, FILE *outfile)
448 dd2e2f52 2022-07-01 thomas return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
449 6d054bb9 2022-09-23 thomas sb2, label2, diff_algo, diff_context, ignore_whitespace,
450 be97ab03 2023-01-19 thomas force_text_diff, ds, outfile);
453 474b4f94 2017-11-30 stsp static const struct got_error *
454 dd2e2f52 2022-07-01 thomas diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
455 19a6a6b5 2022-07-01 thomas const char *label, mode_t mode, struct got_repository *repo,
456 a0f32f33 2022-06-13 thomas got_diff_blob_cb cb, void *cb_arg)
458 4e22badc 2017-11-30 stsp const struct got_error *err;
459 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
460 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
462 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
464 4e22badc 2017-11-30 stsp return err;
466 dd2e2f52 2022-07-01 thomas err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
469 dd2e2f52 2022-07-01 thomas err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
470 a0f32f33 2022-06-13 thomas NULL, label, 0, mode, repo);
472 2acfca77 2018-04-01 stsp got_object_close(obj);
474 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
475 2acfca77 2018-04-01 stsp return err;
478 474b4f94 2017-11-30 stsp static const struct got_error *
479 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
480 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
481 19a6a6b5 2022-07-01 thomas const char *label1, const char *label2,
482 a0f32f33 2022-06-13 thomas mode_t mode1, mode_t mode2, struct got_repository *repo,
483 a0f32f33 2022-06-13 thomas got_diff_blob_cb cb, void *cb_arg)
485 6a213ccb 2017-11-30 stsp const struct got_error *err;
486 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
487 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
488 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
489 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
491 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
493 730a8aa0 2018-04-24 stsp return err;
495 15a94983 2018-12-23 stsp if (obj1->type != GOT_OBJ_TYPE_BLOB) {
496 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
500 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
503 15a94983 2018-12-23 stsp if (obj2->type != GOT_OBJ_TYPE_BLOB) {
504 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
508 f4ae6ddb 2022-07-01 thomas err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
512 f4ae6ddb 2022-07-01 thomas err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
516 a0f32f33 2022-06-13 thomas err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
517 a0f32f33 2022-06-13 thomas mode1, mode2, repo);
520 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
522 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
524 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
526 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
527 6a213ccb 2017-11-30 stsp return err;
530 474b4f94 2017-11-30 stsp static const struct got_error *
531 dd2e2f52 2022-07-01 thomas diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
532 dd2e2f52 2022-07-01 thomas FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
533 19a6a6b5 2022-07-01 thomas got_diff_blob_cb cb, void *cb_arg)
535 365fb436 2017-11-30 stsp const struct got_error *err;
536 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
537 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
539 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
541 365fb436 2017-11-30 stsp return err;
543 dd2e2f52 2022-07-01 thomas err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
546 dd2e2f52 2022-07-01 thomas err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
547 a0f32f33 2022-06-13 thomas mode, 0, repo);
549 2acfca77 2018-04-01 stsp got_object_close(obj);
551 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
552 2acfca77 2018-04-01 stsp return err;
555 474b4f94 2017-11-30 stsp static const struct got_error *
556 dd2e2f52 2022-07-01 thomas diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
557 dd2e2f52 2022-07-01 thomas const char *label, struct got_repository *repo, got_diff_blob_cb cb,
558 dd2e2f52 2022-07-01 thomas void *cb_arg, int diff_content)
560 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
561 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
562 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
564 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
568 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
569 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
573 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
577 dd2e2f52 2022-07-01 thomas err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
578 a0f32f33 2022-06-13 thomas repo, cb, cb_arg, diff_content);
581 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
582 9c70d4c3 2017-11-30 stsp if (treeobj)
583 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
584 9c70d4c3 2017-11-30 stsp return err;
587 474b4f94 2017-11-30 stsp static const struct got_error *
588 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
589 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
590 19a6a6b5 2022-07-01 thomas const char *label1, const char *label2,
591 a0f32f33 2022-06-13 thomas struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
592 a0f32f33 2022-06-13 thomas int diff_content)
594 f6861a81 2018-09-13 stsp const struct got_error *err;
595 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
596 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
597 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
598 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
600 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
604 15a94983 2018-12-23 stsp if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
605 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
609 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
613 15a94983 2018-12-23 stsp if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
614 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
618 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
622 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
626 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
627 19a6a6b5 2022-07-01 thomas label1, label2, repo, cb, cb_arg, diff_content);
631 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
633 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
634 789689b5 2017-11-30 stsp if (treeobj1)
635 789689b5 2017-11-30 stsp got_object_close(treeobj1);
636 789689b5 2017-11-30 stsp if (treeobj2)
637 789689b5 2017-11-30 stsp got_object_close(treeobj2);
638 789689b5 2017-11-30 stsp return err;
641 474b4f94 2017-11-30 stsp static const struct got_error *
642 dd2e2f52 2022-07-01 thomas diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
643 dd2e2f52 2022-07-01 thomas FILE *f2, const char *label, struct got_repository *repo,
644 19a6a6b5 2022-07-01 thomas got_diff_blob_cb cb, void *cb_arg, int diff_content)
646 f6861a81 2018-09-13 stsp const struct got_error *err;
647 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
648 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
650 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
654 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
655 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
659 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
663 dd2e2f52 2022-07-01 thomas err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
664 a0f32f33 2022-06-13 thomas repo, cb, cb_arg, diff_content);
667 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
668 2c56f2ce 2017-11-30 stsp if (treeobj)
669 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
670 2c56f2ce 2017-11-30 stsp return err;
673 474b4f94 2017-11-30 stsp static const struct got_error *
674 74671950 2018-02-11 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
675 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
676 aaa13589 2019-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
678 013404a9 2017-11-30 stsp /* XXX TODO */
679 474b4f94 2017-11-30 stsp return NULL;
682 474b4f94 2017-11-30 stsp static const struct got_error *
683 a0f32f33 2022-06-13 thomas diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
684 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
685 19a6a6b5 2022-07-01 thomas const char *label1, const char *label2,
686 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
687 31b4484f 2019-07-27 stsp int diff_content)
689 f6861a81 2018-09-13 stsp const struct got_error *err = NULL;
690 19ae6da1 2018-11-05 stsp int id_match;
692 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te1))
693 63c5ca5d 2019-08-24 stsp return NULL;
695 474b4f94 2017-11-30 stsp if (te2 == NULL) {
696 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
697 dd2e2f52 2022-07-01 thomas err = diff_deleted_tree(&te1->id, f1, fd1, f2,
698 dd2e2f52 2022-07-01 thomas label1, repo, cb, cb_arg, diff_content);
700 31b4484f 2019-07-27 stsp if (diff_content)
701 19a6a6b5 2022-07-01 thomas err = diff_deleted_blob(&te1->id, f1, fd1,
702 dd2e2f52 2022-07-01 thomas f2, label1, te1->mode, repo, cb, cb_arg);
704 a0f32f33 2022-06-13 thomas err = cb(cb_arg, NULL, NULL, NULL, NULL,
705 a0f32f33 2022-06-13 thomas &te1->id, NULL, label1, NULL,
706 a0f32f33 2022-06-13 thomas te1->mode, 0, repo);
708 f6861a81 2018-09-13 stsp return err;
709 63c5ca5d 2019-08-24 stsp } else if (got_object_tree_entry_is_submodule(te2))
710 63c5ca5d 2019-08-24 stsp return NULL;
712 56e0773d 2019-11-28 stsp id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
713 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
714 19ae6da1 2018-11-05 stsp if (!id_match)
715 a0f32f33 2022-06-13 thomas return diff_modified_tree(&te1->id, &te2->id, f1, f2,
716 19a6a6b5 2022-07-01 thomas fd1, fd2, label1, label2, repo, cb, cb_arg,
717 19a6a6b5 2022-07-01 thomas diff_content);
718 40dde666 2020-07-23 stsp } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
719 40dde666 2020-07-23 stsp (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
720 46f68b20 2019-10-19 stsp if (!id_match ||
721 40dde666 2020-07-23 stsp ((te1->mode & (S_IFLNK | S_IXUSR))) !=
722 40dde666 2020-07-23 stsp (te2->mode & (S_IFLNK | S_IXUSR))) {
723 31b4484f 2019-07-27 stsp if (diff_content)
724 56e0773d 2019-11-28 stsp return diff_modified_blob(&te1->id, &te2->id,
725 19a6a6b5 2022-07-01 thomas f1, f2, fd1, fd2, label1, label2,
726 19a6a6b5 2022-07-01 thomas te1->mode, te2->mode, repo, cb, cb_arg);
728 a0f32f33 2022-06-13 thomas return cb(cb_arg, NULL, NULL, NULL, NULL,
729 a0f32f33 2022-06-13 thomas &te1->id, &te2->id, label1, label2,
730 a0f32f33 2022-06-13 thomas te1->mode, te2->mode, repo);
734 19ae6da1 2018-11-05 stsp if (id_match)
735 f6861a81 2018-09-13 stsp return NULL;
737 56e0773d 2019-11-28 stsp return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
738 aaa13589 2019-06-01 stsp cb, cb_arg);
741 474b4f94 2017-11-30 stsp static const struct got_error *
742 56e0773d 2019-11-28 stsp diff_entry_new_old(struct got_tree_entry *te2,
743 dd2e2f52 2022-07-01 thomas struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
744 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
745 31b4484f 2019-07-27 stsp int diff_content)
747 f6861a81 2018-09-13 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
748 63c5ca5d 2019-08-24 stsp return NULL;
750 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te2))
751 f6861a81 2018-09-13 stsp return NULL;
753 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
754 dd2e2f52 2022-07-01 thomas return diff_added_tree(&te2->id, f1, f2, fd2, label2,
755 a0f32f33 2022-06-13 thomas repo, cb, cb_arg, diff_content);
757 31b4484f 2019-07-27 stsp if (diff_content)
758 dd2e2f52 2022-07-01 thomas return diff_added_blob(&te2->id, f1, f2, fd2,
759 19a6a6b5 2022-07-01 thomas label2, te2->mode, repo, cb, cb_arg);
761 a0f32f33 2022-06-13 thomas return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
762 a0f32f33 2022-06-13 thomas NULL, label2, 0, te2->mode, repo);
765 0208f208 2020-05-05 stsp const struct got_error *
766 772fcad5 2023-01-07 thomas got_diff_tree_compute_diffstat(void *arg, struct got_blob_object *blob1,
767 772fcad5 2023-01-07 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
768 772fcad5 2023-01-07 thomas struct got_object_id *id1, struct got_object_id *id2,
769 772fcad5 2023-01-07 thomas const char *label1, const char *label2,
770 772fcad5 2023-01-07 thomas mode_t mode1, mode_t mode2, struct got_repository *repo)
772 772fcad5 2023-01-07 thomas const struct got_error *err = NULL;
773 772fcad5 2023-01-07 thomas struct got_diffreg_result *result = NULL;
774 772fcad5 2023-01-07 thomas struct got_diffstat_cb_arg *a = arg;
775 772fcad5 2023-01-07 thomas char *path = NULL;
776 53d03f97 2023-01-10 thomas int status = GOT_STATUS_NO_CHANGE;
778 772fcad5 2023-01-07 thomas path = strdup(label2 ? label2 : label1);
779 772fcad5 2023-01-07 thomas if (path == NULL)
780 e80ef83e 2023-01-10 thomas return got_error_from_errno("strdup");
782 772fcad5 2023-01-07 thomas if (id1 == NULL)
783 53d03f97 2023-01-10 thomas status = GOT_STATUS_ADD;
784 772fcad5 2023-01-07 thomas else if (id2 == NULL)
785 53d03f97 2023-01-10 thomas status = GOT_STATUS_DELETE;
787 772fcad5 2023-01-07 thomas if (got_object_id_cmp(id1, id2) != 0)
788 53d03f97 2023-01-10 thomas status = GOT_STATUS_MODIFY;
789 772fcad5 2023-01-07 thomas else if (mode1 != mode2)
790 53d03f97 2023-01-10 thomas status = GOT_STATUS_MODE_CHANGE;
793 772fcad5 2023-01-07 thomas if (f1) {
794 772fcad5 2023-01-07 thomas err = got_opentemp_truncate(f1);
796 772fcad5 2023-01-07 thomas goto done;
798 772fcad5 2023-01-07 thomas if (f2) {
799 772fcad5 2023-01-07 thomas err = got_opentemp_truncate(f2);
801 772fcad5 2023-01-07 thomas goto done;
804 772fcad5 2023-01-07 thomas if (blob1) {
805 772fcad5 2023-01-07 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1,
808 772fcad5 2023-01-07 thomas goto done;
810 772fcad5 2023-01-07 thomas if (blob2) {
811 772fcad5 2023-01-07 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2,
814 772fcad5 2023-01-07 thomas goto done;
817 772fcad5 2023-01-07 thomas err = got_diffreg(&result, f1, f2, a->diff_algo, a->ignore_ws,
818 772fcad5 2023-01-07 thomas a->force_text);
820 772fcad5 2023-01-07 thomas goto done;
822 53d03f97 2023-01-10 thomas err = get_diffstat(a, path, result->result, a->force_text, status);
825 772fcad5 2023-01-07 thomas if (result) {
826 772fcad5 2023-01-07 thomas const struct got_error *free_err;
828 772fcad5 2023-01-07 thomas free_err = got_diffreg_result_free(result);
829 772fcad5 2023-01-07 thomas if (free_err && err == NULL)
830 772fcad5 2023-01-07 thomas err = free_err;
833 772fcad5 2023-01-07 thomas free(path);
834 772fcad5 2023-01-07 thomas return err;
837 772fcad5 2023-01-07 thomas const struct got_error *
838 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
839 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
840 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
841 a0f32f33 2022-06-13 thomas const char *label1, const char *label2,
842 0208f208 2020-05-05 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
844 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
845 0208f208 2020-05-05 stsp struct got_pathlist_head *paths = arg;
846 0208f208 2020-05-05 stsp struct got_diff_changed_path *change = NULL;
847 0208f208 2020-05-05 stsp char *path = NULL;
849 0208f208 2020-05-05 stsp path = strdup(label2 ? label2 : label1);
850 0208f208 2020-05-05 stsp if (path == NULL)
851 e80ef83e 2023-01-10 thomas return got_error_from_errno("strdup");
853 0208f208 2020-05-05 stsp change = malloc(sizeof(*change));
854 0208f208 2020-05-05 stsp if (change == NULL) {
855 0208f208 2020-05-05 stsp err = got_error_from_errno("malloc");
859 0208f208 2020-05-05 stsp change->status = GOT_STATUS_NO_CHANGE;
860 0208f208 2020-05-05 stsp if (id1 == NULL)
861 0208f208 2020-05-05 stsp change->status = GOT_STATUS_ADD;
862 0208f208 2020-05-05 stsp else if (id2 == NULL)
863 0208f208 2020-05-05 stsp change->status = GOT_STATUS_DELETE;
865 0208f208 2020-05-05 stsp if (got_object_id_cmp(id1, id2) != 0)
866 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODIFY;
867 0208f208 2020-05-05 stsp else if (mode1 != mode2)
868 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODE_CHANGE;
871 4c71f93b 2021-12-31 thomas err = got_pathlist_append(paths, path, change);
874 0208f208 2020-05-05 stsp free(path);
875 0208f208 2020-05-05 stsp free(change);
877 0208f208 2020-05-05 stsp return err;
880 474b4f94 2017-11-30 stsp const struct got_error *
881 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
882 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
883 19a6a6b5 2022-07-01 thomas const char *label1, const char *label2,
884 a0f32f33 2022-06-13 thomas struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
885 a0f32f33 2022-06-13 thomas int diff_content)
887 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
888 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
889 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
890 f6861a81 2018-09-13 stsp char *l1 = NULL, *l2 = NULL;
891 56e0773d 2019-11-28 stsp int tidx1 = 0, tidx2 = 0;
893 883f0469 2018-06-23 stsp if (tree1) {
894 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, 0);
895 60f50a58 2018-09-15 stsp if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
896 f6861a81 2018-09-13 stsp te1->name) == -1)
897 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
899 883f0469 2018-06-23 stsp if (tree2) {
900 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, 0);
901 60f50a58 2018-09-15 stsp if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
902 92a56907 2023-01-14 thomas te2->name) == -1) {
903 92a56907 2023-01-14 thomas err = got_error_from_errno("asprintf");
904 92a56907 2023-01-14 thomas goto done;
910 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
913 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree2,
914 1de5e065 2019-06-01 stsp te1->name);
918 f6861a81 2018-09-13 stsp if (te && asprintf(&l2, "%s%s%s", label2,
919 92a56907 2023-01-14 thomas label2[0] ? "/" : "", te->name) == -1) {
920 92a56907 2023-01-14 thomas err = got_error_from_errno("asprintf");
921 92a56907 2023-01-14 thomas goto done;
925 19a6a6b5 2022-07-01 thomas err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
926 19a6a6b5 2022-07-01 thomas l1, l2, repo, cb, cb_arg, diff_content);
932 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
935 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree1,
936 1de5e065 2019-06-01 stsp te2->name);
939 92a56907 2023-01-14 thomas l2 = NULL;
941 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
942 92a56907 2023-01-14 thomas label2[0] ? "/" : "", te->name) == -1) {
943 92a56907 2023-01-14 thomas err = got_error_from_errno("asprintf");
944 92a56907 2023-01-14 thomas goto done;
947 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
948 92a56907 2023-01-14 thomas label2[0] ? "/" : "", te2->name) == -1) {
949 92a56907 2023-01-14 thomas err = got_error_from_errno("asprintf");
950 92a56907 2023-01-14 thomas goto done;
954 dd2e2f52 2022-07-01 thomas err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
955 dd2e2f52 2022-07-01 thomas repo, cb, cb_arg, diff_content);
964 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, tidx1);
966 f6861a81 2018-09-13 stsp asprintf(&l1, "%s%s%s", label1,
967 92a56907 2023-01-14 thomas label1[0] ? "/" : "", te1->name) == -1) {
968 92a56907 2023-01-14 thomas err = got_error_from_errno("asprintf");
969 92a56907 2023-01-14 thomas goto done;
977 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, tidx2);
979 f6861a81 2018-09-13 stsp asprintf(&l2, "%s%s%s", label2,
980 92a56907 2023-01-14 thomas label2[0] ? "/" : "", te2->name) == -1) {
981 92a56907 2023-01-14 thomas err = got_error_from_errno("asprintf");
982 92a56907 2023-01-14 thomas goto done;
985 474b4f94 2017-11-30 stsp } while (te1 || te2);
988 92a56907 2023-01-14 thomas free(l1);
989 92a56907 2023-01-14 thomas free(l2);
990 11528a82 2018-05-19 stsp return err;
993 11528a82 2018-05-19 stsp const struct got_error *
994 82c78e96 2022-08-06 thomas got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
995 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
996 19a6a6b5 2022-07-01 thomas struct got_object_id *id1, struct got_object_id *id2,
997 25ec7006 2022-07-01 thomas const char *label1, const char *label2,
998 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo, int diff_context,
999 be97ab03 2023-01-19 thomas int ignore_whitespace, int force_text_diff, struct got_diffstat_cb_arg *ds,
1000 be97ab03 2023-01-19 thomas struct got_repository *repo, FILE *outfile)
1002 11528a82 2018-05-19 stsp const struct got_error *err;
1003 11528a82 2018-05-19 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1005 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
1006 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
1008 15a94983 2018-12-23 stsp if (id1) {
1009 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
1011 cd0acaa7 2018-05-20 stsp goto done;
1013 15a94983 2018-12-23 stsp if (id2) {
1014 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
1016 cd0acaa7 2018-05-20 stsp goto done;
1018 82c78e96 2022-08-06 thomas err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
1019 82c78e96 2022-08-06 thomas diff_algo, diff_context, ignore_whitespace, force_text_diff,
1020 be97ab03 2023-01-19 thomas ds, outfile);
1022 cc8021af 2021-10-12 thomas if (blob1)
1023 cc8021af 2021-10-12 thomas got_object_blob_close(blob1);
1024 cc8021af 2021-10-12 thomas if (blob2)
1025 cc8021af 2021-10-12 thomas got_object_blob_close(blob2);
1026 cc8021af 2021-10-12 thomas return err;
1029 cc8021af 2021-10-12 thomas static const struct got_error *
1030 cc8021af 2021-10-12 thomas diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
1031 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
1032 a0f32f33 2022-06-13 thomas struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
1034 cc8021af 2021-10-12 thomas const struct got_error *err = NULL;
1035 cc8021af 2021-10-12 thomas struct got_pathlist_entry *pe;
1036 cc8021af 2021-10-12 thomas struct got_object_id *id1 = NULL, *id2 = NULL;
1037 cc8021af 2021-10-12 thomas struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
1038 cc8021af 2021-10-12 thomas struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1040 cc8021af 2021-10-12 thomas TAILQ_FOREACH(pe, paths, entry) {
1041 cc8021af 2021-10-12 thomas int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
1042 cc8021af 2021-10-12 thomas mode_t mode1 = 0, mode2 = 0;
1044 cc8021af 2021-10-12 thomas free(id1);
1045 cc8021af 2021-10-12 thomas id1 = NULL;
1046 cc8021af 2021-10-12 thomas free(id2);
1047 cc8021af 2021-10-12 thomas id2 = NULL;
1048 cc8021af 2021-10-12 thomas if (subtree1) {
1049 cc8021af 2021-10-12 thomas got_object_tree_close(subtree1);
1050 cc8021af 2021-10-12 thomas subtree1 = NULL;
1052 cc8021af 2021-10-12 thomas if (subtree2) {
1053 cc8021af 2021-10-12 thomas got_object_tree_close(subtree2);
1054 cc8021af 2021-10-12 thomas subtree2 = NULL;
1056 cc8021af 2021-10-12 thomas if (blob1) {
1057 cc8021af 2021-10-12 thomas got_object_blob_close(blob1);
1058 cc8021af 2021-10-12 thomas blob1 = NULL;
1060 cc8021af 2021-10-12 thomas if (blob2) {
1061 cc8021af 2021-10-12 thomas got_object_blob_close(blob2);
1062 cc8021af 2021-10-12 thomas blob2 = NULL;
1065 cc8021af 2021-10-12 thomas err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
1066 cc8021af 2021-10-12 thomas pe->path);
1067 cc8021af 2021-10-12 thomas if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1068 cc8021af 2021-10-12 thomas goto done;
1069 cc8021af 2021-10-12 thomas err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
1070 cc8021af 2021-10-12 thomas pe->path);
1071 cc8021af 2021-10-12 thomas if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1072 cc8021af 2021-10-12 thomas goto done;
1073 cc8021af 2021-10-12 thomas if (id1 == NULL && id2 == NULL) {
1074 cc8021af 2021-10-12 thomas err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
1075 cc8021af 2021-10-12 thomas goto done;
1077 cc8021af 2021-10-12 thomas if (id1) {
1078 cc8021af 2021-10-12 thomas err = got_object_get_type(&type1, repo, id1);
1079 cc8021af 2021-10-12 thomas if (err)
1080 cc8021af 2021-10-12 thomas goto done;
1082 cc8021af 2021-10-12 thomas if (id2) {
1083 cc8021af 2021-10-12 thomas err = got_object_get_type(&type2, repo, id2);
1084 cc8021af 2021-10-12 thomas if (err)
1085 cc8021af 2021-10-12 thomas goto done;
1087 cc8021af 2021-10-12 thomas if (type1 == GOT_OBJ_TYPE_ANY &&
1088 cc8021af 2021-10-12 thomas type2 == GOT_OBJ_TYPE_ANY) {
1089 cc8021af 2021-10-12 thomas err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
1090 cc8021af 2021-10-12 thomas goto done;
1091 cc8021af 2021-10-12 thomas } else if (type1 != GOT_OBJ_TYPE_ANY &&
1092 cc8021af 2021-10-12 thomas type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
1093 cc8021af 2021-10-12 thomas err = got_error(GOT_ERR_OBJ_TYPE);
1094 cc8021af 2021-10-12 thomas goto done;
1097 cc8021af 2021-10-12 thomas if (type1 == GOT_OBJ_TYPE_BLOB ||
1098 cc8021af 2021-10-12 thomas type2 == GOT_OBJ_TYPE_BLOB) {
1099 cc8021af 2021-10-12 thomas if (id1) {
1100 cc8021af 2021-10-12 thomas err = got_object_open_as_blob(&blob1, repo,
1101 f4ae6ddb 2022-07-01 thomas id1, 8192, fd1);
1102 cc8021af 2021-10-12 thomas if (err)
1103 cc8021af 2021-10-12 thomas goto done;
1105 cc8021af 2021-10-12 thomas if (id2) {
1106 cc8021af 2021-10-12 thomas err = got_object_open_as_blob(&blob2, repo,
1107 f4ae6ddb 2022-07-01 thomas id2, 8192, fd2);
1108 cc8021af 2021-10-12 thomas if (err)
1109 cc8021af 2021-10-12 thomas goto done;
1111 a0f32f33 2022-06-13 thomas err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
1112 cc8021af 2021-10-12 thomas id1 ? pe->path : "/dev/null",
1113 cc8021af 2021-10-12 thomas id2 ? pe->path : "/dev/null",
1114 cc8021af 2021-10-12 thomas mode1, mode2, repo);
1115 cc8021af 2021-10-12 thomas if (err)
1116 cc8021af 2021-10-12 thomas goto done;
1117 cc8021af 2021-10-12 thomas } else if (type1 == GOT_OBJ_TYPE_TREE ||
1118 cc8021af 2021-10-12 thomas type2 == GOT_OBJ_TYPE_TREE) {
1119 cc8021af 2021-10-12 thomas if (id1) {
1120 cc8021af 2021-10-12 thomas err = got_object_open_as_tree(&subtree1, repo,
1122 cc8021af 2021-10-12 thomas if (err)
1123 cc8021af 2021-10-12 thomas goto done;
1125 cc8021af 2021-10-12 thomas if (id2) {
1126 cc8021af 2021-10-12 thomas err = got_object_open_as_tree(&subtree2, repo,
1128 cc8021af 2021-10-12 thomas if (err)
1129 cc8021af 2021-10-12 thomas goto done;
1131 a0f32f33 2022-06-13 thomas err = got_diff_tree(subtree1, subtree2, f1, f2,
1132 19a6a6b5 2022-07-01 thomas fd1, fd2,
1133 cc8021af 2021-10-12 thomas id1 ? pe->path : "/dev/null",
1134 cc8021af 2021-10-12 thomas id2 ? pe->path : "/dev/null",
1135 cc8021af 2021-10-12 thomas repo, cb, cb_arg, 1);
1136 cc8021af 2021-10-12 thomas if (err)
1137 cc8021af 2021-10-12 thomas goto done;
1138 cc8021af 2021-10-12 thomas } else {
1139 cc8021af 2021-10-12 thomas err = got_error(GOT_ERR_OBJ_TYPE);
1140 cc8021af 2021-10-12 thomas goto done;
1144 cc8021af 2021-10-12 thomas free(id1);
1145 cc8021af 2021-10-12 thomas free(id2);
1146 cc8021af 2021-10-12 thomas if (subtree1)
1147 cc8021af 2021-10-12 thomas got_object_tree_close(subtree1);
1148 cc8021af 2021-10-12 thomas if (subtree2)
1149 cc8021af 2021-10-12 thomas got_object_tree_close(subtree2);
1150 11528a82 2018-05-19 stsp if (blob1)
1151 11528a82 2018-05-19 stsp got_object_blob_close(blob1);
1152 11528a82 2018-05-19 stsp if (blob2)
1153 11528a82 2018-05-19 stsp got_object_blob_close(blob2);
1154 474b4f94 2017-11-30 stsp return err;
1157 9b4458b4 2022-06-26 thomas static const struct got_error *
1158 82c78e96 2022-08-06 thomas show_object_id(struct got_diff_line **lines, size_t *nlines,
1159 82c78e96 2022-08-06 thomas const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
1161 9b4458b4 2022-06-26 thomas const struct got_error *err;
1163 9b4458b4 2022-06-26 thomas off_t outoff = 0;
1165 9b4458b4 2022-06-26 thomas n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
1166 7e1f3a3d 2022-08-16 thomas if (n < 0)
1167 7e1f3a3d 2022-08-16 thomas return got_error_from_errno("fprintf");
1169 82c78e96 2022-08-06 thomas if (lines != NULL && *lines != NULL) {
1170 9b4458b4 2022-06-26 thomas if (*nlines == 0) {
1171 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0,
1172 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
1173 9b4458b4 2022-06-26 thomas if (err)
1174 9b4458b4 2022-06-26 thomas return err;
1176 82c78e96 2022-08-06 thomas outoff = (*lines)[*nlines - 1].offset;
1178 9b4458b4 2022-06-26 thomas outoff += n;
1179 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
1180 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
1181 9b4458b4 2022-06-26 thomas if (err)
1182 9b4458b4 2022-06-26 thomas return err;
1185 9b4458b4 2022-06-26 thomas return NULL;
1188 9b4458b4 2022-06-26 thomas static const struct got_error *
1189 82c78e96 2022-08-06 thomas diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1190 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
1191 19a6a6b5 2022-07-01 thomas struct got_object_id *id1, struct got_object_id *id2,
1192 0c6f49ba 2022-07-01 thomas struct got_pathlist_head *paths, const char *label1, const char *label2,
1193 0c6f49ba 2022-07-01 thomas int diff_context, int ignore_whitespace, int force_text_diff,
1194 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg *dsa, struct got_repository *repo,
1195 be97ab03 2023-01-19 thomas FILE *outfile, enum got_diff_algorithm diff_algo)
1197 11528a82 2018-05-19 stsp const struct got_error *err;
1198 11528a82 2018-05-19 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1199 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
1200 82c78e96 2022-08-06 thomas int want_linemeta = (lines != NULL && *lines != NULL);
1202 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
1203 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
1205 15a94983 2018-12-23 stsp if (id1) {
1206 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo, id1);
1208 cd0acaa7 2018-05-20 stsp goto done;
1210 15a94983 2018-12-23 stsp if (id2) {
1211 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo, id2);
1213 cd0acaa7 2018-05-20 stsp goto done;
1216 25ec7006 2022-07-01 thomas arg.diff_algo = diff_algo;
1217 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1218 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
1219 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
1220 53d03f97 2023-01-10 thomas arg.diffstat = dsa;
1221 aaa13589 2019-06-01 stsp arg.outfile = outfile;
1222 82c78e96 2022-08-06 thomas if (want_linemeta) {
1223 82c78e96 2022-08-06 thomas arg.lines = *lines;
1224 fe621944 2020-11-10 stsp arg.nlines = *nlines;
1226 82c78e96 2022-08-06 thomas arg.lines = NULL;
1227 fe621944 2020-11-10 stsp arg.nlines = 0;
1229 53d03f97 2023-01-10 thomas if (paths == NULL || TAILQ_EMPTY(paths))
1230 53d03f97 2023-01-10 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, label1,
1231 53d03f97 2023-01-10 thomas label2, repo, got_diff_blob_output_unidiff, &arg, 1);
1233 19a6a6b5 2022-07-01 thomas err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
1234 cc8021af 2021-10-12 thomas got_diff_blob_output_unidiff, &arg);
1235 82c78e96 2022-08-06 thomas if (want_linemeta) {
1236 82c78e96 2022-08-06 thomas *lines = arg.lines; /* was likely re-allocated */
1237 fe621944 2020-11-10 stsp *nlines = arg.nlines;
1240 11528a82 2018-05-19 stsp if (tree1)
1241 11528a82 2018-05-19 stsp got_object_tree_close(tree1);
1242 11528a82 2018-05-19 stsp if (tree2)
1243 11528a82 2018-05-19 stsp got_object_tree_close(tree2);
1244 11528a82 2018-05-19 stsp return err;
1247 11528a82 2018-05-19 stsp const struct got_error *
1248 82c78e96 2022-08-06 thomas got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1249 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
1250 19a6a6b5 2022-07-01 thomas struct got_object_id *id1, struct got_object_id *id2,
1251 0c6f49ba 2022-07-01 thomas struct got_pathlist_head *paths, const char *label1, const char *label2,
1252 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1253 be97ab03 2023-01-19 thomas int force_text_diff, struct got_diffstat_cb_arg *dsa,
1254 53d03f97 2023-01-10 thomas struct got_repository *repo, FILE *outfile)
1256 9b4458b4 2022-06-26 thomas const struct got_error *err;
1257 9b4458b4 2022-06-26 thomas char *idstr = NULL;
1259 9b4458b4 2022-06-26 thomas if (id1 == NULL && id2 == NULL)
1260 9b4458b4 2022-06-26 thomas return got_error(GOT_ERR_NO_OBJ);
1262 9b4458b4 2022-06-26 thomas if (id1) {
1263 9b4458b4 2022-06-26 thomas err = got_object_id_str(&idstr, id1);
1264 9b4458b4 2022-06-26 thomas if (err)
1265 9b4458b4 2022-06-26 thomas goto done;
1266 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1267 9b4458b4 2022-06-26 thomas if (err)
1268 9b4458b4 2022-06-26 thomas goto done;
1269 9b4458b4 2022-06-26 thomas free(idstr);
1270 9b4458b4 2022-06-26 thomas idstr = NULL;
1271 9b4458b4 2022-06-26 thomas } else {
1272 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1273 82c78e96 2022-08-06 thomas outfile);
1274 9b4458b4 2022-06-26 thomas if (err)
1275 9b4458b4 2022-06-26 thomas goto done;
1278 9b4458b4 2022-06-26 thomas if (id2) {
1279 9b4458b4 2022-06-26 thomas err = got_object_id_str(&idstr, id2);
1280 9b4458b4 2022-06-26 thomas if (err)
1281 9b4458b4 2022-06-26 thomas goto done;
1282 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1283 9b4458b4 2022-06-26 thomas if (err)
1284 9b4458b4 2022-06-26 thomas goto done;
1285 9b4458b4 2022-06-26 thomas free(idstr);
1286 9b4458b4 2022-06-26 thomas idstr = NULL;
1287 9b4458b4 2022-06-26 thomas } else {
1288 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1289 82c78e96 2022-08-06 thomas outfile);
1290 9b4458b4 2022-06-26 thomas if (err)
1291 9b4458b4 2022-06-26 thomas goto done;
1294 82c78e96 2022-08-06 thomas err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1295 82c78e96 2022-08-06 thomas paths, label1, label2, diff_context, ignore_whitespace,
1296 be97ab03 2023-01-19 thomas force_text_diff, dsa, repo, outfile, diff_algo);
1298 9b4458b4 2022-06-26 thomas free(idstr);
1299 9b4458b4 2022-06-26 thomas return err;
1302 9b4458b4 2022-06-26 thomas const struct got_error *
1303 82c78e96 2022-08-06 thomas got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1304 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
1305 19a6a6b5 2022-07-01 thomas struct got_object_id *id1, struct got_object_id *id2,
1306 25ec7006 2022-07-01 thomas struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1307 64453f7e 2020-11-21 stsp int diff_context, int ignore_whitespace, int force_text_diff,
1308 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg *dsa, struct got_repository *repo, FILE *outfile)
1310 11528a82 2018-05-19 stsp const struct got_error *err;
1311 11528a82 2018-05-19 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1312 9b4458b4 2022-06-26 thomas char *idstr = NULL;
1314 15a94983 2018-12-23 stsp if (id2 == NULL)
1315 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
1317 15a94983 2018-12-23 stsp if (id1) {
1318 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit1, repo, id1);
1320 cd0acaa7 2018-05-20 stsp goto done;
1321 9b4458b4 2022-06-26 thomas err = got_object_id_str(&idstr, id1);
1322 9b4458b4 2022-06-26 thomas if (err)
1323 9b4458b4 2022-06-26 thomas goto done;
1324 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "commit", '-', idstr,
1325 82c78e96 2022-08-06 thomas outfile);
1326 9b4458b4 2022-06-26 thomas if (err)
1327 9b4458b4 2022-06-26 thomas goto done;
1328 9b4458b4 2022-06-26 thomas free(idstr);
1329 9b4458b4 2022-06-26 thomas idstr = NULL;
1330 9b4458b4 2022-06-26 thomas } else {
1331 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1332 82c78e96 2022-08-06 thomas outfile);
1333 9b4458b4 2022-06-26 thomas if (err)
1334 9b4458b4 2022-06-26 thomas goto done;
1337 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, repo, id2);
1339 9b697879 2018-05-20 stsp goto done;
1341 9b4458b4 2022-06-26 thomas err = got_object_id_str(&idstr, id2);
1342 9b4458b4 2022-06-26 thomas if (err)
1343 9b4458b4 2022-06-26 thomas goto done;
1344 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1345 9b4458b4 2022-06-26 thomas if (err)
1346 9b4458b4 2022-06-26 thomas goto done;
1348 82c78e96 2022-08-06 thomas err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1349 15a94983 2018-12-23 stsp commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1350 cc8021af 2021-10-12 thomas got_object_commit_get_tree_id(commit2), paths, "", "",
1351 be97ab03 2023-01-19 thomas diff_context, ignore_whitespace, force_text_diff, dsa, repo,
1352 be97ab03 2023-01-19 thomas outfile, diff_algo);
1354 11528a82 2018-05-19 stsp if (commit1)
1355 11528a82 2018-05-19 stsp got_object_commit_close(commit1);
1356 11528a82 2018-05-19 stsp if (commit2)
1357 11528a82 2018-05-19 stsp got_object_commit_close(commit2);
1358 9b4458b4 2022-06-26 thomas free(idstr);
1359 11528a82 2018-05-19 stsp return err;
1362 dc424a06 2019-08-07 stsp const struct got_error *
1363 fe621944 2020-11-10 stsp got_diff_files(struct got_diffreg_result **resultp,
1364 dd2e2f52 2022-07-01 thomas FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1365 dd2e2f52 2022-07-01 thomas const char *label2, int diff_context, int ignore_whitespace,
1366 25ec7006 2022-07-01 thomas int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1368 dc424a06 2019-08-07 stsp const struct got_error *err = NULL;
1369 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
1371 fe621944 2020-11-10 stsp if (resultp)
1372 fe621944 2020-11-10 stsp *resultp = NULL;
1374 dc424a06 2019-08-07 stsp if (outfile) {
1375 dc424a06 2019-08-07 stsp fprintf(outfile, "file - %s\n",
1376 dd2e2f52 2022-07-01 thomas f1_exists ? label1 : "/dev/null");
1377 dc424a06 2019-08-07 stsp fprintf(outfile, "file + %s\n",
1378 dd2e2f52 2022-07-01 thomas f2_exists ? label2 : "/dev/null");
1381 25ec7006 2022-07-01 thomas err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1382 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
1384 fe621944 2020-11-10 stsp goto done;
1386 fe621944 2020-11-10 stsp if (outfile) {
1387 fe621944 2020-11-10 stsp err = got_diffreg_output(NULL, NULL, diffreg_result,
1388 dd2e2f52 2022-07-01 thomas f1_exists, f2_exists, label1, label2,
1389 1cb46f00 2020-11-21 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1391 dc424a06 2019-08-07 stsp goto done;
1395 fe621944 2020-11-10 stsp if (resultp && err == NULL)
1396 fe621944 2020-11-10 stsp *resultp = diffreg_result;
1397 fe621944 2020-11-10 stsp else if (diffreg_result) {
1398 fe621944 2020-11-10 stsp const struct got_error *free_err;
1400 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
1401 fe621944 2020-11-10 stsp if (free_err && err == NULL)
1402 fe621944 2020-11-10 stsp err = free_err;
1405 dc424a06 2019-08-07 stsp return err;