2 7d283eee 2017-11-29 stsp * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
4 7d283eee 2017-11-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 7d283eee 2017-11-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 7d283eee 2017-11-29 stsp * copyright notice and this permission notice appear in all copies.
8 7d283eee 2017-11-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 7d283eee 2017-11-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 7d283eee 2017-11-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 7d283eee 2017-11-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 7d283eee 2017-11-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 7d283eee 2017-11-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 7d283eee 2017-11-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 7d283eee 2017-11-29 stsp #include <sys/queue.h>
18 1c7f0520 2017-11-29 stsp #include <sys/stat.h>
20 7d283eee 2017-11-29 stsp #include <stdio.h>
21 7d283eee 2017-11-29 stsp #include <stdlib.h>
22 7d283eee 2017-11-29 stsp #include <string.h>
23 f9d67749 2017-11-30 stsp #include <limits.h>
24 7d283eee 2017-11-29 stsp #include <sha1.h>
25 7d283eee 2017-11-29 stsp #include <zlib.h>
27 7d283eee 2017-11-29 stsp #include "got_object.h"
28 e09a504c 2019-06-28 stsp #include "got_repository.h"
29 7d283eee 2017-11-29 stsp #include "got_error.h"
30 789689b5 2017-11-30 stsp #include "got_diff.h"
31 324d37e7 2019-05-11 stsp #include "got_path.h"
32 0208f208 2020-05-05 stsp #include "got_cancel.h"
33 0208f208 2020-05-05 stsp #include "got_worktree.h"
34 a558dd1b 2022-06-08 stsp #include "got_opentemp.h"
36 718b3ab0 2018-03-17 stsp #include "got_lib_diff.h"
37 15a94983 2018-12-23 stsp #include "got_lib_delta.h"
38 15a94983 2018-12-23 stsp #include "got_lib_inflate.h"
39 15a94983 2018-12-23 stsp #include "got_lib_object.h"
41 404c43c4 2018-06-21 stsp static const struct got_error *
42 c7d5c43c 2022-08-04 mark add_line_metadata(struct got_diff_line **lines, size_t *nlines,
43 c7d5c43c 2022-08-04 mark off_t off, uint8_t type)
45 c7d5c43c 2022-08-04 mark struct got_diff_line *p;
47 c7d5c43c 2022-08-04 mark p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
48 fe621944 2020-11-10 stsp if (p == NULL)
49 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
51 c7d5c43c 2022-08-04 mark (*lines)[*nlines].offset = off;
52 c7d5c43c 2022-08-04 mark (*lines)[*nlines].type = type;
53 fe621944 2020-11-10 stsp (*nlines)++;
55 fe621944 2020-11-10 stsp return NULL;
58 fe621944 2020-11-10 stsp static const struct got_error *
59 c7d5c43c 2022-08-04 mark diff_blobs(struct got_diff_line **lines, size_t *nlines,
60 fe621944 2020-11-10 stsp struct got_diffreg_result **resultp, struct got_blob_object *blob1,
61 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
62 46f68b20 2019-10-19 stsp const char *label1, const char *label2, mode_t mode1, mode_t mode2,
63 4b752015 2022-06-30 stsp int diff_context, int ignore_whitespace, int force_text_diff, FILE *outfile,
64 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo)
66 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
67 f78b0693 2017-11-29 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
68 f78b0693 2017-11-29 stsp char hex2[SHA1_DIGEST_STRING_LENGTH];
69 58e31a80 2022-06-27 op const char *idstr1 = NULL, *idstr2 = NULL;
70 be659d10 2020-11-18 stsp off_t size1, size2;
71 fe621944 2020-11-10 stsp struct got_diffreg_result *result;
72 fe621944 2020-11-10 stsp off_t outoff = 0;
75 2d61e381 2022-08-30 mark if (lines && *lines && *nlines > 0)
76 c7d5c43c 2022-08-04 mark outoff = (*lines)[*nlines - 1].offset;
77 c7d5c43c 2022-08-04 mark else if (lines) {
78 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
83 fe621944 2020-11-10 stsp if (resultp)
84 fe621944 2020-11-10 stsp *resultp = NULL;
87 a558dd1b 2022-06-08 stsp err = got_opentemp_truncate(f1);
92 a558dd1b 2022-06-08 stsp err = got_opentemp_truncate(f2);
98 98abbc84 2017-11-30 stsp if (blob1) {
99 f934cf2c 2018-02-12 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
100 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
105 98abbc84 2017-11-30 stsp idstr1 = "/dev/null";
108 98abbc84 2017-11-30 stsp if (blob2) {
109 f934cf2c 2018-02-12 stsp idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
110 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
115 98abbc84 2017-11-30 stsp idstr2 = "/dev/null";
117 09de383e 2018-12-24 stsp if (outfile) {
118 46f68b20 2019-10-19 stsp char *modestr1 = NULL, *modestr2 = NULL;
119 40dde666 2020-07-23 stsp int modebits;
120 46f68b20 2019-10-19 stsp if (mode1 && mode1 != mode2) {
121 40dde666 2020-07-23 stsp if (S_ISLNK(mode1))
122 40dde666 2020-07-23 stsp modebits = S_IFLNK;
124 40dde666 2020-07-23 stsp modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
125 46f68b20 2019-10-19 stsp if (asprintf(&modestr1, " (mode %o)",
126 40dde666 2020-07-23 stsp mode1 & modebits) == -1) {
127 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
131 46f68b20 2019-10-19 stsp if (mode2 && mode1 != mode2) {
132 40dde666 2020-07-23 stsp if (S_ISLNK(mode2))
133 40dde666 2020-07-23 stsp modebits = S_IFLNK;
135 40dde666 2020-07-23 stsp modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
136 46f68b20 2019-10-19 stsp if (asprintf(&modestr2, " (mode %o)",
137 40dde666 2020-07-23 stsp mode2 & modebits) == -1) {
138 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
142 fe621944 2020-11-10 stsp n = fprintf(outfile, "blob - %s%s\n", idstr1,
143 46f68b20 2019-10-19 stsp modestr1 ? modestr1 : "");
146 fe621944 2020-11-10 stsp outoff += n;
147 c7d5c43c 2022-08-04 mark if (lines) {
148 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
149 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_BLOB_MIN);
154 fe621944 2020-11-10 stsp n = fprintf(outfile, "blob + %s%s\n", idstr2,
155 46f68b20 2019-10-19 stsp modestr2 ? modestr2 : "");
158 fe621944 2020-11-10 stsp outoff += n;
159 c7d5c43c 2022-08-04 mark if (lines) {
160 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
161 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_BLOB_PLUS);
166 46f68b20 2019-10-19 stsp free(modestr1);
167 46f68b20 2019-10-19 stsp free(modestr2);
169 4b752015 2022-06-30 stsp err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
170 4b752015 2022-06-30 stsp force_text_diff);
174 fe621944 2020-11-10 stsp if (outfile) {
175 c7d5c43c 2022-08-04 mark err = got_diffreg_output(lines, nlines, result,
176 1cb46f00 2020-11-21 stsp blob1 != NULL, blob2 != NULL,
177 fe621944 2020-11-10 stsp label1 ? label1 : idstr1,
178 fe621944 2020-11-10 stsp label2 ? label2 : idstr2,
179 fe621944 2020-11-10 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
184 fe621944 2020-11-10 stsp if (resultp && err == NULL)
185 fe621944 2020-11-10 stsp *resultp = result;
187 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
188 fe621944 2020-11-10 stsp if (free_err && err == NULL)
189 fe621944 2020-11-10 stsp err = free_err;
192 7d283eee 2017-11-29 stsp return err;
195 aaa13589 2019-06-01 stsp const struct got_error *
196 aaa13589 2019-06-01 stsp got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
197 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
198 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
199 b72706c3 2022-06-01 stsp const char *label1, const char *label2, mode_t mode1, mode_t mode2,
200 b72706c3 2022-06-01 stsp struct got_repository *repo)
202 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg *a = arg;
204 c7d5c43c 2022-08-04 mark return diff_blobs(&a->lines, &a->nlines, NULL,
205 b72706c3 2022-06-01 stsp blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
206 4b752015 2022-06-30 stsp a->ignore_whitespace, a->force_text_diff, a->outfile, a->diff_algo);
209 404c43c4 2018-06-21 stsp const struct got_error *
210 c7d5c43c 2022-08-04 mark got_diff_blob(struct got_diff_line **lines, size_t*nlines,
211 fe621944 2020-11-10 stsp struct got_blob_object *blob1, struct got_blob_object *blob2,
212 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, const char *label1, const char *label2,
213 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo, int diff_context,
214 4b752015 2022-06-30 stsp int ignore_whitespace, int force_text_diff, FILE *outfile)
216 c7d5c43c 2022-08-04 mark return diff_blobs(lines, nlines, NULL, blob1, blob2, f1, f2,
217 64453f7e 2020-11-21 stsp label1, label2, 0, 0, diff_context, ignore_whitespace,
218 4b752015 2022-06-30 stsp force_text_diff, outfile, diff_algo);
221 7f1f93af 2019-08-06 stsp static const struct got_error *
222 fe621944 2020-11-10 stsp diff_blob_file(struct got_diffreg_result **resultp,
223 b72706c3 2022-06-01 stsp struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
224 c87842d5 2022-09-23 mark FILE *f2, int f2_exists, struct stat *sb2, const char *label2,
225 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
226 4b752015 2022-06-30 stsp int force_text_diff, FILE *outfile)
228 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
229 b72f483a 2019-02-05 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
230 58e31a80 2022-06-27 op const char *idstr1 = NULL;
231 fe621944 2020-11-10 stsp struct got_diffreg_result *result = NULL;
233 fe621944 2020-11-10 stsp if (resultp)
234 fe621944 2020-11-10 stsp *resultp = NULL;
237 b72f483a 2019-02-05 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
239 b72f483a 2019-02-05 stsp idstr1 = "/dev/null";
241 7f1f93af 2019-08-06 stsp if (outfile) {
242 c87842d5 2022-09-23 mark char *mode = NULL;
244 c87842d5 2022-09-23 mark /* display file mode for new added files only */
245 c87842d5 2022-09-23 mark if (f2_exists && blob1 == NULL) {
246 c87842d5 2022-09-23 mark int mmask = (S_IRWXU | S_IRWXG | S_IRWXO);
248 c87842d5 2022-09-23 mark if (S_ISLNK(sb2->st_mode))
249 c87842d5 2022-09-23 mark mmask = S_IFLNK;
250 c87842d5 2022-09-23 mark if (asprintf(&mode, " (mode %o)",
251 c87842d5 2022-09-23 mark sb2->st_mode & mmask) == -1)
252 c87842d5 2022-09-23 mark return got_error_from_errno("asprintf");
254 4ce46740 2019-08-08 stsp fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
255 c87842d5 2022-09-23 mark fprintf(outfile, "file + %s%s\n",
256 c87842d5 2022-09-23 mark f2_exists ? label2 : "/dev/null", mode ? mode : "");
257 c87842d5 2022-09-23 mark free(mode);
260 4b752015 2022-06-30 stsp err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
261 4b752015 2022-06-30 stsp force_text_diff);
265 fe621944 2020-11-10 stsp if (outfile) {
266 1cb46f00 2020-11-21 stsp err = got_diffreg_output(NULL, NULL, result,
267 49d4a017 2022-06-30 stsp blob1 != NULL, f2_exists,
268 1cb46f00 2020-11-21 stsp label2, /* show local file's path, not a blob ID */
269 1cb46f00 2020-11-21 stsp label2, GOT_DIFF_OUTPUT_UNIDIFF,
270 1cb46f00 2020-11-21 stsp diff_context, outfile);
275 fe621944 2020-11-10 stsp if (resultp && err == NULL)
276 fe621944 2020-11-10 stsp *resultp = result;
277 fe621944 2020-11-10 stsp else if (result) {
278 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
279 fe621944 2020-11-10 stsp if (free_err && err == NULL)
280 fe621944 2020-11-10 stsp err = free_err;
283 b72f483a 2019-02-05 stsp return err;
286 7f1f93af 2019-08-06 stsp const struct got_error *
287 b72706c3 2022-06-01 stsp got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
288 c87842d5 2022-09-23 mark const char *label1, FILE *f2, int f2_exists, struct stat *sb2,
289 4b752015 2022-06-30 stsp const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
290 4b752015 2022-06-30 stsp int ignore_whitespace, int force_text_diff, FILE *outfile)
292 49d4a017 2022-06-30 stsp return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
293 c87842d5 2022-09-23 mark sb2, label2, diff_algo, diff_context, ignore_whitespace,
294 c87842d5 2022-09-23 mark force_text_diff, outfile);
297 474b4f94 2017-11-30 stsp static const struct got_error *
298 49d4a017 2022-06-30 stsp diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
299 f9d37699 2022-06-28 stsp const char *label, mode_t mode, struct got_repository *repo,
300 b72706c3 2022-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
302 4e22badc 2017-11-30 stsp const struct got_error *err;
303 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
304 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
306 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
308 4e22badc 2017-11-30 stsp return err;
310 49d4a017 2022-06-30 stsp err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
313 49d4a017 2022-06-30 stsp err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
314 b72706c3 2022-06-01 stsp NULL, label, 0, mode, repo);
316 2acfca77 2018-04-01 stsp got_object_close(obj);
318 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
319 2acfca77 2018-04-01 stsp return err;
322 474b4f94 2017-11-30 stsp static const struct got_error *
323 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
324 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
325 f9d37699 2022-06-28 stsp const char *label1, const char *label2,
326 b72706c3 2022-06-01 stsp mode_t mode1, mode_t mode2, struct got_repository *repo,
327 b72706c3 2022-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
329 6a213ccb 2017-11-30 stsp const struct got_error *err;
330 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
331 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
332 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
333 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
335 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
337 730a8aa0 2018-04-24 stsp return err;
339 15a94983 2018-12-23 stsp if (obj1->type != GOT_OBJ_TYPE_BLOB) {
340 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
344 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
347 15a94983 2018-12-23 stsp if (obj2->type != GOT_OBJ_TYPE_BLOB) {
348 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
352 eb81bc23 2022-06-28 tracey err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
356 eb81bc23 2022-06-28 tracey err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
360 b72706c3 2022-06-01 stsp err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
361 b72706c3 2022-06-01 stsp mode1, mode2, repo);
364 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
366 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
368 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
370 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
371 6a213ccb 2017-11-30 stsp return err;
374 474b4f94 2017-11-30 stsp static const struct got_error *
375 49d4a017 2022-06-30 stsp diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
376 49d4a017 2022-06-30 stsp FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
377 f9d37699 2022-06-28 stsp got_diff_blob_cb cb, void *cb_arg)
379 365fb436 2017-11-30 stsp const struct got_error *err;
380 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
381 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
383 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
385 365fb436 2017-11-30 stsp return err;
387 49d4a017 2022-06-30 stsp err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
390 49d4a017 2022-06-30 stsp err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
391 b72706c3 2022-06-01 stsp mode, 0, repo);
393 2acfca77 2018-04-01 stsp got_object_close(obj);
395 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
396 2acfca77 2018-04-01 stsp return err;
399 474b4f94 2017-11-30 stsp static const struct got_error *
400 49d4a017 2022-06-30 stsp diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
401 49d4a017 2022-06-30 stsp const char *label, struct got_repository *repo, got_diff_blob_cb cb,
402 49d4a017 2022-06-30 stsp void *cb_arg, int diff_content)
404 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
405 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
406 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
408 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
412 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
413 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
417 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
421 49d4a017 2022-06-30 stsp err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
422 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
425 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
426 9c70d4c3 2017-11-30 stsp if (treeobj)
427 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
428 9c70d4c3 2017-11-30 stsp return err;
431 474b4f94 2017-11-30 stsp static const struct got_error *
432 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
433 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
434 f9d37699 2022-06-28 stsp const char *label1, const char *label2,
435 b72706c3 2022-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
436 b72706c3 2022-06-01 stsp int diff_content)
438 f6861a81 2018-09-13 stsp const struct got_error *err;
439 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
440 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
441 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
442 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
444 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
448 15a94983 2018-12-23 stsp if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
449 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
453 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
457 15a94983 2018-12-23 stsp if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
458 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
462 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
466 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
470 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
471 f9d37699 2022-06-28 stsp label1, label2, repo, cb, cb_arg, diff_content);
475 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
477 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
478 789689b5 2017-11-30 stsp if (treeobj1)
479 789689b5 2017-11-30 stsp got_object_close(treeobj1);
480 789689b5 2017-11-30 stsp if (treeobj2)
481 789689b5 2017-11-30 stsp got_object_close(treeobj2);
482 789689b5 2017-11-30 stsp return err;
485 474b4f94 2017-11-30 stsp static const struct got_error *
486 49d4a017 2022-06-30 stsp diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
487 49d4a017 2022-06-30 stsp FILE *f2, const char *label, struct got_repository *repo,
488 f9d37699 2022-06-28 stsp got_diff_blob_cb cb, void *cb_arg, int diff_content)
490 f6861a81 2018-09-13 stsp const struct got_error *err;
491 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
492 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
494 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
498 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
499 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
503 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
507 49d4a017 2022-06-30 stsp err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
508 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
511 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
512 2c56f2ce 2017-11-30 stsp if (treeobj)
513 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
514 2c56f2ce 2017-11-30 stsp return err;
517 474b4f94 2017-11-30 stsp static const struct got_error *
518 74671950 2018-02-11 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
519 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
520 aaa13589 2019-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
522 013404a9 2017-11-30 stsp /* XXX TODO */
523 474b4f94 2017-11-30 stsp return NULL;
526 474b4f94 2017-11-30 stsp static const struct got_error *
527 b72706c3 2022-06-01 stsp diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
528 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
529 f9d37699 2022-06-28 stsp const char *label1, const char *label2,
530 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
531 31b4484f 2019-07-27 stsp int diff_content)
533 f6861a81 2018-09-13 stsp const struct got_error *err = NULL;
534 19ae6da1 2018-11-05 stsp int id_match;
536 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te1))
537 63c5ca5d 2019-08-24 stsp return NULL;
539 474b4f94 2017-11-30 stsp if (te2 == NULL) {
540 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
541 49d4a017 2022-06-30 stsp err = diff_deleted_tree(&te1->id, f1, fd1, f2,
542 49d4a017 2022-06-30 stsp label1, repo, cb, cb_arg, diff_content);
544 31b4484f 2019-07-27 stsp if (diff_content)
545 f9d37699 2022-06-28 stsp err = diff_deleted_blob(&te1->id, f1, fd1,
546 49d4a017 2022-06-30 stsp f2, label1, te1->mode, repo, cb, cb_arg);
548 b72706c3 2022-06-01 stsp err = cb(cb_arg, NULL, NULL, NULL, NULL,
549 b72706c3 2022-06-01 stsp &te1->id, NULL, label1, NULL,
550 b72706c3 2022-06-01 stsp te1->mode, 0, repo);
552 f6861a81 2018-09-13 stsp return err;
553 63c5ca5d 2019-08-24 stsp } else if (got_object_tree_entry_is_submodule(te2))
554 63c5ca5d 2019-08-24 stsp return NULL;
556 56e0773d 2019-11-28 stsp id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
557 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
558 19ae6da1 2018-11-05 stsp if (!id_match)
559 b72706c3 2022-06-01 stsp return diff_modified_tree(&te1->id, &te2->id, f1, f2,
560 f9d37699 2022-06-28 stsp fd1, fd2, label1, label2, repo, cb, cb_arg,
561 f9d37699 2022-06-28 stsp diff_content);
562 40dde666 2020-07-23 stsp } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
563 40dde666 2020-07-23 stsp (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
564 46f68b20 2019-10-19 stsp if (!id_match ||
565 40dde666 2020-07-23 stsp ((te1->mode & (S_IFLNK | S_IXUSR))) !=
566 40dde666 2020-07-23 stsp (te2->mode & (S_IFLNK | S_IXUSR))) {
567 31b4484f 2019-07-27 stsp if (diff_content)
568 56e0773d 2019-11-28 stsp return diff_modified_blob(&te1->id, &te2->id,
569 f9d37699 2022-06-28 stsp f1, f2, fd1, fd2, label1, label2,
570 f9d37699 2022-06-28 stsp te1->mode, te2->mode, repo, cb, cb_arg);
572 b72706c3 2022-06-01 stsp return cb(cb_arg, NULL, NULL, NULL, NULL,
573 b72706c3 2022-06-01 stsp &te1->id, &te2->id, label1, label2,
574 b72706c3 2022-06-01 stsp te1->mode, te2->mode, repo);
578 19ae6da1 2018-11-05 stsp if (id_match)
579 f6861a81 2018-09-13 stsp return NULL;
581 56e0773d 2019-11-28 stsp return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
582 aaa13589 2019-06-01 stsp cb, cb_arg);
585 474b4f94 2017-11-30 stsp static const struct got_error *
586 56e0773d 2019-11-28 stsp diff_entry_new_old(struct got_tree_entry *te2,
587 49d4a017 2022-06-30 stsp struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
588 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
589 31b4484f 2019-07-27 stsp int diff_content)
591 f6861a81 2018-09-13 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
592 63c5ca5d 2019-08-24 stsp return NULL;
594 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te2))
595 f6861a81 2018-09-13 stsp return NULL;
597 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
598 49d4a017 2022-06-30 stsp return diff_added_tree(&te2->id, f1, f2, fd2, label2,
599 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
601 31b4484f 2019-07-27 stsp if (diff_content)
602 49d4a017 2022-06-30 stsp return diff_added_blob(&te2->id, f1, f2, fd2,
603 f9d37699 2022-06-28 stsp label2, te2->mode, repo, cb, cb_arg);
605 b72706c3 2022-06-01 stsp return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
606 b72706c3 2022-06-01 stsp NULL, label2, 0, te2->mode, repo);
609 0208f208 2020-05-05 stsp const struct got_error *
610 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
611 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
612 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
613 b72706c3 2022-06-01 stsp const char *label1, const char *label2,
614 0208f208 2020-05-05 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
616 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
617 0208f208 2020-05-05 stsp struct got_pathlist_head *paths = arg;
618 0208f208 2020-05-05 stsp struct got_diff_changed_path *change = NULL;
619 0208f208 2020-05-05 stsp char *path = NULL;
621 0208f208 2020-05-05 stsp path = strdup(label2 ? label2 : label1);
622 0208f208 2020-05-05 stsp if (path == NULL)
623 0208f208 2020-05-05 stsp return got_error_from_errno("malloc");
625 0208f208 2020-05-05 stsp change = malloc(sizeof(*change));
626 0208f208 2020-05-05 stsp if (change == NULL) {
627 0208f208 2020-05-05 stsp err = got_error_from_errno("malloc");
631 0208f208 2020-05-05 stsp change->status = GOT_STATUS_NO_CHANGE;
632 0208f208 2020-05-05 stsp if (id1 == NULL)
633 0208f208 2020-05-05 stsp change->status = GOT_STATUS_ADD;
634 0208f208 2020-05-05 stsp else if (id2 == NULL)
635 0208f208 2020-05-05 stsp change->status = GOT_STATUS_DELETE;
637 0208f208 2020-05-05 stsp if (got_object_id_cmp(id1, id2) != 0)
638 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODIFY;
639 0208f208 2020-05-05 stsp else if (mode1 != mode2)
640 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODE_CHANGE;
643 46ea77db 2021-12-20 stsp err = got_pathlist_append(paths, path, change);
646 0208f208 2020-05-05 stsp free(path);
647 0208f208 2020-05-05 stsp free(change);
649 0208f208 2020-05-05 stsp return err;
652 474b4f94 2017-11-30 stsp const struct got_error *
653 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
654 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
655 f9d37699 2022-06-28 stsp const char *label1, const char *label2,
656 b72706c3 2022-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
657 b72706c3 2022-06-01 stsp int diff_content)
659 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
660 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
661 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
662 f6861a81 2018-09-13 stsp char *l1 = NULL, *l2 = NULL;
663 56e0773d 2019-11-28 stsp int tidx1 = 0, tidx2 = 0;
665 883f0469 2018-06-23 stsp if (tree1) {
666 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, 0);
667 60f50a58 2018-09-15 stsp if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
668 f6861a81 2018-09-13 stsp te1->name) == -1)
669 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
671 883f0469 2018-06-23 stsp if (tree2) {
672 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, 0);
673 60f50a58 2018-09-15 stsp if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
674 f6861a81 2018-09-13 stsp te2->name) == -1)
675 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
680 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
682 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree2,
683 1de5e065 2019-06-01 stsp te1->name);
687 f6861a81 2018-09-13 stsp if (te && asprintf(&l2, "%s%s%s", label2,
688 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te->name) == -1)
690 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
692 f9d37699 2022-06-28 stsp err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
693 f9d37699 2022-06-28 stsp l1, l2, repo, cb, cb_arg, diff_content);
699 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
701 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree1,
702 1de5e065 2019-06-01 stsp te2->name);
705 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
706 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te->name) == -1)
708 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
710 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
711 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te2->name) == -1)
713 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
715 49d4a017 2022-06-30 stsp err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
716 49d4a017 2022-06-30 stsp repo, cb, cb_arg, diff_content);
725 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, tidx1);
727 f6861a81 2018-09-13 stsp asprintf(&l1, "%s%s%s", label1,
728 f6861a81 2018-09-13 stsp label1[0] ? "/" : "", te1->name) == -1)
729 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
735 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, tidx2);
737 f6861a81 2018-09-13 stsp asprintf(&l2, "%s%s%s", label2,
738 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te2->name) == -1)
739 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
741 474b4f94 2017-11-30 stsp } while (te1 || te2);
743 11528a82 2018-05-19 stsp return err;
746 11528a82 2018-05-19 stsp const struct got_error *
747 c7d5c43c 2022-08-04 mark got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
748 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
749 f9d37699 2022-06-28 stsp struct got_object_id *id1, struct got_object_id *id2,
750 4b752015 2022-06-30 stsp const char *label1, const char *label2,
751 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo, int diff_context,
752 64453f7e 2020-11-21 stsp int ignore_whitespace, int force_text_diff,
753 64453f7e 2020-11-21 stsp struct got_repository *repo, FILE *outfile)
755 11528a82 2018-05-19 stsp const struct got_error *err;
756 11528a82 2018-05-19 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
758 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
759 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
762 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
767 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
771 c7d5c43c 2022-08-04 mark err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
772 c7d5c43c 2022-08-04 mark diff_algo, diff_context, ignore_whitespace, force_text_diff,
776 67b631c9 2021-10-10 stsp got_object_blob_close(blob1);
778 67b631c9 2021-10-10 stsp got_object_blob_close(blob2);
779 67b631c9 2021-10-10 stsp return err;
782 67b631c9 2021-10-10 stsp static const struct got_error *
783 67b631c9 2021-10-10 stsp diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
784 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
785 b72706c3 2022-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
787 67b631c9 2021-10-10 stsp const struct got_error *err = NULL;
788 67b631c9 2021-10-10 stsp struct got_pathlist_entry *pe;
789 67b631c9 2021-10-10 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
790 67b631c9 2021-10-10 stsp struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
791 67b631c9 2021-10-10 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
793 67b631c9 2021-10-10 stsp TAILQ_FOREACH(pe, paths, entry) {
794 67b631c9 2021-10-10 stsp int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
795 67b631c9 2021-10-10 stsp mode_t mode1 = 0, mode2 = 0;
798 67b631c9 2021-10-10 stsp id1 = NULL;
800 67b631c9 2021-10-10 stsp id2 = NULL;
801 67b631c9 2021-10-10 stsp if (subtree1) {
802 67b631c9 2021-10-10 stsp got_object_tree_close(subtree1);
803 67b631c9 2021-10-10 stsp subtree1 = NULL;
805 67b631c9 2021-10-10 stsp if (subtree2) {
806 67b631c9 2021-10-10 stsp got_object_tree_close(subtree2);
807 67b631c9 2021-10-10 stsp subtree2 = NULL;
809 67b631c9 2021-10-10 stsp if (blob1) {
810 67b631c9 2021-10-10 stsp got_object_blob_close(blob1);
811 67b631c9 2021-10-10 stsp blob1 = NULL;
813 67b631c9 2021-10-10 stsp if (blob2) {
814 67b631c9 2021-10-10 stsp got_object_blob_close(blob2);
815 67b631c9 2021-10-10 stsp blob2 = NULL;
818 67b631c9 2021-10-10 stsp err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
820 67b631c9 2021-10-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
822 67b631c9 2021-10-10 stsp err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
824 67b631c9 2021-10-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
826 67b631c9 2021-10-10 stsp if (id1 == NULL && id2 == NULL) {
827 67b631c9 2021-10-10 stsp err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
831 67b631c9 2021-10-10 stsp err = got_object_get_type(&type1, repo, id1);
836 67b631c9 2021-10-10 stsp err = got_object_get_type(&type2, repo, id2);
840 67b631c9 2021-10-10 stsp if (type1 == GOT_OBJ_TYPE_ANY &&
841 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_ANY) {
842 67b631c9 2021-10-10 stsp err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
844 67b631c9 2021-10-10 stsp } else if (type1 != GOT_OBJ_TYPE_ANY &&
845 67b631c9 2021-10-10 stsp type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
846 67b631c9 2021-10-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
850 67b631c9 2021-10-10 stsp if (type1 == GOT_OBJ_TYPE_BLOB ||
851 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_BLOB) {
853 67b631c9 2021-10-10 stsp err = got_object_open_as_blob(&blob1, repo,
854 eb81bc23 2022-06-28 tracey id1, 8192, fd1);
859 67b631c9 2021-10-10 stsp err = got_object_open_as_blob(&blob2, repo,
860 eb81bc23 2022-06-28 tracey id2, 8192, fd2);
864 b72706c3 2022-06-01 stsp err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
865 67b631c9 2021-10-10 stsp id1 ? pe->path : "/dev/null",
866 67b631c9 2021-10-10 stsp id2 ? pe->path : "/dev/null",
867 67b631c9 2021-10-10 stsp mode1, mode2, repo);
870 67b631c9 2021-10-10 stsp } else if (type1 == GOT_OBJ_TYPE_TREE ||
871 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_TREE) {
873 67b631c9 2021-10-10 stsp err = got_object_open_as_tree(&subtree1, repo,
879 67b631c9 2021-10-10 stsp err = got_object_open_as_tree(&subtree2, repo,
884 b72706c3 2022-06-01 stsp err = got_diff_tree(subtree1, subtree2, f1, f2,
886 67b631c9 2021-10-10 stsp id1 ? pe->path : "/dev/null",
887 67b631c9 2021-10-10 stsp id2 ? pe->path : "/dev/null",
888 67b631c9 2021-10-10 stsp repo, cb, cb_arg, 1);
892 67b631c9 2021-10-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
899 67b631c9 2021-10-10 stsp if (subtree1)
900 67b631c9 2021-10-10 stsp got_object_tree_close(subtree1);
901 67b631c9 2021-10-10 stsp if (subtree2)
902 67b631c9 2021-10-10 stsp got_object_tree_close(subtree2);
904 11528a82 2018-05-19 stsp got_object_blob_close(blob1);
906 11528a82 2018-05-19 stsp got_object_blob_close(blob2);
907 474b4f94 2017-11-30 stsp return err;
910 8469d821 2022-06-25 stsp static const struct got_error *
911 c7d5c43c 2022-08-04 mark show_object_id(struct got_diff_line **lines, size_t *nlines,
912 c7d5c43c 2022-08-04 mark const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
914 8469d821 2022-06-25 stsp const struct got_error *err;
916 8469d821 2022-06-25 stsp off_t outoff = 0;
918 8469d821 2022-06-25 stsp n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
920 36e83e5e 2022-08-16 op return got_error_from_errno("fprintf");
922 c7d5c43c 2022-08-04 mark if (lines != NULL && *lines != NULL) {
923 8469d821 2022-06-25 stsp if (*nlines == 0) {
924 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, 0,
925 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_META);
927 8469d821 2022-06-25 stsp return err;
929 c7d5c43c 2022-08-04 mark outoff = (*lines)[*nlines - 1].offset;
931 8469d821 2022-06-25 stsp outoff += n;
932 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
933 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_META);
935 8469d821 2022-06-25 stsp return err;
938 8469d821 2022-06-25 stsp return NULL;
941 8469d821 2022-06-25 stsp static const struct got_error *
942 c7d5c43c 2022-08-04 mark diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
943 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
944 f9d37699 2022-06-28 stsp struct got_object_id *id1, struct got_object_id *id2,
945 58e31a80 2022-06-27 op struct got_pathlist_head *paths, const char *label1, const char *label2,
946 58e31a80 2022-06-27 op int diff_context, int ignore_whitespace, int force_text_diff,
947 4b752015 2022-06-30 stsp struct got_repository *repo, FILE *outfile,
948 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo)
950 11528a82 2018-05-19 stsp const struct got_error *err;
951 11528a82 2018-05-19 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
952 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
953 c7d5c43c 2022-08-04 mark int want_linemeta = (lines != NULL && *lines != NULL);
955 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
956 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
959 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo, id1);
964 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo, id2);
969 4b752015 2022-06-30 stsp arg.diff_algo = diff_algo;
970 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
971 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
972 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
973 aaa13589 2019-06-01 stsp arg.outfile = outfile;
974 c7d5c43c 2022-08-04 mark if (want_linemeta) {
975 c7d5c43c 2022-08-04 mark arg.lines = *lines;
976 fe621944 2020-11-10 stsp arg.nlines = *nlines;
978 c7d5c43c 2022-08-04 mark arg.lines = NULL;
979 fe621944 2020-11-10 stsp arg.nlines = 0;
981 67b631c9 2021-10-10 stsp if (paths == NULL || TAILQ_EMPTY(paths)) {
982 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
983 f9d37699 2022-06-28 stsp label1, label2, repo,
984 f9d37699 2022-06-28 stsp got_diff_blob_output_unidiff, &arg, 1);
986 f9d37699 2022-06-28 stsp err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
987 67b631c9 2021-10-10 stsp got_diff_blob_output_unidiff, &arg);
989 c7d5c43c 2022-08-04 mark if (want_linemeta) {
990 c7d5c43c 2022-08-04 mark *lines = arg.lines; /* was likely re-allocated */
991 fe621944 2020-11-10 stsp *nlines = arg.nlines;
995 11528a82 2018-05-19 stsp got_object_tree_close(tree1);
997 11528a82 2018-05-19 stsp got_object_tree_close(tree2);
998 11528a82 2018-05-19 stsp return err;
1001 11528a82 2018-05-19 stsp const struct got_error *
1002 c7d5c43c 2022-08-04 mark got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1003 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
1004 f9d37699 2022-06-28 stsp struct got_object_id *id1, struct got_object_id *id2,
1005 58e31a80 2022-06-27 op struct got_pathlist_head *paths, const char *label1, const char *label2,
1006 4b752015 2022-06-30 stsp enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1007 4b752015 2022-06-30 stsp int force_text_diff, struct got_repository *repo, FILE *outfile)
1009 8469d821 2022-06-25 stsp const struct got_error *err;
1010 8469d821 2022-06-25 stsp char *idstr = NULL;
1012 8469d821 2022-06-25 stsp if (id1 == NULL && id2 == NULL)
1013 8469d821 2022-06-25 stsp return got_error(GOT_ERR_NO_OBJ);
1015 8469d821 2022-06-25 stsp if (id1) {
1016 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id1);
1018 8469d821 2022-06-25 stsp goto done;
1019 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1021 8469d821 2022-06-25 stsp goto done;
1022 8469d821 2022-06-25 stsp free(idstr);
1023 8469d821 2022-06-25 stsp idstr = NULL;
1025 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1028 8469d821 2022-06-25 stsp goto done;
1031 8469d821 2022-06-25 stsp if (id2) {
1032 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id2);
1034 8469d821 2022-06-25 stsp goto done;
1035 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1037 8469d821 2022-06-25 stsp goto done;
1038 8469d821 2022-06-25 stsp free(idstr);
1039 8469d821 2022-06-25 stsp idstr = NULL;
1041 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1044 8469d821 2022-06-25 stsp goto done;
1047 c7d5c43c 2022-08-04 mark err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1048 c7d5c43c 2022-08-04 mark paths, label1, label2, diff_context, ignore_whitespace,
1049 4b752015 2022-06-30 stsp force_text_diff, repo, outfile, diff_algo);
1051 8469d821 2022-06-25 stsp free(idstr);
1052 8469d821 2022-06-25 stsp return err;
1055 8469d821 2022-06-25 stsp const struct got_error *
1056 c7d5c43c 2022-08-04 mark got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1057 f9d37699 2022-06-28 stsp FILE *f1, FILE *f2, int fd1, int fd2,
1058 f9d37699 2022-06-28 stsp struct got_object_id *id1, struct got_object_id *id2,
1059 4b752015 2022-06-30 stsp struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1060 64453f7e 2020-11-21 stsp int diff_context, int ignore_whitespace, int force_text_diff,
1061 15a94983 2018-12-23 stsp struct got_repository *repo, FILE *outfile)
1063 11528a82 2018-05-19 stsp const struct got_error *err;
1064 11528a82 2018-05-19 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1065 8469d821 2022-06-25 stsp char *idstr = NULL;
1067 15a94983 2018-12-23 stsp if (id2 == NULL)
1068 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
1070 15a94983 2018-12-23 stsp if (id1) {
1071 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit1, repo, id1);
1073 cd0acaa7 2018-05-20 stsp goto done;
1074 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id1);
1076 8469d821 2022-06-25 stsp goto done;
1077 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "commit", '-', idstr,
1080 8469d821 2022-06-25 stsp goto done;
1081 8469d821 2022-06-25 stsp free(idstr);
1082 8469d821 2022-06-25 stsp idstr = NULL;
1084 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1087 8469d821 2022-06-25 stsp goto done;
1090 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, repo, id2);
1092 9b697879 2018-05-20 stsp goto done;
1094 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id2);
1096 8469d821 2022-06-25 stsp goto done;
1097 c7d5c43c 2022-08-04 mark err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1099 8469d821 2022-06-25 stsp goto done;
1101 c7d5c43c 2022-08-04 mark err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1102 15a94983 2018-12-23 stsp commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1103 67b631c9 2021-10-10 stsp got_object_commit_get_tree_id(commit2), paths, "", "",
1104 4b752015 2022-06-30 stsp diff_context, ignore_whitespace, force_text_diff, repo, outfile,
1105 4b752015 2022-06-30 stsp diff_algo);
1107 11528a82 2018-05-19 stsp if (commit1)
1108 11528a82 2018-05-19 stsp got_object_commit_close(commit1);
1109 11528a82 2018-05-19 stsp if (commit2)
1110 11528a82 2018-05-19 stsp got_object_commit_close(commit2);
1111 8469d821 2022-06-25 stsp free(idstr);
1112 11528a82 2018-05-19 stsp return err;
1115 dc424a06 2019-08-07 stsp const struct got_error *
1116 fe621944 2020-11-10 stsp got_diff_files(struct got_diffreg_result **resultp,
1117 49d4a017 2022-06-30 stsp FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1118 49d4a017 2022-06-30 stsp const char *label2, int diff_context, int ignore_whitespace,
1119 4b752015 2022-06-30 stsp int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1121 dc424a06 2019-08-07 stsp const struct got_error *err = NULL;
1122 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
1124 fe621944 2020-11-10 stsp if (resultp)
1125 fe621944 2020-11-10 stsp *resultp = NULL;
1127 dc424a06 2019-08-07 stsp if (outfile) {
1128 dc424a06 2019-08-07 stsp fprintf(outfile, "file - %s\n",
1129 49d4a017 2022-06-30 stsp f1_exists ? label1 : "/dev/null");
1130 dc424a06 2019-08-07 stsp fprintf(outfile, "file + %s\n",
1131 49d4a017 2022-06-30 stsp f2_exists ? label2 : "/dev/null");
1134 4b752015 2022-06-30 stsp err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1135 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
1137 fe621944 2020-11-10 stsp goto done;
1139 fe621944 2020-11-10 stsp if (outfile) {
1140 fe621944 2020-11-10 stsp err = got_diffreg_output(NULL, NULL, diffreg_result,
1141 49d4a017 2022-06-30 stsp f1_exists, f2_exists, label1, label2,
1142 1cb46f00 2020-11-21 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1144 dc424a06 2019-08-07 stsp goto done;
1148 fe621944 2020-11-10 stsp if (resultp && err == NULL)
1149 fe621944 2020-11-10 stsp *resultp = diffreg_result;
1150 fe621944 2020-11-10 stsp else if (diffreg_result) {
1151 fe621944 2020-11-10 stsp const struct got_error *free_err;
1152 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
1153 fe621944 2020-11-10 stsp if (free_err && err == NULL)
1154 fe621944 2020-11-10 stsp err = free_err;
1157 dc424a06 2019-08-07 stsp return err;