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 b72706c3 2022-06-01 stsp FILE *f2, 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 7f1f93af 2019-08-06 stsp f2 == NULL ? "/dev/null" : label2);
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 b72706c3 2022-06-01 stsp f1 != NULL, f2 != NULL,
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 b72706c3 2022-06-01 stsp const char *label1, FILE *f2, size_t size2, const char *label2,
269 b72706c3 2022-06-01 stsp int diff_context, int ignore_whitespace, int force_text_diff, FILE *outfile)
271 b72706c3 2022-06-01 stsp return diff_blob_file(NULL, blob1, f1, size1, label1, f2, size2, label2,
272 64453f7e 2020-11-21 stsp diff_context, ignore_whitespace, force_text_diff, outfile);
275 474b4f94 2017-11-30 stsp static const struct got_error *
276 b72706c3 2022-06-01 stsp diff_added_blob(struct got_object_id *id, FILE *f, const char *label,
277 b72706c3 2022-06-01 stsp mode_t mode, struct got_repository *repo,
278 b72706c3 2022-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
280 4e22badc 2017-11-30 stsp const struct got_error *err;
281 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
282 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
284 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
286 4e22badc 2017-11-30 stsp return err;
288 2acfca77 2018-04-01 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
291 b72706c3 2022-06-01 stsp err = cb(cb_arg, NULL, blob, NULL, f, NULL, id,
292 b72706c3 2022-06-01 stsp NULL, label, 0, mode, repo);
294 2acfca77 2018-04-01 stsp got_object_close(obj);
296 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
297 2acfca77 2018-04-01 stsp return err;
300 474b4f94 2017-11-30 stsp static const struct got_error *
301 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
302 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, const char *label1, const char *label2,
303 b72706c3 2022-06-01 stsp mode_t mode1, mode_t mode2, struct got_repository *repo,
304 b72706c3 2022-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
306 6a213ccb 2017-11-30 stsp const struct got_error *err;
307 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
308 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
309 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
310 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
312 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
314 730a8aa0 2018-04-24 stsp return err;
315 15a94983 2018-12-23 stsp if (obj1->type != GOT_OBJ_TYPE_BLOB) {
316 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
320 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
323 15a94983 2018-12-23 stsp if (obj2->type != GOT_OBJ_TYPE_BLOB) {
324 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
328 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob1, repo, obj1, 8192);
332 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob2, repo, obj2, 8192);
336 b72706c3 2022-06-01 stsp err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
337 b72706c3 2022-06-01 stsp mode1, mode2, repo);
340 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
342 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
344 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
346 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
347 6a213ccb 2017-11-30 stsp return err;
350 474b4f94 2017-11-30 stsp static const struct got_error *
351 b72706c3 2022-06-01 stsp diff_deleted_blob(struct got_object_id *id, FILE *f, const char *label,
352 b72706c3 2022-06-01 stsp mode_t mode, struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
354 365fb436 2017-11-30 stsp const struct got_error *err;
355 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
356 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
358 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
360 365fb436 2017-11-30 stsp return err;
362 2acfca77 2018-04-01 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
365 b72706c3 2022-06-01 stsp err = cb(cb_arg, blob, NULL, f, NULL, id, NULL, label, NULL,
366 b72706c3 2022-06-01 stsp mode, 0, repo);
368 2acfca77 2018-04-01 stsp got_object_close(obj);
370 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
371 2acfca77 2018-04-01 stsp return err;
374 474b4f94 2017-11-30 stsp static const struct got_error *
375 b72706c3 2022-06-01 stsp diff_added_tree(struct got_object_id *id, FILE *f, const char *label,
376 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
377 31b4484f 2019-07-27 stsp int diff_content)
379 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
380 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
381 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
383 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
387 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
388 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
392 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
396 b72706c3 2022-06-01 stsp err = got_diff_tree(NULL, tree, NULL, f, NULL, label,
397 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
400 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
401 9c70d4c3 2017-11-30 stsp if (treeobj)
402 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
403 9c70d4c3 2017-11-30 stsp return err;
406 474b4f94 2017-11-30 stsp static const struct got_error *
407 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
408 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, const char *label1, const char *label2,
409 b72706c3 2022-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
410 b72706c3 2022-06-01 stsp int diff_content)
412 f6861a81 2018-09-13 stsp const struct got_error *err;
413 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
414 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
415 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
416 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
418 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
422 15a94983 2018-12-23 stsp if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
423 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
427 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
431 15a94983 2018-12-23 stsp if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
432 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
436 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
440 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
444 b72706c3 2022-06-01 stsp err = got_diff_tree(tree1, tree2, f1, f2, label1, label2,
445 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
449 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
451 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
452 789689b5 2017-11-30 stsp if (treeobj1)
453 789689b5 2017-11-30 stsp got_object_close(treeobj1);
454 789689b5 2017-11-30 stsp if (treeobj2)
455 789689b5 2017-11-30 stsp got_object_close(treeobj2);
456 789689b5 2017-11-30 stsp return err;
459 474b4f94 2017-11-30 stsp static const struct got_error *
460 b72706c3 2022-06-01 stsp diff_deleted_tree(struct got_object_id *id, FILE *f, const char *label,
461 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
462 31b4484f 2019-07-27 stsp int diff_content)
464 f6861a81 2018-09-13 stsp const struct got_error *err;
465 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
466 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
468 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
472 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
473 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
477 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
481 b72706c3 2022-06-01 stsp err = got_diff_tree(tree, NULL, f, NULL, label, NULL,
482 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
485 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
486 2c56f2ce 2017-11-30 stsp if (treeobj)
487 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
488 2c56f2ce 2017-11-30 stsp return err;
491 474b4f94 2017-11-30 stsp static const struct got_error *
492 74671950 2018-02-11 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
493 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
494 aaa13589 2019-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
496 013404a9 2017-11-30 stsp /* XXX TODO */
497 474b4f94 2017-11-30 stsp return NULL;
500 474b4f94 2017-11-30 stsp static const struct got_error *
501 b72706c3 2022-06-01 stsp diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
502 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, const char *label1, const char *label2,
503 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
504 31b4484f 2019-07-27 stsp int diff_content)
506 f6861a81 2018-09-13 stsp const struct got_error *err = NULL;
507 19ae6da1 2018-11-05 stsp int id_match;
509 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te1))
510 63c5ca5d 2019-08-24 stsp return NULL;
512 474b4f94 2017-11-30 stsp if (te2 == NULL) {
513 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
514 b72706c3 2022-06-01 stsp err = diff_deleted_tree(&te1->id, f1, label1, repo,
515 31b4484f 2019-07-27 stsp cb, cb_arg, diff_content);
517 31b4484f 2019-07-27 stsp if (diff_content)
518 b72706c3 2022-06-01 stsp err = diff_deleted_blob(&te1->id, f1, label1,
519 46f68b20 2019-10-19 stsp te1->mode, repo, cb, cb_arg);
521 b72706c3 2022-06-01 stsp err = cb(cb_arg, NULL, NULL, NULL, NULL,
522 b72706c3 2022-06-01 stsp &te1->id, NULL, label1, NULL,
523 b72706c3 2022-06-01 stsp te1->mode, 0, repo);
525 f6861a81 2018-09-13 stsp return err;
526 63c5ca5d 2019-08-24 stsp } else if (got_object_tree_entry_is_submodule(te2))
527 63c5ca5d 2019-08-24 stsp return NULL;
529 56e0773d 2019-11-28 stsp id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
530 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
531 19ae6da1 2018-11-05 stsp if (!id_match)
532 b72706c3 2022-06-01 stsp return diff_modified_tree(&te1->id, &te2->id, f1, f2,
533 31b4484f 2019-07-27 stsp label1, label2, repo, cb, cb_arg, diff_content);
534 40dde666 2020-07-23 stsp } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
535 40dde666 2020-07-23 stsp (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
536 46f68b20 2019-10-19 stsp if (!id_match ||
537 40dde666 2020-07-23 stsp ((te1->mode & (S_IFLNK | S_IXUSR))) !=
538 40dde666 2020-07-23 stsp (te2->mode & (S_IFLNK | S_IXUSR))) {
539 31b4484f 2019-07-27 stsp if (diff_content)
540 56e0773d 2019-11-28 stsp return diff_modified_blob(&te1->id, &te2->id,
541 b72706c3 2022-06-01 stsp f1, f2, label1, label2, te1->mode,
542 b72706c3 2022-06-01 stsp te2->mode, repo, cb, cb_arg);
544 b72706c3 2022-06-01 stsp return cb(cb_arg, NULL, NULL, NULL, NULL,
545 b72706c3 2022-06-01 stsp &te1->id, &te2->id, label1, label2,
546 b72706c3 2022-06-01 stsp te1->mode, te2->mode, repo);
550 19ae6da1 2018-11-05 stsp if (id_match)
551 f6861a81 2018-09-13 stsp return NULL;
553 56e0773d 2019-11-28 stsp return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
554 aaa13589 2019-06-01 stsp cb, cb_arg);
557 474b4f94 2017-11-30 stsp static const struct got_error *
558 56e0773d 2019-11-28 stsp diff_entry_new_old(struct got_tree_entry *te2,
559 b72706c3 2022-06-01 stsp struct got_tree_entry *te1, FILE *f2, const char *label2,
560 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
561 31b4484f 2019-07-27 stsp int diff_content)
563 f6861a81 2018-09-13 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
564 63c5ca5d 2019-08-24 stsp return NULL;
566 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te2))
567 f6861a81 2018-09-13 stsp return NULL;
569 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
570 b72706c3 2022-06-01 stsp return diff_added_tree(&te2->id, f2, label2,
571 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
573 31b4484f 2019-07-27 stsp if (diff_content)
574 b72706c3 2022-06-01 stsp return diff_added_blob(&te2->id, f2, label2, te2->mode,
575 b72706c3 2022-06-01 stsp repo, cb, cb_arg);
577 b72706c3 2022-06-01 stsp return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
578 b72706c3 2022-06-01 stsp NULL, label2, 0, te2->mode, repo);
581 0208f208 2020-05-05 stsp const struct got_error *
582 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
583 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
584 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
585 b72706c3 2022-06-01 stsp const char *label1, const char *label2,
586 0208f208 2020-05-05 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
588 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
589 0208f208 2020-05-05 stsp struct got_pathlist_head *paths = arg;
590 0208f208 2020-05-05 stsp struct got_diff_changed_path *change = NULL;
591 0208f208 2020-05-05 stsp char *path = NULL;
593 0208f208 2020-05-05 stsp path = strdup(label2 ? label2 : label1);
594 0208f208 2020-05-05 stsp if (path == NULL)
595 0208f208 2020-05-05 stsp return got_error_from_errno("malloc");
597 0208f208 2020-05-05 stsp change = malloc(sizeof(*change));
598 0208f208 2020-05-05 stsp if (change == NULL) {
599 0208f208 2020-05-05 stsp err = got_error_from_errno("malloc");
603 0208f208 2020-05-05 stsp change->status = GOT_STATUS_NO_CHANGE;
604 0208f208 2020-05-05 stsp if (id1 == NULL)
605 0208f208 2020-05-05 stsp change->status = GOT_STATUS_ADD;
606 0208f208 2020-05-05 stsp else if (id2 == NULL)
607 0208f208 2020-05-05 stsp change->status = GOT_STATUS_DELETE;
609 0208f208 2020-05-05 stsp if (got_object_id_cmp(id1, id2) != 0)
610 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODIFY;
611 0208f208 2020-05-05 stsp else if (mode1 != mode2)
612 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODE_CHANGE;
615 46ea77db 2021-12-20 stsp err = got_pathlist_append(paths, path, change);
618 0208f208 2020-05-05 stsp free(path);
619 0208f208 2020-05-05 stsp free(change);
621 0208f208 2020-05-05 stsp return err;
624 474b4f94 2017-11-30 stsp const struct got_error *
625 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
626 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, const char *label1, const char *label2,
627 b72706c3 2022-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
628 b72706c3 2022-06-01 stsp int diff_content)
630 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
631 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
632 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
633 f6861a81 2018-09-13 stsp char *l1 = NULL, *l2 = NULL;
634 56e0773d 2019-11-28 stsp int tidx1 = 0, tidx2 = 0;
636 883f0469 2018-06-23 stsp if (tree1) {
637 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, 0);
638 60f50a58 2018-09-15 stsp if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
639 f6861a81 2018-09-13 stsp te1->name) == -1)
640 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
642 883f0469 2018-06-23 stsp if (tree2) {
643 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, 0);
644 60f50a58 2018-09-15 stsp if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
645 f6861a81 2018-09-13 stsp te2->name) == -1)
646 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
651 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
653 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree2,
654 1de5e065 2019-06-01 stsp te1->name);
658 f6861a81 2018-09-13 stsp if (te && asprintf(&l2, "%s%s%s", label2,
659 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te->name) == -1)
661 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
663 b72706c3 2022-06-01 stsp err = diff_entry_old_new(te1, te, f1, f2, l1, l2,
664 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
670 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
672 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree1,
673 1de5e065 2019-06-01 stsp te2->name);
676 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
677 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te->name) == -1)
679 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
681 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
682 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te2->name) == -1)
684 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
686 b72706c3 2022-06-01 stsp err = diff_entry_new_old(te2, te, f2, l2, repo,
687 31b4484f 2019-07-27 stsp cb, cb_arg, diff_content);
696 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, tidx1);
698 f6861a81 2018-09-13 stsp asprintf(&l1, "%s%s%s", label1,
699 f6861a81 2018-09-13 stsp label1[0] ? "/" : "", te1->name) == -1)
700 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
706 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, tidx2);
708 f6861a81 2018-09-13 stsp asprintf(&l2, "%s%s%s", label2,
709 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te2->name) == -1)
710 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
712 474b4f94 2017-11-30 stsp } while (te1 || te2);
714 11528a82 2018-05-19 stsp return err;
717 11528a82 2018-05-19 stsp const struct got_error *
718 fe621944 2020-11-10 stsp got_diff_objects_as_blobs(off_t **line_offsets, size_t *nlines,
719 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
720 54156555 2018-12-24 stsp const char *label1, const char *label2, int diff_context,
721 64453f7e 2020-11-21 stsp int ignore_whitespace, int force_text_diff,
722 64453f7e 2020-11-21 stsp struct got_repository *repo, FILE *outfile)
724 11528a82 2018-05-19 stsp const struct got_error *err;
725 11528a82 2018-05-19 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
727 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
728 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
731 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob1, repo, id1, 8192);
736 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob2, repo, id2, 8192);
740 b72706c3 2022-06-01 stsp err = got_diff_blob(line_offsets, nlines, blob1, blob2, f1, f2,
741 64453f7e 2020-11-21 stsp label1, label2, diff_context, ignore_whitespace, force_text_diff,
745 67b631c9 2021-10-10 stsp got_object_blob_close(blob1);
747 67b631c9 2021-10-10 stsp got_object_blob_close(blob2);
748 67b631c9 2021-10-10 stsp return err;
751 67b631c9 2021-10-10 stsp static const struct got_error *
752 67b631c9 2021-10-10 stsp diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
753 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, struct got_pathlist_head *paths,
754 b72706c3 2022-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
756 67b631c9 2021-10-10 stsp const struct got_error *err = NULL;
757 67b631c9 2021-10-10 stsp struct got_pathlist_entry *pe;
758 67b631c9 2021-10-10 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
759 67b631c9 2021-10-10 stsp struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
760 67b631c9 2021-10-10 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
762 67b631c9 2021-10-10 stsp TAILQ_FOREACH(pe, paths, entry) {
763 67b631c9 2021-10-10 stsp int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
764 67b631c9 2021-10-10 stsp mode_t mode1 = 0, mode2 = 0;
767 67b631c9 2021-10-10 stsp id1 = NULL;
769 67b631c9 2021-10-10 stsp id2 = NULL;
770 67b631c9 2021-10-10 stsp if (subtree1) {
771 67b631c9 2021-10-10 stsp got_object_tree_close(subtree1);
772 67b631c9 2021-10-10 stsp subtree1 = NULL;
774 67b631c9 2021-10-10 stsp if (subtree2) {
775 67b631c9 2021-10-10 stsp got_object_tree_close(subtree2);
776 67b631c9 2021-10-10 stsp subtree2 = NULL;
778 67b631c9 2021-10-10 stsp if (blob1) {
779 67b631c9 2021-10-10 stsp got_object_blob_close(blob1);
780 67b631c9 2021-10-10 stsp blob1 = NULL;
782 67b631c9 2021-10-10 stsp if (blob2) {
783 67b631c9 2021-10-10 stsp got_object_blob_close(blob2);
784 67b631c9 2021-10-10 stsp blob2 = NULL;
787 67b631c9 2021-10-10 stsp err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
789 67b631c9 2021-10-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
791 67b631c9 2021-10-10 stsp err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
793 67b631c9 2021-10-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
795 67b631c9 2021-10-10 stsp if (id1 == NULL && id2 == NULL) {
796 67b631c9 2021-10-10 stsp err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
800 67b631c9 2021-10-10 stsp err = got_object_get_type(&type1, repo, id1);
805 67b631c9 2021-10-10 stsp err = got_object_get_type(&type2, repo, id2);
809 67b631c9 2021-10-10 stsp if (type1 == GOT_OBJ_TYPE_ANY &&
810 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_ANY) {
811 67b631c9 2021-10-10 stsp err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
813 67b631c9 2021-10-10 stsp } else if (type1 != GOT_OBJ_TYPE_ANY &&
814 67b631c9 2021-10-10 stsp type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
815 67b631c9 2021-10-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
819 67b631c9 2021-10-10 stsp if (type1 == GOT_OBJ_TYPE_BLOB ||
820 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_BLOB) {
822 67b631c9 2021-10-10 stsp err = got_object_open_as_blob(&blob1, repo,
823 67b631c9 2021-10-10 stsp id1, 8192);
828 67b631c9 2021-10-10 stsp err = got_object_open_as_blob(&blob2, repo,
829 67b631c9 2021-10-10 stsp id2, 8192);
833 b72706c3 2022-06-01 stsp err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
834 67b631c9 2021-10-10 stsp id1 ? pe->path : "/dev/null",
835 67b631c9 2021-10-10 stsp id2 ? pe->path : "/dev/null",
836 67b631c9 2021-10-10 stsp mode1, mode2, repo);
839 67b631c9 2021-10-10 stsp } else if (type1 == GOT_OBJ_TYPE_TREE ||
840 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_TREE) {
842 67b631c9 2021-10-10 stsp err = got_object_open_as_tree(&subtree1, repo,
848 67b631c9 2021-10-10 stsp err = got_object_open_as_tree(&subtree2, repo,
853 b72706c3 2022-06-01 stsp err = got_diff_tree(subtree1, subtree2, f1, f2,
854 67b631c9 2021-10-10 stsp id1 ? pe->path : "/dev/null",
855 67b631c9 2021-10-10 stsp id2 ? pe->path : "/dev/null",
856 67b631c9 2021-10-10 stsp repo, cb, cb_arg, 1);
860 67b631c9 2021-10-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
867 67b631c9 2021-10-10 stsp if (subtree1)
868 67b631c9 2021-10-10 stsp got_object_tree_close(subtree1);
869 67b631c9 2021-10-10 stsp if (subtree2)
870 67b631c9 2021-10-10 stsp got_object_tree_close(subtree2);
872 11528a82 2018-05-19 stsp got_object_blob_close(blob1);
874 11528a82 2018-05-19 stsp got_object_blob_close(blob2);
875 474b4f94 2017-11-30 stsp return err;
878 8469d821 2022-06-25 stsp static const struct got_error *
879 8469d821 2022-06-25 stsp show_object_id(off_t **line_offsets, size_t *nlines, const char *obj_typestr,
880 8469d821 2022-06-25 stsp int ch, const char *id_str, FILE *outfile)
882 8469d821 2022-06-25 stsp const struct got_error *err;
884 8469d821 2022-06-25 stsp off_t outoff = 0;
886 8469d821 2022-06-25 stsp n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
887 8469d821 2022-06-25 stsp if (line_offsets != NULL && *line_offsets != NULL) {
888 8469d821 2022-06-25 stsp if (*nlines == 0) {
889 8469d821 2022-06-25 stsp err = add_line_offset(line_offsets, nlines, 0);
891 8469d821 2022-06-25 stsp return err;
893 8469d821 2022-06-25 stsp outoff = (*line_offsets)[*nlines - 1];
895 8469d821 2022-06-25 stsp outoff += n;
896 8469d821 2022-06-25 stsp err = add_line_offset(line_offsets, nlines, outoff);
898 8469d821 2022-06-25 stsp return err;
901 8469d821 2022-06-25 stsp return NULL;
904 8469d821 2022-06-25 stsp static const struct got_error *
905 8469d821 2022-06-25 stsp diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
906 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
907 58e31a80 2022-06-27 op struct got_pathlist_head *paths, const char *label1, const char *label2,
908 58e31a80 2022-06-27 op int diff_context, int ignore_whitespace, int force_text_diff,
909 58e31a80 2022-06-27 op struct got_repository *repo, FILE *outfile)
911 11528a82 2018-05-19 stsp const struct got_error *err;
912 11528a82 2018-05-19 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
913 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
914 fe621944 2020-11-10 stsp int want_lineoffsets = (line_offsets != NULL && *line_offsets != NULL);
916 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
917 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
920 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo, id1);
925 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo, id2);
930 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
931 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
932 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
933 aaa13589 2019-06-01 stsp arg.outfile = outfile;
934 fe621944 2020-11-10 stsp if (want_lineoffsets) {
935 fe621944 2020-11-10 stsp arg.line_offsets = *line_offsets;
936 fe621944 2020-11-10 stsp arg.nlines = *nlines;
938 fe621944 2020-11-10 stsp arg.line_offsets = NULL;
939 fe621944 2020-11-10 stsp arg.nlines = 0;
941 67b631c9 2021-10-10 stsp if (paths == NULL || TAILQ_EMPTY(paths)) {
942 b72706c3 2022-06-01 stsp err = got_diff_tree(tree1, tree2, f1, f2, label1, label2,
943 b72706c3 2022-06-01 stsp repo, got_diff_blob_output_unidiff, &arg, 1);
945 b72706c3 2022-06-01 stsp err = diff_paths(tree1, tree2, f1, f2, paths, repo,
946 67b631c9 2021-10-10 stsp got_diff_blob_output_unidiff, &arg);
948 fe621944 2020-11-10 stsp if (want_lineoffsets) {
949 fe621944 2020-11-10 stsp *line_offsets = arg.line_offsets; /* was likely re-allocated */
950 fe621944 2020-11-10 stsp *nlines = arg.nlines;
954 11528a82 2018-05-19 stsp got_object_tree_close(tree1);
956 11528a82 2018-05-19 stsp got_object_tree_close(tree2);
957 11528a82 2018-05-19 stsp return err;
960 11528a82 2018-05-19 stsp const struct got_error *
961 8469d821 2022-06-25 stsp got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
962 8469d821 2022-06-25 stsp FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
963 58e31a80 2022-06-27 op struct got_pathlist_head *paths, const char *label1, const char *label2,
964 58e31a80 2022-06-27 op int diff_context, int ignore_whitespace, int force_text_diff,
965 58e31a80 2022-06-27 op struct got_repository *repo, FILE *outfile)
967 8469d821 2022-06-25 stsp const struct got_error *err;
968 8469d821 2022-06-25 stsp char *idstr = NULL;
970 8469d821 2022-06-25 stsp if (id1 == NULL && id2 == NULL)
971 8469d821 2022-06-25 stsp return got_error(GOT_ERR_NO_OBJ);
974 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id1);
977 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "tree", '-',
978 8469d821 2022-06-25 stsp idstr, outfile);
981 8469d821 2022-06-25 stsp free(idstr);
982 8469d821 2022-06-25 stsp idstr = NULL;
984 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "tree", '-',
985 8469d821 2022-06-25 stsp "/dev/null", outfile);
991 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id2);
994 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "tree", '+',
995 8469d821 2022-06-25 stsp idstr, outfile);
998 8469d821 2022-06-25 stsp free(idstr);
999 8469d821 2022-06-25 stsp idstr = NULL;
1001 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "tree", '+',
1002 8469d821 2022-06-25 stsp "/dev/null", outfile);
1004 8469d821 2022-06-25 stsp goto done;
1007 8469d821 2022-06-25 stsp err = diff_objects_as_trees(line_offsets, nlines, f1, f2, id1, id2,
1008 8469d821 2022-06-25 stsp paths, label1, label2, diff_context, ignore_whitespace,
1009 8469d821 2022-06-25 stsp force_text_diff, repo, outfile);
1011 8469d821 2022-06-25 stsp free(idstr);
1012 8469d821 2022-06-25 stsp return err;
1015 8469d821 2022-06-25 stsp const struct got_error *
1016 fe621944 2020-11-10 stsp got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
1017 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
1018 67b631c9 2021-10-10 stsp struct got_pathlist_head *paths,
1019 64453f7e 2020-11-21 stsp int diff_context, int ignore_whitespace, int force_text_diff,
1020 15a94983 2018-12-23 stsp struct got_repository *repo, FILE *outfile)
1022 11528a82 2018-05-19 stsp const struct got_error *err;
1023 11528a82 2018-05-19 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1024 8469d821 2022-06-25 stsp char *idstr = NULL;
1026 15a94983 2018-12-23 stsp if (id2 == NULL)
1027 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
1029 15a94983 2018-12-23 stsp if (id1) {
1030 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit1, repo, id1);
1032 cd0acaa7 2018-05-20 stsp goto done;
1033 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id1);
1035 8469d821 2022-06-25 stsp goto done;
1036 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "commit", '-',
1037 8469d821 2022-06-25 stsp idstr, outfile);
1039 8469d821 2022-06-25 stsp goto done;
1040 8469d821 2022-06-25 stsp free(idstr);
1041 8469d821 2022-06-25 stsp idstr = NULL;
1043 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "commit", '-',
1044 8469d821 2022-06-25 stsp "/dev/null", outfile);
1046 8469d821 2022-06-25 stsp goto done;
1049 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, repo, id2);
1051 9b697879 2018-05-20 stsp goto done;
1053 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id2);
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;
1061 8469d821 2022-06-25 stsp err = diff_objects_as_trees(line_offsets, nlines, f1, f2,
1062 15a94983 2018-12-23 stsp commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1063 67b631c9 2021-10-10 stsp got_object_commit_get_tree_id(commit2), paths, "", "",
1064 67b631c9 2021-10-10 stsp diff_context, ignore_whitespace, force_text_diff, repo, outfile);
1066 11528a82 2018-05-19 stsp if (commit1)
1067 11528a82 2018-05-19 stsp got_object_commit_close(commit1);
1068 11528a82 2018-05-19 stsp if (commit2)
1069 11528a82 2018-05-19 stsp got_object_commit_close(commit2);
1070 8469d821 2022-06-25 stsp free(idstr);
1071 11528a82 2018-05-19 stsp return err;
1074 dc424a06 2019-08-07 stsp const struct got_error *
1075 fe621944 2020-11-10 stsp got_diff_files(struct got_diffreg_result **resultp,
1076 fe621944 2020-11-10 stsp FILE *f1, const char *label1, FILE *f2, const char *label2,
1077 64453f7e 2020-11-21 stsp int diff_context, int ignore_whitespace, int force_text_diff,
1078 64453f7e 2020-11-21 stsp FILE *outfile)
1080 dc424a06 2019-08-07 stsp const struct got_error *err = NULL;
1081 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
1083 fe621944 2020-11-10 stsp if (resultp)
1084 fe621944 2020-11-10 stsp *resultp = NULL;
1086 dc424a06 2019-08-07 stsp if (outfile) {
1087 dc424a06 2019-08-07 stsp fprintf(outfile, "file - %s\n",
1088 dc424a06 2019-08-07 stsp f1 == NULL ? "/dev/null" : label1);
1089 dc424a06 2019-08-07 stsp fprintf(outfile, "file + %s\n",
1090 dc424a06 2019-08-07 stsp f2 == NULL ? "/dev/null" : label2);
1093 fe621944 2020-11-10 stsp err = got_diffreg(&diffreg_result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
1094 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
1096 fe621944 2020-11-10 stsp goto done;
1098 fe621944 2020-11-10 stsp if (outfile) {
1099 fe621944 2020-11-10 stsp err = got_diffreg_output(NULL, NULL, diffreg_result,
1100 1cb46f00 2020-11-21 stsp f1 != NULL, f2 != NULL, label1, label2,
1101 1cb46f00 2020-11-21 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1103 dc424a06 2019-08-07 stsp goto done;
1107 fe621944 2020-11-10 stsp if (resultp && err == NULL)
1108 fe621944 2020-11-10 stsp *resultp = diffreg_result;
1109 fe621944 2020-11-10 stsp else if (diffreg_result) {
1110 fe621944 2020-11-10 stsp const struct got_error *free_err;
1111 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
1112 fe621944 2020-11-10 stsp if (free_err && err == NULL)
1113 fe621944 2020-11-10 stsp err = free_err;
1116 dc424a06 2019-08-07 stsp return err;