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 1c7f0520 2017-11-29 stsp #include <sys/stat.h>
19 7d283eee 2017-11-29 stsp #include <stdio.h>
20 7d283eee 2017-11-29 stsp #include <stdlib.h>
21 7d283eee 2017-11-29 stsp #include <string.h>
22 f9d67749 2017-11-30 stsp #include <limits.h>
23 7d283eee 2017-11-29 stsp #include <zlib.h>
25 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
27 7d283eee 2017-11-29 stsp #include "got_object.h"
28 e09a504c 2019-06-28 stsp #include "got_repository.h"
29 7d283eee 2017-11-29 stsp #include "got_error.h"
30 789689b5 2017-11-30 stsp #include "got_diff.h"
31 324d37e7 2019-05-11 stsp #include "got_path.h"
32 0208f208 2020-05-05 stsp #include "got_cancel.h"
33 0208f208 2020-05-05 stsp #include "got_worktree.h"
34 1758cce7 2022-06-13 thomas #include "got_opentemp.h"
36 718b3ab0 2018-03-17 stsp #include "got_lib_diff.h"
37 15a94983 2018-12-23 stsp #include "got_lib_delta.h"
38 15a94983 2018-12-23 stsp #include "got_lib_inflate.h"
39 15a94983 2018-12-23 stsp #include "got_lib_object.h"
41 404c43c4 2018-06-21 stsp static const struct got_error *
42 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
46 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
47 fe621944 2020-11-10 stsp if (p == NULL)
48 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
49 fe621944 2020-11-10 stsp *line_offsets = p;
50 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
51 fe621944 2020-11-10 stsp (*nlines)++;
52 fe621944 2020-11-10 stsp return NULL;
55 fe621944 2020-11-10 stsp static const struct got_error *
56 fe621944 2020-11-10 stsp diff_blobs(off_t **line_offsets, size_t *nlines,
57 fe621944 2020-11-10 stsp struct got_diffreg_result **resultp, struct got_blob_object *blob1,
58 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
59 46f68b20 2019-10-19 stsp const char *label1, const char *label2, mode_t mode1, mode_t mode2,
60 25ec7006 2022-07-01 thomas int diff_context, int ignore_whitespace, int force_text_diff, FILE *outfile,
61 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo)
63 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
64 f78b0693 2017-11-29 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
65 f78b0693 2017-11-29 stsp char hex2[SHA1_DIGEST_STRING_LENGTH];
66 0c6f49ba 2022-07-01 thomas const char *idstr1 = NULL, *idstr2 = NULL;
67 be659d10 2020-11-18 stsp off_t size1, size2;
68 fe621944 2020-11-10 stsp struct got_diffreg_result *result;
69 fe621944 2020-11-10 stsp off_t outoff = 0;
72 fe621944 2020-11-10 stsp if (line_offsets && *line_offsets && *nlines > 0)
73 fe621944 2020-11-10 stsp outoff = (*line_offsets)[*nlines - 1];
74 9c659ea0 2020-11-22 stsp else if (line_offsets) {
75 9c659ea0 2020-11-22 stsp err = add_line_offset(line_offsets, nlines, 0);
80 fe621944 2020-11-10 stsp if (resultp)
81 fe621944 2020-11-10 stsp *resultp = NULL;
84 1758cce7 2022-06-13 thomas err = got_opentemp_truncate(f1);
86 a0f32f33 2022-06-13 thomas goto done;
89 1758cce7 2022-06-13 thomas err = got_opentemp_truncate(f2);
91 a0f32f33 2022-06-13 thomas goto done;
95 98abbc84 2017-11-30 stsp if (blob1) {
96 f934cf2c 2018-02-12 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
97 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
102 98abbc84 2017-11-30 stsp idstr1 = "/dev/null";
105 98abbc84 2017-11-30 stsp if (blob2) {
106 f934cf2c 2018-02-12 stsp idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
107 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
112 98abbc84 2017-11-30 stsp idstr2 = "/dev/null";
114 09de383e 2018-12-24 stsp if (outfile) {
115 46f68b20 2019-10-19 stsp char *modestr1 = NULL, *modestr2 = NULL;
116 40dde666 2020-07-23 stsp int modebits;
117 46f68b20 2019-10-19 stsp if (mode1 && mode1 != mode2) {
118 40dde666 2020-07-23 stsp if (S_ISLNK(mode1))
119 40dde666 2020-07-23 stsp modebits = S_IFLNK;
121 40dde666 2020-07-23 stsp modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
122 46f68b20 2019-10-19 stsp if (asprintf(&modestr1, " (mode %o)",
123 40dde666 2020-07-23 stsp mode1 & modebits) == -1) {
124 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
128 46f68b20 2019-10-19 stsp if (mode2 && mode1 != mode2) {
129 40dde666 2020-07-23 stsp if (S_ISLNK(mode2))
130 40dde666 2020-07-23 stsp modebits = S_IFLNK;
132 40dde666 2020-07-23 stsp modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
133 46f68b20 2019-10-19 stsp if (asprintf(&modestr2, " (mode %o)",
134 40dde666 2020-07-23 stsp mode2 & modebits) == -1) {
135 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
139 fe621944 2020-11-10 stsp n = fprintf(outfile, "blob - %s%s\n", idstr1,
140 46f68b20 2019-10-19 stsp modestr1 ? modestr1 : "");
143 fe621944 2020-11-10 stsp outoff += n;
144 fe621944 2020-11-10 stsp if (line_offsets) {
145 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
150 fe621944 2020-11-10 stsp n = fprintf(outfile, "blob + %s%s\n", idstr2,
151 46f68b20 2019-10-19 stsp modestr2 ? modestr2 : "");
154 fe621944 2020-11-10 stsp outoff += n;
155 fe621944 2020-11-10 stsp if (line_offsets) {
156 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
161 46f68b20 2019-10-19 stsp free(modestr1);
162 46f68b20 2019-10-19 stsp free(modestr2);
164 25ec7006 2022-07-01 thomas err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
165 25ec7006 2022-07-01 thomas force_text_diff);
169 fe621944 2020-11-10 stsp if (outfile) {
170 1cb46f00 2020-11-21 stsp err = got_diffreg_output(line_offsets, nlines, result,
171 1cb46f00 2020-11-21 stsp blob1 != NULL, blob2 != NULL,
172 fe621944 2020-11-10 stsp label1 ? label1 : idstr1,
173 fe621944 2020-11-10 stsp label2 ? label2 : idstr2,
174 fe621944 2020-11-10 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
179 fe621944 2020-11-10 stsp if (resultp && err == NULL)
180 fe621944 2020-11-10 stsp *resultp = result;
182 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
183 fe621944 2020-11-10 stsp if (free_err && err == NULL)
184 fe621944 2020-11-10 stsp err = free_err;
187 7d283eee 2017-11-29 stsp return err;
190 aaa13589 2019-06-01 stsp const struct got_error *
191 aaa13589 2019-06-01 stsp got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
192 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
193 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
194 a0f32f33 2022-06-13 thomas const char *label1, const char *label2, mode_t mode1, mode_t mode2,
195 a0f32f33 2022-06-13 thomas struct got_repository *repo)
197 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg *a = arg;
199 fe621944 2020-11-10 stsp return diff_blobs(&a->line_offsets, &a->nlines, NULL,
200 a0f32f33 2022-06-13 thomas blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
201 25ec7006 2022-07-01 thomas a->ignore_whitespace, a->force_text_diff, a->outfile, a->diff_algo);
204 404c43c4 2018-06-21 stsp const struct got_error *
205 fe621944 2020-11-10 stsp got_diff_blob(off_t **line_offsets, size_t *nlines,
206 fe621944 2020-11-10 stsp struct got_blob_object *blob1, struct got_blob_object *blob2,
207 a0f32f33 2022-06-13 thomas FILE *f1, FILE *f2, const char *label1, const char *label2,
208 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo, int diff_context,
209 25ec7006 2022-07-01 thomas int ignore_whitespace, int force_text_diff, FILE *outfile)
211 a0f32f33 2022-06-13 thomas return diff_blobs(line_offsets, nlines, NULL, blob1, blob2, f1, f2,
212 64453f7e 2020-11-21 stsp label1, label2, 0, 0, diff_context, ignore_whitespace,
213 25ec7006 2022-07-01 thomas force_text_diff, outfile, diff_algo);
216 7f1f93af 2019-08-06 stsp static const struct got_error *
217 fe621944 2020-11-10 stsp diff_blob_file(struct got_diffreg_result **resultp,
218 a0f32f33 2022-06-13 thomas struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
219 25ec7006 2022-07-01 thomas FILE *f2, int f2_exists, size_t size2, const char *label2,
220 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
221 25ec7006 2022-07-01 thomas int force_text_diff, FILE *outfile)
223 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
224 b72f483a 2019-02-05 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
225 0c6f49ba 2022-07-01 thomas const char *idstr1 = NULL;
226 fe621944 2020-11-10 stsp struct got_diffreg_result *result = NULL;
228 fe621944 2020-11-10 stsp if (resultp)
229 fe621944 2020-11-10 stsp *resultp = NULL;
231 a0f32f33 2022-06-13 thomas if (blob1)
232 b72f483a 2019-02-05 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
234 b72f483a 2019-02-05 stsp idstr1 = "/dev/null";
236 7f1f93af 2019-08-06 stsp if (outfile) {
237 4ce46740 2019-08-08 stsp fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
238 7f1f93af 2019-08-06 stsp fprintf(outfile, "file + %s\n",
239 dd2e2f52 2022-07-01 thomas f2_exists ? label2 : "/dev/null");
242 25ec7006 2022-07-01 thomas err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
243 25ec7006 2022-07-01 thomas force_text_diff);
247 fe621944 2020-11-10 stsp if (outfile) {
248 1cb46f00 2020-11-21 stsp err = got_diffreg_output(NULL, NULL, result,
249 dd2e2f52 2022-07-01 thomas blob1 != NULL, f2_exists,
250 1cb46f00 2020-11-21 stsp label2, /* show local file's path, not a blob ID */
251 1cb46f00 2020-11-21 stsp label2, GOT_DIFF_OUTPUT_UNIDIFF,
252 1cb46f00 2020-11-21 stsp diff_context, outfile);
257 fe621944 2020-11-10 stsp if (resultp && err == NULL)
258 fe621944 2020-11-10 stsp *resultp = result;
259 fe621944 2020-11-10 stsp else if (result) {
260 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
261 fe621944 2020-11-10 stsp if (free_err && err == NULL)
262 fe621944 2020-11-10 stsp err = free_err;
265 b72f483a 2019-02-05 stsp return err;
268 7f1f93af 2019-08-06 stsp const struct got_error *
269 a0f32f33 2022-06-13 thomas got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
270 dd2e2f52 2022-07-01 thomas const char *label1, FILE *f2, int f2_exists, size_t size2,
271 25ec7006 2022-07-01 thomas const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
272 25ec7006 2022-07-01 thomas int ignore_whitespace, int force_text_diff, FILE *outfile)
274 dd2e2f52 2022-07-01 thomas return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
275 25ec7006 2022-07-01 thomas size2, label2, diff_algo, diff_context, ignore_whitespace,
276 25ec7006 2022-07-01 thomas force_text_diff, outfile );
279 474b4f94 2017-11-30 stsp static const struct got_error *
280 dd2e2f52 2022-07-01 thomas diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
281 19a6a6b5 2022-07-01 thomas const char *label, mode_t mode, struct got_repository *repo,
282 a0f32f33 2022-06-13 thomas got_diff_blob_cb cb, void *cb_arg)
284 4e22badc 2017-11-30 stsp const struct got_error *err;
285 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
286 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
288 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
290 4e22badc 2017-11-30 stsp return err;
292 dd2e2f52 2022-07-01 thomas err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
295 dd2e2f52 2022-07-01 thomas err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
296 a0f32f33 2022-06-13 thomas NULL, label, 0, mode, repo);
298 2acfca77 2018-04-01 stsp got_object_close(obj);
300 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
301 2acfca77 2018-04-01 stsp return err;
304 474b4f94 2017-11-30 stsp static const struct got_error *
305 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
306 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
307 19a6a6b5 2022-07-01 thomas const char *label1, const char *label2,
308 a0f32f33 2022-06-13 thomas mode_t mode1, mode_t mode2, struct got_repository *repo,
309 a0f32f33 2022-06-13 thomas got_diff_blob_cb cb, void *cb_arg)
311 6a213ccb 2017-11-30 stsp const struct got_error *err;
312 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
313 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
314 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
315 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
317 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
319 730a8aa0 2018-04-24 stsp return err;
321 15a94983 2018-12-23 stsp if (obj1->type != GOT_OBJ_TYPE_BLOB) {
322 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
326 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
329 15a94983 2018-12-23 stsp if (obj2->type != GOT_OBJ_TYPE_BLOB) {
330 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
334 f4ae6ddb 2022-07-01 thomas err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
338 f4ae6ddb 2022-07-01 thomas err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
342 a0f32f33 2022-06-13 thomas err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
343 a0f32f33 2022-06-13 thomas mode1, mode2, repo);
346 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
348 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
350 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
352 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
353 6a213ccb 2017-11-30 stsp return err;
356 474b4f94 2017-11-30 stsp static const struct got_error *
357 dd2e2f52 2022-07-01 thomas diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
358 dd2e2f52 2022-07-01 thomas FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
359 19a6a6b5 2022-07-01 thomas got_diff_blob_cb cb, void *cb_arg)
361 365fb436 2017-11-30 stsp const struct got_error *err;
362 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
363 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
365 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
367 365fb436 2017-11-30 stsp return err;
369 dd2e2f52 2022-07-01 thomas err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
372 dd2e2f52 2022-07-01 thomas err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
373 a0f32f33 2022-06-13 thomas mode, 0, repo);
375 2acfca77 2018-04-01 stsp got_object_close(obj);
377 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
378 2acfca77 2018-04-01 stsp return err;
381 474b4f94 2017-11-30 stsp static const struct got_error *
382 dd2e2f52 2022-07-01 thomas diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
383 dd2e2f52 2022-07-01 thomas const char *label, struct got_repository *repo, got_diff_blob_cb cb,
384 dd2e2f52 2022-07-01 thomas void *cb_arg, int diff_content)
386 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
387 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
388 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
390 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
394 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
395 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
399 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
403 dd2e2f52 2022-07-01 thomas err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
404 a0f32f33 2022-06-13 thomas repo, cb, cb_arg, diff_content);
407 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
408 9c70d4c3 2017-11-30 stsp if (treeobj)
409 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
410 9c70d4c3 2017-11-30 stsp return err;
413 474b4f94 2017-11-30 stsp static const struct got_error *
414 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
415 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
416 19a6a6b5 2022-07-01 thomas const char *label1, const char *label2,
417 a0f32f33 2022-06-13 thomas struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
418 a0f32f33 2022-06-13 thomas int diff_content)
420 f6861a81 2018-09-13 stsp const struct got_error *err;
421 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
422 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
423 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
424 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
426 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
430 15a94983 2018-12-23 stsp if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
431 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
435 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
439 15a94983 2018-12-23 stsp if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
440 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
444 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
448 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
452 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
453 19a6a6b5 2022-07-01 thomas label1, label2, repo, cb, cb_arg, diff_content);
457 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
459 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
460 789689b5 2017-11-30 stsp if (treeobj1)
461 789689b5 2017-11-30 stsp got_object_close(treeobj1);
462 789689b5 2017-11-30 stsp if (treeobj2)
463 789689b5 2017-11-30 stsp got_object_close(treeobj2);
464 789689b5 2017-11-30 stsp return err;
467 474b4f94 2017-11-30 stsp static const struct got_error *
468 dd2e2f52 2022-07-01 thomas diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
469 dd2e2f52 2022-07-01 thomas FILE *f2, const char *label, struct got_repository *repo,
470 19a6a6b5 2022-07-01 thomas got_diff_blob_cb cb, void *cb_arg, int diff_content)
472 f6861a81 2018-09-13 stsp const struct got_error *err;
473 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
474 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
476 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
480 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
481 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
485 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
489 dd2e2f52 2022-07-01 thomas err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
490 a0f32f33 2022-06-13 thomas repo, cb, cb_arg, diff_content);
493 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
494 2c56f2ce 2017-11-30 stsp if (treeobj)
495 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
496 2c56f2ce 2017-11-30 stsp return err;
499 474b4f94 2017-11-30 stsp static const struct got_error *
500 74671950 2018-02-11 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
501 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
502 aaa13589 2019-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
504 013404a9 2017-11-30 stsp /* XXX TODO */
505 474b4f94 2017-11-30 stsp return NULL;
508 474b4f94 2017-11-30 stsp static const struct got_error *
509 a0f32f33 2022-06-13 thomas diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
510 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
511 19a6a6b5 2022-07-01 thomas const char *label1, const char *label2,
512 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
513 31b4484f 2019-07-27 stsp int diff_content)
515 f6861a81 2018-09-13 stsp const struct got_error *err = NULL;
516 19ae6da1 2018-11-05 stsp int id_match;
518 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te1))
519 63c5ca5d 2019-08-24 stsp return NULL;
521 474b4f94 2017-11-30 stsp if (te2 == NULL) {
522 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
523 dd2e2f52 2022-07-01 thomas err = diff_deleted_tree(&te1->id, f1, fd1, f2,
524 dd2e2f52 2022-07-01 thomas label1, repo, cb, cb_arg, diff_content);
526 31b4484f 2019-07-27 stsp if (diff_content)
527 19a6a6b5 2022-07-01 thomas err = diff_deleted_blob(&te1->id, f1, fd1,
528 dd2e2f52 2022-07-01 thomas f2, label1, te1->mode, repo, cb, cb_arg);
530 a0f32f33 2022-06-13 thomas err = cb(cb_arg, NULL, NULL, NULL, NULL,
531 a0f32f33 2022-06-13 thomas &te1->id, NULL, label1, NULL,
532 a0f32f33 2022-06-13 thomas te1->mode, 0, repo);
534 f6861a81 2018-09-13 stsp return err;
535 63c5ca5d 2019-08-24 stsp } else if (got_object_tree_entry_is_submodule(te2))
536 63c5ca5d 2019-08-24 stsp return NULL;
538 56e0773d 2019-11-28 stsp id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
539 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
540 19ae6da1 2018-11-05 stsp if (!id_match)
541 a0f32f33 2022-06-13 thomas return diff_modified_tree(&te1->id, &te2->id, f1, f2,
542 19a6a6b5 2022-07-01 thomas fd1, fd2, label1, label2, repo, cb, cb_arg,
543 19a6a6b5 2022-07-01 thomas diff_content);
544 40dde666 2020-07-23 stsp } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
545 40dde666 2020-07-23 stsp (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
546 46f68b20 2019-10-19 stsp if (!id_match ||
547 40dde666 2020-07-23 stsp ((te1->mode & (S_IFLNK | S_IXUSR))) !=
548 40dde666 2020-07-23 stsp (te2->mode & (S_IFLNK | S_IXUSR))) {
549 31b4484f 2019-07-27 stsp if (diff_content)
550 56e0773d 2019-11-28 stsp return diff_modified_blob(&te1->id, &te2->id,
551 19a6a6b5 2022-07-01 thomas f1, f2, fd1, fd2, label1, label2,
552 19a6a6b5 2022-07-01 thomas te1->mode, te2->mode, repo, cb, cb_arg);
554 a0f32f33 2022-06-13 thomas return cb(cb_arg, NULL, NULL, NULL, NULL,
555 a0f32f33 2022-06-13 thomas &te1->id, &te2->id, label1, label2,
556 a0f32f33 2022-06-13 thomas te1->mode, te2->mode, repo);
560 19ae6da1 2018-11-05 stsp if (id_match)
561 f6861a81 2018-09-13 stsp return NULL;
563 56e0773d 2019-11-28 stsp return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
564 aaa13589 2019-06-01 stsp cb, cb_arg);
567 474b4f94 2017-11-30 stsp static const struct got_error *
568 56e0773d 2019-11-28 stsp diff_entry_new_old(struct got_tree_entry *te2,
569 dd2e2f52 2022-07-01 thomas struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
570 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
571 31b4484f 2019-07-27 stsp int diff_content)
573 f6861a81 2018-09-13 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
574 63c5ca5d 2019-08-24 stsp return NULL;
576 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te2))
577 f6861a81 2018-09-13 stsp return NULL;
579 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
580 dd2e2f52 2022-07-01 thomas return diff_added_tree(&te2->id, f1, f2, fd2, label2,
581 a0f32f33 2022-06-13 thomas repo, cb, cb_arg, diff_content);
583 31b4484f 2019-07-27 stsp if (diff_content)
584 dd2e2f52 2022-07-01 thomas return diff_added_blob(&te2->id, f1, f2, fd2,
585 19a6a6b5 2022-07-01 thomas label2, te2->mode, repo, cb, cb_arg);
587 a0f32f33 2022-06-13 thomas return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
588 a0f32f33 2022-06-13 thomas NULL, label2, 0, te2->mode, repo);
591 0208f208 2020-05-05 stsp const struct got_error *
592 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
593 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
594 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
595 a0f32f33 2022-06-13 thomas const char *label1, const char *label2,
596 0208f208 2020-05-05 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
598 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
599 0208f208 2020-05-05 stsp struct got_pathlist_head *paths = arg;
600 0208f208 2020-05-05 stsp struct got_diff_changed_path *change = NULL;
601 0208f208 2020-05-05 stsp char *path = NULL;
603 0208f208 2020-05-05 stsp path = strdup(label2 ? label2 : label1);
604 0208f208 2020-05-05 stsp if (path == NULL)
605 0208f208 2020-05-05 stsp return got_error_from_errno("malloc");
607 0208f208 2020-05-05 stsp change = malloc(sizeof(*change));
608 0208f208 2020-05-05 stsp if (change == NULL) {
609 0208f208 2020-05-05 stsp err = got_error_from_errno("malloc");
613 0208f208 2020-05-05 stsp change->status = GOT_STATUS_NO_CHANGE;
614 0208f208 2020-05-05 stsp if (id1 == NULL)
615 0208f208 2020-05-05 stsp change->status = GOT_STATUS_ADD;
616 0208f208 2020-05-05 stsp else if (id2 == NULL)
617 0208f208 2020-05-05 stsp change->status = GOT_STATUS_DELETE;
619 0208f208 2020-05-05 stsp if (got_object_id_cmp(id1, id2) != 0)
620 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODIFY;
621 0208f208 2020-05-05 stsp else if (mode1 != mode2)
622 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODE_CHANGE;
625 4c71f93b 2021-12-31 thomas err = got_pathlist_append(paths, path, change);
628 0208f208 2020-05-05 stsp free(path);
629 0208f208 2020-05-05 stsp free(change);
631 0208f208 2020-05-05 stsp return err;
634 474b4f94 2017-11-30 stsp const struct got_error *
635 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
636 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
637 19a6a6b5 2022-07-01 thomas const char *label1, const char *label2,
638 a0f32f33 2022-06-13 thomas struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
639 a0f32f33 2022-06-13 thomas int diff_content)
641 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
642 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
643 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
644 f6861a81 2018-09-13 stsp char *l1 = NULL, *l2 = NULL;
645 56e0773d 2019-11-28 stsp int tidx1 = 0, tidx2 = 0;
647 883f0469 2018-06-23 stsp if (tree1) {
648 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, 0);
649 60f50a58 2018-09-15 stsp if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
650 f6861a81 2018-09-13 stsp te1->name) == -1)
651 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
653 883f0469 2018-06-23 stsp if (tree2) {
654 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, 0);
655 60f50a58 2018-09-15 stsp if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
656 f6861a81 2018-09-13 stsp te2->name) == -1)
657 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
662 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
664 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree2,
665 1de5e065 2019-06-01 stsp te1->name);
669 f6861a81 2018-09-13 stsp if (te && asprintf(&l2, "%s%s%s", label2,
670 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te->name) == -1)
672 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
674 19a6a6b5 2022-07-01 thomas err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
675 19a6a6b5 2022-07-01 thomas l1, l2, repo, cb, cb_arg, diff_content);
681 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
683 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree1,
684 1de5e065 2019-06-01 stsp te2->name);
687 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
688 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te->name) == -1)
690 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
692 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
693 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te2->name) == -1)
695 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
697 dd2e2f52 2022-07-01 thomas err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
698 dd2e2f52 2022-07-01 thomas repo, cb, cb_arg, diff_content);
707 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, tidx1);
709 f6861a81 2018-09-13 stsp asprintf(&l1, "%s%s%s", label1,
710 f6861a81 2018-09-13 stsp label1[0] ? "/" : "", te1->name) == -1)
711 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
717 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, tidx2);
719 f6861a81 2018-09-13 stsp asprintf(&l2, "%s%s%s", label2,
720 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te2->name) == -1)
721 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
723 474b4f94 2017-11-30 stsp } while (te1 || te2);
725 11528a82 2018-05-19 stsp return err;
728 11528a82 2018-05-19 stsp const struct got_error *
729 fe621944 2020-11-10 stsp got_diff_objects_as_blobs(off_t **line_offsets, size_t *nlines,
730 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
731 19a6a6b5 2022-07-01 thomas struct got_object_id *id1, struct got_object_id *id2,
732 25ec7006 2022-07-01 thomas const char *label1, const char *label2,
733 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo, int diff_context,
734 64453f7e 2020-11-21 stsp int ignore_whitespace, int force_text_diff,
735 64453f7e 2020-11-21 stsp struct got_repository *repo, FILE *outfile)
737 11528a82 2018-05-19 stsp const struct got_error *err;
738 11528a82 2018-05-19 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
740 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
741 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
744 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
749 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
753 a0f32f33 2022-06-13 thomas err = got_diff_blob(line_offsets, nlines, blob1, blob2, f1, f2,
754 25ec7006 2022-07-01 thomas label1, label2, diff_algo, diff_context, ignore_whitespace,
755 25ec7006 2022-07-01 thomas force_text_diff, outfile);
757 cc8021af 2021-10-12 thomas if (blob1)
758 cc8021af 2021-10-12 thomas got_object_blob_close(blob1);
759 cc8021af 2021-10-12 thomas if (blob2)
760 cc8021af 2021-10-12 thomas got_object_blob_close(blob2);
761 cc8021af 2021-10-12 thomas return err;
764 cc8021af 2021-10-12 thomas static const struct got_error *
765 cc8021af 2021-10-12 thomas diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
766 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
767 a0f32f33 2022-06-13 thomas struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
769 cc8021af 2021-10-12 thomas const struct got_error *err = NULL;
770 cc8021af 2021-10-12 thomas struct got_pathlist_entry *pe;
771 cc8021af 2021-10-12 thomas struct got_object_id *id1 = NULL, *id2 = NULL;
772 cc8021af 2021-10-12 thomas struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
773 cc8021af 2021-10-12 thomas struct got_blob_object *blob1 = NULL, *blob2 = NULL;
775 cc8021af 2021-10-12 thomas TAILQ_FOREACH(pe, paths, entry) {
776 cc8021af 2021-10-12 thomas int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
777 cc8021af 2021-10-12 thomas mode_t mode1 = 0, mode2 = 0;
779 cc8021af 2021-10-12 thomas free(id1);
780 cc8021af 2021-10-12 thomas id1 = NULL;
781 cc8021af 2021-10-12 thomas free(id2);
782 cc8021af 2021-10-12 thomas id2 = NULL;
783 cc8021af 2021-10-12 thomas if (subtree1) {
784 cc8021af 2021-10-12 thomas got_object_tree_close(subtree1);
785 cc8021af 2021-10-12 thomas subtree1 = NULL;
787 cc8021af 2021-10-12 thomas if (subtree2) {
788 cc8021af 2021-10-12 thomas got_object_tree_close(subtree2);
789 cc8021af 2021-10-12 thomas subtree2 = NULL;
791 cc8021af 2021-10-12 thomas if (blob1) {
792 cc8021af 2021-10-12 thomas got_object_blob_close(blob1);
793 cc8021af 2021-10-12 thomas blob1 = NULL;
795 cc8021af 2021-10-12 thomas if (blob2) {
796 cc8021af 2021-10-12 thomas got_object_blob_close(blob2);
797 cc8021af 2021-10-12 thomas blob2 = NULL;
800 cc8021af 2021-10-12 thomas err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
801 cc8021af 2021-10-12 thomas pe->path);
802 cc8021af 2021-10-12 thomas if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
803 cc8021af 2021-10-12 thomas goto done;
804 cc8021af 2021-10-12 thomas err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
805 cc8021af 2021-10-12 thomas pe->path);
806 cc8021af 2021-10-12 thomas if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
807 cc8021af 2021-10-12 thomas goto done;
808 cc8021af 2021-10-12 thomas if (id1 == NULL && id2 == NULL) {
809 cc8021af 2021-10-12 thomas err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
810 cc8021af 2021-10-12 thomas goto done;
812 cc8021af 2021-10-12 thomas if (id1) {
813 cc8021af 2021-10-12 thomas err = got_object_get_type(&type1, repo, id1);
815 cc8021af 2021-10-12 thomas goto done;
817 cc8021af 2021-10-12 thomas if (id2) {
818 cc8021af 2021-10-12 thomas err = got_object_get_type(&type2, repo, id2);
820 cc8021af 2021-10-12 thomas goto done;
822 cc8021af 2021-10-12 thomas if (type1 == GOT_OBJ_TYPE_ANY &&
823 cc8021af 2021-10-12 thomas type2 == GOT_OBJ_TYPE_ANY) {
824 cc8021af 2021-10-12 thomas err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
825 cc8021af 2021-10-12 thomas goto done;
826 cc8021af 2021-10-12 thomas } else if (type1 != GOT_OBJ_TYPE_ANY &&
827 cc8021af 2021-10-12 thomas type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
828 cc8021af 2021-10-12 thomas err = got_error(GOT_ERR_OBJ_TYPE);
829 cc8021af 2021-10-12 thomas goto done;
832 cc8021af 2021-10-12 thomas if (type1 == GOT_OBJ_TYPE_BLOB ||
833 cc8021af 2021-10-12 thomas type2 == GOT_OBJ_TYPE_BLOB) {
834 cc8021af 2021-10-12 thomas if (id1) {
835 cc8021af 2021-10-12 thomas err = got_object_open_as_blob(&blob1, repo,
836 f4ae6ddb 2022-07-01 thomas id1, 8192, fd1);
838 cc8021af 2021-10-12 thomas goto done;
840 cc8021af 2021-10-12 thomas if (id2) {
841 cc8021af 2021-10-12 thomas err = got_object_open_as_blob(&blob2, repo,
842 f4ae6ddb 2022-07-01 thomas id2, 8192, fd2);
844 cc8021af 2021-10-12 thomas goto done;
846 a0f32f33 2022-06-13 thomas err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
847 cc8021af 2021-10-12 thomas id1 ? pe->path : "/dev/null",
848 cc8021af 2021-10-12 thomas id2 ? pe->path : "/dev/null",
849 cc8021af 2021-10-12 thomas mode1, mode2, repo);
851 cc8021af 2021-10-12 thomas goto done;
852 cc8021af 2021-10-12 thomas } else if (type1 == GOT_OBJ_TYPE_TREE ||
853 cc8021af 2021-10-12 thomas type2 == GOT_OBJ_TYPE_TREE) {
854 cc8021af 2021-10-12 thomas if (id1) {
855 cc8021af 2021-10-12 thomas err = got_object_open_as_tree(&subtree1, repo,
858 cc8021af 2021-10-12 thomas goto done;
860 cc8021af 2021-10-12 thomas if (id2) {
861 cc8021af 2021-10-12 thomas err = got_object_open_as_tree(&subtree2, repo,
864 cc8021af 2021-10-12 thomas goto done;
866 a0f32f33 2022-06-13 thomas err = got_diff_tree(subtree1, subtree2, f1, f2,
867 19a6a6b5 2022-07-01 thomas fd1, fd2,
868 cc8021af 2021-10-12 thomas id1 ? pe->path : "/dev/null",
869 cc8021af 2021-10-12 thomas id2 ? pe->path : "/dev/null",
870 cc8021af 2021-10-12 thomas repo, cb, cb_arg, 1);
872 cc8021af 2021-10-12 thomas goto done;
874 cc8021af 2021-10-12 thomas err = got_error(GOT_ERR_OBJ_TYPE);
875 cc8021af 2021-10-12 thomas goto done;
879 cc8021af 2021-10-12 thomas free(id1);
880 cc8021af 2021-10-12 thomas free(id2);
881 cc8021af 2021-10-12 thomas if (subtree1)
882 cc8021af 2021-10-12 thomas got_object_tree_close(subtree1);
883 cc8021af 2021-10-12 thomas if (subtree2)
884 cc8021af 2021-10-12 thomas got_object_tree_close(subtree2);
886 11528a82 2018-05-19 stsp got_object_blob_close(blob1);
888 11528a82 2018-05-19 stsp got_object_blob_close(blob2);
889 474b4f94 2017-11-30 stsp return err;
892 9b4458b4 2022-06-26 thomas static const struct got_error *
893 9b4458b4 2022-06-26 thomas show_object_id(off_t **line_offsets, size_t *nlines, const char *obj_typestr,
894 9b4458b4 2022-06-26 thomas int ch, const char *id_str, FILE *outfile)
896 9b4458b4 2022-06-26 thomas const struct got_error *err;
898 9b4458b4 2022-06-26 thomas off_t outoff = 0;
900 9b4458b4 2022-06-26 thomas n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
901 9b4458b4 2022-06-26 thomas if (line_offsets != NULL && *line_offsets != NULL) {
902 9b4458b4 2022-06-26 thomas if (*nlines == 0) {
903 9b4458b4 2022-06-26 thomas err = add_line_offset(line_offsets, nlines, 0);
905 9b4458b4 2022-06-26 thomas return err;
907 9b4458b4 2022-06-26 thomas outoff = (*line_offsets)[*nlines - 1];
909 9b4458b4 2022-06-26 thomas outoff += n;
910 9b4458b4 2022-06-26 thomas err = add_line_offset(line_offsets, nlines, outoff);
912 9b4458b4 2022-06-26 thomas return err;
915 9b4458b4 2022-06-26 thomas return NULL;
918 9b4458b4 2022-06-26 thomas static const struct got_error *
919 9b4458b4 2022-06-26 thomas diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
920 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
921 19a6a6b5 2022-07-01 thomas struct got_object_id *id1, struct got_object_id *id2,
922 0c6f49ba 2022-07-01 thomas struct got_pathlist_head *paths, const char *label1, const char *label2,
923 0c6f49ba 2022-07-01 thomas int diff_context, int ignore_whitespace, int force_text_diff,
924 25ec7006 2022-07-01 thomas struct got_repository *repo, FILE *outfile,
925 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo)
927 11528a82 2018-05-19 stsp const struct got_error *err;
928 11528a82 2018-05-19 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
929 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
930 fe621944 2020-11-10 stsp int want_lineoffsets = (line_offsets != NULL && *line_offsets != NULL);
932 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
933 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
936 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo, id1);
941 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo, id2);
946 25ec7006 2022-07-01 thomas arg.diff_algo = diff_algo;
947 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
948 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
949 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
950 aaa13589 2019-06-01 stsp arg.outfile = outfile;
951 fe621944 2020-11-10 stsp if (want_lineoffsets) {
952 fe621944 2020-11-10 stsp arg.line_offsets = *line_offsets;
953 fe621944 2020-11-10 stsp arg.nlines = *nlines;
955 fe621944 2020-11-10 stsp arg.line_offsets = NULL;
956 fe621944 2020-11-10 stsp arg.nlines = 0;
958 cc8021af 2021-10-12 thomas if (paths == NULL || TAILQ_EMPTY(paths)) {
959 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
960 19a6a6b5 2022-07-01 thomas label1, label2, repo,
961 19a6a6b5 2022-07-01 thomas got_diff_blob_output_unidiff, &arg, 1);
963 19a6a6b5 2022-07-01 thomas err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
964 cc8021af 2021-10-12 thomas got_diff_blob_output_unidiff, &arg);
966 fe621944 2020-11-10 stsp if (want_lineoffsets) {
967 fe621944 2020-11-10 stsp *line_offsets = arg.line_offsets; /* was likely re-allocated */
968 fe621944 2020-11-10 stsp *nlines = arg.nlines;
972 11528a82 2018-05-19 stsp got_object_tree_close(tree1);
974 11528a82 2018-05-19 stsp got_object_tree_close(tree2);
975 11528a82 2018-05-19 stsp return err;
978 11528a82 2018-05-19 stsp const struct got_error *
979 9b4458b4 2022-06-26 thomas got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
980 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
981 19a6a6b5 2022-07-01 thomas struct got_object_id *id1, struct got_object_id *id2,
982 0c6f49ba 2022-07-01 thomas struct got_pathlist_head *paths, const char *label1, const char *label2,
983 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
984 25ec7006 2022-07-01 thomas int force_text_diff, struct got_repository *repo, FILE *outfile)
986 9b4458b4 2022-06-26 thomas const struct got_error *err;
987 9b4458b4 2022-06-26 thomas char *idstr = NULL;
989 9b4458b4 2022-06-26 thomas if (id1 == NULL && id2 == NULL)
990 9b4458b4 2022-06-26 thomas return got_error(GOT_ERR_NO_OBJ);
992 9b4458b4 2022-06-26 thomas if (id1) {
993 9b4458b4 2022-06-26 thomas err = got_object_id_str(&idstr, id1);
995 9b4458b4 2022-06-26 thomas goto done;
996 9b4458b4 2022-06-26 thomas err = show_object_id(line_offsets, nlines, "tree", '-',
997 9b4458b4 2022-06-26 thomas idstr, outfile);
999 9b4458b4 2022-06-26 thomas goto done;
1000 9b4458b4 2022-06-26 thomas free(idstr);
1001 9b4458b4 2022-06-26 thomas idstr = NULL;
1002 9b4458b4 2022-06-26 thomas } else {
1003 9b4458b4 2022-06-26 thomas err = show_object_id(line_offsets, nlines, "tree", '-',
1004 9b4458b4 2022-06-26 thomas "/dev/null", outfile);
1005 9b4458b4 2022-06-26 thomas if (err)
1006 9b4458b4 2022-06-26 thomas goto done;
1009 9b4458b4 2022-06-26 thomas if (id2) {
1010 9b4458b4 2022-06-26 thomas err = got_object_id_str(&idstr, id2);
1011 9b4458b4 2022-06-26 thomas if (err)
1012 9b4458b4 2022-06-26 thomas goto done;
1013 9b4458b4 2022-06-26 thomas err = show_object_id(line_offsets, nlines, "tree", '+',
1014 9b4458b4 2022-06-26 thomas idstr, outfile);
1015 9b4458b4 2022-06-26 thomas if (err)
1016 9b4458b4 2022-06-26 thomas goto done;
1017 9b4458b4 2022-06-26 thomas free(idstr);
1018 9b4458b4 2022-06-26 thomas idstr = NULL;
1019 9b4458b4 2022-06-26 thomas } else {
1020 9b4458b4 2022-06-26 thomas err = show_object_id(line_offsets, nlines, "tree", '+',
1021 9b4458b4 2022-06-26 thomas "/dev/null", outfile);
1022 9b4458b4 2022-06-26 thomas if (err)
1023 9b4458b4 2022-06-26 thomas goto done;
1026 19a6a6b5 2022-07-01 thomas err = diff_objects_as_trees(line_offsets, nlines, f1, f2, fd1, fd2,
1027 19a6a6b5 2022-07-01 thomas id1, id2, paths, label1, label2, diff_context, ignore_whitespace,
1028 25ec7006 2022-07-01 thomas force_text_diff, repo, outfile, diff_algo);
1030 9b4458b4 2022-06-26 thomas free(idstr);
1031 9b4458b4 2022-06-26 thomas return err;
1034 9b4458b4 2022-06-26 thomas const struct got_error *
1035 fe621944 2020-11-10 stsp got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
1036 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
1037 19a6a6b5 2022-07-01 thomas struct got_object_id *id1, struct got_object_id *id2,
1038 25ec7006 2022-07-01 thomas struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1039 64453f7e 2020-11-21 stsp int diff_context, int ignore_whitespace, int force_text_diff,
1040 15a94983 2018-12-23 stsp struct got_repository *repo, FILE *outfile)
1042 11528a82 2018-05-19 stsp const struct got_error *err;
1043 11528a82 2018-05-19 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1044 9b4458b4 2022-06-26 thomas char *idstr = NULL;
1046 15a94983 2018-12-23 stsp if (id2 == NULL)
1047 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
1049 15a94983 2018-12-23 stsp if (id1) {
1050 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit1, repo, id1);
1052 cd0acaa7 2018-05-20 stsp goto done;
1053 9b4458b4 2022-06-26 thomas err = got_object_id_str(&idstr, id1);
1054 9b4458b4 2022-06-26 thomas if (err)
1055 9b4458b4 2022-06-26 thomas goto done;
1056 9b4458b4 2022-06-26 thomas err = show_object_id(line_offsets, nlines, "commit", '-',
1057 9b4458b4 2022-06-26 thomas idstr, outfile);
1058 9b4458b4 2022-06-26 thomas if (err)
1059 9b4458b4 2022-06-26 thomas goto done;
1060 9b4458b4 2022-06-26 thomas free(idstr);
1061 9b4458b4 2022-06-26 thomas idstr = NULL;
1062 9b4458b4 2022-06-26 thomas } else {
1063 9b4458b4 2022-06-26 thomas err = show_object_id(line_offsets, nlines, "commit", '-',
1064 9b4458b4 2022-06-26 thomas "/dev/null", outfile);
1065 9b4458b4 2022-06-26 thomas if (err)
1066 9b4458b4 2022-06-26 thomas goto done;
1069 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, repo, id2);
1071 9b697879 2018-05-20 stsp goto done;
1073 9b4458b4 2022-06-26 thomas err = got_object_id_str(&idstr, id2);
1074 9b4458b4 2022-06-26 thomas if (err)
1075 9b4458b4 2022-06-26 thomas goto done;
1076 9b4458b4 2022-06-26 thomas err = show_object_id(line_offsets, nlines, "commit", '+',
1077 9b4458b4 2022-06-26 thomas idstr, outfile);
1078 9b4458b4 2022-06-26 thomas if (err)
1079 9b4458b4 2022-06-26 thomas goto done;
1081 19a6a6b5 2022-07-01 thomas err = diff_objects_as_trees(line_offsets, nlines, f1, f2, fd1, fd2,
1082 15a94983 2018-12-23 stsp commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1083 cc8021af 2021-10-12 thomas got_object_commit_get_tree_id(commit2), paths, "", "",
1084 25ec7006 2022-07-01 thomas diff_context, ignore_whitespace, force_text_diff, repo, outfile,
1085 25ec7006 2022-07-01 thomas diff_algo);
1087 11528a82 2018-05-19 stsp if (commit1)
1088 11528a82 2018-05-19 stsp got_object_commit_close(commit1);
1089 11528a82 2018-05-19 stsp if (commit2)
1090 11528a82 2018-05-19 stsp got_object_commit_close(commit2);
1091 9b4458b4 2022-06-26 thomas free(idstr);
1092 11528a82 2018-05-19 stsp return err;
1095 dc424a06 2019-08-07 stsp const struct got_error *
1096 fe621944 2020-11-10 stsp got_diff_files(struct got_diffreg_result **resultp,
1097 dd2e2f52 2022-07-01 thomas FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1098 dd2e2f52 2022-07-01 thomas const char *label2, int diff_context, int ignore_whitespace,
1099 25ec7006 2022-07-01 thomas int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1101 dc424a06 2019-08-07 stsp const struct got_error *err = NULL;
1102 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
1104 fe621944 2020-11-10 stsp if (resultp)
1105 fe621944 2020-11-10 stsp *resultp = NULL;
1107 dc424a06 2019-08-07 stsp if (outfile) {
1108 dc424a06 2019-08-07 stsp fprintf(outfile, "file - %s\n",
1109 dd2e2f52 2022-07-01 thomas f1_exists ? label1 : "/dev/null");
1110 dc424a06 2019-08-07 stsp fprintf(outfile, "file + %s\n",
1111 dd2e2f52 2022-07-01 thomas f2_exists ? label2 : "/dev/null");
1114 25ec7006 2022-07-01 thomas err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1115 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
1117 fe621944 2020-11-10 stsp goto done;
1119 fe621944 2020-11-10 stsp if (outfile) {
1120 fe621944 2020-11-10 stsp err = got_diffreg_output(NULL, NULL, diffreg_result,
1121 dd2e2f52 2022-07-01 thomas f1_exists, f2_exists, label1, label2,
1122 1cb46f00 2020-11-21 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1124 dc424a06 2019-08-07 stsp goto done;
1128 fe621944 2020-11-10 stsp if (resultp && err == NULL)
1129 fe621944 2020-11-10 stsp *resultp = diffreg_result;
1130 fe621944 2020-11-10 stsp else if (diffreg_result) {
1131 fe621944 2020-11-10 stsp const struct got_error *free_err;
1132 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
1133 fe621944 2020-11-10 stsp if (free_err && err == NULL)
1134 fe621944 2020-11-10 stsp err = free_err;
1137 dc424a06 2019-08-07 stsp return err;