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 7d283eee 2017-11-29 stsp #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 7d283eee 2017-11-29 stsp #include <sha1.h>
25 7d283eee 2017-11-29 stsp #include <zlib.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 a558dd1b 2022-06-08 stsp #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 b72706c3 2022-06-01 stsp 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 64453f7e 2020-11-21 stsp int diff_context, int ignore_whitespace, int force_text_diff, FILE *outfile)
62 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
63 f78b0693 2017-11-29 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
64 f78b0693 2017-11-29 stsp char hex2[SHA1_DIGEST_STRING_LENGTH];
65 58e31a80 2022-06-27 op const char *idstr1 = NULL, *idstr2 = NULL;
66 be659d10 2020-11-18 stsp off_t size1, size2;
67 fe621944 2020-11-10 stsp struct got_diffreg_result *result;
68 fe621944 2020-11-10 stsp off_t outoff = 0;
71 fe621944 2020-11-10 stsp if (line_offsets && *line_offsets && *nlines > 0)
72 fe621944 2020-11-10 stsp outoff = (*line_offsets)[*nlines - 1];
73 9c659ea0 2020-11-22 stsp else if (line_offsets) {
74 9c659ea0 2020-11-22 stsp err = add_line_offset(line_offsets, nlines, 0);
79 fe621944 2020-11-10 stsp if (resultp)
80 fe621944 2020-11-10 stsp *resultp = NULL;
83 a558dd1b 2022-06-08 stsp err = got_opentemp_truncate(f1);
88 a558dd1b 2022-06-08 stsp err = got_opentemp_truncate(f2);
94 98abbc84 2017-11-30 stsp if (blob1) {
95 f934cf2c 2018-02-12 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
96 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
101 98abbc84 2017-11-30 stsp idstr1 = "/dev/null";
104 98abbc84 2017-11-30 stsp if (blob2) {
105 f934cf2c 2018-02-12 stsp idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
106 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
111 98abbc84 2017-11-30 stsp idstr2 = "/dev/null";
113 09de383e 2018-12-24 stsp if (outfile) {
114 46f68b20 2019-10-19 stsp char *modestr1 = NULL, *modestr2 = NULL;
115 40dde666 2020-07-23 stsp int modebits;
116 46f68b20 2019-10-19 stsp if (mode1 && mode1 != mode2) {
117 40dde666 2020-07-23 stsp if (S_ISLNK(mode1))
118 40dde666 2020-07-23 stsp modebits = S_IFLNK;
120 40dde666 2020-07-23 stsp modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
121 46f68b20 2019-10-19 stsp if (asprintf(&modestr1, " (mode %o)",
122 40dde666 2020-07-23 stsp mode1 & modebits) == -1) {
123 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
127 46f68b20 2019-10-19 stsp if (mode2 && mode1 != mode2) {
128 40dde666 2020-07-23 stsp if (S_ISLNK(mode2))
129 40dde666 2020-07-23 stsp modebits = S_IFLNK;
131 40dde666 2020-07-23 stsp modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
132 46f68b20 2019-10-19 stsp if (asprintf(&modestr2, " (mode %o)",
133 40dde666 2020-07-23 stsp mode2 & modebits) == -1) {
134 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
138 fe621944 2020-11-10 stsp n = fprintf(outfile, "blob - %s%s\n", idstr1,
139 46f68b20 2019-10-19 stsp modestr1 ? modestr1 : "");
142 fe621944 2020-11-10 stsp outoff += n;
143 fe621944 2020-11-10 stsp if (line_offsets) {
144 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
149 fe621944 2020-11-10 stsp n = fprintf(outfile, "blob + %s%s\n", idstr2,
150 46f68b20 2019-10-19 stsp modestr2 ? modestr2 : "");
153 fe621944 2020-11-10 stsp outoff += n;
154 fe621944 2020-11-10 stsp if (line_offsets) {
155 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
160 46f68b20 2019-10-19 stsp free(modestr1);
161 46f68b20 2019-10-19 stsp free(modestr2);
163 fe621944 2020-11-10 stsp err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
164 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
168 fe621944 2020-11-10 stsp if (outfile) {
169 1cb46f00 2020-11-21 stsp err = got_diffreg_output(line_offsets, nlines, result,
170 1cb46f00 2020-11-21 stsp blob1 != NULL, blob2 != NULL,
171 fe621944 2020-11-10 stsp label1 ? label1 : idstr1,
172 fe621944 2020-11-10 stsp label2 ? label2 : idstr2,
173 fe621944 2020-11-10 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
178 fe621944 2020-11-10 stsp if (resultp && err == NULL)
179 fe621944 2020-11-10 stsp *resultp = result;
181 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
182 fe621944 2020-11-10 stsp if (free_err && err == NULL)
183 fe621944 2020-11-10 stsp err = free_err;
186 7d283eee 2017-11-29 stsp return err;
189 aaa13589 2019-06-01 stsp const struct got_error *
190 aaa13589 2019-06-01 stsp got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
191 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
192 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
193 b72706c3 2022-06-01 stsp const char *label1, const char *label2, mode_t mode1, mode_t mode2,
194 b72706c3 2022-06-01 stsp struct got_repository *repo)
196 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg *a = arg;
198 fe621944 2020-11-10 stsp return diff_blobs(&a->line_offsets, &a->nlines, NULL,
199 b72706c3 2022-06-01 stsp blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
200 64453f7e 2020-11-21 stsp a->ignore_whitespace, a->force_text_diff, a->outfile);
203 404c43c4 2018-06-21 stsp const struct got_error *
204 fe621944 2020-11-10 stsp got_diff_blob(off_t **line_offsets, size_t *nlines,
205 fe621944 2020-11-10 stsp struct got_blob_object *blob1, struct got_blob_object *blob2,
206 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, const char *label1, const char *label2,
207 b72706c3 2022-06-01 stsp int diff_context, int ignore_whitespace, int force_text_diff,
208 b72706c3 2022-06-01 stsp FILE *outfile)
210 b72706c3 2022-06-01 stsp return diff_blobs(line_offsets, nlines, NULL, blob1, blob2, f1, f2,
211 64453f7e 2020-11-21 stsp label1, label2, 0, 0, diff_context, ignore_whitespace,
212 64453f7e 2020-11-21 stsp force_text_diff, outfile);
215 7f1f93af 2019-08-06 stsp static const struct got_error *
216 fe621944 2020-11-10 stsp diff_blob_file(struct got_diffreg_result **resultp,
217 b72706c3 2022-06-01 stsp struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
218 49d4a017 2022-06-30 stsp FILE *f2, int f2_exists, size_t size2, const char *label2, int diff_context,
219 b72706c3 2022-06-01 stsp int ignore_whitespace, int force_text_diff, FILE *outfile)
221 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
222 b72f483a 2019-02-05 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
223 58e31a80 2022-06-27 op const char *idstr1 = NULL;
224 fe621944 2020-11-10 stsp struct got_diffreg_result *result = NULL;
226 fe621944 2020-11-10 stsp if (resultp)
227 fe621944 2020-11-10 stsp *resultp = NULL;
230 b72f483a 2019-02-05 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
232 b72f483a 2019-02-05 stsp idstr1 = "/dev/null";
234 7f1f93af 2019-08-06 stsp if (outfile) {
235 4ce46740 2019-08-08 stsp fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
236 7f1f93af 2019-08-06 stsp fprintf(outfile, "file + %s\n",
237 49d4a017 2022-06-30 stsp f2_exists ? label2 : "/dev/null");
240 fe621944 2020-11-10 stsp err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
241 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
245 fe621944 2020-11-10 stsp if (outfile) {
246 1cb46f00 2020-11-21 stsp err = got_diffreg_output(NULL, NULL, result,
247 49d4a017 2022-06-30 stsp blob1 != NULL, f2_exists,
248 1cb46f00 2020-11-21 stsp label2, /* show local file's path, not a blob ID */
249 1cb46f00 2020-11-21 stsp label2, GOT_DIFF_OUTPUT_UNIDIFF,
250 1cb46f00 2020-11-21 stsp diff_context, outfile);
255 fe621944 2020-11-10 stsp if (resultp && err == NULL)
256 fe621944 2020-11-10 stsp *resultp = result;
257 fe621944 2020-11-10 stsp else if (result) {
258 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
259 fe621944 2020-11-10 stsp if (free_err && err == NULL)
260 fe621944 2020-11-10 stsp err = free_err;
263 b72f483a 2019-02-05 stsp return err;
266 7f1f93af 2019-08-06 stsp const struct got_error *
267 b72706c3 2022-06-01 stsp got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
268 49d4a017 2022-06-30 stsp const char *label1, FILE *f2, int f2_exists, size_t size2,
269 49d4a017 2022-06-30 stsp const char *label2, int diff_context, int ignore_whitespace,
270 49d4a017 2022-06-30 stsp int force_text_diff, FILE *outfile)
272 49d4a017 2022-06-30 stsp return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
273 49d4a017 2022-06-30 stsp size2, label2, diff_context, ignore_whitespace, force_text_diff,
277 474b4f94 2017-11-30 stsp static const struct got_error *
278 49d4a017 2022-06-30 stsp diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
279 f9d37699 2022-06-28 stsp const char *label, mode_t mode, struct got_repository *repo,
280 b72706c3 2022-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
282 4e22badc 2017-11-30 stsp const struct got_error *err;
283 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
284 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
286 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
288 4e22badc 2017-11-30 stsp return err;
290 49d4a017 2022-06-30 stsp err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
293 49d4a017 2022-06-30 stsp err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
294 b72706c3 2022-06-01 stsp NULL, label, 0, mode, repo);
296 2acfca77 2018-04-01 stsp got_object_close(obj);
298 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
299 2acfca77 2018-04-01 stsp return err;
302 474b4f94 2017-11-30 stsp static const struct got_error *
303 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
304 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
305 f9d37699 2022-06-28 stsp const char *label1, const char *label2,
306 b72706c3 2022-06-01 stsp mode_t mode1, mode_t mode2, struct got_repository *repo,
307 b72706c3 2022-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
309 6a213ccb 2017-11-30 stsp const struct got_error *err;
310 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
311 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
312 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
313 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
315 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
317 730a8aa0 2018-04-24 stsp return err;
319 15a94983 2018-12-23 stsp if (obj1->type != GOT_OBJ_TYPE_BLOB) {
320 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
324 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
327 15a94983 2018-12-23 stsp if (obj2->type != GOT_OBJ_TYPE_BLOB) {
328 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
332 eb81bc23 2022-06-28 tracey err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
336 eb81bc23 2022-06-28 tracey err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
340 b72706c3 2022-06-01 stsp err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
341 b72706c3 2022-06-01 stsp mode1, mode2, repo);
344 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
346 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
348 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
350 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
351 6a213ccb 2017-11-30 stsp return err;
354 474b4f94 2017-11-30 stsp static const struct got_error *
355 49d4a017 2022-06-30 stsp diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
356 49d4a017 2022-06-30 stsp FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
357 f9d37699 2022-06-28 stsp got_diff_blob_cb cb, void *cb_arg)
359 365fb436 2017-11-30 stsp const struct got_error *err;
360 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
361 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
363 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
365 365fb436 2017-11-30 stsp return err;
367 49d4a017 2022-06-30 stsp err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
370 49d4a017 2022-06-30 stsp err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
371 b72706c3 2022-06-01 stsp mode, 0, repo);
373 2acfca77 2018-04-01 stsp got_object_close(obj);
375 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
376 2acfca77 2018-04-01 stsp return err;
379 474b4f94 2017-11-30 stsp static const struct got_error *
380 49d4a017 2022-06-30 stsp diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
381 49d4a017 2022-06-30 stsp const char *label, struct got_repository *repo, got_diff_blob_cb cb,
382 49d4a017 2022-06-30 stsp void *cb_arg, int diff_content)
384 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
385 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
386 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
388 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
392 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
393 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
397 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
401 49d4a017 2022-06-30 stsp err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
402 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
405 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
406 9c70d4c3 2017-11-30 stsp if (treeobj)
407 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
408 9c70d4c3 2017-11-30 stsp return err;
411 474b4f94 2017-11-30 stsp static const struct got_error *
412 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
413 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
414 f9d37699 2022-06-28 stsp const char *label1, const char *label2,
415 b72706c3 2022-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
416 b72706c3 2022-06-01 stsp int diff_content)
418 f6861a81 2018-09-13 stsp const struct got_error *err;
419 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
420 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
421 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
422 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
424 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
428 15a94983 2018-12-23 stsp if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
429 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
433 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
437 15a94983 2018-12-23 stsp if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
438 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
442 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
446 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
450 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
451 f9d37699 2022-06-28 stsp label1, label2, repo, cb, cb_arg, diff_content);
455 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
457 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
458 789689b5 2017-11-30 stsp if (treeobj1)
459 789689b5 2017-11-30 stsp got_object_close(treeobj1);
460 789689b5 2017-11-30 stsp if (treeobj2)
461 789689b5 2017-11-30 stsp got_object_close(treeobj2);
462 789689b5 2017-11-30 stsp return err;
465 474b4f94 2017-11-30 stsp static const struct got_error *
466 49d4a017 2022-06-30 stsp diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
467 49d4a017 2022-06-30 stsp FILE *f2, const char *label, struct got_repository *repo,
468 f9d37699 2022-06-28 stsp got_diff_blob_cb cb, void *cb_arg, int diff_content)
470 f6861a81 2018-09-13 stsp const struct got_error *err;
471 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
472 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
474 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
478 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
479 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
483 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
487 49d4a017 2022-06-30 stsp err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
488 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
491 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
492 2c56f2ce 2017-11-30 stsp if (treeobj)
493 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
494 2c56f2ce 2017-11-30 stsp return err;
497 474b4f94 2017-11-30 stsp static const struct got_error *
498 74671950 2018-02-11 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
499 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
500 aaa13589 2019-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
502 013404a9 2017-11-30 stsp /* XXX TODO */
503 474b4f94 2017-11-30 stsp return NULL;
506 474b4f94 2017-11-30 stsp static const struct got_error *
507 b72706c3 2022-06-01 stsp diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
508 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
509 f9d37699 2022-06-28 stsp const char *label1, const char *label2,
510 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
511 31b4484f 2019-07-27 stsp int diff_content)
513 f6861a81 2018-09-13 stsp const struct got_error *err = NULL;
514 19ae6da1 2018-11-05 stsp int id_match;
516 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te1))
517 63c5ca5d 2019-08-24 stsp return NULL;
519 474b4f94 2017-11-30 stsp if (te2 == NULL) {
520 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
521 49d4a017 2022-06-30 stsp err = diff_deleted_tree(&te1->id, f1, fd1, f2,
522 49d4a017 2022-06-30 stsp label1, repo, cb, cb_arg, diff_content);
524 31b4484f 2019-07-27 stsp if (diff_content)
525 f9d37699 2022-06-28 stsp err = diff_deleted_blob(&te1->id, f1, fd1,
526 49d4a017 2022-06-30 stsp f2, label1, te1->mode, repo, cb, cb_arg);
528 b72706c3 2022-06-01 stsp err = cb(cb_arg, NULL, NULL, NULL, NULL,
529 b72706c3 2022-06-01 stsp &te1->id, NULL, label1, NULL,
530 b72706c3 2022-06-01 stsp te1->mode, 0, repo);
532 f6861a81 2018-09-13 stsp return err;
533 63c5ca5d 2019-08-24 stsp } else if (got_object_tree_entry_is_submodule(te2))
534 63c5ca5d 2019-08-24 stsp return NULL;
536 56e0773d 2019-11-28 stsp id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
537 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
538 19ae6da1 2018-11-05 stsp if (!id_match)
539 b72706c3 2022-06-01 stsp return diff_modified_tree(&te1->id, &te2->id, f1, f2,
540 f9d37699 2022-06-28 stsp fd1, fd2, label1, label2, repo, cb, cb_arg,
541 f9d37699 2022-06-28 stsp diff_content);
542 40dde666 2020-07-23 stsp } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
543 40dde666 2020-07-23 stsp (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
544 46f68b20 2019-10-19 stsp if (!id_match ||
545 40dde666 2020-07-23 stsp ((te1->mode & (S_IFLNK | S_IXUSR))) !=
546 40dde666 2020-07-23 stsp (te2->mode & (S_IFLNK | S_IXUSR))) {
547 31b4484f 2019-07-27 stsp if (diff_content)
548 56e0773d 2019-11-28 stsp return diff_modified_blob(&te1->id, &te2->id,
549 f9d37699 2022-06-28 stsp f1, f2, fd1, fd2, label1, label2,
550 f9d37699 2022-06-28 stsp te1->mode, te2->mode, repo, cb, cb_arg);
552 b72706c3 2022-06-01 stsp return cb(cb_arg, NULL, NULL, NULL, NULL,
553 b72706c3 2022-06-01 stsp &te1->id, &te2->id, label1, label2,
554 b72706c3 2022-06-01 stsp te1->mode, te2->mode, repo);
558 19ae6da1 2018-11-05 stsp if (id_match)
559 f6861a81 2018-09-13 stsp return NULL;
561 56e0773d 2019-11-28 stsp return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
562 aaa13589 2019-06-01 stsp cb, cb_arg);
565 474b4f94 2017-11-30 stsp static const struct got_error *
566 56e0773d 2019-11-28 stsp diff_entry_new_old(struct got_tree_entry *te2,
567 49d4a017 2022-06-30 stsp struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
568 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
569 31b4484f 2019-07-27 stsp int diff_content)
571 f6861a81 2018-09-13 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
572 63c5ca5d 2019-08-24 stsp return NULL;
574 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te2))
575 f6861a81 2018-09-13 stsp return NULL;
577 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
578 49d4a017 2022-06-30 stsp return diff_added_tree(&te2->id, f1, f2, fd2, label2,
579 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
581 31b4484f 2019-07-27 stsp if (diff_content)
582 49d4a017 2022-06-30 stsp return diff_added_blob(&te2->id, f1, f2, fd2,
583 f9d37699 2022-06-28 stsp label2, te2->mode, repo, cb, cb_arg);
585 b72706c3 2022-06-01 stsp return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
586 b72706c3 2022-06-01 stsp NULL, label2, 0, te2->mode, repo);
589 0208f208 2020-05-05 stsp const struct got_error *
590 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
591 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
592 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
593 b72706c3 2022-06-01 stsp const char *label1, const char *label2,
594 0208f208 2020-05-05 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
596 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
597 0208f208 2020-05-05 stsp struct got_pathlist_head *paths = arg;
598 0208f208 2020-05-05 stsp struct got_diff_changed_path *change = NULL;
599 0208f208 2020-05-05 stsp char *path = NULL;
601 0208f208 2020-05-05 stsp path = strdup(label2 ? label2 : label1);
602 0208f208 2020-05-05 stsp if (path == NULL)
603 0208f208 2020-05-05 stsp return got_error_from_errno("malloc");
605 0208f208 2020-05-05 stsp change = malloc(sizeof(*change));
606 0208f208 2020-05-05 stsp if (change == NULL) {
607 0208f208 2020-05-05 stsp err = got_error_from_errno("malloc");
611 0208f208 2020-05-05 stsp change->status = GOT_STATUS_NO_CHANGE;
612 0208f208 2020-05-05 stsp if (id1 == NULL)
613 0208f208 2020-05-05 stsp change->status = GOT_STATUS_ADD;
614 0208f208 2020-05-05 stsp else if (id2 == NULL)
615 0208f208 2020-05-05 stsp change->status = GOT_STATUS_DELETE;
617 0208f208 2020-05-05 stsp if (got_object_id_cmp(id1, id2) != 0)
618 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODIFY;
619 0208f208 2020-05-05 stsp else if (mode1 != mode2)
620 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODE_CHANGE;
623 46ea77db 2021-12-20 stsp err = got_pathlist_append(paths, path, change);
626 0208f208 2020-05-05 stsp free(path);
627 0208f208 2020-05-05 stsp free(change);
629 0208f208 2020-05-05 stsp return err;
632 474b4f94 2017-11-30 stsp const struct got_error *
633 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
634 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
635 f9d37699 2022-06-28 stsp const char *label1, const char *label2,
636 b72706c3 2022-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
637 b72706c3 2022-06-01 stsp int diff_content)
639 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
640 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
641 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
642 f6861a81 2018-09-13 stsp char *l1 = NULL, *l2 = NULL;
643 56e0773d 2019-11-28 stsp int tidx1 = 0, tidx2 = 0;
645 883f0469 2018-06-23 stsp if (tree1) {
646 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, 0);
647 60f50a58 2018-09-15 stsp if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
648 f6861a81 2018-09-13 stsp te1->name) == -1)
649 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
651 883f0469 2018-06-23 stsp if (tree2) {
652 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, 0);
653 60f50a58 2018-09-15 stsp if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
654 f6861a81 2018-09-13 stsp te2->name) == -1)
655 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
660 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
662 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree2,
663 1de5e065 2019-06-01 stsp te1->name);
667 f6861a81 2018-09-13 stsp if (te && asprintf(&l2, "%s%s%s", label2,
668 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te->name) == -1)
670 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
672 f9d37699 2022-06-28 stsp err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
673 f9d37699 2022-06-28 stsp l1, l2, repo, cb, cb_arg, diff_content);
679 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
681 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree1,
682 1de5e065 2019-06-01 stsp te2->name);
685 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
686 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te->name) == -1)
688 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
690 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
691 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te2->name) == -1)
693 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
695 49d4a017 2022-06-30 stsp err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
696 49d4a017 2022-06-30 stsp repo, cb, cb_arg, diff_content);
705 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, tidx1);
707 f6861a81 2018-09-13 stsp asprintf(&l1, "%s%s%s", label1,
708 f6861a81 2018-09-13 stsp label1[0] ? "/" : "", te1->name) == -1)
709 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
715 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, tidx2);
717 f6861a81 2018-09-13 stsp asprintf(&l2, "%s%s%s", label2,
718 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te2->name) == -1)
719 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
721 474b4f94 2017-11-30 stsp } while (te1 || te2);
723 11528a82 2018-05-19 stsp return err;
726 11528a82 2018-05-19 stsp const struct got_error *
727 fe621944 2020-11-10 stsp got_diff_objects_as_blobs(off_t **line_offsets, size_t *nlines,
728 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
729 f9d37699 2022-06-28 stsp struct got_object_id *id1, struct got_object_id *id2,
730 54156555 2018-12-24 stsp const char *label1, const char *label2, int diff_context,
731 64453f7e 2020-11-21 stsp int ignore_whitespace, int force_text_diff,
732 64453f7e 2020-11-21 stsp struct got_repository *repo, FILE *outfile)
734 11528a82 2018-05-19 stsp const struct got_error *err;
735 11528a82 2018-05-19 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
737 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
738 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
741 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
746 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
750 b72706c3 2022-06-01 stsp err = got_diff_blob(line_offsets, nlines, blob1, blob2, f1, f2,
751 64453f7e 2020-11-21 stsp label1, label2, diff_context, ignore_whitespace, force_text_diff,
755 67b631c9 2021-10-10 stsp got_object_blob_close(blob1);
757 67b631c9 2021-10-10 stsp got_object_blob_close(blob2);
758 67b631c9 2021-10-10 stsp return err;
761 67b631c9 2021-10-10 stsp static const struct got_error *
762 67b631c9 2021-10-10 stsp diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
763 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
764 b72706c3 2022-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
766 67b631c9 2021-10-10 stsp const struct got_error *err = NULL;
767 67b631c9 2021-10-10 stsp struct got_pathlist_entry *pe;
768 67b631c9 2021-10-10 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
769 67b631c9 2021-10-10 stsp struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
770 67b631c9 2021-10-10 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
772 67b631c9 2021-10-10 stsp TAILQ_FOREACH(pe, paths, entry) {
773 67b631c9 2021-10-10 stsp int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
774 67b631c9 2021-10-10 stsp mode_t mode1 = 0, mode2 = 0;
777 67b631c9 2021-10-10 stsp id1 = NULL;
779 67b631c9 2021-10-10 stsp id2 = NULL;
780 67b631c9 2021-10-10 stsp if (subtree1) {
781 67b631c9 2021-10-10 stsp got_object_tree_close(subtree1);
782 67b631c9 2021-10-10 stsp subtree1 = NULL;
784 67b631c9 2021-10-10 stsp if (subtree2) {
785 67b631c9 2021-10-10 stsp got_object_tree_close(subtree2);
786 67b631c9 2021-10-10 stsp subtree2 = NULL;
788 67b631c9 2021-10-10 stsp if (blob1) {
789 67b631c9 2021-10-10 stsp got_object_blob_close(blob1);
790 67b631c9 2021-10-10 stsp blob1 = NULL;
792 67b631c9 2021-10-10 stsp if (blob2) {
793 67b631c9 2021-10-10 stsp got_object_blob_close(blob2);
794 67b631c9 2021-10-10 stsp blob2 = NULL;
797 67b631c9 2021-10-10 stsp err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
799 67b631c9 2021-10-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
801 67b631c9 2021-10-10 stsp err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
803 67b631c9 2021-10-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
805 67b631c9 2021-10-10 stsp if (id1 == NULL && id2 == NULL) {
806 67b631c9 2021-10-10 stsp err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
810 67b631c9 2021-10-10 stsp err = got_object_get_type(&type1, repo, id1);
815 67b631c9 2021-10-10 stsp err = got_object_get_type(&type2, repo, id2);
819 67b631c9 2021-10-10 stsp if (type1 == GOT_OBJ_TYPE_ANY &&
820 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_ANY) {
821 67b631c9 2021-10-10 stsp err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
823 67b631c9 2021-10-10 stsp } else if (type1 != GOT_OBJ_TYPE_ANY &&
824 67b631c9 2021-10-10 stsp type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
825 67b631c9 2021-10-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
829 67b631c9 2021-10-10 stsp if (type1 == GOT_OBJ_TYPE_BLOB ||
830 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_BLOB) {
832 67b631c9 2021-10-10 stsp err = got_object_open_as_blob(&blob1, repo,
833 eb81bc23 2022-06-28 tracey id1, 8192, fd1);
838 67b631c9 2021-10-10 stsp err = got_object_open_as_blob(&blob2, repo,
839 eb81bc23 2022-06-28 tracey id2, 8192, fd2);
843 b72706c3 2022-06-01 stsp err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
844 67b631c9 2021-10-10 stsp id1 ? pe->path : "/dev/null",
845 67b631c9 2021-10-10 stsp id2 ? pe->path : "/dev/null",
846 67b631c9 2021-10-10 stsp mode1, mode2, repo);
849 67b631c9 2021-10-10 stsp } else if (type1 == GOT_OBJ_TYPE_TREE ||
850 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_TREE) {
852 67b631c9 2021-10-10 stsp err = got_object_open_as_tree(&subtree1, repo,
858 67b631c9 2021-10-10 stsp err = got_object_open_as_tree(&subtree2, repo,
863 b72706c3 2022-06-01 stsp err = got_diff_tree(subtree1, subtree2, f1, f2,
865 67b631c9 2021-10-10 stsp id1 ? pe->path : "/dev/null",
866 67b631c9 2021-10-10 stsp id2 ? pe->path : "/dev/null",
867 67b631c9 2021-10-10 stsp repo, cb, cb_arg, 1);
871 67b631c9 2021-10-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
878 67b631c9 2021-10-10 stsp if (subtree1)
879 67b631c9 2021-10-10 stsp got_object_tree_close(subtree1);
880 67b631c9 2021-10-10 stsp if (subtree2)
881 67b631c9 2021-10-10 stsp got_object_tree_close(subtree2);
883 11528a82 2018-05-19 stsp got_object_blob_close(blob1);
885 11528a82 2018-05-19 stsp got_object_blob_close(blob2);
886 474b4f94 2017-11-30 stsp return err;
889 8469d821 2022-06-25 stsp static const struct got_error *
890 8469d821 2022-06-25 stsp show_object_id(off_t **line_offsets, size_t *nlines, const char *obj_typestr,
891 8469d821 2022-06-25 stsp int ch, const char *id_str, FILE *outfile)
893 8469d821 2022-06-25 stsp const struct got_error *err;
895 8469d821 2022-06-25 stsp off_t outoff = 0;
897 8469d821 2022-06-25 stsp n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
898 8469d821 2022-06-25 stsp if (line_offsets != NULL && *line_offsets != NULL) {
899 8469d821 2022-06-25 stsp if (*nlines == 0) {
900 8469d821 2022-06-25 stsp err = add_line_offset(line_offsets, nlines, 0);
902 8469d821 2022-06-25 stsp return err;
904 8469d821 2022-06-25 stsp outoff = (*line_offsets)[*nlines - 1];
906 8469d821 2022-06-25 stsp outoff += n;
907 8469d821 2022-06-25 stsp err = add_line_offset(line_offsets, nlines, outoff);
909 8469d821 2022-06-25 stsp return err;
912 8469d821 2022-06-25 stsp return NULL;
915 8469d821 2022-06-25 stsp static const struct got_error *
916 8469d821 2022-06-25 stsp diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
917 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
918 f9d37699 2022-06-28 stsp struct got_object_id *id1, struct got_object_id *id2,
919 58e31a80 2022-06-27 op struct got_pathlist_head *paths, const char *label1, const char *label2,
920 58e31a80 2022-06-27 op int diff_context, int ignore_whitespace, int force_text_diff,
921 58e31a80 2022-06-27 op struct got_repository *repo, FILE *outfile)
923 11528a82 2018-05-19 stsp const struct got_error *err;
924 11528a82 2018-05-19 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
925 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
926 fe621944 2020-11-10 stsp int want_lineoffsets = (line_offsets != NULL && *line_offsets != NULL);
928 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
929 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
932 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo, id1);
937 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo, id2);
942 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
943 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
944 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
945 aaa13589 2019-06-01 stsp arg.outfile = outfile;
946 fe621944 2020-11-10 stsp if (want_lineoffsets) {
947 fe621944 2020-11-10 stsp arg.line_offsets = *line_offsets;
948 fe621944 2020-11-10 stsp arg.nlines = *nlines;
950 fe621944 2020-11-10 stsp arg.line_offsets = NULL;
951 fe621944 2020-11-10 stsp arg.nlines = 0;
953 67b631c9 2021-10-10 stsp if (paths == NULL || TAILQ_EMPTY(paths)) {
954 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
955 f9d37699 2022-06-28 stsp label1, label2, repo,
956 f9d37699 2022-06-28 stsp got_diff_blob_output_unidiff, &arg, 1);
958 f9d37699 2022-06-28 stsp err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
959 67b631c9 2021-10-10 stsp got_diff_blob_output_unidiff, &arg);
961 fe621944 2020-11-10 stsp if (want_lineoffsets) {
962 fe621944 2020-11-10 stsp *line_offsets = arg.line_offsets; /* was likely re-allocated */
963 fe621944 2020-11-10 stsp *nlines = arg.nlines;
967 11528a82 2018-05-19 stsp got_object_tree_close(tree1);
969 11528a82 2018-05-19 stsp got_object_tree_close(tree2);
970 11528a82 2018-05-19 stsp return err;
973 11528a82 2018-05-19 stsp const struct got_error *
974 8469d821 2022-06-25 stsp got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
975 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
976 f9d37699 2022-06-28 stsp struct got_object_id *id1, struct got_object_id *id2,
977 58e31a80 2022-06-27 op struct got_pathlist_head *paths, const char *label1, const char *label2,
978 58e31a80 2022-06-27 op int diff_context, int ignore_whitespace, int force_text_diff,
979 58e31a80 2022-06-27 op struct got_repository *repo, FILE *outfile)
981 8469d821 2022-06-25 stsp const struct got_error *err;
982 8469d821 2022-06-25 stsp char *idstr = NULL;
984 8469d821 2022-06-25 stsp if (id1 == NULL && id2 == NULL)
985 8469d821 2022-06-25 stsp return got_error(GOT_ERR_NO_OBJ);
988 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id1);
991 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "tree", '-',
992 8469d821 2022-06-25 stsp idstr, outfile);
995 8469d821 2022-06-25 stsp free(idstr);
996 8469d821 2022-06-25 stsp idstr = NULL;
998 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "tree", '-',
999 8469d821 2022-06-25 stsp "/dev/null", outfile);
1001 8469d821 2022-06-25 stsp goto done;
1004 8469d821 2022-06-25 stsp if (id2) {
1005 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id2);
1007 8469d821 2022-06-25 stsp goto done;
1008 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "tree", '+',
1009 8469d821 2022-06-25 stsp idstr, outfile);
1011 8469d821 2022-06-25 stsp goto done;
1012 8469d821 2022-06-25 stsp free(idstr);
1013 8469d821 2022-06-25 stsp idstr = NULL;
1015 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "tree", '+',
1016 8469d821 2022-06-25 stsp "/dev/null", outfile);
1018 8469d821 2022-06-25 stsp goto done;
1021 f9d37699 2022-06-28 stsp err = diff_objects_as_trees(line_offsets, nlines, f1, f2, fd1, fd2,
1022 f9d37699 2022-06-28 stsp id1, id2, paths, label1, label2, diff_context, ignore_whitespace,
1023 8469d821 2022-06-25 stsp force_text_diff, repo, outfile);
1025 8469d821 2022-06-25 stsp free(idstr);
1026 8469d821 2022-06-25 stsp return err;
1029 8469d821 2022-06-25 stsp const struct got_error *
1030 fe621944 2020-11-10 stsp got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
1031 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
1032 f9d37699 2022-06-28 stsp struct got_object_id *id1, struct got_object_id *id2,
1033 67b631c9 2021-10-10 stsp struct got_pathlist_head *paths,
1034 64453f7e 2020-11-21 stsp int diff_context, int ignore_whitespace, int force_text_diff,
1035 15a94983 2018-12-23 stsp struct got_repository *repo, FILE *outfile)
1037 11528a82 2018-05-19 stsp const struct got_error *err;
1038 11528a82 2018-05-19 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1039 8469d821 2022-06-25 stsp char *idstr = NULL;
1041 15a94983 2018-12-23 stsp if (id2 == NULL)
1042 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
1044 15a94983 2018-12-23 stsp if (id1) {
1045 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit1, repo, id1);
1047 cd0acaa7 2018-05-20 stsp goto done;
1048 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id1);
1050 8469d821 2022-06-25 stsp goto done;
1051 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "commit", '-',
1052 8469d821 2022-06-25 stsp idstr, outfile);
1054 8469d821 2022-06-25 stsp goto done;
1055 8469d821 2022-06-25 stsp free(idstr);
1056 8469d821 2022-06-25 stsp idstr = NULL;
1058 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "commit", '-',
1059 8469d821 2022-06-25 stsp "/dev/null", outfile);
1061 8469d821 2022-06-25 stsp goto done;
1064 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, repo, id2);
1066 9b697879 2018-05-20 stsp goto done;
1068 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id2);
1070 8469d821 2022-06-25 stsp goto done;
1071 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "commit", '+',
1072 8469d821 2022-06-25 stsp idstr, outfile);
1074 8469d821 2022-06-25 stsp goto done;
1076 f9d37699 2022-06-28 stsp err = diff_objects_as_trees(line_offsets, nlines, f1, f2, fd1, fd2,
1077 15a94983 2018-12-23 stsp commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1078 67b631c9 2021-10-10 stsp got_object_commit_get_tree_id(commit2), paths, "", "",
1079 67b631c9 2021-10-10 stsp diff_context, ignore_whitespace, force_text_diff, repo, outfile);
1081 11528a82 2018-05-19 stsp if (commit1)
1082 11528a82 2018-05-19 stsp got_object_commit_close(commit1);
1083 11528a82 2018-05-19 stsp if (commit2)
1084 11528a82 2018-05-19 stsp got_object_commit_close(commit2);
1085 8469d821 2022-06-25 stsp free(idstr);
1086 11528a82 2018-05-19 stsp return err;
1089 dc424a06 2019-08-07 stsp const struct got_error *
1090 fe621944 2020-11-10 stsp got_diff_files(struct got_diffreg_result **resultp,
1091 49d4a017 2022-06-30 stsp FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1092 49d4a017 2022-06-30 stsp const char *label2, int diff_context, int ignore_whitespace,
1093 49d4a017 2022-06-30 stsp int force_text_diff, FILE *outfile)
1095 dc424a06 2019-08-07 stsp const struct got_error *err = NULL;
1096 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
1098 fe621944 2020-11-10 stsp if (resultp)
1099 fe621944 2020-11-10 stsp *resultp = NULL;
1101 dc424a06 2019-08-07 stsp if (outfile) {
1102 dc424a06 2019-08-07 stsp fprintf(outfile, "file - %s\n",
1103 49d4a017 2022-06-30 stsp f1_exists ? label1 : "/dev/null");
1104 dc424a06 2019-08-07 stsp fprintf(outfile, "file + %s\n",
1105 49d4a017 2022-06-30 stsp f2_exists ? label2 : "/dev/null");
1108 fe621944 2020-11-10 stsp err = got_diffreg(&diffreg_result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
1109 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
1111 fe621944 2020-11-10 stsp goto done;
1113 fe621944 2020-11-10 stsp if (outfile) {
1114 fe621944 2020-11-10 stsp err = got_diffreg_output(NULL, NULL, diffreg_result,
1115 49d4a017 2022-06-30 stsp f1_exists, f2_exists, label1, label2,
1116 1cb46f00 2020-11-21 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1118 dc424a06 2019-08-07 stsp goto done;
1122 fe621944 2020-11-10 stsp if (resultp && err == NULL)
1123 fe621944 2020-11-10 stsp *resultp = diffreg_result;
1124 fe621944 2020-11-10 stsp else if (diffreg_result) {
1125 fe621944 2020-11-10 stsp const struct got_error *free_err;
1126 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
1127 fe621944 2020-11-10 stsp if (free_err && err == NULL)
1128 fe621944 2020-11-10 stsp err = free_err;
1131 dc424a06 2019-08-07 stsp return err;