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"
35 718b3ab0 2018-03-17 stsp #include "got_lib_diff.h"
36 15a94983 2018-12-23 stsp #include "got_lib_delta.h"
37 15a94983 2018-12-23 stsp #include "got_lib_inflate.h"
38 15a94983 2018-12-23 stsp #include "got_lib_object.h"
40 404c43c4 2018-06-21 stsp static const struct got_error *
41 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
45 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
46 fe621944 2020-11-10 stsp if (p == NULL)
47 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
48 fe621944 2020-11-10 stsp *line_offsets = p;
49 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
50 fe621944 2020-11-10 stsp (*nlines)++;
51 fe621944 2020-11-10 stsp return NULL;
54 fe621944 2020-11-10 stsp static const struct got_error *
55 b72706c3 2022-06-01 stsp reset_file(FILE *f)
57 b72706c3 2022-06-01 stsp if (fpurge(f) == EOF)
58 b72706c3 2022-06-01 stsp return got_error_from_errno("fpurge");
59 b72706c3 2022-06-01 stsp if (ftruncate(fileno(f), 0L) == -1)
60 b72706c3 2022-06-01 stsp return got_error_from_errno("ftruncate");
61 b72706c3 2022-06-01 stsp if (fseeko(f, 0L, SEEK_SET) == -1)
62 b72706c3 2022-06-01 stsp return got_error_from_errno("fseeko");
63 b72706c3 2022-06-01 stsp return NULL;
66 b72706c3 2022-06-01 stsp static const struct got_error *
67 fe621944 2020-11-10 stsp diff_blobs(off_t **line_offsets, size_t *nlines,
68 fe621944 2020-11-10 stsp struct got_diffreg_result **resultp, struct got_blob_object *blob1,
69 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
70 46f68b20 2019-10-19 stsp const char *label1, const char *label2, mode_t mode1, mode_t mode2,
71 64453f7e 2020-11-21 stsp int diff_context, int ignore_whitespace, int force_text_diff, FILE *outfile)
73 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
74 f78b0693 2017-11-29 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
75 f78b0693 2017-11-29 stsp char hex2[SHA1_DIGEST_STRING_LENGTH];
76 98abbc84 2017-11-30 stsp char *idstr1 = NULL, *idstr2 = NULL;
77 be659d10 2020-11-18 stsp off_t size1, size2;
78 fe621944 2020-11-10 stsp struct got_diffreg_result *result;
79 fe621944 2020-11-10 stsp off_t outoff = 0;
82 fe621944 2020-11-10 stsp if (line_offsets && *line_offsets && *nlines > 0)
83 fe621944 2020-11-10 stsp outoff = (*line_offsets)[*nlines - 1];
84 9c659ea0 2020-11-22 stsp else if (line_offsets) {
85 9c659ea0 2020-11-22 stsp err = add_line_offset(line_offsets, nlines, 0);
90 fe621944 2020-11-10 stsp if (resultp)
91 fe621944 2020-11-10 stsp *resultp = NULL;
94 b72706c3 2022-06-01 stsp err = reset_file(f1);
99 b72706c3 2022-06-01 stsp err = reset_file(f2);
105 98abbc84 2017-11-30 stsp if (blob1) {
106 f934cf2c 2018-02-12 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
107 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
112 98abbc84 2017-11-30 stsp idstr1 = "/dev/null";
115 98abbc84 2017-11-30 stsp if (blob2) {
116 f934cf2c 2018-02-12 stsp idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
117 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
122 98abbc84 2017-11-30 stsp idstr2 = "/dev/null";
124 09de383e 2018-12-24 stsp if (outfile) {
125 46f68b20 2019-10-19 stsp char *modestr1 = NULL, *modestr2 = NULL;
126 40dde666 2020-07-23 stsp int modebits;
127 46f68b20 2019-10-19 stsp if (mode1 && mode1 != mode2) {
128 40dde666 2020-07-23 stsp if (S_ISLNK(mode1))
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(&modestr1, " (mode %o)",
133 40dde666 2020-07-23 stsp mode1 & modebits) == -1) {
134 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
138 46f68b20 2019-10-19 stsp if (mode2 && mode1 != mode2) {
139 40dde666 2020-07-23 stsp if (S_ISLNK(mode2))
140 40dde666 2020-07-23 stsp modebits = S_IFLNK;
142 40dde666 2020-07-23 stsp modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
143 46f68b20 2019-10-19 stsp if (asprintf(&modestr2, " (mode %o)",
144 40dde666 2020-07-23 stsp mode2 & modebits) == -1) {
145 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
149 fe621944 2020-11-10 stsp n = fprintf(outfile, "blob - %s%s\n", idstr1,
150 46f68b20 2019-10-19 stsp modestr1 ? modestr1 : "");
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 fe621944 2020-11-10 stsp n = fprintf(outfile, "blob + %s%s\n", idstr2,
161 46f68b20 2019-10-19 stsp modestr2 ? modestr2 : "");
164 fe621944 2020-11-10 stsp outoff += n;
165 fe621944 2020-11-10 stsp if (line_offsets) {
166 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
171 46f68b20 2019-10-19 stsp free(modestr1);
172 46f68b20 2019-10-19 stsp free(modestr2);
174 fe621944 2020-11-10 stsp err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
175 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
179 fe621944 2020-11-10 stsp if (outfile) {
180 1cb46f00 2020-11-21 stsp err = got_diffreg_output(line_offsets, nlines, result,
181 1cb46f00 2020-11-21 stsp blob1 != NULL, blob2 != NULL,
182 fe621944 2020-11-10 stsp label1 ? label1 : idstr1,
183 fe621944 2020-11-10 stsp label2 ? label2 : idstr2,
184 fe621944 2020-11-10 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
189 fe621944 2020-11-10 stsp if (resultp && err == NULL)
190 fe621944 2020-11-10 stsp *resultp = result;
192 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
193 fe621944 2020-11-10 stsp if (free_err && err == NULL)
194 fe621944 2020-11-10 stsp err = free_err;
197 7d283eee 2017-11-29 stsp return err;
200 aaa13589 2019-06-01 stsp const struct got_error *
201 aaa13589 2019-06-01 stsp got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
202 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
203 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
204 b72706c3 2022-06-01 stsp const char *label1, const char *label2, mode_t mode1, mode_t mode2,
205 b72706c3 2022-06-01 stsp struct got_repository *repo)
207 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg *a = arg;
209 fe621944 2020-11-10 stsp return diff_blobs(&a->line_offsets, &a->nlines, NULL,
210 b72706c3 2022-06-01 stsp blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
211 64453f7e 2020-11-21 stsp a->ignore_whitespace, a->force_text_diff, a->outfile);
214 404c43c4 2018-06-21 stsp const struct got_error *
215 fe621944 2020-11-10 stsp got_diff_blob(off_t **line_offsets, size_t *nlines,
216 fe621944 2020-11-10 stsp struct got_blob_object *blob1, struct got_blob_object *blob2,
217 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, const char *label1, const char *label2,
218 b72706c3 2022-06-01 stsp int diff_context, int ignore_whitespace, int force_text_diff,
219 b72706c3 2022-06-01 stsp FILE *outfile)
221 b72706c3 2022-06-01 stsp return diff_blobs(line_offsets, nlines, NULL, blob1, blob2, f1, f2,
222 64453f7e 2020-11-21 stsp label1, label2, 0, 0, diff_context, ignore_whitespace,
223 64453f7e 2020-11-21 stsp force_text_diff, outfile);
226 7f1f93af 2019-08-06 stsp static const struct got_error *
227 fe621944 2020-11-10 stsp diff_blob_file(struct got_diffreg_result **resultp,
228 b72706c3 2022-06-01 stsp struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
229 b72706c3 2022-06-01 stsp FILE *f2, size_t size2, const char *label2, int diff_context,
230 b72706c3 2022-06-01 stsp int ignore_whitespace, int force_text_diff, FILE *outfile)
232 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
233 b72f483a 2019-02-05 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
234 b72f483a 2019-02-05 stsp char *idstr1 = NULL;
235 fe621944 2020-11-10 stsp struct got_diffreg_result *result = NULL;
237 fe621944 2020-11-10 stsp if (resultp)
238 fe621944 2020-11-10 stsp *resultp = NULL;
241 b72f483a 2019-02-05 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
243 b72f483a 2019-02-05 stsp idstr1 = "/dev/null";
245 7f1f93af 2019-08-06 stsp if (outfile) {
246 4ce46740 2019-08-08 stsp fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
247 7f1f93af 2019-08-06 stsp fprintf(outfile, "file + %s\n",
248 7f1f93af 2019-08-06 stsp f2 == NULL ? "/dev/null" : label2);
251 fe621944 2020-11-10 stsp err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
252 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
256 fe621944 2020-11-10 stsp if (outfile) {
257 1cb46f00 2020-11-21 stsp err = got_diffreg_output(NULL, NULL, result,
258 b72706c3 2022-06-01 stsp f1 != NULL, f2 != NULL,
259 1cb46f00 2020-11-21 stsp label2, /* show local file's path, not a blob ID */
260 1cb46f00 2020-11-21 stsp label2, GOT_DIFF_OUTPUT_UNIDIFF,
261 1cb46f00 2020-11-21 stsp diff_context, outfile);
266 fe621944 2020-11-10 stsp if (resultp && err == NULL)
267 fe621944 2020-11-10 stsp *resultp = result;
268 fe621944 2020-11-10 stsp else if (result) {
269 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
270 fe621944 2020-11-10 stsp if (free_err && err == NULL)
271 fe621944 2020-11-10 stsp err = free_err;
274 b72f483a 2019-02-05 stsp return err;
277 7f1f93af 2019-08-06 stsp const struct got_error *
278 b72706c3 2022-06-01 stsp got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
279 b72706c3 2022-06-01 stsp const char *label1, FILE *f2, size_t size2, const char *label2,
280 b72706c3 2022-06-01 stsp int diff_context, int ignore_whitespace, int force_text_diff, FILE *outfile)
282 b72706c3 2022-06-01 stsp return diff_blob_file(NULL, blob1, f1, size1, label1, f2, size2, label2,
283 64453f7e 2020-11-21 stsp diff_context, ignore_whitespace, force_text_diff, outfile);
286 474b4f94 2017-11-30 stsp static const struct got_error *
287 b72706c3 2022-06-01 stsp diff_added_blob(struct got_object_id *id, FILE *f, const char *label,
288 b72706c3 2022-06-01 stsp mode_t mode, struct got_repository *repo,
289 b72706c3 2022-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
291 4e22badc 2017-11-30 stsp const struct got_error *err;
292 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
293 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
295 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
297 4e22badc 2017-11-30 stsp return err;
299 2acfca77 2018-04-01 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
302 b72706c3 2022-06-01 stsp err = cb(cb_arg, NULL, blob, NULL, f, NULL, id,
303 b72706c3 2022-06-01 stsp NULL, label, 0, mode, repo);
305 2acfca77 2018-04-01 stsp got_object_close(obj);
307 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
308 2acfca77 2018-04-01 stsp return err;
311 474b4f94 2017-11-30 stsp static const struct got_error *
312 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
313 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, const char *label1, const char *label2,
314 b72706c3 2022-06-01 stsp mode_t mode1, mode_t mode2, struct got_repository *repo,
315 b72706c3 2022-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
317 6a213ccb 2017-11-30 stsp const struct got_error *err;
318 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
319 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
320 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
321 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
323 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
325 730a8aa0 2018-04-24 stsp return err;
326 15a94983 2018-12-23 stsp if (obj1->type != GOT_OBJ_TYPE_BLOB) {
327 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
331 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
334 15a94983 2018-12-23 stsp if (obj2->type != GOT_OBJ_TYPE_BLOB) {
335 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
339 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob1, repo, obj1, 8192);
343 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob2, repo, obj2, 8192);
347 b72706c3 2022-06-01 stsp err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
348 b72706c3 2022-06-01 stsp mode1, mode2, repo);
351 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
353 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
355 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
357 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
358 6a213ccb 2017-11-30 stsp return err;
361 474b4f94 2017-11-30 stsp static const struct got_error *
362 b72706c3 2022-06-01 stsp diff_deleted_blob(struct got_object_id *id, FILE *f, const char *label,
363 b72706c3 2022-06-01 stsp mode_t mode, struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
365 365fb436 2017-11-30 stsp const struct got_error *err;
366 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
367 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
369 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
371 365fb436 2017-11-30 stsp return err;
373 2acfca77 2018-04-01 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
376 b72706c3 2022-06-01 stsp err = cb(cb_arg, blob, NULL, f, NULL, id, NULL, label, NULL,
377 b72706c3 2022-06-01 stsp mode, 0, repo);
379 2acfca77 2018-04-01 stsp got_object_close(obj);
381 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
382 2acfca77 2018-04-01 stsp return err;
385 474b4f94 2017-11-30 stsp static const struct got_error *
386 b72706c3 2022-06-01 stsp diff_added_tree(struct got_object_id *id, FILE *f, const char *label,
387 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
388 31b4484f 2019-07-27 stsp int diff_content)
390 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
391 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
392 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
394 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
398 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
399 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
403 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
407 b72706c3 2022-06-01 stsp err = got_diff_tree(NULL, tree, NULL, f, NULL, label,
408 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
411 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
412 9c70d4c3 2017-11-30 stsp if (treeobj)
413 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
414 9c70d4c3 2017-11-30 stsp return err;
417 474b4f94 2017-11-30 stsp static const struct got_error *
418 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
419 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, const char *label1, const char *label2,
420 b72706c3 2022-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
421 b72706c3 2022-06-01 stsp int diff_content)
423 f6861a81 2018-09-13 stsp const struct got_error *err;
424 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
425 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
426 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
427 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
429 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
433 15a94983 2018-12-23 stsp if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
434 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
438 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
442 15a94983 2018-12-23 stsp if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
443 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
447 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
451 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
455 b72706c3 2022-06-01 stsp err = got_diff_tree(tree1, tree2, f1, f2, label1, label2,
456 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
460 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
462 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
463 789689b5 2017-11-30 stsp if (treeobj1)
464 789689b5 2017-11-30 stsp got_object_close(treeobj1);
465 789689b5 2017-11-30 stsp if (treeobj2)
466 789689b5 2017-11-30 stsp got_object_close(treeobj2);
467 789689b5 2017-11-30 stsp return err;
470 474b4f94 2017-11-30 stsp static const struct got_error *
471 b72706c3 2022-06-01 stsp diff_deleted_tree(struct got_object_id *id, FILE *f, const char *label,
472 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
473 31b4484f 2019-07-27 stsp int diff_content)
475 f6861a81 2018-09-13 stsp const struct got_error *err;
476 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
477 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
479 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
483 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
484 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
488 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
492 b72706c3 2022-06-01 stsp err = got_diff_tree(tree, NULL, f, NULL, label, NULL,
493 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
496 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
497 2c56f2ce 2017-11-30 stsp if (treeobj)
498 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
499 2c56f2ce 2017-11-30 stsp return err;
502 474b4f94 2017-11-30 stsp static const struct got_error *
503 74671950 2018-02-11 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
504 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
505 aaa13589 2019-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
507 013404a9 2017-11-30 stsp /* XXX TODO */
508 474b4f94 2017-11-30 stsp return NULL;
511 474b4f94 2017-11-30 stsp static const struct got_error *
512 b72706c3 2022-06-01 stsp diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
513 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, const char *label1, const char *label2,
514 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
515 31b4484f 2019-07-27 stsp int diff_content)
517 f6861a81 2018-09-13 stsp const struct got_error *err = NULL;
518 19ae6da1 2018-11-05 stsp int id_match;
520 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te1))
521 63c5ca5d 2019-08-24 stsp return NULL;
523 474b4f94 2017-11-30 stsp if (te2 == NULL) {
524 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
525 b72706c3 2022-06-01 stsp err = diff_deleted_tree(&te1->id, f1, label1, repo,
526 31b4484f 2019-07-27 stsp cb, cb_arg, diff_content);
528 31b4484f 2019-07-27 stsp if (diff_content)
529 b72706c3 2022-06-01 stsp err = diff_deleted_blob(&te1->id, f1, label1,
530 46f68b20 2019-10-19 stsp te1->mode, repo, cb, cb_arg);
532 b72706c3 2022-06-01 stsp err = cb(cb_arg, NULL, NULL, NULL, NULL,
533 b72706c3 2022-06-01 stsp &te1->id, NULL, label1, NULL,
534 b72706c3 2022-06-01 stsp te1->mode, 0, repo);
536 f6861a81 2018-09-13 stsp return err;
537 63c5ca5d 2019-08-24 stsp } else if (got_object_tree_entry_is_submodule(te2))
538 63c5ca5d 2019-08-24 stsp return NULL;
540 56e0773d 2019-11-28 stsp id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
541 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
542 19ae6da1 2018-11-05 stsp if (!id_match)
543 b72706c3 2022-06-01 stsp return diff_modified_tree(&te1->id, &te2->id, f1, f2,
544 31b4484f 2019-07-27 stsp label1, label2, repo, cb, cb_arg, diff_content);
545 40dde666 2020-07-23 stsp } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
546 40dde666 2020-07-23 stsp (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
547 46f68b20 2019-10-19 stsp if (!id_match ||
548 40dde666 2020-07-23 stsp ((te1->mode & (S_IFLNK | S_IXUSR))) !=
549 40dde666 2020-07-23 stsp (te2->mode & (S_IFLNK | S_IXUSR))) {
550 31b4484f 2019-07-27 stsp if (diff_content)
551 56e0773d 2019-11-28 stsp return diff_modified_blob(&te1->id, &te2->id,
552 b72706c3 2022-06-01 stsp f1, f2, label1, label2, te1->mode,
553 b72706c3 2022-06-01 stsp te2->mode, repo, cb, cb_arg);
555 b72706c3 2022-06-01 stsp return cb(cb_arg, NULL, NULL, NULL, NULL,
556 b72706c3 2022-06-01 stsp &te1->id, &te2->id, label1, label2,
557 b72706c3 2022-06-01 stsp te1->mode, te2->mode, repo);
561 19ae6da1 2018-11-05 stsp if (id_match)
562 f6861a81 2018-09-13 stsp return NULL;
564 56e0773d 2019-11-28 stsp return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
565 aaa13589 2019-06-01 stsp cb, cb_arg);
568 474b4f94 2017-11-30 stsp static const struct got_error *
569 56e0773d 2019-11-28 stsp diff_entry_new_old(struct got_tree_entry *te2,
570 b72706c3 2022-06-01 stsp struct got_tree_entry *te1, FILE *f2, const char *label2,
571 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
572 31b4484f 2019-07-27 stsp int diff_content)
574 f6861a81 2018-09-13 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
575 63c5ca5d 2019-08-24 stsp return NULL;
577 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te2))
578 f6861a81 2018-09-13 stsp return NULL;
580 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
581 b72706c3 2022-06-01 stsp return diff_added_tree(&te2->id, f2, label2,
582 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
584 31b4484f 2019-07-27 stsp if (diff_content)
585 b72706c3 2022-06-01 stsp return diff_added_blob(&te2->id, f2, label2, te2->mode,
586 b72706c3 2022-06-01 stsp repo, cb, cb_arg);
588 b72706c3 2022-06-01 stsp return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
589 b72706c3 2022-06-01 stsp NULL, label2, 0, te2->mode, repo);
592 0208f208 2020-05-05 stsp const struct got_error *
593 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
594 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
595 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
596 b72706c3 2022-06-01 stsp const char *label1, const char *label2,
597 0208f208 2020-05-05 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
599 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
600 0208f208 2020-05-05 stsp struct got_pathlist_head *paths = arg;
601 0208f208 2020-05-05 stsp struct got_diff_changed_path *change = NULL;
602 0208f208 2020-05-05 stsp char *path = NULL;
604 0208f208 2020-05-05 stsp path = strdup(label2 ? label2 : label1);
605 0208f208 2020-05-05 stsp if (path == NULL)
606 0208f208 2020-05-05 stsp return got_error_from_errno("malloc");
608 0208f208 2020-05-05 stsp change = malloc(sizeof(*change));
609 0208f208 2020-05-05 stsp if (change == NULL) {
610 0208f208 2020-05-05 stsp err = got_error_from_errno("malloc");
614 0208f208 2020-05-05 stsp change->status = GOT_STATUS_NO_CHANGE;
615 0208f208 2020-05-05 stsp if (id1 == NULL)
616 0208f208 2020-05-05 stsp change->status = GOT_STATUS_ADD;
617 0208f208 2020-05-05 stsp else if (id2 == NULL)
618 0208f208 2020-05-05 stsp change->status = GOT_STATUS_DELETE;
620 0208f208 2020-05-05 stsp if (got_object_id_cmp(id1, id2) != 0)
621 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODIFY;
622 0208f208 2020-05-05 stsp else if (mode1 != mode2)
623 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODE_CHANGE;
626 46ea77db 2021-12-20 stsp err = got_pathlist_append(paths, path, change);
629 0208f208 2020-05-05 stsp free(path);
630 0208f208 2020-05-05 stsp free(change);
632 0208f208 2020-05-05 stsp return err;
635 474b4f94 2017-11-30 stsp const struct got_error *
636 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
637 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, 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 b72706c3 2022-06-01 stsp err = diff_entry_old_new(te1, te, f1, f2, l1, l2,
675 b72706c3 2022-06-01 stsp 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 b72706c3 2022-06-01 stsp err = diff_entry_new_old(te2, te, f2, l2, repo,
698 31b4484f 2019-07-27 stsp 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 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
731 54156555 2018-12-24 stsp const char *label1, const char *label2, int diff_context,
732 64453f7e 2020-11-21 stsp int ignore_whitespace, int force_text_diff,
733 64453f7e 2020-11-21 stsp struct got_repository *repo, FILE *outfile)
735 11528a82 2018-05-19 stsp const struct got_error *err;
736 11528a82 2018-05-19 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
738 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
739 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
742 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob1, repo, id1, 8192);
747 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob2, repo, id2, 8192);
751 b72706c3 2022-06-01 stsp err = got_diff_blob(line_offsets, nlines, blob1, blob2, f1, f2,
752 64453f7e 2020-11-21 stsp label1, label2, diff_context, ignore_whitespace, force_text_diff,
756 67b631c9 2021-10-10 stsp got_object_blob_close(blob1);
758 67b631c9 2021-10-10 stsp got_object_blob_close(blob2);
759 67b631c9 2021-10-10 stsp return err;
762 67b631c9 2021-10-10 stsp static const struct got_error *
763 67b631c9 2021-10-10 stsp diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
764 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, struct got_pathlist_head *paths,
765 b72706c3 2022-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
767 67b631c9 2021-10-10 stsp const struct got_error *err = NULL;
768 67b631c9 2021-10-10 stsp struct got_pathlist_entry *pe;
769 67b631c9 2021-10-10 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
770 67b631c9 2021-10-10 stsp struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
771 67b631c9 2021-10-10 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
773 67b631c9 2021-10-10 stsp TAILQ_FOREACH(pe, paths, entry) {
774 67b631c9 2021-10-10 stsp int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
775 67b631c9 2021-10-10 stsp mode_t mode1 = 0, mode2 = 0;
778 67b631c9 2021-10-10 stsp id1 = NULL;
780 67b631c9 2021-10-10 stsp id2 = NULL;
781 67b631c9 2021-10-10 stsp if (subtree1) {
782 67b631c9 2021-10-10 stsp got_object_tree_close(subtree1);
783 67b631c9 2021-10-10 stsp subtree1 = NULL;
785 67b631c9 2021-10-10 stsp if (subtree2) {
786 67b631c9 2021-10-10 stsp got_object_tree_close(subtree2);
787 67b631c9 2021-10-10 stsp subtree2 = NULL;
789 67b631c9 2021-10-10 stsp if (blob1) {
790 67b631c9 2021-10-10 stsp got_object_blob_close(blob1);
791 67b631c9 2021-10-10 stsp blob1 = NULL;
793 67b631c9 2021-10-10 stsp if (blob2) {
794 67b631c9 2021-10-10 stsp got_object_blob_close(blob2);
795 67b631c9 2021-10-10 stsp blob2 = NULL;
798 67b631c9 2021-10-10 stsp err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
800 67b631c9 2021-10-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
802 67b631c9 2021-10-10 stsp err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
804 67b631c9 2021-10-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
806 67b631c9 2021-10-10 stsp if (id1 == NULL && id2 == NULL) {
807 67b631c9 2021-10-10 stsp err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
811 67b631c9 2021-10-10 stsp err = got_object_get_type(&type1, repo, id1);
816 67b631c9 2021-10-10 stsp err = got_object_get_type(&type2, repo, id2);
820 67b631c9 2021-10-10 stsp if (type1 == GOT_OBJ_TYPE_ANY &&
821 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_ANY) {
822 67b631c9 2021-10-10 stsp err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
824 67b631c9 2021-10-10 stsp } else if (type1 != GOT_OBJ_TYPE_ANY &&
825 67b631c9 2021-10-10 stsp type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
826 67b631c9 2021-10-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
830 67b631c9 2021-10-10 stsp if (type1 == GOT_OBJ_TYPE_BLOB ||
831 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_BLOB) {
833 67b631c9 2021-10-10 stsp err = got_object_open_as_blob(&blob1, repo,
834 67b631c9 2021-10-10 stsp id1, 8192);
839 67b631c9 2021-10-10 stsp err = got_object_open_as_blob(&blob2, repo,
840 67b631c9 2021-10-10 stsp id2, 8192);
844 b72706c3 2022-06-01 stsp err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
845 67b631c9 2021-10-10 stsp id1 ? pe->path : "/dev/null",
846 67b631c9 2021-10-10 stsp id2 ? pe->path : "/dev/null",
847 67b631c9 2021-10-10 stsp mode1, mode2, repo);
850 67b631c9 2021-10-10 stsp } else if (type1 == GOT_OBJ_TYPE_TREE ||
851 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_TREE) {
853 67b631c9 2021-10-10 stsp err = got_object_open_as_tree(&subtree1, repo,
859 67b631c9 2021-10-10 stsp err = got_object_open_as_tree(&subtree2, repo,
864 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 11528a82 2018-05-19 stsp const struct got_error *
890 fe621944 2020-11-10 stsp got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
891 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
892 67b631c9 2021-10-10 stsp struct got_pathlist_head *paths,
893 63035f9f 2019-10-06 stsp char *label1, char *label2, int diff_context, int ignore_whitespace,
894 64453f7e 2020-11-21 stsp int force_text_diff, struct got_repository *repo, FILE *outfile)
896 11528a82 2018-05-19 stsp const struct got_error *err;
897 11528a82 2018-05-19 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
898 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
899 fe621944 2020-11-10 stsp int want_lineoffsets = (line_offsets != NULL && *line_offsets != NULL);
901 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
902 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
905 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo, id1);
910 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo, id2);
915 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
916 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
917 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
918 aaa13589 2019-06-01 stsp arg.outfile = outfile;
919 fe621944 2020-11-10 stsp if (want_lineoffsets) {
920 fe621944 2020-11-10 stsp arg.line_offsets = *line_offsets;
921 fe621944 2020-11-10 stsp arg.nlines = *nlines;
923 fe621944 2020-11-10 stsp arg.line_offsets = NULL;
924 fe621944 2020-11-10 stsp arg.nlines = 0;
926 67b631c9 2021-10-10 stsp if (paths == NULL || TAILQ_EMPTY(paths)) {
927 b72706c3 2022-06-01 stsp err = got_diff_tree(tree1, tree2, f1, f2, label1, label2,
928 b72706c3 2022-06-01 stsp repo, got_diff_blob_output_unidiff, &arg, 1);
930 b72706c3 2022-06-01 stsp err = diff_paths(tree1, tree2, f1, f2, paths, repo,
931 67b631c9 2021-10-10 stsp got_diff_blob_output_unidiff, &arg);
933 fe621944 2020-11-10 stsp if (want_lineoffsets) {
934 fe621944 2020-11-10 stsp *line_offsets = arg.line_offsets; /* was likely re-allocated */
935 fe621944 2020-11-10 stsp *nlines = arg.nlines;
939 11528a82 2018-05-19 stsp got_object_tree_close(tree1);
941 11528a82 2018-05-19 stsp got_object_tree_close(tree2);
942 11528a82 2018-05-19 stsp return err;
945 11528a82 2018-05-19 stsp const struct got_error *
946 fe621944 2020-11-10 stsp got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
947 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
948 67b631c9 2021-10-10 stsp struct got_pathlist_head *paths,
949 64453f7e 2020-11-21 stsp int diff_context, int ignore_whitespace, int force_text_diff,
950 15a94983 2018-12-23 stsp struct got_repository *repo, FILE *outfile)
952 11528a82 2018-05-19 stsp const struct got_error *err;
953 11528a82 2018-05-19 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
955 15a94983 2018-12-23 stsp if (id2 == NULL)
956 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
959 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit1, repo, id1);
964 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, repo, id2);
968 b72706c3 2022-06-01 stsp err = got_diff_objects_as_trees(line_offsets, nlines, f1, f2,
969 15a94983 2018-12-23 stsp commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
970 67b631c9 2021-10-10 stsp got_object_commit_get_tree_id(commit2), paths, "", "",
971 67b631c9 2021-10-10 stsp diff_context, ignore_whitespace, force_text_diff, repo, outfile);
973 11528a82 2018-05-19 stsp if (commit1)
974 11528a82 2018-05-19 stsp got_object_commit_close(commit1);
975 11528a82 2018-05-19 stsp if (commit2)
976 11528a82 2018-05-19 stsp got_object_commit_close(commit2);
977 11528a82 2018-05-19 stsp return err;
980 dc424a06 2019-08-07 stsp const struct got_error *
981 fe621944 2020-11-10 stsp got_diff_files(struct got_diffreg_result **resultp,
982 fe621944 2020-11-10 stsp FILE *f1, const char *label1, FILE *f2, const char *label2,
983 64453f7e 2020-11-21 stsp int diff_context, int ignore_whitespace, int force_text_diff,
984 64453f7e 2020-11-21 stsp FILE *outfile)
986 dc424a06 2019-08-07 stsp const struct got_error *err = NULL;
987 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
989 fe621944 2020-11-10 stsp if (resultp)
990 fe621944 2020-11-10 stsp *resultp = NULL;
992 dc424a06 2019-08-07 stsp if (outfile) {
993 dc424a06 2019-08-07 stsp fprintf(outfile, "file - %s\n",
994 dc424a06 2019-08-07 stsp f1 == NULL ? "/dev/null" : label1);
995 dc424a06 2019-08-07 stsp fprintf(outfile, "file + %s\n",
996 dc424a06 2019-08-07 stsp f2 == NULL ? "/dev/null" : label2);
999 fe621944 2020-11-10 stsp err = got_diffreg(&diffreg_result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
1000 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
1002 fe621944 2020-11-10 stsp goto done;
1004 fe621944 2020-11-10 stsp if (outfile) {
1005 fe621944 2020-11-10 stsp err = got_diffreg_output(NULL, NULL, diffreg_result,
1006 1cb46f00 2020-11-21 stsp f1 != NULL, f2 != NULL, label1, label2,
1007 1cb46f00 2020-11-21 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1009 dc424a06 2019-08-07 stsp goto done;
1013 fe621944 2020-11-10 stsp if (resultp && err == NULL)
1014 fe621944 2020-11-10 stsp *resultp = diffreg_result;
1015 fe621944 2020-11-10 stsp else if (diffreg_result) {
1016 fe621944 2020-11-10 stsp const struct got_error *free_err;
1017 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
1018 fe621944 2020-11-10 stsp if (free_err && err == NULL)
1019 fe621944 2020-11-10 stsp err = free_err;
1022 dc424a06 2019-08-07 stsp return err;