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 4b752015 2022-06-30 stsp int diff_context, int ignore_whitespace, int force_text_diff, FILE *outfile,
61 4b752015 2022-06-30 stsp 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 58e31a80 2022-06-27 op 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 a558dd1b 2022-06-08 stsp err = got_opentemp_truncate(f1);
89 a558dd1b 2022-06-08 stsp err = got_opentemp_truncate(f2);
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 4b752015 2022-06-30 stsp err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
165 4b752015 2022-06-30 stsp 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 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
193 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
194 b72706c3 2022-06-01 stsp const char *label1, const char *label2, mode_t mode1, mode_t mode2,
195 b72706c3 2022-06-01 stsp 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 b72706c3 2022-06-01 stsp blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
201 4b752015 2022-06-30 stsp 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 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, const char *label1, const char *label2,
208 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo, int diff_context,
209 4b752015 2022-06-30 stsp int ignore_whitespace, int force_text_diff, FILE *outfile)
211 b72706c3 2022-06-01 stsp 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 4b752015 2022-06-30 stsp 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 b72706c3 2022-06-01 stsp struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
219 4b752015 2022-06-30 stsp FILE *f2, int f2_exists, size_t size2, const char *label2,
220 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
221 4b752015 2022-06-30 stsp 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 58e31a80 2022-06-27 op 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;
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 49d4a017 2022-06-30 stsp f2_exists ? label2 : "/dev/null");
242 4b752015 2022-06-30 stsp err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
243 4b752015 2022-06-30 stsp 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 49d4a017 2022-06-30 stsp 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 b72706c3 2022-06-01 stsp got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
270 49d4a017 2022-06-30 stsp const char *label1, FILE *f2, int f2_exists, size_t size2,
271 4b752015 2022-06-30 stsp const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
272 4b752015 2022-06-30 stsp int ignore_whitespace, int force_text_diff, FILE *outfile)
274 49d4a017 2022-06-30 stsp return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
275 4b752015 2022-06-30 stsp size2, label2, diff_algo, diff_context, ignore_whitespace,
276 4b752015 2022-06-30 stsp force_text_diff, outfile );
279 474b4f94 2017-11-30 stsp static const struct got_error *
280 49d4a017 2022-06-30 stsp diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
281 f9d37699 2022-06-28 stsp const char *label, mode_t mode, struct got_repository *repo,
282 b72706c3 2022-06-01 stsp 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 49d4a017 2022-06-30 stsp err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
295 49d4a017 2022-06-30 stsp err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
296 b72706c3 2022-06-01 stsp 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 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
307 f9d37699 2022-06-28 stsp const char *label1, const char *label2,
308 b72706c3 2022-06-01 stsp mode_t mode1, mode_t mode2, struct got_repository *repo,
309 b72706c3 2022-06-01 stsp 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 eb81bc23 2022-06-28 tracey err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
338 eb81bc23 2022-06-28 tracey err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
342 b72706c3 2022-06-01 stsp err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
343 b72706c3 2022-06-01 stsp 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 49d4a017 2022-06-30 stsp diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
358 49d4a017 2022-06-30 stsp FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
359 f9d37699 2022-06-28 stsp 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 49d4a017 2022-06-30 stsp err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
372 49d4a017 2022-06-30 stsp err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
373 b72706c3 2022-06-01 stsp 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 49d4a017 2022-06-30 stsp diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
383 49d4a017 2022-06-30 stsp const char *label, struct got_repository *repo, got_diff_blob_cb cb,
384 49d4a017 2022-06-30 stsp 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 49d4a017 2022-06-30 stsp err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
404 b72706c3 2022-06-01 stsp 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 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
416 f9d37699 2022-06-28 stsp const char *label1, const char *label2,
417 b72706c3 2022-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
418 b72706c3 2022-06-01 stsp 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 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
453 f9d37699 2022-06-28 stsp 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 49d4a017 2022-06-30 stsp diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
469 49d4a017 2022-06-30 stsp FILE *f2, const char *label, struct got_repository *repo,
470 f9d37699 2022-06-28 stsp 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 49d4a017 2022-06-30 stsp err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
490 b72706c3 2022-06-01 stsp 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 b72706c3 2022-06-01 stsp diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
510 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
511 f9d37699 2022-06-28 stsp 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 49d4a017 2022-06-30 stsp err = diff_deleted_tree(&te1->id, f1, fd1, f2,
524 49d4a017 2022-06-30 stsp label1, repo, cb, cb_arg, diff_content);
526 31b4484f 2019-07-27 stsp if (diff_content)
527 f9d37699 2022-06-28 stsp err = diff_deleted_blob(&te1->id, f1, fd1,
528 49d4a017 2022-06-30 stsp f2, label1, te1->mode, repo, cb, cb_arg);
530 b72706c3 2022-06-01 stsp err = cb(cb_arg, NULL, NULL, NULL, NULL,
531 b72706c3 2022-06-01 stsp &te1->id, NULL, label1, NULL,
532 b72706c3 2022-06-01 stsp 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 b72706c3 2022-06-01 stsp return diff_modified_tree(&te1->id, &te2->id, f1, f2,
542 f9d37699 2022-06-28 stsp fd1, fd2, label1, label2, repo, cb, cb_arg,
543 f9d37699 2022-06-28 stsp 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 f9d37699 2022-06-28 stsp f1, f2, fd1, fd2, label1, label2,
552 f9d37699 2022-06-28 stsp te1->mode, te2->mode, repo, cb, cb_arg);
554 b72706c3 2022-06-01 stsp return cb(cb_arg, NULL, NULL, NULL, NULL,
555 b72706c3 2022-06-01 stsp &te1->id, &te2->id, label1, label2,
556 b72706c3 2022-06-01 stsp 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 49d4a017 2022-06-30 stsp 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 49d4a017 2022-06-30 stsp return diff_added_tree(&te2->id, f1, f2, fd2, label2,
581 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
583 31b4484f 2019-07-27 stsp if (diff_content)
584 49d4a017 2022-06-30 stsp return diff_added_blob(&te2->id, f1, f2, fd2,
585 f9d37699 2022-06-28 stsp label2, te2->mode, repo, cb, cb_arg);
587 b72706c3 2022-06-01 stsp return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
588 b72706c3 2022-06-01 stsp 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 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
594 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
595 b72706c3 2022-06-01 stsp 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 46ea77db 2021-12-20 stsp 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 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
637 f9d37699 2022-06-28 stsp const char *label1, const char *label2,
638 b72706c3 2022-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
639 b72706c3 2022-06-01 stsp 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 f9d37699 2022-06-28 stsp err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
675 f9d37699 2022-06-28 stsp 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 49d4a017 2022-06-30 stsp err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
698 49d4a017 2022-06-30 stsp 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 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
731 f9d37699 2022-06-28 stsp struct got_object_id *id1, struct got_object_id *id2,
732 4b752015 2022-06-30 stsp const char *label1, const char *label2,
733 4b752015 2022-06-30 stsp 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 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
749 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
753 b72706c3 2022-06-01 stsp err = got_diff_blob(line_offsets, nlines, blob1, blob2, f1, f2,
754 4b752015 2022-06-30 stsp label1, label2, diff_algo, diff_context, ignore_whitespace,
755 4b752015 2022-06-30 stsp force_text_diff, outfile);
758 67b631c9 2021-10-10 stsp got_object_blob_close(blob1);
760 67b631c9 2021-10-10 stsp got_object_blob_close(blob2);
761 67b631c9 2021-10-10 stsp return err;
764 67b631c9 2021-10-10 stsp static const struct got_error *
765 67b631c9 2021-10-10 stsp diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
766 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
767 b72706c3 2022-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
769 67b631c9 2021-10-10 stsp const struct got_error *err = NULL;
770 67b631c9 2021-10-10 stsp struct got_pathlist_entry *pe;
771 67b631c9 2021-10-10 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
772 67b631c9 2021-10-10 stsp struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
773 67b631c9 2021-10-10 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
775 67b631c9 2021-10-10 stsp TAILQ_FOREACH(pe, paths, entry) {
776 67b631c9 2021-10-10 stsp int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
777 67b631c9 2021-10-10 stsp mode_t mode1 = 0, mode2 = 0;
780 67b631c9 2021-10-10 stsp id1 = NULL;
782 67b631c9 2021-10-10 stsp id2 = NULL;
783 67b631c9 2021-10-10 stsp if (subtree1) {
784 67b631c9 2021-10-10 stsp got_object_tree_close(subtree1);
785 67b631c9 2021-10-10 stsp subtree1 = NULL;
787 67b631c9 2021-10-10 stsp if (subtree2) {
788 67b631c9 2021-10-10 stsp got_object_tree_close(subtree2);
789 67b631c9 2021-10-10 stsp subtree2 = NULL;
791 67b631c9 2021-10-10 stsp if (blob1) {
792 67b631c9 2021-10-10 stsp got_object_blob_close(blob1);
793 67b631c9 2021-10-10 stsp blob1 = NULL;
795 67b631c9 2021-10-10 stsp if (blob2) {
796 67b631c9 2021-10-10 stsp got_object_blob_close(blob2);
797 67b631c9 2021-10-10 stsp blob2 = NULL;
800 67b631c9 2021-10-10 stsp err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
802 67b631c9 2021-10-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
804 67b631c9 2021-10-10 stsp err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
806 67b631c9 2021-10-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
808 67b631c9 2021-10-10 stsp if (id1 == NULL && id2 == NULL) {
809 67b631c9 2021-10-10 stsp err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
813 67b631c9 2021-10-10 stsp err = got_object_get_type(&type1, repo, id1);
818 67b631c9 2021-10-10 stsp err = got_object_get_type(&type2, repo, id2);
822 67b631c9 2021-10-10 stsp if (type1 == GOT_OBJ_TYPE_ANY &&
823 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_ANY) {
824 67b631c9 2021-10-10 stsp err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
826 67b631c9 2021-10-10 stsp } else if (type1 != GOT_OBJ_TYPE_ANY &&
827 67b631c9 2021-10-10 stsp type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
828 67b631c9 2021-10-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
832 67b631c9 2021-10-10 stsp if (type1 == GOT_OBJ_TYPE_BLOB ||
833 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_BLOB) {
835 67b631c9 2021-10-10 stsp err = got_object_open_as_blob(&blob1, repo,
836 eb81bc23 2022-06-28 tracey id1, 8192, fd1);
841 67b631c9 2021-10-10 stsp err = got_object_open_as_blob(&blob2, repo,
842 eb81bc23 2022-06-28 tracey id2, 8192, fd2);
846 b72706c3 2022-06-01 stsp err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
847 67b631c9 2021-10-10 stsp id1 ? pe->path : "/dev/null",
848 67b631c9 2021-10-10 stsp id2 ? pe->path : "/dev/null",
849 67b631c9 2021-10-10 stsp mode1, mode2, repo);
852 67b631c9 2021-10-10 stsp } else if (type1 == GOT_OBJ_TYPE_TREE ||
853 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_TREE) {
855 67b631c9 2021-10-10 stsp err = got_object_open_as_tree(&subtree1, repo,
861 67b631c9 2021-10-10 stsp err = got_object_open_as_tree(&subtree2, repo,
866 b72706c3 2022-06-01 stsp err = got_diff_tree(subtree1, subtree2, f1, f2,
868 67b631c9 2021-10-10 stsp id1 ? pe->path : "/dev/null",
869 67b631c9 2021-10-10 stsp id2 ? pe->path : "/dev/null",
870 67b631c9 2021-10-10 stsp repo, cb, cb_arg, 1);
874 67b631c9 2021-10-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
881 67b631c9 2021-10-10 stsp if (subtree1)
882 67b631c9 2021-10-10 stsp got_object_tree_close(subtree1);
883 67b631c9 2021-10-10 stsp if (subtree2)
884 67b631c9 2021-10-10 stsp 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 8469d821 2022-06-25 stsp static const struct got_error *
893 8469d821 2022-06-25 stsp show_object_id(off_t **line_offsets, size_t *nlines, const char *obj_typestr,
894 8469d821 2022-06-25 stsp int ch, const char *id_str, FILE *outfile)
896 8469d821 2022-06-25 stsp const struct got_error *err;
898 8469d821 2022-06-25 stsp off_t outoff = 0;
900 8469d821 2022-06-25 stsp n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
901 8469d821 2022-06-25 stsp if (line_offsets != NULL && *line_offsets != NULL) {
902 8469d821 2022-06-25 stsp if (*nlines == 0) {
903 8469d821 2022-06-25 stsp err = add_line_offset(line_offsets, nlines, 0);
905 8469d821 2022-06-25 stsp return err;
907 8469d821 2022-06-25 stsp outoff = (*line_offsets)[*nlines - 1];
909 8469d821 2022-06-25 stsp outoff += n;
910 8469d821 2022-06-25 stsp err = add_line_offset(line_offsets, nlines, outoff);
912 8469d821 2022-06-25 stsp return err;
915 8469d821 2022-06-25 stsp return NULL;
918 8469d821 2022-06-25 stsp static const struct got_error *
919 8469d821 2022-06-25 stsp diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
920 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
921 f9d37699 2022-06-28 stsp struct got_object_id *id1, struct got_object_id *id2,
922 58e31a80 2022-06-27 op struct got_pathlist_head *paths, const char *label1, const char *label2,
923 58e31a80 2022-06-27 op int diff_context, int ignore_whitespace, int force_text_diff,
924 4b752015 2022-06-30 stsp struct got_repository *repo, FILE *outfile,
925 4b752015 2022-06-30 stsp 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 4b752015 2022-06-30 stsp 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 67b631c9 2021-10-10 stsp if (paths == NULL || TAILQ_EMPTY(paths)) {
959 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
960 f9d37699 2022-06-28 stsp label1, label2, repo,
961 f9d37699 2022-06-28 stsp got_diff_blob_output_unidiff, &arg, 1);
963 f9d37699 2022-06-28 stsp err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
964 67b631c9 2021-10-10 stsp 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 8469d821 2022-06-25 stsp got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
980 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
981 f9d37699 2022-06-28 stsp struct got_object_id *id1, struct got_object_id *id2,
982 58e31a80 2022-06-27 op struct got_pathlist_head *paths, const char *label1, const char *label2,
983 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
984 4b752015 2022-06-30 stsp int force_text_diff, struct got_repository *repo, FILE *outfile)
986 8469d821 2022-06-25 stsp const struct got_error *err;
987 8469d821 2022-06-25 stsp char *idstr = NULL;
989 8469d821 2022-06-25 stsp if (id1 == NULL && id2 == NULL)
990 8469d821 2022-06-25 stsp return got_error(GOT_ERR_NO_OBJ);
993 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id1);
996 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "tree", '-',
997 8469d821 2022-06-25 stsp idstr, outfile);
1000 8469d821 2022-06-25 stsp free(idstr);
1001 8469d821 2022-06-25 stsp idstr = NULL;
1003 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "tree", '-',
1004 8469d821 2022-06-25 stsp "/dev/null", outfile);
1006 8469d821 2022-06-25 stsp goto done;
1009 8469d821 2022-06-25 stsp if (id2) {
1010 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id2);
1012 8469d821 2022-06-25 stsp goto done;
1013 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "tree", '+',
1014 8469d821 2022-06-25 stsp idstr, outfile);
1016 8469d821 2022-06-25 stsp goto done;
1017 8469d821 2022-06-25 stsp free(idstr);
1018 8469d821 2022-06-25 stsp idstr = NULL;
1020 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "tree", '+',
1021 8469d821 2022-06-25 stsp "/dev/null", outfile);
1023 8469d821 2022-06-25 stsp goto done;
1026 f9d37699 2022-06-28 stsp err = diff_objects_as_trees(line_offsets, nlines, f1, f2, fd1, fd2,
1027 f9d37699 2022-06-28 stsp id1, id2, paths, label1, label2, diff_context, ignore_whitespace,
1028 4b752015 2022-06-30 stsp force_text_diff, repo, outfile, diff_algo);
1030 8469d821 2022-06-25 stsp free(idstr);
1031 8469d821 2022-06-25 stsp return err;
1034 8469d821 2022-06-25 stsp const struct got_error *
1035 fe621944 2020-11-10 stsp got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
1036 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
1037 f9d37699 2022-06-28 stsp struct got_object_id *id1, struct got_object_id *id2,
1038 4b752015 2022-06-30 stsp 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 8469d821 2022-06-25 stsp 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 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id1);
1055 8469d821 2022-06-25 stsp goto done;
1056 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "commit", '-',
1057 8469d821 2022-06-25 stsp idstr, outfile);
1059 8469d821 2022-06-25 stsp goto done;
1060 8469d821 2022-06-25 stsp free(idstr);
1061 8469d821 2022-06-25 stsp idstr = NULL;
1063 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "commit", '-',
1064 8469d821 2022-06-25 stsp "/dev/null", outfile);
1066 8469d821 2022-06-25 stsp 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 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id2);
1075 8469d821 2022-06-25 stsp goto done;
1076 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "commit", '+',
1077 8469d821 2022-06-25 stsp idstr, outfile);
1079 8469d821 2022-06-25 stsp goto done;
1081 f9d37699 2022-06-28 stsp 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 67b631c9 2021-10-10 stsp got_object_commit_get_tree_id(commit2), paths, "", "",
1084 4b752015 2022-06-30 stsp diff_context, ignore_whitespace, force_text_diff, repo, outfile,
1085 4b752015 2022-06-30 stsp 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 8469d821 2022-06-25 stsp 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 49d4a017 2022-06-30 stsp FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1098 49d4a017 2022-06-30 stsp const char *label2, int diff_context, int ignore_whitespace,
1099 4b752015 2022-06-30 stsp 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 49d4a017 2022-06-30 stsp f1_exists ? label1 : "/dev/null");
1110 dc424a06 2019-08-07 stsp fprintf(outfile, "file + %s\n",
1111 49d4a017 2022-06-30 stsp f2_exists ? label2 : "/dev/null");
1114 4b752015 2022-06-30 stsp 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 49d4a017 2022-06-30 stsp 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;