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 511a516b 2018-05-19 stsp #include "got_opentemp.h"
32 324d37e7 2019-05-11 stsp #include "got_path.h"
33 0208f208 2020-05-05 stsp #include "got_cancel.h"
34 0208f208 2020-05-05 stsp #include "got_worktree.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 fe621944 2020-11-10 stsp struct got_blob_object *blob2,
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 4e22badc 2017-11-30 stsp FILE *f1 = NULL, *f2 = NULL;
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 98abbc84 2017-11-30 stsp 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;
83 4e22badc 2017-11-30 stsp if (blob1) {
84 a1fd68d8 2018-01-12 stsp f1 = got_opentemp();
85 4e22badc 2017-11-30 stsp if (f1 == NULL)
86 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentemp");
89 4e22badc 2017-11-30 stsp if (blob2) {
90 a1fd68d8 2018-01-12 stsp f2 = got_opentemp();
91 4e22badc 2017-11-30 stsp if (f2 == NULL) {
92 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
100 98abbc84 2017-11-30 stsp if (blob1) {
101 f934cf2c 2018-02-12 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
102 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
107 98abbc84 2017-11-30 stsp idstr1 = "/dev/null";
110 98abbc84 2017-11-30 stsp if (blob2) {
111 f934cf2c 2018-02-12 stsp idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
112 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
117 98abbc84 2017-11-30 stsp idstr2 = "/dev/null";
119 09de383e 2018-12-24 stsp if (outfile) {
120 46f68b20 2019-10-19 stsp char *modestr1 = NULL, *modestr2 = NULL;
121 40dde666 2020-07-23 stsp int modebits;
122 46f68b20 2019-10-19 stsp if (mode1 && mode1 != mode2) {
123 40dde666 2020-07-23 stsp if (S_ISLNK(mode1))
124 40dde666 2020-07-23 stsp modebits = S_IFLNK;
126 40dde666 2020-07-23 stsp modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
127 46f68b20 2019-10-19 stsp if (asprintf(&modestr1, " (mode %o)",
128 40dde666 2020-07-23 stsp mode1 & modebits) == -1) {
129 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
133 46f68b20 2019-10-19 stsp if (mode2 && mode1 != mode2) {
134 40dde666 2020-07-23 stsp if (S_ISLNK(mode2))
135 40dde666 2020-07-23 stsp modebits = S_IFLNK;
137 40dde666 2020-07-23 stsp modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
138 46f68b20 2019-10-19 stsp if (asprintf(&modestr2, " (mode %o)",
139 40dde666 2020-07-23 stsp mode2 & modebits) == -1) {
140 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
144 fe621944 2020-11-10 stsp n = fprintf(outfile, "blob - %s%s\n", idstr1,
145 46f68b20 2019-10-19 stsp modestr1 ? modestr1 : "");
148 fe621944 2020-11-10 stsp outoff += n;
149 fe621944 2020-11-10 stsp if (line_offsets) {
150 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
155 fe621944 2020-11-10 stsp n = fprintf(outfile, "blob + %s%s\n", idstr2,
156 46f68b20 2019-10-19 stsp modestr2 ? modestr2 : "");
159 fe621944 2020-11-10 stsp outoff += n;
160 fe621944 2020-11-10 stsp if (line_offsets) {
161 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
166 46f68b20 2019-10-19 stsp free(modestr1);
167 46f68b20 2019-10-19 stsp free(modestr2);
169 fe621944 2020-11-10 stsp err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
170 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
174 fe621944 2020-11-10 stsp if (outfile) {
175 1cb46f00 2020-11-21 stsp err = got_diffreg_output(line_offsets, nlines, result,
176 1cb46f00 2020-11-21 stsp blob1 != NULL, blob2 != NULL,
177 fe621944 2020-11-10 stsp label1 ? label1 : idstr1,
178 fe621944 2020-11-10 stsp label2 ? label2 : idstr2,
179 fe621944 2020-11-10 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
184 fe621944 2020-11-10 stsp if (resultp && err == NULL)
185 fe621944 2020-11-10 stsp *resultp = result;
187 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
188 fe621944 2020-11-10 stsp if (free_err && err == NULL)
189 fe621944 2020-11-10 stsp err = free_err;
192 56b63ca4 2021-01-22 stsp if (f1 && fclose(f1) == EOF && err == NULL)
193 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
194 56b63ca4 2021-01-22 stsp if (f2 && fclose(f2) == EOF && err == NULL)
195 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
196 7d283eee 2017-11-29 stsp return err;
199 aaa13589 2019-06-01 stsp const struct got_error *
200 aaa13589 2019-06-01 stsp got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
201 aaa13589 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
202 aaa13589 2019-06-01 stsp struct got_object_id *id2, const char *label1, const char *label2,
203 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
205 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg *a = arg;
207 fe621944 2020-11-10 stsp return diff_blobs(&a->line_offsets, &a->nlines, NULL,
208 fe621944 2020-11-10 stsp blob1, blob2, label1, label2, mode1, mode2, a->diff_context,
209 64453f7e 2020-11-21 stsp a->ignore_whitespace, a->force_text_diff, a->outfile);
212 404c43c4 2018-06-21 stsp const struct got_error *
213 fe621944 2020-11-10 stsp got_diff_blob(off_t **line_offsets, size_t *nlines,
214 fe621944 2020-11-10 stsp struct got_blob_object *blob1, struct got_blob_object *blob2,
215 63035f9f 2019-10-06 stsp const char *label1, const char *label2, int diff_context,
216 64453f7e 2020-11-21 stsp int ignore_whitespace, int force_text_diff, FILE *outfile)
218 fe621944 2020-11-10 stsp return diff_blobs(line_offsets, nlines, NULL, blob1, blob2,
219 64453f7e 2020-11-21 stsp label1, label2, 0, 0, diff_context, ignore_whitespace,
220 64453f7e 2020-11-21 stsp force_text_diff, outfile);
223 7f1f93af 2019-08-06 stsp static const struct got_error *
224 fe621944 2020-11-10 stsp diff_blob_file(struct got_diffreg_result **resultp,
225 4ce46740 2019-08-08 stsp struct got_blob_object *blob1, const char *label1, FILE *f2, size_t size2,
226 64453f7e 2020-11-21 stsp const char *label2, int diff_context, int ignore_whitespace,
227 64453f7e 2020-11-21 stsp int force_text_diff, FILE *outfile)
229 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
230 b72f483a 2019-02-05 stsp FILE *f1 = NULL;
231 b72f483a 2019-02-05 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
232 b72f483a 2019-02-05 stsp char *idstr1 = NULL;
233 be659d10 2020-11-18 stsp off_t size1;
234 fe621944 2020-11-10 stsp struct got_diffreg_result *result = NULL;
236 fe621944 2020-11-10 stsp if (resultp)
237 fe621944 2020-11-10 stsp *resultp = NULL;
240 b72f483a 2019-02-05 stsp if (blob1) {
241 b72f483a 2019-02-05 stsp f1 = got_opentemp();
242 b72f483a 2019-02-05 stsp if (f1 == NULL)
243 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentemp");
244 b72f483a 2019-02-05 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
245 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
250 b72f483a 2019-02-05 stsp idstr1 = "/dev/null";
253 7f1f93af 2019-08-06 stsp if (outfile) {
254 4ce46740 2019-08-08 stsp fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
255 7f1f93af 2019-08-06 stsp fprintf(outfile, "file + %s\n",
256 7f1f93af 2019-08-06 stsp f2 == NULL ? "/dev/null" : label2);
259 fe621944 2020-11-10 stsp err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
260 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
264 fe621944 2020-11-10 stsp if (outfile) {
265 1cb46f00 2020-11-21 stsp err = got_diffreg_output(NULL, NULL, result,
266 1cb46f00 2020-11-21 stsp blob1 != NULL, f2 != NULL,
267 1cb46f00 2020-11-21 stsp label2, /* show local file's path, not a blob ID */
268 1cb46f00 2020-11-21 stsp label2, GOT_DIFF_OUTPUT_UNIDIFF,
269 1cb46f00 2020-11-21 stsp diff_context, outfile);
274 fe621944 2020-11-10 stsp if (resultp && err == NULL)
275 fe621944 2020-11-10 stsp *resultp = result;
276 fe621944 2020-11-10 stsp else if (result) {
277 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
278 fe621944 2020-11-10 stsp if (free_err && err == NULL)
279 fe621944 2020-11-10 stsp err = free_err;
282 56b63ca4 2021-01-22 stsp if (f1 && fclose(f1) == EOF && err == NULL)
283 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
284 b72f483a 2019-02-05 stsp return err;
287 7f1f93af 2019-08-06 stsp const struct got_error *
288 4ce46740 2019-08-08 stsp got_diff_blob_file(struct got_blob_object *blob1, const char *label1,
289 4ce46740 2019-08-08 stsp FILE *f2, size_t size2, const char *label2, int diff_context,
290 64453f7e 2020-11-21 stsp int ignore_whitespace, int force_text_diff, FILE *outfile)
292 4ce46740 2019-08-08 stsp return diff_blob_file(NULL, blob1, label1, f2, size2, label2,
293 64453f7e 2020-11-21 stsp diff_context, ignore_whitespace, force_text_diff, outfile);
296 474b4f94 2017-11-30 stsp static const struct got_error *
297 46f68b20 2019-10-19 stsp diff_added_blob(struct got_object_id *id, const char *label, mode_t mode,
298 aaa13589 2019-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
300 4e22badc 2017-11-30 stsp const struct got_error *err;
301 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
302 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
304 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
306 4e22badc 2017-11-30 stsp return err;
308 2acfca77 2018-04-01 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
311 46f68b20 2019-10-19 stsp err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, 0, mode, repo);
313 2acfca77 2018-04-01 stsp got_object_close(obj);
315 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
316 2acfca77 2018-04-01 stsp return err;
319 474b4f94 2017-11-30 stsp static const struct got_error *
320 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
321 46f68b20 2019-10-19 stsp const char *label1, const char *label2, mode_t mode1, mode_t mode2,
322 46f68b20 2019-10-19 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
324 6a213ccb 2017-11-30 stsp const struct got_error *err;
325 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
326 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
327 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
328 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
330 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
332 730a8aa0 2018-04-24 stsp return err;
333 15a94983 2018-12-23 stsp if (obj1->type != GOT_OBJ_TYPE_BLOB) {
334 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
338 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
341 15a94983 2018-12-23 stsp if (obj2->type != GOT_OBJ_TYPE_BLOB) {
342 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
346 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob1, repo, obj1, 8192);
350 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob2, repo, obj2, 8192);
354 46f68b20 2019-10-19 stsp err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, mode1, mode2,
358 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
360 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
362 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
364 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
365 6a213ccb 2017-11-30 stsp return err;
368 474b4f94 2017-11-30 stsp static const struct got_error *
369 46f68b20 2019-10-19 stsp diff_deleted_blob(struct got_object_id *id, const char *label, mode_t mode,
370 aaa13589 2019-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
372 365fb436 2017-11-30 stsp const struct got_error *err;
373 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
374 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
376 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
378 365fb436 2017-11-30 stsp return err;
380 2acfca77 2018-04-01 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
383 46f68b20 2019-10-19 stsp err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, mode, 0, repo);
385 2acfca77 2018-04-01 stsp got_object_close(obj);
387 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
388 2acfca77 2018-04-01 stsp return err;
391 474b4f94 2017-11-30 stsp static const struct got_error *
392 54156555 2018-12-24 stsp diff_added_tree(struct got_object_id *id, const char *label,
393 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
394 31b4484f 2019-07-27 stsp int diff_content)
396 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
397 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
398 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
400 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
404 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
405 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
409 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
413 31b4484f 2019-07-27 stsp err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
414 31b4484f 2019-07-27 stsp diff_content);
417 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
418 9c70d4c3 2017-11-30 stsp if (treeobj)
419 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
420 9c70d4c3 2017-11-30 stsp return err;
423 474b4f94 2017-11-30 stsp static const struct got_error *
424 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
425 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
426 31b4484f 2019-07-27 stsp got_diff_blob_cb cb, void *cb_arg, int diff_content)
428 f6861a81 2018-09-13 stsp const struct got_error *err;
429 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
430 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
431 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
432 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
434 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
438 15a94983 2018-12-23 stsp if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
439 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
443 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
447 15a94983 2018-12-23 stsp if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
448 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
452 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
456 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
460 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
461 31b4484f 2019-07-27 stsp diff_content);
465 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
467 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
468 789689b5 2017-11-30 stsp if (treeobj1)
469 789689b5 2017-11-30 stsp got_object_close(treeobj1);
470 789689b5 2017-11-30 stsp if (treeobj2)
471 789689b5 2017-11-30 stsp got_object_close(treeobj2);
472 789689b5 2017-11-30 stsp return err;
475 474b4f94 2017-11-30 stsp static const struct got_error *
476 54156555 2018-12-24 stsp diff_deleted_tree(struct got_object_id *id, const char *label,
477 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
478 31b4484f 2019-07-27 stsp int diff_content)
480 f6861a81 2018-09-13 stsp const struct got_error *err;
481 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
482 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
484 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
488 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
489 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
493 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
497 31b4484f 2019-07-27 stsp err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
498 31b4484f 2019-07-27 stsp diff_content);
501 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
502 2c56f2ce 2017-11-30 stsp if (treeobj)
503 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
504 2c56f2ce 2017-11-30 stsp return err;
507 474b4f94 2017-11-30 stsp static const struct got_error *
508 74671950 2018-02-11 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
509 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
510 aaa13589 2019-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
512 013404a9 2017-11-30 stsp /* XXX TODO */
513 474b4f94 2017-11-30 stsp return NULL;
516 474b4f94 2017-11-30 stsp static const struct got_error *
517 56e0773d 2019-11-28 stsp diff_entry_old_new(struct got_tree_entry *te1,
518 56e0773d 2019-11-28 stsp struct got_tree_entry *te2, const char *label1, const char *label2,
519 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
520 31b4484f 2019-07-27 stsp int diff_content)
522 f6861a81 2018-09-13 stsp const struct got_error *err = NULL;
523 19ae6da1 2018-11-05 stsp int id_match;
525 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te1))
526 63c5ca5d 2019-08-24 stsp return NULL;
528 474b4f94 2017-11-30 stsp if (te2 == NULL) {
529 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
530 56e0773d 2019-11-28 stsp err = diff_deleted_tree(&te1->id, label1, repo,
531 31b4484f 2019-07-27 stsp cb, cb_arg, diff_content);
533 31b4484f 2019-07-27 stsp if (diff_content)
534 56e0773d 2019-11-28 stsp err = diff_deleted_blob(&te1->id, label1,
535 46f68b20 2019-10-19 stsp te1->mode, repo, cb, cb_arg);
537 56e0773d 2019-11-28 stsp err = cb(cb_arg, NULL, NULL, &te1->id, NULL,
538 46f68b20 2019-10-19 stsp label1, NULL, te1->mode, 0, repo);
540 f6861a81 2018-09-13 stsp return err;
541 63c5ca5d 2019-08-24 stsp } else if (got_object_tree_entry_is_submodule(te2))
542 63c5ca5d 2019-08-24 stsp return NULL;
544 56e0773d 2019-11-28 stsp id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
545 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
546 19ae6da1 2018-11-05 stsp if (!id_match)
547 56e0773d 2019-11-28 stsp return diff_modified_tree(&te1->id, &te2->id,
548 31b4484f 2019-07-27 stsp label1, label2, repo, cb, cb_arg, diff_content);
549 40dde666 2020-07-23 stsp } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
550 40dde666 2020-07-23 stsp (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
551 46f68b20 2019-10-19 stsp if (!id_match ||
552 40dde666 2020-07-23 stsp ((te1->mode & (S_IFLNK | S_IXUSR))) !=
553 40dde666 2020-07-23 stsp (te2->mode & (S_IFLNK | S_IXUSR))) {
554 31b4484f 2019-07-27 stsp if (diff_content)
555 56e0773d 2019-11-28 stsp return diff_modified_blob(&te1->id, &te2->id,
556 46f68b20 2019-10-19 stsp label1, label2, te1->mode, te2->mode,
557 46f68b20 2019-10-19 stsp repo, cb, cb_arg);
559 56e0773d 2019-11-28 stsp return cb(cb_arg, NULL, NULL, &te1->id,
560 56e0773d 2019-11-28 stsp &te2->id, label1, label2, te1->mode,
561 46f68b20 2019-10-19 stsp te2->mode, repo);
565 19ae6da1 2018-11-05 stsp if (id_match)
566 f6861a81 2018-09-13 stsp return NULL;
568 56e0773d 2019-11-28 stsp return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
569 aaa13589 2019-06-01 stsp cb, cb_arg);
572 474b4f94 2017-11-30 stsp static const struct got_error *
573 56e0773d 2019-11-28 stsp diff_entry_new_old(struct got_tree_entry *te2,
574 56e0773d 2019-11-28 stsp struct got_tree_entry *te1, const char *label2,
575 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
576 31b4484f 2019-07-27 stsp int diff_content)
578 f6861a81 2018-09-13 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
579 63c5ca5d 2019-08-24 stsp return NULL;
581 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te2))
582 f6861a81 2018-09-13 stsp return NULL;
584 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
585 56e0773d 2019-11-28 stsp return diff_added_tree(&te2->id, label2, repo, cb, cb_arg,
586 31b4484f 2019-07-27 stsp diff_content);
588 31b4484f 2019-07-27 stsp if (diff_content)
589 56e0773d 2019-11-28 stsp return diff_added_blob(&te2->id, label2, te2->mode, repo, cb,
592 56e0773d 2019-11-28 stsp return cb(cb_arg, NULL, NULL, NULL, &te2->id, NULL, label2, 0,
593 46f68b20 2019-10-19 stsp te2->mode, repo);
596 0208f208 2020-05-05 stsp const struct got_error *
597 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
598 0208f208 2020-05-05 stsp struct got_blob_object *blob2, struct got_object_id *id1,
599 0208f208 2020-05-05 stsp struct got_object_id *id2, const char *label1, const char *label2,
600 0208f208 2020-05-05 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
602 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
603 0208f208 2020-05-05 stsp struct got_pathlist_head *paths = arg;
604 0208f208 2020-05-05 stsp struct got_diff_changed_path *change = NULL;
605 0208f208 2020-05-05 stsp char *path = NULL;
607 0208f208 2020-05-05 stsp path = strdup(label2 ? label2 : label1);
608 0208f208 2020-05-05 stsp if (path == NULL)
609 0208f208 2020-05-05 stsp return got_error_from_errno("malloc");
611 0208f208 2020-05-05 stsp change = malloc(sizeof(*change));
612 0208f208 2020-05-05 stsp if (change == NULL) {
613 0208f208 2020-05-05 stsp err = got_error_from_errno("malloc");
617 0208f208 2020-05-05 stsp change->status = GOT_STATUS_NO_CHANGE;
618 0208f208 2020-05-05 stsp if (id1 == NULL)
619 0208f208 2020-05-05 stsp change->status = GOT_STATUS_ADD;
620 0208f208 2020-05-05 stsp else if (id2 == NULL)
621 0208f208 2020-05-05 stsp change->status = GOT_STATUS_DELETE;
623 0208f208 2020-05-05 stsp if (got_object_id_cmp(id1, id2) != 0)
624 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODIFY;
625 0208f208 2020-05-05 stsp else if (mode1 != mode2)
626 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODE_CHANGE;
629 46ea77db 2021-12-20 stsp err = got_pathlist_append(paths, path, change);
632 0208f208 2020-05-05 stsp free(path);
633 0208f208 2020-05-05 stsp free(change);
635 0208f208 2020-05-05 stsp return err;
638 474b4f94 2017-11-30 stsp const struct got_error *
639 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
640 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
641 31b4484f 2019-07-27 stsp got_diff_blob_cb cb, void *cb_arg, int diff_content)
643 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
644 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
645 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
646 f6861a81 2018-09-13 stsp char *l1 = NULL, *l2 = NULL;
647 56e0773d 2019-11-28 stsp int tidx1 = 0, tidx2 = 0;
649 883f0469 2018-06-23 stsp if (tree1) {
650 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, 0);
651 60f50a58 2018-09-15 stsp if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
652 f6861a81 2018-09-13 stsp te1->name) == -1)
653 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
655 883f0469 2018-06-23 stsp if (tree2) {
656 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, 0);
657 60f50a58 2018-09-15 stsp if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
658 f6861a81 2018-09-13 stsp te2->name) == -1)
659 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
664 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
666 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree2,
667 1de5e065 2019-06-01 stsp te1->name);
671 f6861a81 2018-09-13 stsp if (te && asprintf(&l2, "%s%s%s", label2,
672 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te->name) == -1)
674 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
676 aaa13589 2019-06-01 stsp err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
677 31b4484f 2019-07-27 stsp cb_arg, diff_content);
683 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
685 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree1,
686 1de5e065 2019-06-01 stsp te2->name);
689 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
690 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te->name) == -1)
692 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
694 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
695 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te2->name) == -1)
697 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
699 31b4484f 2019-07-27 stsp err = diff_entry_new_old(te2, te, l2, repo,
700 31b4484f 2019-07-27 stsp cb, cb_arg, diff_content);
709 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, tidx1);
711 f6861a81 2018-09-13 stsp asprintf(&l1, "%s%s%s", label1,
712 f6861a81 2018-09-13 stsp label1[0] ? "/" : "", te1->name) == -1)
713 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
719 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, tidx2);
721 f6861a81 2018-09-13 stsp asprintf(&l2, "%s%s%s", label2,
722 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te2->name) == -1)
723 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
725 474b4f94 2017-11-30 stsp } while (te1 || te2);
727 11528a82 2018-05-19 stsp return err;
730 11528a82 2018-05-19 stsp const struct got_error *
731 fe621944 2020-11-10 stsp got_diff_objects_as_blobs(off_t **line_offsets, size_t *nlines,
732 fe621944 2020-11-10 stsp struct got_object_id *id1, struct got_object_id *id2,
733 54156555 2018-12-24 stsp const char *label1, const char *label2, 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 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob1, repo, id1, 8192);
749 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob2, repo, id2, 8192);
753 fe621944 2020-11-10 stsp err = got_diff_blob(line_offsets, nlines, blob1, blob2,
754 64453f7e 2020-11-21 stsp label1, label2, diff_context, ignore_whitespace, force_text_diff,
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 67b631c9 2021-10-10 stsp struct got_pathlist_head *paths, struct got_repository *repo,
767 67b631c9 2021-10-10 stsp 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 67b631c9 2021-10-10 stsp id1, 8192);
841 67b631c9 2021-10-10 stsp err = got_object_open_as_blob(&blob2, repo,
842 67b631c9 2021-10-10 stsp id2, 8192);
846 67b631c9 2021-10-10 stsp err = cb(cb_arg, blob1, blob2, 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 67b631c9 2021-10-10 stsp err = got_diff_tree(subtree1, subtree2,
867 67b631c9 2021-10-10 stsp id1 ? pe->path : "/dev/null",
868 67b631c9 2021-10-10 stsp id2 ? pe->path : "/dev/null",
869 67b631c9 2021-10-10 stsp repo, cb, cb_arg, 1);
873 67b631c9 2021-10-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
880 67b631c9 2021-10-10 stsp if (subtree1)
881 67b631c9 2021-10-10 stsp got_object_tree_close(subtree1);
882 67b631c9 2021-10-10 stsp if (subtree2)
883 67b631c9 2021-10-10 stsp got_object_tree_close(subtree2);
885 11528a82 2018-05-19 stsp got_object_blob_close(blob1);
887 11528a82 2018-05-19 stsp got_object_blob_close(blob2);
888 474b4f94 2017-11-30 stsp return err;
891 11528a82 2018-05-19 stsp const struct got_error *
892 fe621944 2020-11-10 stsp got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
893 fe621944 2020-11-10 stsp struct got_object_id *id1, struct got_object_id *id2,
894 67b631c9 2021-10-10 stsp struct got_pathlist_head *paths,
895 63035f9f 2019-10-06 stsp char *label1, char *label2, int diff_context, int ignore_whitespace,
896 64453f7e 2020-11-21 stsp int force_text_diff, struct got_repository *repo, FILE *outfile)
898 11528a82 2018-05-19 stsp const struct got_error *err;
899 11528a82 2018-05-19 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
900 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
901 fe621944 2020-11-10 stsp int want_lineoffsets = (line_offsets != NULL && *line_offsets != NULL);
903 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
904 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
907 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo, id1);
912 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo, id2);
917 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
918 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
919 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
920 aaa13589 2019-06-01 stsp arg.outfile = outfile;
921 fe621944 2020-11-10 stsp if (want_lineoffsets) {
922 fe621944 2020-11-10 stsp arg.line_offsets = *line_offsets;
923 fe621944 2020-11-10 stsp arg.nlines = *nlines;
925 fe621944 2020-11-10 stsp arg.line_offsets = NULL;
926 fe621944 2020-11-10 stsp arg.nlines = 0;
928 67b631c9 2021-10-10 stsp if (paths == NULL || TAILQ_EMPTY(paths)) {
929 67b631c9 2021-10-10 stsp err = got_diff_tree(tree1, tree2, label1, label2, repo,
930 67b631c9 2021-10-10 stsp got_diff_blob_output_unidiff, &arg, 1);
932 67b631c9 2021-10-10 stsp err = diff_paths(tree1, tree2, paths, repo,
933 67b631c9 2021-10-10 stsp got_diff_blob_output_unidiff, &arg);
935 fe621944 2020-11-10 stsp if (want_lineoffsets) {
936 fe621944 2020-11-10 stsp *line_offsets = arg.line_offsets; /* was likely re-allocated */
937 fe621944 2020-11-10 stsp *nlines = arg.nlines;
941 11528a82 2018-05-19 stsp got_object_tree_close(tree1);
943 11528a82 2018-05-19 stsp got_object_tree_close(tree2);
944 11528a82 2018-05-19 stsp return err;
947 11528a82 2018-05-19 stsp const struct got_error *
948 fe621944 2020-11-10 stsp got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
949 fe621944 2020-11-10 stsp struct got_object_id *id1, struct got_object_id *id2,
950 67b631c9 2021-10-10 stsp struct got_pathlist_head *paths,
951 64453f7e 2020-11-21 stsp int diff_context, int ignore_whitespace, int force_text_diff,
952 15a94983 2018-12-23 stsp struct got_repository *repo, FILE *outfile)
954 11528a82 2018-05-19 stsp const struct got_error *err;
955 11528a82 2018-05-19 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
957 15a94983 2018-12-23 stsp if (id2 == NULL)
958 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
961 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit1, repo, id1);
966 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, repo, id2);
970 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(line_offsets, nlines,
971 15a94983 2018-12-23 stsp commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
972 67b631c9 2021-10-10 stsp got_object_commit_get_tree_id(commit2), paths, "", "",
973 67b631c9 2021-10-10 stsp diff_context, ignore_whitespace, force_text_diff, repo, outfile);
975 11528a82 2018-05-19 stsp if (commit1)
976 11528a82 2018-05-19 stsp got_object_commit_close(commit1);
977 11528a82 2018-05-19 stsp if (commit2)
978 11528a82 2018-05-19 stsp got_object_commit_close(commit2);
979 11528a82 2018-05-19 stsp return err;
982 dc424a06 2019-08-07 stsp const struct got_error *
983 fe621944 2020-11-10 stsp got_diff_files(struct got_diffreg_result **resultp,
984 fe621944 2020-11-10 stsp FILE *f1, const char *label1, FILE *f2, const char *label2,
985 64453f7e 2020-11-21 stsp int diff_context, int ignore_whitespace, int force_text_diff,
986 64453f7e 2020-11-21 stsp FILE *outfile)
988 dc424a06 2019-08-07 stsp const struct got_error *err = NULL;
989 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
991 fe621944 2020-11-10 stsp if (resultp)
992 fe621944 2020-11-10 stsp *resultp = NULL;
994 dc424a06 2019-08-07 stsp if (outfile) {
995 dc424a06 2019-08-07 stsp fprintf(outfile, "file - %s\n",
996 dc424a06 2019-08-07 stsp f1 == NULL ? "/dev/null" : label1);
997 dc424a06 2019-08-07 stsp fprintf(outfile, "file + %s\n",
998 dc424a06 2019-08-07 stsp f2 == NULL ? "/dev/null" : label2);
1001 fe621944 2020-11-10 stsp err = got_diffreg(&diffreg_result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
1002 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
1004 fe621944 2020-11-10 stsp goto done;
1006 fe621944 2020-11-10 stsp if (outfile) {
1007 fe621944 2020-11-10 stsp err = got_diffreg_output(NULL, NULL, diffreg_result,
1008 1cb46f00 2020-11-21 stsp f1 != NULL, f2 != NULL, label1, label2,
1009 1cb46f00 2020-11-21 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1011 dc424a06 2019-08-07 stsp goto done;
1015 fe621944 2020-11-10 stsp if (resultp && err == NULL)
1016 fe621944 2020-11-10 stsp *resultp = diffreg_result;
1017 fe621944 2020-11-10 stsp else if (diffreg_result) {
1018 fe621944 2020-11-10 stsp const struct got_error *free_err;
1019 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
1020 fe621944 2020-11-10 stsp if (free_err && err == NULL)
1021 fe621944 2020-11-10 stsp err = free_err;
1024 dc424a06 2019-08-07 stsp return err;