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 6d054bb9 2022-09-23 thomas FILE *f2, int f2_exists, struct stat *sb2, 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 6d054bb9 2022-09-23 thomas char *mode = NULL;
245 6d054bb9 2022-09-23 thomas /* display file mode for new added files only */
246 6d054bb9 2022-09-23 thomas if (f2_exists && blob1 == NULL) {
247 6d054bb9 2022-09-23 thomas int mmask = (S_IRWXU | S_IRWXG | S_IRWXO);
249 6d054bb9 2022-09-23 thomas if (S_ISLNK(sb2->st_mode))
250 6d054bb9 2022-09-23 thomas mmask = S_IFLNK;
251 6d054bb9 2022-09-23 thomas if (asprintf(&mode, " (mode %o)",
252 6d054bb9 2022-09-23 thomas sb2->st_mode & mmask) == -1)
253 6d054bb9 2022-09-23 thomas return got_error_from_errno("asprintf");
255 4ce46740 2019-08-08 stsp fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
256 6d054bb9 2022-09-23 thomas fprintf(outfile, "file + %s%s\n",
257 6d054bb9 2022-09-23 thomas f2_exists ? label2 : "/dev/null", mode ? mode : "");
258 6d054bb9 2022-09-23 thomas free(mode);
261 25ec7006 2022-07-01 thomas err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
262 25ec7006 2022-07-01 thomas force_text_diff);
266 fe621944 2020-11-10 stsp if (outfile) {
267 1cb46f00 2020-11-21 stsp err = got_diffreg_output(NULL, NULL, result,
268 dd2e2f52 2022-07-01 thomas blob1 != NULL, f2_exists,
269 1cb46f00 2020-11-21 stsp label2, /* show local file's path, not a blob ID */
270 1cb46f00 2020-11-21 stsp label2, GOT_DIFF_OUTPUT_UNIDIFF,
271 1cb46f00 2020-11-21 stsp diff_context, outfile);
276 fe621944 2020-11-10 stsp if (resultp && err == NULL)
277 fe621944 2020-11-10 stsp *resultp = result;
278 fe621944 2020-11-10 stsp else if (result) {
279 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
280 fe621944 2020-11-10 stsp if (free_err && err == NULL)
281 fe621944 2020-11-10 stsp err = free_err;
284 b72f483a 2019-02-05 stsp return err;
287 7f1f93af 2019-08-06 stsp const struct got_error *
288 a0f32f33 2022-06-13 thomas got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
289 6d054bb9 2022-09-23 thomas const char *label1, FILE *f2, int f2_exists, struct stat *sb2,
290 25ec7006 2022-07-01 thomas const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
291 25ec7006 2022-07-01 thomas int ignore_whitespace, int force_text_diff, FILE *outfile)
293 dd2e2f52 2022-07-01 thomas return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
294 6d054bb9 2022-09-23 thomas sb2, label2, diff_algo, diff_context, ignore_whitespace,
295 6d054bb9 2022-09-23 thomas force_text_diff, outfile);
298 474b4f94 2017-11-30 stsp static const struct got_error *
299 dd2e2f52 2022-07-01 thomas diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
300 19a6a6b5 2022-07-01 thomas const char *label, mode_t mode, struct got_repository *repo,
301 a0f32f33 2022-06-13 thomas got_diff_blob_cb cb, void *cb_arg)
303 4e22badc 2017-11-30 stsp const struct got_error *err;
304 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
305 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
307 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
309 4e22badc 2017-11-30 stsp return err;
311 dd2e2f52 2022-07-01 thomas err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
314 dd2e2f52 2022-07-01 thomas err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
315 a0f32f33 2022-06-13 thomas NULL, label, 0, mode, repo);
317 2acfca77 2018-04-01 stsp got_object_close(obj);
319 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
320 2acfca77 2018-04-01 stsp return err;
323 474b4f94 2017-11-30 stsp static const struct got_error *
324 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
325 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
326 19a6a6b5 2022-07-01 thomas const char *label1, const char *label2,
327 a0f32f33 2022-06-13 thomas mode_t mode1, mode_t mode2, struct got_repository *repo,
328 a0f32f33 2022-06-13 thomas got_diff_blob_cb cb, void *cb_arg)
330 6a213ccb 2017-11-30 stsp const struct got_error *err;
331 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
332 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
333 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
334 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
336 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
338 730a8aa0 2018-04-24 stsp return err;
340 15a94983 2018-12-23 stsp if (obj1->type != GOT_OBJ_TYPE_BLOB) {
341 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
345 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
348 15a94983 2018-12-23 stsp if (obj2->type != GOT_OBJ_TYPE_BLOB) {
349 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
353 f4ae6ddb 2022-07-01 thomas err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
357 f4ae6ddb 2022-07-01 thomas err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
361 a0f32f33 2022-06-13 thomas err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
362 a0f32f33 2022-06-13 thomas mode1, mode2, repo);
365 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
367 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
369 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
371 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
372 6a213ccb 2017-11-30 stsp return err;
375 474b4f94 2017-11-30 stsp static const struct got_error *
376 dd2e2f52 2022-07-01 thomas diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
377 dd2e2f52 2022-07-01 thomas FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
378 19a6a6b5 2022-07-01 thomas got_diff_blob_cb cb, void *cb_arg)
380 365fb436 2017-11-30 stsp const struct got_error *err;
381 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
382 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
384 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
386 365fb436 2017-11-30 stsp return err;
388 dd2e2f52 2022-07-01 thomas err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
391 dd2e2f52 2022-07-01 thomas err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
392 a0f32f33 2022-06-13 thomas mode, 0, repo);
394 2acfca77 2018-04-01 stsp got_object_close(obj);
396 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
397 2acfca77 2018-04-01 stsp return err;
400 474b4f94 2017-11-30 stsp static const struct got_error *
401 dd2e2f52 2022-07-01 thomas diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
402 dd2e2f52 2022-07-01 thomas const char *label, struct got_repository *repo, got_diff_blob_cb cb,
403 dd2e2f52 2022-07-01 thomas void *cb_arg, int diff_content)
405 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
406 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
407 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
409 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
413 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
414 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
418 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
422 dd2e2f52 2022-07-01 thomas err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
423 a0f32f33 2022-06-13 thomas repo, cb, cb_arg, diff_content);
426 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
427 9c70d4c3 2017-11-30 stsp if (treeobj)
428 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
429 9c70d4c3 2017-11-30 stsp return err;
432 474b4f94 2017-11-30 stsp static const struct got_error *
433 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
434 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
435 19a6a6b5 2022-07-01 thomas const char *label1, const char *label2,
436 a0f32f33 2022-06-13 thomas struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
437 a0f32f33 2022-06-13 thomas int diff_content)
439 f6861a81 2018-09-13 stsp const struct got_error *err;
440 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
441 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
442 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
443 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
445 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
449 15a94983 2018-12-23 stsp if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
450 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
454 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
458 15a94983 2018-12-23 stsp if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
459 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
463 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
467 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
471 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
472 19a6a6b5 2022-07-01 thomas label1, label2, repo, cb, cb_arg, diff_content);
476 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
478 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
479 789689b5 2017-11-30 stsp if (treeobj1)
480 789689b5 2017-11-30 stsp got_object_close(treeobj1);
481 789689b5 2017-11-30 stsp if (treeobj2)
482 789689b5 2017-11-30 stsp got_object_close(treeobj2);
483 789689b5 2017-11-30 stsp return err;
486 474b4f94 2017-11-30 stsp static const struct got_error *
487 dd2e2f52 2022-07-01 thomas diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
488 dd2e2f52 2022-07-01 thomas FILE *f2, const char *label, struct got_repository *repo,
489 19a6a6b5 2022-07-01 thomas got_diff_blob_cb cb, void *cb_arg, int diff_content)
491 f6861a81 2018-09-13 stsp const struct got_error *err;
492 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
493 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
495 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
499 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
500 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
504 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
508 dd2e2f52 2022-07-01 thomas err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
509 a0f32f33 2022-06-13 thomas repo, cb, cb_arg, diff_content);
512 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
513 2c56f2ce 2017-11-30 stsp if (treeobj)
514 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
515 2c56f2ce 2017-11-30 stsp return err;
518 474b4f94 2017-11-30 stsp static const struct got_error *
519 74671950 2018-02-11 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
520 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
521 aaa13589 2019-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
523 013404a9 2017-11-30 stsp /* XXX TODO */
524 474b4f94 2017-11-30 stsp return NULL;
527 474b4f94 2017-11-30 stsp static const struct got_error *
528 a0f32f33 2022-06-13 thomas diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
529 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
530 19a6a6b5 2022-07-01 thomas const char *label1, const char *label2,
531 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
532 31b4484f 2019-07-27 stsp int diff_content)
534 f6861a81 2018-09-13 stsp const struct got_error *err = NULL;
535 19ae6da1 2018-11-05 stsp int id_match;
537 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te1))
538 63c5ca5d 2019-08-24 stsp return NULL;
540 474b4f94 2017-11-30 stsp if (te2 == NULL) {
541 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
542 dd2e2f52 2022-07-01 thomas err = diff_deleted_tree(&te1->id, f1, fd1, f2,
543 dd2e2f52 2022-07-01 thomas label1, repo, cb, cb_arg, diff_content);
545 31b4484f 2019-07-27 stsp if (diff_content)
546 19a6a6b5 2022-07-01 thomas err = diff_deleted_blob(&te1->id, f1, fd1,
547 dd2e2f52 2022-07-01 thomas f2, label1, te1->mode, repo, cb, cb_arg);
549 a0f32f33 2022-06-13 thomas err = cb(cb_arg, NULL, NULL, NULL, NULL,
550 a0f32f33 2022-06-13 thomas &te1->id, NULL, label1, NULL,
551 a0f32f33 2022-06-13 thomas te1->mode, 0, repo);
553 f6861a81 2018-09-13 stsp return err;
554 63c5ca5d 2019-08-24 stsp } else if (got_object_tree_entry_is_submodule(te2))
555 63c5ca5d 2019-08-24 stsp return NULL;
557 56e0773d 2019-11-28 stsp id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
558 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
559 19ae6da1 2018-11-05 stsp if (!id_match)
560 a0f32f33 2022-06-13 thomas return diff_modified_tree(&te1->id, &te2->id, f1, f2,
561 19a6a6b5 2022-07-01 thomas fd1, fd2, label1, label2, repo, cb, cb_arg,
562 19a6a6b5 2022-07-01 thomas diff_content);
563 40dde666 2020-07-23 stsp } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
564 40dde666 2020-07-23 stsp (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
565 46f68b20 2019-10-19 stsp if (!id_match ||
566 40dde666 2020-07-23 stsp ((te1->mode & (S_IFLNK | S_IXUSR))) !=
567 40dde666 2020-07-23 stsp (te2->mode & (S_IFLNK | S_IXUSR))) {
568 31b4484f 2019-07-27 stsp if (diff_content)
569 56e0773d 2019-11-28 stsp return diff_modified_blob(&te1->id, &te2->id,
570 19a6a6b5 2022-07-01 thomas f1, f2, fd1, fd2, label1, label2,
571 19a6a6b5 2022-07-01 thomas te1->mode, te2->mode, repo, cb, cb_arg);
573 a0f32f33 2022-06-13 thomas return cb(cb_arg, NULL, NULL, NULL, NULL,
574 a0f32f33 2022-06-13 thomas &te1->id, &te2->id, label1, label2,
575 a0f32f33 2022-06-13 thomas te1->mode, te2->mode, repo);
579 19ae6da1 2018-11-05 stsp if (id_match)
580 f6861a81 2018-09-13 stsp return NULL;
582 56e0773d 2019-11-28 stsp return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
583 aaa13589 2019-06-01 stsp cb, cb_arg);
586 474b4f94 2017-11-30 stsp static const struct got_error *
587 56e0773d 2019-11-28 stsp diff_entry_new_old(struct got_tree_entry *te2,
588 dd2e2f52 2022-07-01 thomas struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
589 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
590 31b4484f 2019-07-27 stsp int diff_content)
592 f6861a81 2018-09-13 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
593 63c5ca5d 2019-08-24 stsp return NULL;
595 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te2))
596 f6861a81 2018-09-13 stsp return NULL;
598 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
599 dd2e2f52 2022-07-01 thomas return diff_added_tree(&te2->id, f1, f2, fd2, label2,
600 a0f32f33 2022-06-13 thomas repo, cb, cb_arg, diff_content);
602 31b4484f 2019-07-27 stsp if (diff_content)
603 dd2e2f52 2022-07-01 thomas return diff_added_blob(&te2->id, f1, f2, fd2,
604 19a6a6b5 2022-07-01 thomas label2, te2->mode, repo, cb, cb_arg);
606 a0f32f33 2022-06-13 thomas return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
607 a0f32f33 2022-06-13 thomas NULL, label2, 0, te2->mode, repo);
610 0208f208 2020-05-05 stsp const struct got_error *
611 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
612 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
613 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
614 a0f32f33 2022-06-13 thomas const char *label1, const char *label2,
615 0208f208 2020-05-05 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
617 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
618 0208f208 2020-05-05 stsp struct got_pathlist_head *paths = arg;
619 0208f208 2020-05-05 stsp struct got_diff_changed_path *change = NULL;
620 0208f208 2020-05-05 stsp char *path = NULL;
622 0208f208 2020-05-05 stsp path = strdup(label2 ? label2 : label1);
623 0208f208 2020-05-05 stsp if (path == NULL)
624 0208f208 2020-05-05 stsp return got_error_from_errno("malloc");
626 0208f208 2020-05-05 stsp change = malloc(sizeof(*change));
627 0208f208 2020-05-05 stsp if (change == NULL) {
628 0208f208 2020-05-05 stsp err = got_error_from_errno("malloc");
632 0208f208 2020-05-05 stsp change->status = GOT_STATUS_NO_CHANGE;
633 0208f208 2020-05-05 stsp if (id1 == NULL)
634 0208f208 2020-05-05 stsp change->status = GOT_STATUS_ADD;
635 0208f208 2020-05-05 stsp else if (id2 == NULL)
636 0208f208 2020-05-05 stsp change->status = GOT_STATUS_DELETE;
638 0208f208 2020-05-05 stsp if (got_object_id_cmp(id1, id2) != 0)
639 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODIFY;
640 0208f208 2020-05-05 stsp else if (mode1 != mode2)
641 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODE_CHANGE;
644 4c71f93b 2021-12-31 thomas err = got_pathlist_append(paths, path, change);
647 0208f208 2020-05-05 stsp free(path);
648 0208f208 2020-05-05 stsp free(change);
650 0208f208 2020-05-05 stsp return err;
653 474b4f94 2017-11-30 stsp const struct got_error *
654 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
655 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
656 19a6a6b5 2022-07-01 thomas const char *label1, const char *label2,
657 a0f32f33 2022-06-13 thomas struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
658 a0f32f33 2022-06-13 thomas int diff_content)
660 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
661 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
662 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
663 f6861a81 2018-09-13 stsp char *l1 = NULL, *l2 = NULL;
664 56e0773d 2019-11-28 stsp int tidx1 = 0, tidx2 = 0;
666 883f0469 2018-06-23 stsp if (tree1) {
667 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, 0);
668 60f50a58 2018-09-15 stsp if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
669 f6861a81 2018-09-13 stsp te1->name) == -1)
670 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
672 883f0469 2018-06-23 stsp if (tree2) {
673 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, 0);
674 60f50a58 2018-09-15 stsp if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
675 f6861a81 2018-09-13 stsp te2->name) == -1)
676 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
681 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
683 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree2,
684 1de5e065 2019-06-01 stsp te1->name);
688 f6861a81 2018-09-13 stsp if (te && asprintf(&l2, "%s%s%s", label2,
689 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te->name) == -1)
691 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
693 19a6a6b5 2022-07-01 thomas err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
694 19a6a6b5 2022-07-01 thomas l1, l2, repo, cb, cb_arg, diff_content);
700 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
702 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree1,
703 1de5e065 2019-06-01 stsp te2->name);
706 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
707 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te->name) == -1)
709 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
711 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
712 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te2->name) == -1)
714 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
716 dd2e2f52 2022-07-01 thomas err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
717 dd2e2f52 2022-07-01 thomas repo, cb, cb_arg, diff_content);
726 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, tidx1);
728 f6861a81 2018-09-13 stsp asprintf(&l1, "%s%s%s", label1,
729 f6861a81 2018-09-13 stsp label1[0] ? "/" : "", te1->name) == -1)
730 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
736 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, tidx2);
738 f6861a81 2018-09-13 stsp asprintf(&l2, "%s%s%s", label2,
739 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te2->name) == -1)
740 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
742 474b4f94 2017-11-30 stsp } while (te1 || te2);
744 11528a82 2018-05-19 stsp return err;
747 11528a82 2018-05-19 stsp const struct got_error *
748 82c78e96 2022-08-06 thomas got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
749 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
750 19a6a6b5 2022-07-01 thomas struct got_object_id *id1, struct got_object_id *id2,
751 25ec7006 2022-07-01 thomas const char *label1, const char *label2,
752 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo, int diff_context,
753 64453f7e 2020-11-21 stsp int ignore_whitespace, int force_text_diff,
754 64453f7e 2020-11-21 stsp struct got_repository *repo, FILE *outfile)
756 11528a82 2018-05-19 stsp const struct got_error *err;
757 11528a82 2018-05-19 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
759 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
760 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
763 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
768 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
772 82c78e96 2022-08-06 thomas err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
773 82c78e96 2022-08-06 thomas diff_algo, diff_context, ignore_whitespace, force_text_diff,
774 82c78e96 2022-08-06 thomas outfile);
776 cc8021af 2021-10-12 thomas if (blob1)
777 cc8021af 2021-10-12 thomas got_object_blob_close(blob1);
778 cc8021af 2021-10-12 thomas if (blob2)
779 cc8021af 2021-10-12 thomas got_object_blob_close(blob2);
780 cc8021af 2021-10-12 thomas return err;
783 cc8021af 2021-10-12 thomas static const struct got_error *
784 cc8021af 2021-10-12 thomas diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
785 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
786 a0f32f33 2022-06-13 thomas struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
788 cc8021af 2021-10-12 thomas const struct got_error *err = NULL;
789 cc8021af 2021-10-12 thomas struct got_pathlist_entry *pe;
790 cc8021af 2021-10-12 thomas struct got_object_id *id1 = NULL, *id2 = NULL;
791 cc8021af 2021-10-12 thomas struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
792 cc8021af 2021-10-12 thomas struct got_blob_object *blob1 = NULL, *blob2 = NULL;
794 cc8021af 2021-10-12 thomas TAILQ_FOREACH(pe, paths, entry) {
795 cc8021af 2021-10-12 thomas int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
796 cc8021af 2021-10-12 thomas mode_t mode1 = 0, mode2 = 0;
798 cc8021af 2021-10-12 thomas free(id1);
799 cc8021af 2021-10-12 thomas id1 = NULL;
800 cc8021af 2021-10-12 thomas free(id2);
801 cc8021af 2021-10-12 thomas id2 = NULL;
802 cc8021af 2021-10-12 thomas if (subtree1) {
803 cc8021af 2021-10-12 thomas got_object_tree_close(subtree1);
804 cc8021af 2021-10-12 thomas subtree1 = NULL;
806 cc8021af 2021-10-12 thomas if (subtree2) {
807 cc8021af 2021-10-12 thomas got_object_tree_close(subtree2);
808 cc8021af 2021-10-12 thomas subtree2 = NULL;
810 cc8021af 2021-10-12 thomas if (blob1) {
811 cc8021af 2021-10-12 thomas got_object_blob_close(blob1);
812 cc8021af 2021-10-12 thomas blob1 = NULL;
814 cc8021af 2021-10-12 thomas if (blob2) {
815 cc8021af 2021-10-12 thomas got_object_blob_close(blob2);
816 cc8021af 2021-10-12 thomas blob2 = NULL;
819 cc8021af 2021-10-12 thomas err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
820 cc8021af 2021-10-12 thomas pe->path);
821 cc8021af 2021-10-12 thomas if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
822 cc8021af 2021-10-12 thomas goto done;
823 cc8021af 2021-10-12 thomas err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
824 cc8021af 2021-10-12 thomas pe->path);
825 cc8021af 2021-10-12 thomas if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
826 cc8021af 2021-10-12 thomas goto done;
827 cc8021af 2021-10-12 thomas if (id1 == NULL && id2 == NULL) {
828 cc8021af 2021-10-12 thomas err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
829 cc8021af 2021-10-12 thomas goto done;
831 cc8021af 2021-10-12 thomas if (id1) {
832 cc8021af 2021-10-12 thomas err = got_object_get_type(&type1, repo, id1);
834 cc8021af 2021-10-12 thomas goto done;
836 cc8021af 2021-10-12 thomas if (id2) {
837 cc8021af 2021-10-12 thomas err = got_object_get_type(&type2, repo, id2);
839 cc8021af 2021-10-12 thomas goto done;
841 cc8021af 2021-10-12 thomas if (type1 == GOT_OBJ_TYPE_ANY &&
842 cc8021af 2021-10-12 thomas type2 == GOT_OBJ_TYPE_ANY) {
843 cc8021af 2021-10-12 thomas err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
844 cc8021af 2021-10-12 thomas goto done;
845 cc8021af 2021-10-12 thomas } else if (type1 != GOT_OBJ_TYPE_ANY &&
846 cc8021af 2021-10-12 thomas type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
847 cc8021af 2021-10-12 thomas err = got_error(GOT_ERR_OBJ_TYPE);
848 cc8021af 2021-10-12 thomas goto done;
851 cc8021af 2021-10-12 thomas if (type1 == GOT_OBJ_TYPE_BLOB ||
852 cc8021af 2021-10-12 thomas type2 == GOT_OBJ_TYPE_BLOB) {
853 cc8021af 2021-10-12 thomas if (id1) {
854 cc8021af 2021-10-12 thomas err = got_object_open_as_blob(&blob1, repo,
855 f4ae6ddb 2022-07-01 thomas id1, 8192, fd1);
857 cc8021af 2021-10-12 thomas goto done;
859 cc8021af 2021-10-12 thomas if (id2) {
860 cc8021af 2021-10-12 thomas err = got_object_open_as_blob(&blob2, repo,
861 f4ae6ddb 2022-07-01 thomas id2, 8192, fd2);
863 cc8021af 2021-10-12 thomas goto done;
865 a0f32f33 2022-06-13 thomas err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
866 cc8021af 2021-10-12 thomas id1 ? pe->path : "/dev/null",
867 cc8021af 2021-10-12 thomas id2 ? pe->path : "/dev/null",
868 cc8021af 2021-10-12 thomas mode1, mode2, repo);
870 cc8021af 2021-10-12 thomas goto done;
871 cc8021af 2021-10-12 thomas } else if (type1 == GOT_OBJ_TYPE_TREE ||
872 cc8021af 2021-10-12 thomas type2 == GOT_OBJ_TYPE_TREE) {
873 cc8021af 2021-10-12 thomas if (id1) {
874 cc8021af 2021-10-12 thomas err = got_object_open_as_tree(&subtree1, repo,
877 cc8021af 2021-10-12 thomas goto done;
879 cc8021af 2021-10-12 thomas if (id2) {
880 cc8021af 2021-10-12 thomas err = got_object_open_as_tree(&subtree2, repo,
883 cc8021af 2021-10-12 thomas goto done;
885 a0f32f33 2022-06-13 thomas err = got_diff_tree(subtree1, subtree2, f1, f2,
886 19a6a6b5 2022-07-01 thomas fd1, fd2,
887 cc8021af 2021-10-12 thomas id1 ? pe->path : "/dev/null",
888 cc8021af 2021-10-12 thomas id2 ? pe->path : "/dev/null",
889 cc8021af 2021-10-12 thomas repo, cb, cb_arg, 1);
891 cc8021af 2021-10-12 thomas goto done;
893 cc8021af 2021-10-12 thomas err = got_error(GOT_ERR_OBJ_TYPE);
894 cc8021af 2021-10-12 thomas goto done;
898 cc8021af 2021-10-12 thomas free(id1);
899 cc8021af 2021-10-12 thomas free(id2);
900 cc8021af 2021-10-12 thomas if (subtree1)
901 cc8021af 2021-10-12 thomas got_object_tree_close(subtree1);
902 cc8021af 2021-10-12 thomas if (subtree2)
903 cc8021af 2021-10-12 thomas got_object_tree_close(subtree2);
905 11528a82 2018-05-19 stsp got_object_blob_close(blob1);
907 11528a82 2018-05-19 stsp got_object_blob_close(blob2);
908 474b4f94 2017-11-30 stsp return err;
911 9b4458b4 2022-06-26 thomas static const struct got_error *
912 82c78e96 2022-08-06 thomas show_object_id(struct got_diff_line **lines, size_t *nlines,
913 82c78e96 2022-08-06 thomas const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
915 9b4458b4 2022-06-26 thomas const struct got_error *err;
917 9b4458b4 2022-06-26 thomas off_t outoff = 0;
919 9b4458b4 2022-06-26 thomas n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
920 7e1f3a3d 2022-08-16 thomas if (n < 0)
921 7e1f3a3d 2022-08-16 thomas return got_error_from_errno("fprintf");
923 82c78e96 2022-08-06 thomas if (lines != NULL && *lines != NULL) {
924 9b4458b4 2022-06-26 thomas if (*nlines == 0) {
925 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0,
926 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
928 9b4458b4 2022-06-26 thomas return err;
930 82c78e96 2022-08-06 thomas outoff = (*lines)[*nlines - 1].offset;
932 9b4458b4 2022-06-26 thomas outoff += n;
933 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
934 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
936 9b4458b4 2022-06-26 thomas return err;
939 9b4458b4 2022-06-26 thomas return NULL;
942 9b4458b4 2022-06-26 thomas static const struct got_error *
943 82c78e96 2022-08-06 thomas diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
944 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
945 19a6a6b5 2022-07-01 thomas struct got_object_id *id1, struct got_object_id *id2,
946 0c6f49ba 2022-07-01 thomas struct got_pathlist_head *paths, const char *label1, const char *label2,
947 0c6f49ba 2022-07-01 thomas int diff_context, int ignore_whitespace, int force_text_diff,
948 25ec7006 2022-07-01 thomas struct got_repository *repo, FILE *outfile,
949 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo)
951 11528a82 2018-05-19 stsp const struct got_error *err;
952 11528a82 2018-05-19 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
953 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
954 82c78e96 2022-08-06 thomas int want_linemeta = (lines != NULL && *lines != NULL);
956 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
957 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
960 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo, id1);
965 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo, id2);
970 25ec7006 2022-07-01 thomas arg.diff_algo = diff_algo;
971 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
972 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
973 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
974 aaa13589 2019-06-01 stsp arg.outfile = outfile;
975 82c78e96 2022-08-06 thomas if (want_linemeta) {
976 82c78e96 2022-08-06 thomas arg.lines = *lines;
977 fe621944 2020-11-10 stsp arg.nlines = *nlines;
979 82c78e96 2022-08-06 thomas arg.lines = NULL;
980 fe621944 2020-11-10 stsp arg.nlines = 0;
982 cc8021af 2021-10-12 thomas if (paths == NULL || TAILQ_EMPTY(paths)) {
983 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
984 19a6a6b5 2022-07-01 thomas label1, label2, repo,
985 19a6a6b5 2022-07-01 thomas got_diff_blob_output_unidiff, &arg, 1);
987 19a6a6b5 2022-07-01 thomas err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
988 cc8021af 2021-10-12 thomas got_diff_blob_output_unidiff, &arg);
990 82c78e96 2022-08-06 thomas if (want_linemeta) {
991 82c78e96 2022-08-06 thomas *lines = arg.lines; /* was likely re-allocated */
992 fe621944 2020-11-10 stsp *nlines = arg.nlines;
996 11528a82 2018-05-19 stsp got_object_tree_close(tree1);
998 11528a82 2018-05-19 stsp got_object_tree_close(tree2);
999 11528a82 2018-05-19 stsp return err;
1002 11528a82 2018-05-19 stsp const struct got_error *
1003 82c78e96 2022-08-06 thomas got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1004 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
1005 19a6a6b5 2022-07-01 thomas struct got_object_id *id1, struct got_object_id *id2,
1006 0c6f49ba 2022-07-01 thomas struct got_pathlist_head *paths, const char *label1, const char *label2,
1007 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1008 25ec7006 2022-07-01 thomas int force_text_diff, struct got_repository *repo, FILE *outfile)
1010 9b4458b4 2022-06-26 thomas const struct got_error *err;
1011 9b4458b4 2022-06-26 thomas char *idstr = NULL;
1013 9b4458b4 2022-06-26 thomas if (id1 == NULL && id2 == NULL)
1014 9b4458b4 2022-06-26 thomas return got_error(GOT_ERR_NO_OBJ);
1016 9b4458b4 2022-06-26 thomas if (id1) {
1017 9b4458b4 2022-06-26 thomas err = got_object_id_str(&idstr, id1);
1018 9b4458b4 2022-06-26 thomas if (err)
1019 9b4458b4 2022-06-26 thomas goto done;
1020 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1021 9b4458b4 2022-06-26 thomas if (err)
1022 9b4458b4 2022-06-26 thomas goto done;
1023 9b4458b4 2022-06-26 thomas free(idstr);
1024 9b4458b4 2022-06-26 thomas idstr = NULL;
1025 9b4458b4 2022-06-26 thomas } else {
1026 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1027 82c78e96 2022-08-06 thomas outfile);
1028 9b4458b4 2022-06-26 thomas if (err)
1029 9b4458b4 2022-06-26 thomas goto done;
1032 9b4458b4 2022-06-26 thomas if (id2) {
1033 9b4458b4 2022-06-26 thomas err = got_object_id_str(&idstr, id2);
1034 9b4458b4 2022-06-26 thomas if (err)
1035 9b4458b4 2022-06-26 thomas goto done;
1036 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1037 9b4458b4 2022-06-26 thomas if (err)
1038 9b4458b4 2022-06-26 thomas goto done;
1039 9b4458b4 2022-06-26 thomas free(idstr);
1040 9b4458b4 2022-06-26 thomas idstr = NULL;
1041 9b4458b4 2022-06-26 thomas } else {
1042 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1043 82c78e96 2022-08-06 thomas outfile);
1044 9b4458b4 2022-06-26 thomas if (err)
1045 9b4458b4 2022-06-26 thomas goto done;
1048 82c78e96 2022-08-06 thomas err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1049 82c78e96 2022-08-06 thomas paths, label1, label2, diff_context, ignore_whitespace,
1050 25ec7006 2022-07-01 thomas force_text_diff, repo, outfile, diff_algo);
1052 9b4458b4 2022-06-26 thomas free(idstr);
1053 9b4458b4 2022-06-26 thomas return err;
1056 9b4458b4 2022-06-26 thomas const struct got_error *
1057 82c78e96 2022-08-06 thomas got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1058 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
1059 19a6a6b5 2022-07-01 thomas struct got_object_id *id1, struct got_object_id *id2,
1060 25ec7006 2022-07-01 thomas struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1061 64453f7e 2020-11-21 stsp int diff_context, int ignore_whitespace, int force_text_diff,
1062 15a94983 2018-12-23 stsp struct got_repository *repo, FILE *outfile)
1064 11528a82 2018-05-19 stsp const struct got_error *err;
1065 11528a82 2018-05-19 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1066 9b4458b4 2022-06-26 thomas char *idstr = NULL;
1068 15a94983 2018-12-23 stsp if (id2 == NULL)
1069 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
1071 15a94983 2018-12-23 stsp if (id1) {
1072 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit1, repo, id1);
1074 cd0acaa7 2018-05-20 stsp goto done;
1075 9b4458b4 2022-06-26 thomas err = got_object_id_str(&idstr, id1);
1076 9b4458b4 2022-06-26 thomas if (err)
1077 9b4458b4 2022-06-26 thomas goto done;
1078 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "commit", '-', idstr,
1079 82c78e96 2022-08-06 thomas outfile);
1080 9b4458b4 2022-06-26 thomas if (err)
1081 9b4458b4 2022-06-26 thomas goto done;
1082 9b4458b4 2022-06-26 thomas free(idstr);
1083 9b4458b4 2022-06-26 thomas idstr = NULL;
1084 9b4458b4 2022-06-26 thomas } else {
1085 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1086 82c78e96 2022-08-06 thomas outfile);
1087 9b4458b4 2022-06-26 thomas if (err)
1088 9b4458b4 2022-06-26 thomas goto done;
1091 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, repo, id2);
1093 9b697879 2018-05-20 stsp goto done;
1095 9b4458b4 2022-06-26 thomas err = got_object_id_str(&idstr, id2);
1096 9b4458b4 2022-06-26 thomas if (err)
1097 9b4458b4 2022-06-26 thomas goto done;
1098 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1099 9b4458b4 2022-06-26 thomas if (err)
1100 9b4458b4 2022-06-26 thomas goto done;
1102 82c78e96 2022-08-06 thomas err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1103 15a94983 2018-12-23 stsp commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1104 cc8021af 2021-10-12 thomas got_object_commit_get_tree_id(commit2), paths, "", "",
1105 25ec7006 2022-07-01 thomas diff_context, ignore_whitespace, force_text_diff, repo, outfile,
1106 25ec7006 2022-07-01 thomas diff_algo);
1108 11528a82 2018-05-19 stsp if (commit1)
1109 11528a82 2018-05-19 stsp got_object_commit_close(commit1);
1110 11528a82 2018-05-19 stsp if (commit2)
1111 11528a82 2018-05-19 stsp got_object_commit_close(commit2);
1112 9b4458b4 2022-06-26 thomas free(idstr);
1113 11528a82 2018-05-19 stsp return err;
1116 dc424a06 2019-08-07 stsp const struct got_error *
1117 fe621944 2020-11-10 stsp got_diff_files(struct got_diffreg_result **resultp,
1118 dd2e2f52 2022-07-01 thomas FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1119 dd2e2f52 2022-07-01 thomas const char *label2, int diff_context, int ignore_whitespace,
1120 25ec7006 2022-07-01 thomas int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1122 dc424a06 2019-08-07 stsp const struct got_error *err = NULL;
1123 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
1125 fe621944 2020-11-10 stsp if (resultp)
1126 fe621944 2020-11-10 stsp *resultp = NULL;
1128 dc424a06 2019-08-07 stsp if (outfile) {
1129 dc424a06 2019-08-07 stsp fprintf(outfile, "file - %s\n",
1130 dd2e2f52 2022-07-01 thomas f1_exists ? label1 : "/dev/null");
1131 dc424a06 2019-08-07 stsp fprintf(outfile, "file + %s\n",
1132 dd2e2f52 2022-07-01 thomas f2_exists ? label2 : "/dev/null");
1135 25ec7006 2022-07-01 thomas err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1136 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
1138 fe621944 2020-11-10 stsp goto done;
1140 fe621944 2020-11-10 stsp if (outfile) {
1141 fe621944 2020-11-10 stsp err = got_diffreg_output(NULL, NULL, diffreg_result,
1142 dd2e2f52 2022-07-01 thomas f1_exists, f2_exists, label1, label2,
1143 1cb46f00 2020-11-21 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1145 dc424a06 2019-08-07 stsp goto done;
1149 fe621944 2020-11-10 stsp if (resultp && err == NULL)
1150 fe621944 2020-11-10 stsp *resultp = diffreg_result;
1151 fe621944 2020-11-10 stsp else if (diffreg_result) {
1152 fe621944 2020-11-10 stsp const struct got_error *free_err;
1153 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
1154 fe621944 2020-11-10 stsp if (free_err && err == NULL)
1155 fe621944 2020-11-10 stsp err = free_err;
1158 dc424a06 2019-08-07 stsp return err;