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 8b925c6c 2022-07-16 thomas #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 <zlib.h>
26 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
28 7d283eee 2017-11-29 stsp #include "got_object.h"
29 e09a504c 2019-06-28 stsp #include "got_repository.h"
30 7d283eee 2017-11-29 stsp #include "got_error.h"
31 789689b5 2017-11-30 stsp #include "got_diff.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"
35 1758cce7 2022-06-13 thomas #include "got_opentemp.h"
37 718b3ab0 2018-03-17 stsp #include "got_lib_diff.h"
38 15a94983 2018-12-23 stsp #include "got_lib_delta.h"
39 15a94983 2018-12-23 stsp #include "got_lib_inflate.h"
40 15a94983 2018-12-23 stsp #include "got_lib_object.h"
42 404c43c4 2018-06-21 stsp static const struct got_error *
43 82c78e96 2022-08-06 thomas add_line_metadata(struct got_diff_line **lines, size_t *nlines,
44 82c78e96 2022-08-06 thomas off_t off, uint8_t type)
46 82c78e96 2022-08-06 thomas struct got_diff_line *p;
48 82c78e96 2022-08-06 thomas p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
49 fe621944 2020-11-10 stsp if (p == NULL)
50 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
51 82c78e96 2022-08-06 thomas *lines = p;
52 82c78e96 2022-08-06 thomas (*lines)[*nlines].offset = off;
53 82c78e96 2022-08-06 thomas (*lines)[*nlines].type = type;
54 fe621944 2020-11-10 stsp (*nlines)++;
56 fe621944 2020-11-10 stsp return NULL;
59 fe621944 2020-11-10 stsp static const struct got_error *
60 82c78e96 2022-08-06 thomas diff_blobs(struct got_diff_line **lines, size_t *nlines,
61 fe621944 2020-11-10 stsp struct got_diffreg_result **resultp, struct got_blob_object *blob1,
62 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
63 46f68b20 2019-10-19 stsp const char *label1, const char *label2, mode_t mode1, mode_t mode2,
64 25ec7006 2022-07-01 thomas int diff_context, int ignore_whitespace, int force_text_diff, FILE *outfile,
65 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo)
67 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
68 f78b0693 2017-11-29 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
69 f78b0693 2017-11-29 stsp char hex2[SHA1_DIGEST_STRING_LENGTH];
70 0c6f49ba 2022-07-01 thomas const char *idstr1 = NULL, *idstr2 = NULL;
71 be659d10 2020-11-18 stsp off_t size1, size2;
72 fe621944 2020-11-10 stsp struct got_diffreg_result *result;
73 fe621944 2020-11-10 stsp off_t outoff = 0;
76 3387ec82 2022-08-30 thomas if (lines && *lines && *nlines > 0)
77 82c78e96 2022-08-06 thomas outoff = (*lines)[*nlines - 1].offset;
78 82c78e96 2022-08-06 thomas else if (lines) {
79 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
84 fe621944 2020-11-10 stsp if (resultp)
85 fe621944 2020-11-10 stsp *resultp = NULL;
88 1758cce7 2022-06-13 thomas err = got_opentemp_truncate(f1);
90 a0f32f33 2022-06-13 thomas goto done;
93 1758cce7 2022-06-13 thomas err = got_opentemp_truncate(f2);
95 a0f32f33 2022-06-13 thomas goto done;
99 98abbc84 2017-11-30 stsp if (blob1) {
100 f934cf2c 2018-02-12 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
101 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
106 98abbc84 2017-11-30 stsp idstr1 = "/dev/null";
109 98abbc84 2017-11-30 stsp if (blob2) {
110 f934cf2c 2018-02-12 stsp idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
111 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
116 98abbc84 2017-11-30 stsp idstr2 = "/dev/null";
118 09de383e 2018-12-24 stsp if (outfile) {
119 46f68b20 2019-10-19 stsp char *modestr1 = NULL, *modestr2 = NULL;
120 40dde666 2020-07-23 stsp int modebits;
121 46f68b20 2019-10-19 stsp if (mode1 && mode1 != mode2) {
122 40dde666 2020-07-23 stsp if (S_ISLNK(mode1))
123 40dde666 2020-07-23 stsp modebits = S_IFLNK;
125 40dde666 2020-07-23 stsp modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
126 46f68b20 2019-10-19 stsp if (asprintf(&modestr1, " (mode %o)",
127 40dde666 2020-07-23 stsp mode1 & modebits) == -1) {
128 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
132 46f68b20 2019-10-19 stsp if (mode2 && mode1 != mode2) {
133 40dde666 2020-07-23 stsp if (S_ISLNK(mode2))
134 40dde666 2020-07-23 stsp modebits = S_IFLNK;
136 40dde666 2020-07-23 stsp modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
137 46f68b20 2019-10-19 stsp if (asprintf(&modestr2, " (mode %o)",
138 40dde666 2020-07-23 stsp mode2 & modebits) == -1) {
139 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
143 fe621944 2020-11-10 stsp n = fprintf(outfile, "blob - %s%s\n", idstr1,
144 46f68b20 2019-10-19 stsp modestr1 ? modestr1 : "");
147 fe621944 2020-11-10 stsp outoff += n;
148 82c78e96 2022-08-06 thomas if (lines) {
149 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
150 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_BLOB_MIN);
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 82c78e96 2022-08-06 thomas if (lines) {
161 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
162 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_BLOB_PLUS);
167 46f68b20 2019-10-19 stsp free(modestr1);
168 46f68b20 2019-10-19 stsp free(modestr2);
170 25ec7006 2022-07-01 thomas err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
171 25ec7006 2022-07-01 thomas force_text_diff);
175 fe621944 2020-11-10 stsp if (outfile) {
176 82c78e96 2022-08-06 thomas err = got_diffreg_output(lines, nlines, result,
177 1cb46f00 2020-11-21 stsp blob1 != NULL, blob2 != NULL,
178 fe621944 2020-11-10 stsp label1 ? label1 : idstr1,
179 fe621944 2020-11-10 stsp label2 ? label2 : idstr2,
180 fe621944 2020-11-10 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
185 fe621944 2020-11-10 stsp if (resultp && err == NULL)
186 fe621944 2020-11-10 stsp *resultp = result;
188 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
189 fe621944 2020-11-10 stsp if (free_err && err == NULL)
190 fe621944 2020-11-10 stsp err = free_err;
193 7d283eee 2017-11-29 stsp return err;
196 aaa13589 2019-06-01 stsp const struct got_error *
197 aaa13589 2019-06-01 stsp got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
198 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
199 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
200 a0f32f33 2022-06-13 thomas const char *label1, const char *label2, mode_t mode1, mode_t mode2,
201 a0f32f33 2022-06-13 thomas struct got_repository *repo)
203 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg *a = arg;
205 82c78e96 2022-08-06 thomas return diff_blobs(&a->lines, &a->nlines, NULL,
206 a0f32f33 2022-06-13 thomas blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
207 25ec7006 2022-07-01 thomas a->ignore_whitespace, a->force_text_diff, a->outfile, a->diff_algo);
210 404c43c4 2018-06-21 stsp const struct got_error *
211 82c78e96 2022-08-06 thomas got_diff_blob(struct got_diff_line **lines, size_t*nlines,
212 fe621944 2020-11-10 stsp struct got_blob_object *blob1, struct got_blob_object *blob2,
213 a0f32f33 2022-06-13 thomas FILE *f1, FILE *f2, const char *label1, const char *label2,
214 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo, int diff_context,
215 25ec7006 2022-07-01 thomas int ignore_whitespace, int force_text_diff, FILE *outfile)
217 82c78e96 2022-08-06 thomas return diff_blobs(lines, nlines, NULL, blob1, blob2, f1, f2,
218 64453f7e 2020-11-21 stsp label1, label2, 0, 0, diff_context, ignore_whitespace,
219 25ec7006 2022-07-01 thomas force_text_diff, outfile, diff_algo);
222 7f1f93af 2019-08-06 stsp static const struct got_error *
223 fe621944 2020-11-10 stsp diff_blob_file(struct got_diffreg_result **resultp,
224 a0f32f33 2022-06-13 thomas struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
225 25ec7006 2022-07-01 thomas FILE *f2, int f2_exists, size_t size2, const char *label2,
226 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
227 25ec7006 2022-07-01 thomas 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 char hex1[SHA1_DIGEST_STRING_LENGTH];
231 0c6f49ba 2022-07-01 thomas const char *idstr1 = NULL;
232 fe621944 2020-11-10 stsp struct got_diffreg_result *result = NULL;
234 fe621944 2020-11-10 stsp if (resultp)
235 fe621944 2020-11-10 stsp *resultp = NULL;
237 a0f32f33 2022-06-13 thomas if (blob1)
238 b72f483a 2019-02-05 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
240 b72f483a 2019-02-05 stsp idstr1 = "/dev/null";
242 7f1f93af 2019-08-06 stsp if (outfile) {
243 4ce46740 2019-08-08 stsp fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
244 7f1f93af 2019-08-06 stsp fprintf(outfile, "file + %s\n",
245 dd2e2f52 2022-07-01 thomas f2_exists ? label2 : "/dev/null");
248 25ec7006 2022-07-01 thomas err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
249 25ec7006 2022-07-01 thomas force_text_diff);
253 fe621944 2020-11-10 stsp if (outfile) {
254 1cb46f00 2020-11-21 stsp err = got_diffreg_output(NULL, NULL, result,
255 dd2e2f52 2022-07-01 thomas blob1 != NULL, f2_exists,
256 1cb46f00 2020-11-21 stsp label2, /* show local file's path, not a blob ID */
257 1cb46f00 2020-11-21 stsp label2, GOT_DIFF_OUTPUT_UNIDIFF,
258 1cb46f00 2020-11-21 stsp diff_context, outfile);
263 fe621944 2020-11-10 stsp if (resultp && err == NULL)
264 fe621944 2020-11-10 stsp *resultp = result;
265 fe621944 2020-11-10 stsp else if (result) {
266 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
267 fe621944 2020-11-10 stsp if (free_err && err == NULL)
268 fe621944 2020-11-10 stsp err = free_err;
271 b72f483a 2019-02-05 stsp return err;
274 7f1f93af 2019-08-06 stsp const struct got_error *
275 a0f32f33 2022-06-13 thomas got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
276 dd2e2f52 2022-07-01 thomas const char *label1, FILE *f2, int f2_exists, size_t size2,
277 25ec7006 2022-07-01 thomas const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
278 25ec7006 2022-07-01 thomas int ignore_whitespace, int force_text_diff, FILE *outfile)
280 dd2e2f52 2022-07-01 thomas return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
281 25ec7006 2022-07-01 thomas size2, label2, diff_algo, diff_context, ignore_whitespace,
282 25ec7006 2022-07-01 thomas force_text_diff, outfile );
285 474b4f94 2017-11-30 stsp static const struct got_error *
286 dd2e2f52 2022-07-01 thomas diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
287 19a6a6b5 2022-07-01 thomas const char *label, mode_t mode, struct got_repository *repo,
288 a0f32f33 2022-06-13 thomas got_diff_blob_cb cb, void *cb_arg)
290 4e22badc 2017-11-30 stsp const struct got_error *err;
291 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
292 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
294 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
296 4e22badc 2017-11-30 stsp return err;
298 dd2e2f52 2022-07-01 thomas err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
301 dd2e2f52 2022-07-01 thomas err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
302 a0f32f33 2022-06-13 thomas NULL, label, 0, mode, repo);
304 2acfca77 2018-04-01 stsp got_object_close(obj);
306 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
307 2acfca77 2018-04-01 stsp return err;
310 474b4f94 2017-11-30 stsp static const struct got_error *
311 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
312 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
313 19a6a6b5 2022-07-01 thomas const char *label1, const char *label2,
314 a0f32f33 2022-06-13 thomas mode_t mode1, mode_t mode2, struct got_repository *repo,
315 a0f32f33 2022-06-13 thomas 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;
327 15a94983 2018-12-23 stsp if (obj1->type != GOT_OBJ_TYPE_BLOB) {
328 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
332 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
335 15a94983 2018-12-23 stsp if (obj2->type != GOT_OBJ_TYPE_BLOB) {
336 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
340 f4ae6ddb 2022-07-01 thomas err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
344 f4ae6ddb 2022-07-01 thomas err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
348 a0f32f33 2022-06-13 thomas err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
349 a0f32f33 2022-06-13 thomas mode1, mode2, repo);
352 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
354 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
356 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
358 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
359 6a213ccb 2017-11-30 stsp return err;
362 474b4f94 2017-11-30 stsp static const struct got_error *
363 dd2e2f52 2022-07-01 thomas diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
364 dd2e2f52 2022-07-01 thomas FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
365 19a6a6b5 2022-07-01 thomas got_diff_blob_cb cb, void *cb_arg)
367 365fb436 2017-11-30 stsp const struct got_error *err;
368 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
369 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
371 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
373 365fb436 2017-11-30 stsp return err;
375 dd2e2f52 2022-07-01 thomas err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
378 dd2e2f52 2022-07-01 thomas err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
379 a0f32f33 2022-06-13 thomas mode, 0, repo);
381 2acfca77 2018-04-01 stsp got_object_close(obj);
383 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
384 2acfca77 2018-04-01 stsp return err;
387 474b4f94 2017-11-30 stsp static const struct got_error *
388 dd2e2f52 2022-07-01 thomas diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
389 dd2e2f52 2022-07-01 thomas const char *label, struct got_repository *repo, got_diff_blob_cb cb,
390 dd2e2f52 2022-07-01 thomas void *cb_arg, int diff_content)
392 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
393 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
394 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
396 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
400 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
401 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
405 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
409 dd2e2f52 2022-07-01 thomas err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
410 a0f32f33 2022-06-13 thomas repo, cb, cb_arg, diff_content);
413 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
414 9c70d4c3 2017-11-30 stsp if (treeobj)
415 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
416 9c70d4c3 2017-11-30 stsp return err;
419 474b4f94 2017-11-30 stsp static const struct got_error *
420 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
421 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
422 19a6a6b5 2022-07-01 thomas const char *label1, const char *label2,
423 a0f32f33 2022-06-13 thomas struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
424 a0f32f33 2022-06-13 thomas int diff_content)
426 f6861a81 2018-09-13 stsp const struct got_error *err;
427 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
428 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
429 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
430 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
432 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
436 15a94983 2018-12-23 stsp if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
437 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
441 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
445 15a94983 2018-12-23 stsp if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
446 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
450 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
454 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
458 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
459 19a6a6b5 2022-07-01 thomas label1, label2, repo, cb, cb_arg, diff_content);
463 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
465 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
466 789689b5 2017-11-30 stsp if (treeobj1)
467 789689b5 2017-11-30 stsp got_object_close(treeobj1);
468 789689b5 2017-11-30 stsp if (treeobj2)
469 789689b5 2017-11-30 stsp got_object_close(treeobj2);
470 789689b5 2017-11-30 stsp return err;
473 474b4f94 2017-11-30 stsp static const struct got_error *
474 dd2e2f52 2022-07-01 thomas diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
475 dd2e2f52 2022-07-01 thomas FILE *f2, const char *label, struct got_repository *repo,
476 19a6a6b5 2022-07-01 thomas got_diff_blob_cb cb, void *cb_arg, int diff_content)
478 f6861a81 2018-09-13 stsp const struct got_error *err;
479 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
480 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
482 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
486 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
487 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
491 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
495 dd2e2f52 2022-07-01 thomas err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
496 a0f32f33 2022-06-13 thomas repo, cb, cb_arg, diff_content);
499 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
500 2c56f2ce 2017-11-30 stsp if (treeobj)
501 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
502 2c56f2ce 2017-11-30 stsp return err;
505 474b4f94 2017-11-30 stsp static const struct got_error *
506 74671950 2018-02-11 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
507 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
508 aaa13589 2019-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
510 013404a9 2017-11-30 stsp /* XXX TODO */
511 474b4f94 2017-11-30 stsp return NULL;
514 474b4f94 2017-11-30 stsp static const struct got_error *
515 a0f32f33 2022-06-13 thomas diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
516 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
517 19a6a6b5 2022-07-01 thomas const char *label1, const char *label2,
518 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
519 31b4484f 2019-07-27 stsp int diff_content)
521 f6861a81 2018-09-13 stsp const struct got_error *err = NULL;
522 19ae6da1 2018-11-05 stsp int id_match;
524 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te1))
525 63c5ca5d 2019-08-24 stsp return NULL;
527 474b4f94 2017-11-30 stsp if (te2 == NULL) {
528 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
529 dd2e2f52 2022-07-01 thomas err = diff_deleted_tree(&te1->id, f1, fd1, f2,
530 dd2e2f52 2022-07-01 thomas label1, repo, cb, cb_arg, diff_content);
532 31b4484f 2019-07-27 stsp if (diff_content)
533 19a6a6b5 2022-07-01 thomas err = diff_deleted_blob(&te1->id, f1, fd1,
534 dd2e2f52 2022-07-01 thomas f2, label1, te1->mode, repo, cb, cb_arg);
536 a0f32f33 2022-06-13 thomas err = cb(cb_arg, NULL, NULL, NULL, NULL,
537 a0f32f33 2022-06-13 thomas &te1->id, NULL, label1, NULL,
538 a0f32f33 2022-06-13 thomas 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 a0f32f33 2022-06-13 thomas return diff_modified_tree(&te1->id, &te2->id, f1, f2,
548 19a6a6b5 2022-07-01 thomas fd1, fd2, label1, label2, repo, cb, cb_arg,
549 19a6a6b5 2022-07-01 thomas diff_content);
550 40dde666 2020-07-23 stsp } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
551 40dde666 2020-07-23 stsp (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
552 46f68b20 2019-10-19 stsp if (!id_match ||
553 40dde666 2020-07-23 stsp ((te1->mode & (S_IFLNK | S_IXUSR))) !=
554 40dde666 2020-07-23 stsp (te2->mode & (S_IFLNK | S_IXUSR))) {
555 31b4484f 2019-07-27 stsp if (diff_content)
556 56e0773d 2019-11-28 stsp return diff_modified_blob(&te1->id, &te2->id,
557 19a6a6b5 2022-07-01 thomas f1, f2, fd1, fd2, label1, label2,
558 19a6a6b5 2022-07-01 thomas te1->mode, te2->mode, repo, cb, cb_arg);
560 a0f32f33 2022-06-13 thomas return cb(cb_arg, NULL, NULL, NULL, NULL,
561 a0f32f33 2022-06-13 thomas &te1->id, &te2->id, label1, label2,
562 a0f32f33 2022-06-13 thomas te1->mode, te2->mode, repo);
566 19ae6da1 2018-11-05 stsp if (id_match)
567 f6861a81 2018-09-13 stsp return NULL;
569 56e0773d 2019-11-28 stsp return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
570 aaa13589 2019-06-01 stsp cb, cb_arg);
573 474b4f94 2017-11-30 stsp static const struct got_error *
574 56e0773d 2019-11-28 stsp diff_entry_new_old(struct got_tree_entry *te2,
575 dd2e2f52 2022-07-01 thomas struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
576 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
577 31b4484f 2019-07-27 stsp int diff_content)
579 f6861a81 2018-09-13 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
580 63c5ca5d 2019-08-24 stsp return NULL;
582 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te2))
583 f6861a81 2018-09-13 stsp return NULL;
585 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
586 dd2e2f52 2022-07-01 thomas return diff_added_tree(&te2->id, f1, f2, fd2, label2,
587 a0f32f33 2022-06-13 thomas repo, cb, cb_arg, diff_content);
589 31b4484f 2019-07-27 stsp if (diff_content)
590 dd2e2f52 2022-07-01 thomas return diff_added_blob(&te2->id, f1, f2, fd2,
591 19a6a6b5 2022-07-01 thomas label2, te2->mode, repo, cb, cb_arg);
593 a0f32f33 2022-06-13 thomas return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
594 a0f32f33 2022-06-13 thomas NULL, label2, 0, te2->mode, repo);
597 0208f208 2020-05-05 stsp const struct got_error *
598 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
599 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
600 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
601 a0f32f33 2022-06-13 thomas const char *label1, const char *label2,
602 0208f208 2020-05-05 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
604 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
605 0208f208 2020-05-05 stsp struct got_pathlist_head *paths = arg;
606 0208f208 2020-05-05 stsp struct got_diff_changed_path *change = NULL;
607 0208f208 2020-05-05 stsp char *path = NULL;
609 0208f208 2020-05-05 stsp path = strdup(label2 ? label2 : label1);
610 0208f208 2020-05-05 stsp if (path == NULL)
611 0208f208 2020-05-05 stsp return got_error_from_errno("malloc");
613 0208f208 2020-05-05 stsp change = malloc(sizeof(*change));
614 0208f208 2020-05-05 stsp if (change == NULL) {
615 0208f208 2020-05-05 stsp err = got_error_from_errno("malloc");
619 0208f208 2020-05-05 stsp change->status = GOT_STATUS_NO_CHANGE;
620 0208f208 2020-05-05 stsp if (id1 == NULL)
621 0208f208 2020-05-05 stsp change->status = GOT_STATUS_ADD;
622 0208f208 2020-05-05 stsp else if (id2 == NULL)
623 0208f208 2020-05-05 stsp change->status = GOT_STATUS_DELETE;
625 0208f208 2020-05-05 stsp if (got_object_id_cmp(id1, id2) != 0)
626 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODIFY;
627 0208f208 2020-05-05 stsp else if (mode1 != mode2)
628 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODE_CHANGE;
631 4c71f93b 2021-12-31 thomas err = got_pathlist_append(paths, path, change);
634 0208f208 2020-05-05 stsp free(path);
635 0208f208 2020-05-05 stsp free(change);
637 0208f208 2020-05-05 stsp return err;
640 474b4f94 2017-11-30 stsp const struct got_error *
641 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
642 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
643 19a6a6b5 2022-07-01 thomas const char *label1, const char *label2,
644 a0f32f33 2022-06-13 thomas struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
645 a0f32f33 2022-06-13 thomas int diff_content)
647 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
648 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
649 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
650 f6861a81 2018-09-13 stsp char *l1 = NULL, *l2 = NULL;
651 56e0773d 2019-11-28 stsp int tidx1 = 0, tidx2 = 0;
653 883f0469 2018-06-23 stsp if (tree1) {
654 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, 0);
655 60f50a58 2018-09-15 stsp if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
656 f6861a81 2018-09-13 stsp te1->name) == -1)
657 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
659 883f0469 2018-06-23 stsp if (tree2) {
660 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, 0);
661 60f50a58 2018-09-15 stsp if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
662 f6861a81 2018-09-13 stsp te2->name) == -1)
663 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
668 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
670 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree2,
671 1de5e065 2019-06-01 stsp te1->name);
675 f6861a81 2018-09-13 stsp if (te && asprintf(&l2, "%s%s%s", label2,
676 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te->name) == -1)
678 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
680 19a6a6b5 2022-07-01 thomas err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
681 19a6a6b5 2022-07-01 thomas l1, l2, repo, cb, cb_arg, diff_content);
687 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
689 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree1,
690 1de5e065 2019-06-01 stsp te2->name);
693 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
694 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te->name) == -1)
696 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
698 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
699 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te2->name) == -1)
701 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
703 dd2e2f52 2022-07-01 thomas err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
704 dd2e2f52 2022-07-01 thomas repo, cb, cb_arg, diff_content);
713 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, tidx1);
715 f6861a81 2018-09-13 stsp asprintf(&l1, "%s%s%s", label1,
716 f6861a81 2018-09-13 stsp label1[0] ? "/" : "", te1->name) == -1)
717 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
723 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, tidx2);
725 f6861a81 2018-09-13 stsp asprintf(&l2, "%s%s%s", label2,
726 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te2->name) == -1)
727 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
729 474b4f94 2017-11-30 stsp } while (te1 || te2);
731 11528a82 2018-05-19 stsp return err;
734 11528a82 2018-05-19 stsp const struct got_error *
735 82c78e96 2022-08-06 thomas got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
736 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
737 19a6a6b5 2022-07-01 thomas struct got_object_id *id1, struct got_object_id *id2,
738 25ec7006 2022-07-01 thomas const char *label1, const char *label2,
739 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo, int diff_context,
740 64453f7e 2020-11-21 stsp int ignore_whitespace, int force_text_diff,
741 64453f7e 2020-11-21 stsp struct got_repository *repo, FILE *outfile)
743 11528a82 2018-05-19 stsp const struct got_error *err;
744 11528a82 2018-05-19 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
746 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
747 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
750 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
755 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
759 82c78e96 2022-08-06 thomas err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
760 82c78e96 2022-08-06 thomas diff_algo, diff_context, ignore_whitespace, force_text_diff,
761 82c78e96 2022-08-06 thomas outfile);
763 cc8021af 2021-10-12 thomas if (blob1)
764 cc8021af 2021-10-12 thomas got_object_blob_close(blob1);
765 cc8021af 2021-10-12 thomas if (blob2)
766 cc8021af 2021-10-12 thomas got_object_blob_close(blob2);
767 cc8021af 2021-10-12 thomas return err;
770 cc8021af 2021-10-12 thomas static const struct got_error *
771 cc8021af 2021-10-12 thomas diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
772 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
773 a0f32f33 2022-06-13 thomas struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
775 cc8021af 2021-10-12 thomas const struct got_error *err = NULL;
776 cc8021af 2021-10-12 thomas struct got_pathlist_entry *pe;
777 cc8021af 2021-10-12 thomas struct got_object_id *id1 = NULL, *id2 = NULL;
778 cc8021af 2021-10-12 thomas struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
779 cc8021af 2021-10-12 thomas struct got_blob_object *blob1 = NULL, *blob2 = NULL;
781 cc8021af 2021-10-12 thomas TAILQ_FOREACH(pe, paths, entry) {
782 cc8021af 2021-10-12 thomas int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
783 cc8021af 2021-10-12 thomas mode_t mode1 = 0, mode2 = 0;
785 cc8021af 2021-10-12 thomas free(id1);
786 cc8021af 2021-10-12 thomas id1 = NULL;
787 cc8021af 2021-10-12 thomas free(id2);
788 cc8021af 2021-10-12 thomas id2 = NULL;
789 cc8021af 2021-10-12 thomas if (subtree1) {
790 cc8021af 2021-10-12 thomas got_object_tree_close(subtree1);
791 cc8021af 2021-10-12 thomas subtree1 = NULL;
793 cc8021af 2021-10-12 thomas if (subtree2) {
794 cc8021af 2021-10-12 thomas got_object_tree_close(subtree2);
795 cc8021af 2021-10-12 thomas subtree2 = NULL;
797 cc8021af 2021-10-12 thomas if (blob1) {
798 cc8021af 2021-10-12 thomas got_object_blob_close(blob1);
799 cc8021af 2021-10-12 thomas blob1 = NULL;
801 cc8021af 2021-10-12 thomas if (blob2) {
802 cc8021af 2021-10-12 thomas got_object_blob_close(blob2);
803 cc8021af 2021-10-12 thomas blob2 = NULL;
806 cc8021af 2021-10-12 thomas err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
807 cc8021af 2021-10-12 thomas pe->path);
808 cc8021af 2021-10-12 thomas if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
809 cc8021af 2021-10-12 thomas goto done;
810 cc8021af 2021-10-12 thomas err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
811 cc8021af 2021-10-12 thomas pe->path);
812 cc8021af 2021-10-12 thomas if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
813 cc8021af 2021-10-12 thomas goto done;
814 cc8021af 2021-10-12 thomas if (id1 == NULL && id2 == NULL) {
815 cc8021af 2021-10-12 thomas err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
816 cc8021af 2021-10-12 thomas goto done;
818 cc8021af 2021-10-12 thomas if (id1) {
819 cc8021af 2021-10-12 thomas err = got_object_get_type(&type1, repo, id1);
821 cc8021af 2021-10-12 thomas goto done;
823 cc8021af 2021-10-12 thomas if (id2) {
824 cc8021af 2021-10-12 thomas err = got_object_get_type(&type2, repo, id2);
826 cc8021af 2021-10-12 thomas goto done;
828 cc8021af 2021-10-12 thomas if (type1 == GOT_OBJ_TYPE_ANY &&
829 cc8021af 2021-10-12 thomas type2 == GOT_OBJ_TYPE_ANY) {
830 cc8021af 2021-10-12 thomas err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
831 cc8021af 2021-10-12 thomas goto done;
832 cc8021af 2021-10-12 thomas } else if (type1 != GOT_OBJ_TYPE_ANY &&
833 cc8021af 2021-10-12 thomas type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
834 cc8021af 2021-10-12 thomas err = got_error(GOT_ERR_OBJ_TYPE);
835 cc8021af 2021-10-12 thomas goto done;
838 cc8021af 2021-10-12 thomas if (type1 == GOT_OBJ_TYPE_BLOB ||
839 cc8021af 2021-10-12 thomas type2 == GOT_OBJ_TYPE_BLOB) {
840 cc8021af 2021-10-12 thomas if (id1) {
841 cc8021af 2021-10-12 thomas err = got_object_open_as_blob(&blob1, repo,
842 f4ae6ddb 2022-07-01 thomas id1, 8192, fd1);
844 cc8021af 2021-10-12 thomas goto done;
846 cc8021af 2021-10-12 thomas if (id2) {
847 cc8021af 2021-10-12 thomas err = got_object_open_as_blob(&blob2, repo,
848 f4ae6ddb 2022-07-01 thomas id2, 8192, fd2);
850 cc8021af 2021-10-12 thomas goto done;
852 a0f32f33 2022-06-13 thomas err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
853 cc8021af 2021-10-12 thomas id1 ? pe->path : "/dev/null",
854 cc8021af 2021-10-12 thomas id2 ? pe->path : "/dev/null",
855 cc8021af 2021-10-12 thomas mode1, mode2, repo);
857 cc8021af 2021-10-12 thomas goto done;
858 cc8021af 2021-10-12 thomas } else if (type1 == GOT_OBJ_TYPE_TREE ||
859 cc8021af 2021-10-12 thomas type2 == GOT_OBJ_TYPE_TREE) {
860 cc8021af 2021-10-12 thomas if (id1) {
861 cc8021af 2021-10-12 thomas err = got_object_open_as_tree(&subtree1, repo,
864 cc8021af 2021-10-12 thomas goto done;
866 cc8021af 2021-10-12 thomas if (id2) {
867 cc8021af 2021-10-12 thomas err = got_object_open_as_tree(&subtree2, repo,
870 cc8021af 2021-10-12 thomas goto done;
872 a0f32f33 2022-06-13 thomas err = got_diff_tree(subtree1, subtree2, f1, f2,
873 19a6a6b5 2022-07-01 thomas fd1, fd2,
874 cc8021af 2021-10-12 thomas id1 ? pe->path : "/dev/null",
875 cc8021af 2021-10-12 thomas id2 ? pe->path : "/dev/null",
876 cc8021af 2021-10-12 thomas repo, cb, cb_arg, 1);
878 cc8021af 2021-10-12 thomas goto done;
880 cc8021af 2021-10-12 thomas err = got_error(GOT_ERR_OBJ_TYPE);
881 cc8021af 2021-10-12 thomas goto done;
885 cc8021af 2021-10-12 thomas free(id1);
886 cc8021af 2021-10-12 thomas free(id2);
887 cc8021af 2021-10-12 thomas if (subtree1)
888 cc8021af 2021-10-12 thomas got_object_tree_close(subtree1);
889 cc8021af 2021-10-12 thomas if (subtree2)
890 cc8021af 2021-10-12 thomas got_object_tree_close(subtree2);
892 11528a82 2018-05-19 stsp got_object_blob_close(blob1);
894 11528a82 2018-05-19 stsp got_object_blob_close(blob2);
895 474b4f94 2017-11-30 stsp return err;
898 9b4458b4 2022-06-26 thomas static const struct got_error *
899 82c78e96 2022-08-06 thomas show_object_id(struct got_diff_line **lines, size_t *nlines,
900 82c78e96 2022-08-06 thomas const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
902 9b4458b4 2022-06-26 thomas const struct got_error *err;
904 9b4458b4 2022-06-26 thomas off_t outoff = 0;
906 9b4458b4 2022-06-26 thomas n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
907 7e1f3a3d 2022-08-16 thomas if (n < 0)
908 7e1f3a3d 2022-08-16 thomas return got_error_from_errno("fprintf");
910 82c78e96 2022-08-06 thomas if (lines != NULL && *lines != NULL) {
911 9b4458b4 2022-06-26 thomas if (*nlines == 0) {
912 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0,
913 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
915 9b4458b4 2022-06-26 thomas return err;
917 82c78e96 2022-08-06 thomas outoff = (*lines)[*nlines - 1].offset;
919 9b4458b4 2022-06-26 thomas outoff += n;
920 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
921 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
923 9b4458b4 2022-06-26 thomas return err;
926 9b4458b4 2022-06-26 thomas return NULL;
929 9b4458b4 2022-06-26 thomas static const struct got_error *
930 82c78e96 2022-08-06 thomas diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
931 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
932 19a6a6b5 2022-07-01 thomas struct got_object_id *id1, struct got_object_id *id2,
933 0c6f49ba 2022-07-01 thomas struct got_pathlist_head *paths, const char *label1, const char *label2,
934 0c6f49ba 2022-07-01 thomas int diff_context, int ignore_whitespace, int force_text_diff,
935 25ec7006 2022-07-01 thomas struct got_repository *repo, FILE *outfile,
936 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo)
938 11528a82 2018-05-19 stsp const struct got_error *err;
939 11528a82 2018-05-19 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
940 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
941 82c78e96 2022-08-06 thomas int want_linemeta = (lines != NULL && *lines != NULL);
943 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
944 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
947 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo, id1);
952 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo, id2);
957 25ec7006 2022-07-01 thomas arg.diff_algo = diff_algo;
958 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
959 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
960 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
961 aaa13589 2019-06-01 stsp arg.outfile = outfile;
962 82c78e96 2022-08-06 thomas if (want_linemeta) {
963 82c78e96 2022-08-06 thomas arg.lines = *lines;
964 fe621944 2020-11-10 stsp arg.nlines = *nlines;
966 82c78e96 2022-08-06 thomas arg.lines = NULL;
967 fe621944 2020-11-10 stsp arg.nlines = 0;
969 cc8021af 2021-10-12 thomas if (paths == NULL || TAILQ_EMPTY(paths)) {
970 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
971 19a6a6b5 2022-07-01 thomas label1, label2, repo,
972 19a6a6b5 2022-07-01 thomas got_diff_blob_output_unidiff, &arg, 1);
974 19a6a6b5 2022-07-01 thomas err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
975 cc8021af 2021-10-12 thomas got_diff_blob_output_unidiff, &arg);
977 82c78e96 2022-08-06 thomas if (want_linemeta) {
978 82c78e96 2022-08-06 thomas *lines = arg.lines; /* was likely re-allocated */
979 fe621944 2020-11-10 stsp *nlines = arg.nlines;
983 11528a82 2018-05-19 stsp got_object_tree_close(tree1);
985 11528a82 2018-05-19 stsp got_object_tree_close(tree2);
986 11528a82 2018-05-19 stsp return err;
989 11528a82 2018-05-19 stsp const struct got_error *
990 82c78e96 2022-08-06 thomas got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
991 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
992 19a6a6b5 2022-07-01 thomas struct got_object_id *id1, struct got_object_id *id2,
993 0c6f49ba 2022-07-01 thomas struct got_pathlist_head *paths, const char *label1, const char *label2,
994 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
995 25ec7006 2022-07-01 thomas int force_text_diff, struct got_repository *repo, FILE *outfile)
997 9b4458b4 2022-06-26 thomas const struct got_error *err;
998 9b4458b4 2022-06-26 thomas char *idstr = NULL;
1000 9b4458b4 2022-06-26 thomas if (id1 == NULL && id2 == NULL)
1001 9b4458b4 2022-06-26 thomas return got_error(GOT_ERR_NO_OBJ);
1003 9b4458b4 2022-06-26 thomas if (id1) {
1004 9b4458b4 2022-06-26 thomas err = got_object_id_str(&idstr, id1);
1005 9b4458b4 2022-06-26 thomas if (err)
1006 9b4458b4 2022-06-26 thomas goto done;
1007 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1008 9b4458b4 2022-06-26 thomas if (err)
1009 9b4458b4 2022-06-26 thomas goto done;
1010 9b4458b4 2022-06-26 thomas free(idstr);
1011 9b4458b4 2022-06-26 thomas idstr = NULL;
1012 9b4458b4 2022-06-26 thomas } else {
1013 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1014 82c78e96 2022-08-06 thomas outfile);
1015 9b4458b4 2022-06-26 thomas if (err)
1016 9b4458b4 2022-06-26 thomas goto done;
1019 9b4458b4 2022-06-26 thomas if (id2) {
1020 9b4458b4 2022-06-26 thomas err = got_object_id_str(&idstr, id2);
1021 9b4458b4 2022-06-26 thomas if (err)
1022 9b4458b4 2022-06-26 thomas goto done;
1023 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1024 9b4458b4 2022-06-26 thomas if (err)
1025 9b4458b4 2022-06-26 thomas goto done;
1026 9b4458b4 2022-06-26 thomas free(idstr);
1027 9b4458b4 2022-06-26 thomas idstr = NULL;
1028 9b4458b4 2022-06-26 thomas } else {
1029 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1030 82c78e96 2022-08-06 thomas outfile);
1031 9b4458b4 2022-06-26 thomas if (err)
1032 9b4458b4 2022-06-26 thomas goto done;
1035 82c78e96 2022-08-06 thomas err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1036 82c78e96 2022-08-06 thomas paths, label1, label2, diff_context, ignore_whitespace,
1037 25ec7006 2022-07-01 thomas force_text_diff, repo, outfile, diff_algo);
1039 9b4458b4 2022-06-26 thomas free(idstr);
1040 9b4458b4 2022-06-26 thomas return err;
1043 9b4458b4 2022-06-26 thomas const struct got_error *
1044 82c78e96 2022-08-06 thomas got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1045 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
1046 19a6a6b5 2022-07-01 thomas struct got_object_id *id1, struct got_object_id *id2,
1047 25ec7006 2022-07-01 thomas struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1048 64453f7e 2020-11-21 stsp int diff_context, int ignore_whitespace, int force_text_diff,
1049 15a94983 2018-12-23 stsp struct got_repository *repo, FILE *outfile)
1051 11528a82 2018-05-19 stsp const struct got_error *err;
1052 11528a82 2018-05-19 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1053 9b4458b4 2022-06-26 thomas char *idstr = NULL;
1055 15a94983 2018-12-23 stsp if (id2 == NULL)
1056 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
1058 15a94983 2018-12-23 stsp if (id1) {
1059 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit1, repo, id1);
1061 cd0acaa7 2018-05-20 stsp goto done;
1062 9b4458b4 2022-06-26 thomas err = got_object_id_str(&idstr, id1);
1063 9b4458b4 2022-06-26 thomas if (err)
1064 9b4458b4 2022-06-26 thomas goto done;
1065 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "commit", '-', idstr,
1066 82c78e96 2022-08-06 thomas outfile);
1067 9b4458b4 2022-06-26 thomas if (err)
1068 9b4458b4 2022-06-26 thomas goto done;
1069 9b4458b4 2022-06-26 thomas free(idstr);
1070 9b4458b4 2022-06-26 thomas idstr = NULL;
1071 9b4458b4 2022-06-26 thomas } else {
1072 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1073 82c78e96 2022-08-06 thomas outfile);
1074 9b4458b4 2022-06-26 thomas if (err)
1075 9b4458b4 2022-06-26 thomas goto done;
1078 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, repo, id2);
1080 9b697879 2018-05-20 stsp goto done;
1082 9b4458b4 2022-06-26 thomas err = got_object_id_str(&idstr, id2);
1083 9b4458b4 2022-06-26 thomas if (err)
1084 9b4458b4 2022-06-26 thomas goto done;
1085 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1086 9b4458b4 2022-06-26 thomas if (err)
1087 9b4458b4 2022-06-26 thomas goto done;
1089 82c78e96 2022-08-06 thomas err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1090 15a94983 2018-12-23 stsp commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1091 cc8021af 2021-10-12 thomas got_object_commit_get_tree_id(commit2), paths, "", "",
1092 25ec7006 2022-07-01 thomas diff_context, ignore_whitespace, force_text_diff, repo, outfile,
1093 25ec7006 2022-07-01 thomas diff_algo);
1095 11528a82 2018-05-19 stsp if (commit1)
1096 11528a82 2018-05-19 stsp got_object_commit_close(commit1);
1097 11528a82 2018-05-19 stsp if (commit2)
1098 11528a82 2018-05-19 stsp got_object_commit_close(commit2);
1099 9b4458b4 2022-06-26 thomas free(idstr);
1100 11528a82 2018-05-19 stsp return err;
1103 dc424a06 2019-08-07 stsp const struct got_error *
1104 fe621944 2020-11-10 stsp got_diff_files(struct got_diffreg_result **resultp,
1105 dd2e2f52 2022-07-01 thomas FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1106 dd2e2f52 2022-07-01 thomas const char *label2, int diff_context, int ignore_whitespace,
1107 25ec7006 2022-07-01 thomas int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1109 dc424a06 2019-08-07 stsp const struct got_error *err = NULL;
1110 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
1112 fe621944 2020-11-10 stsp if (resultp)
1113 fe621944 2020-11-10 stsp *resultp = NULL;
1115 dc424a06 2019-08-07 stsp if (outfile) {
1116 dc424a06 2019-08-07 stsp fprintf(outfile, "file - %s\n",
1117 dd2e2f52 2022-07-01 thomas f1_exists ? label1 : "/dev/null");
1118 dc424a06 2019-08-07 stsp fprintf(outfile, "file + %s\n",
1119 dd2e2f52 2022-07-01 thomas f2_exists ? label2 : "/dev/null");
1122 25ec7006 2022-07-01 thomas err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1123 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
1125 fe621944 2020-11-10 stsp goto done;
1127 fe621944 2020-11-10 stsp if (outfile) {
1128 fe621944 2020-11-10 stsp err = got_diffreg_output(NULL, NULL, diffreg_result,
1129 dd2e2f52 2022-07-01 thomas f1_exists, f2_exists, label1, label2,
1130 1cb46f00 2020-11-21 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1132 dc424a06 2019-08-07 stsp goto done;
1136 fe621944 2020-11-10 stsp if (resultp && err == NULL)
1137 fe621944 2020-11-10 stsp *resultp = diffreg_result;
1138 fe621944 2020-11-10 stsp else if (diffreg_result) {
1139 fe621944 2020-11-10 stsp const struct got_error *free_err;
1140 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
1141 fe621944 2020-11-10 stsp if (free_err && err == NULL)
1142 fe621944 2020-11-10 stsp err = free_err;
1145 dc424a06 2019-08-07 stsp return err;