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 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
46 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
47 fe621944 2020-11-10 stsp if (p == NULL)
48 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
49 fe621944 2020-11-10 stsp *line_offsets = p;
50 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
51 fe621944 2020-11-10 stsp (*nlines)++;
52 fe621944 2020-11-10 stsp return NULL;
55 fe621944 2020-11-10 stsp static const struct got_error *
56 fe621944 2020-11-10 stsp diff_blobs(off_t **line_offsets, size_t *nlines,
57 fe621944 2020-11-10 stsp struct got_diffreg_result **resultp, struct got_blob_object *blob1,
58 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
59 46f68b20 2019-10-19 stsp const char *label1, const char *label2, mode_t mode1, mode_t mode2,
60 64453f7e 2020-11-21 stsp int diff_context, int ignore_whitespace, int force_text_diff, FILE *outfile)
62 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
63 f78b0693 2017-11-29 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
64 f78b0693 2017-11-29 stsp char hex2[SHA1_DIGEST_STRING_LENGTH];
65 58e31a80 2022-06-27 op const char *idstr1 = NULL, *idstr2 = NULL;
66 be659d10 2020-11-18 stsp off_t size1, size2;
67 fe621944 2020-11-10 stsp struct got_diffreg_result *result;
68 fe621944 2020-11-10 stsp off_t outoff = 0;
71 fe621944 2020-11-10 stsp if (line_offsets && *line_offsets && *nlines > 0)
72 fe621944 2020-11-10 stsp outoff = (*line_offsets)[*nlines - 1];
73 9c659ea0 2020-11-22 stsp else if (line_offsets) {
74 9c659ea0 2020-11-22 stsp err = add_line_offset(line_offsets, nlines, 0);
79 fe621944 2020-11-10 stsp if (resultp)
80 fe621944 2020-11-10 stsp *resultp = NULL;
83 a558dd1b 2022-06-08 stsp err = got_opentemp_truncate(f1);
88 a558dd1b 2022-06-08 stsp err = got_opentemp_truncate(f2);
94 98abbc84 2017-11-30 stsp if (blob1) {
95 f934cf2c 2018-02-12 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
96 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
101 98abbc84 2017-11-30 stsp idstr1 = "/dev/null";
104 98abbc84 2017-11-30 stsp if (blob2) {
105 f934cf2c 2018-02-12 stsp idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
106 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
111 98abbc84 2017-11-30 stsp idstr2 = "/dev/null";
113 09de383e 2018-12-24 stsp if (outfile) {
114 46f68b20 2019-10-19 stsp char *modestr1 = NULL, *modestr2 = NULL;
115 40dde666 2020-07-23 stsp int modebits;
116 46f68b20 2019-10-19 stsp if (mode1 && mode1 != mode2) {
117 40dde666 2020-07-23 stsp if (S_ISLNK(mode1))
118 40dde666 2020-07-23 stsp modebits = S_IFLNK;
120 40dde666 2020-07-23 stsp modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
121 46f68b20 2019-10-19 stsp if (asprintf(&modestr1, " (mode %o)",
122 40dde666 2020-07-23 stsp mode1 & modebits) == -1) {
123 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
127 46f68b20 2019-10-19 stsp if (mode2 && mode1 != mode2) {
128 40dde666 2020-07-23 stsp if (S_ISLNK(mode2))
129 40dde666 2020-07-23 stsp modebits = S_IFLNK;
131 40dde666 2020-07-23 stsp modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
132 46f68b20 2019-10-19 stsp if (asprintf(&modestr2, " (mode %o)",
133 40dde666 2020-07-23 stsp mode2 & modebits) == -1) {
134 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
138 fe621944 2020-11-10 stsp n = fprintf(outfile, "blob - %s%s\n", idstr1,
139 46f68b20 2019-10-19 stsp modestr1 ? modestr1 : "");
142 fe621944 2020-11-10 stsp outoff += n;
143 fe621944 2020-11-10 stsp if (line_offsets) {
144 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
149 fe621944 2020-11-10 stsp n = fprintf(outfile, "blob + %s%s\n", idstr2,
150 46f68b20 2019-10-19 stsp modestr2 ? modestr2 : "");
153 fe621944 2020-11-10 stsp outoff += n;
154 fe621944 2020-11-10 stsp if (line_offsets) {
155 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
160 46f68b20 2019-10-19 stsp free(modestr1);
161 46f68b20 2019-10-19 stsp free(modestr2);
163 fe621944 2020-11-10 stsp err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
164 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
168 fe621944 2020-11-10 stsp if (outfile) {
169 1cb46f00 2020-11-21 stsp err = got_diffreg_output(line_offsets, nlines, result,
170 1cb46f00 2020-11-21 stsp blob1 != NULL, blob2 != NULL,
171 fe621944 2020-11-10 stsp label1 ? label1 : idstr1,
172 fe621944 2020-11-10 stsp label2 ? label2 : idstr2,
173 fe621944 2020-11-10 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
178 fe621944 2020-11-10 stsp if (resultp && err == NULL)
179 fe621944 2020-11-10 stsp *resultp = result;
181 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
182 fe621944 2020-11-10 stsp if (free_err && err == NULL)
183 fe621944 2020-11-10 stsp err = free_err;
186 7d283eee 2017-11-29 stsp return err;
189 aaa13589 2019-06-01 stsp const struct got_error *
190 aaa13589 2019-06-01 stsp got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
191 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
192 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
193 b72706c3 2022-06-01 stsp const char *label1, const char *label2, mode_t mode1, mode_t mode2,
194 b72706c3 2022-06-01 stsp struct got_repository *repo)
196 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg *a = arg;
198 fe621944 2020-11-10 stsp return diff_blobs(&a->line_offsets, &a->nlines, NULL,
199 b72706c3 2022-06-01 stsp blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
200 64453f7e 2020-11-21 stsp a->ignore_whitespace, a->force_text_diff, a->outfile);
203 404c43c4 2018-06-21 stsp const struct got_error *
204 fe621944 2020-11-10 stsp got_diff_blob(off_t **line_offsets, size_t *nlines,
205 fe621944 2020-11-10 stsp struct got_blob_object *blob1, struct got_blob_object *blob2,
206 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, const char *label1, const char *label2,
207 b72706c3 2022-06-01 stsp int diff_context, int ignore_whitespace, int force_text_diff,
208 b72706c3 2022-06-01 stsp FILE *outfile)
210 b72706c3 2022-06-01 stsp return diff_blobs(line_offsets, nlines, NULL, blob1, blob2, f1, f2,
211 64453f7e 2020-11-21 stsp label1, label2, 0, 0, diff_context, ignore_whitespace,
212 64453f7e 2020-11-21 stsp force_text_diff, outfile);
215 7f1f93af 2019-08-06 stsp static const struct got_error *
216 fe621944 2020-11-10 stsp diff_blob_file(struct got_diffreg_result **resultp,
217 b72706c3 2022-06-01 stsp struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
218 b72706c3 2022-06-01 stsp FILE *f2, size_t size2, const char *label2, int diff_context,
219 b72706c3 2022-06-01 stsp int ignore_whitespace, int force_text_diff, FILE *outfile)
221 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
222 b72f483a 2019-02-05 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
223 58e31a80 2022-06-27 op const char *idstr1 = NULL;
224 fe621944 2020-11-10 stsp struct got_diffreg_result *result = NULL;
226 fe621944 2020-11-10 stsp if (resultp)
227 fe621944 2020-11-10 stsp *resultp = NULL;
230 b72f483a 2019-02-05 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
232 b72f483a 2019-02-05 stsp idstr1 = "/dev/null";
234 7f1f93af 2019-08-06 stsp if (outfile) {
235 4ce46740 2019-08-08 stsp fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
236 7f1f93af 2019-08-06 stsp fprintf(outfile, "file + %s\n",
237 7f1f93af 2019-08-06 stsp f2 == NULL ? "/dev/null" : label2);
240 fe621944 2020-11-10 stsp err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
241 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
245 fe621944 2020-11-10 stsp if (outfile) {
246 1cb46f00 2020-11-21 stsp err = got_diffreg_output(NULL, NULL, result,
247 b72706c3 2022-06-01 stsp f1 != NULL, f2 != NULL,
248 1cb46f00 2020-11-21 stsp label2, /* show local file's path, not a blob ID */
249 1cb46f00 2020-11-21 stsp label2, GOT_DIFF_OUTPUT_UNIDIFF,
250 1cb46f00 2020-11-21 stsp diff_context, outfile);
255 fe621944 2020-11-10 stsp if (resultp && err == NULL)
256 fe621944 2020-11-10 stsp *resultp = result;
257 fe621944 2020-11-10 stsp else if (result) {
258 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
259 fe621944 2020-11-10 stsp if (free_err && err == NULL)
260 fe621944 2020-11-10 stsp err = free_err;
263 b72f483a 2019-02-05 stsp return err;
266 7f1f93af 2019-08-06 stsp const struct got_error *
267 b72706c3 2022-06-01 stsp got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
268 b72706c3 2022-06-01 stsp const char *label1, FILE *f2, size_t size2, const char *label2,
269 b72706c3 2022-06-01 stsp int diff_context, int ignore_whitespace, int force_text_diff, FILE *outfile)
271 b72706c3 2022-06-01 stsp return diff_blob_file(NULL, blob1, f1, size1, label1, f2, size2, label2,
272 64453f7e 2020-11-21 stsp diff_context, ignore_whitespace, force_text_diff, outfile);
275 474b4f94 2017-11-30 stsp static const struct got_error *
276 b72706c3 2022-06-01 stsp diff_added_blob(struct got_object_id *id, FILE *f, const char *label,
277 b72706c3 2022-06-01 stsp mode_t mode, struct got_repository *repo,
278 b72706c3 2022-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
280 4e22badc 2017-11-30 stsp const struct got_error *err;
281 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
282 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
283 eb81bc23 2022-06-28 tracey int fd = -1;
285 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
287 4e22badc 2017-11-30 stsp return err;
289 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
290 eb81bc23 2022-06-28 tracey if (fd == -1) {
291 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
292 eb81bc23 2022-06-28 tracey goto done;
295 eb81bc23 2022-06-28 tracey err = got_object_blob_open(&blob, repo, obj, 8192, fd);
298 b72706c3 2022-06-01 stsp err = cb(cb_arg, NULL, blob, NULL, f, NULL, id,
299 b72706c3 2022-06-01 stsp NULL, label, 0, mode, repo);
301 2acfca77 2018-04-01 stsp got_object_close(obj);
302 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
303 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
305 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
306 2acfca77 2018-04-01 stsp return err;
309 474b4f94 2017-11-30 stsp static const struct got_error *
310 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
311 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, const char *label1, const char *label2,
312 b72706c3 2022-06-01 stsp mode_t mode1, mode_t mode2, struct got_repository *repo,
313 b72706c3 2022-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
315 6a213ccb 2017-11-30 stsp const struct got_error *err;
316 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
317 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
318 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
319 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
320 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
322 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
324 730a8aa0 2018-04-24 stsp return err;
326 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
327 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
328 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
329 eb81bc23 2022-06-28 tracey goto done;
331 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
332 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
333 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
334 eb81bc23 2022-06-28 tracey goto done;
337 15a94983 2018-12-23 stsp if (obj1->type != GOT_OBJ_TYPE_BLOB) {
338 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
342 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
345 15a94983 2018-12-23 stsp if (obj2->type != GOT_OBJ_TYPE_BLOB) {
346 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
350 eb81bc23 2022-06-28 tracey err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
354 eb81bc23 2022-06-28 tracey err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
358 b72706c3 2022-06-01 stsp err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
359 b72706c3 2022-06-01 stsp mode1, mode2, repo);
362 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
364 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
365 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
366 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
368 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
369 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
370 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
372 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
373 6a213ccb 2017-11-30 stsp return err;
376 474b4f94 2017-11-30 stsp static const struct got_error *
377 b72706c3 2022-06-01 stsp diff_deleted_blob(struct got_object_id *id, FILE *f, const char *label,
378 b72706c3 2022-06-01 stsp mode_t mode, struct got_repository *repo, 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;
383 eb81bc23 2022-06-28 tracey int fd = -1;
385 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
386 eb81bc23 2022-06-28 tracey if (fd == -1)
387 eb81bc23 2022-06-28 tracey return got_error_from_errno("got_opentempfd");
389 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
391 365fb436 2017-11-30 stsp return err;
393 eb81bc23 2022-06-28 tracey err = got_object_blob_open(&blob, repo, obj, 8192, fd);
396 b72706c3 2022-06-01 stsp err = cb(cb_arg, blob, NULL, f, NULL, id, NULL, label, NULL,
397 b72706c3 2022-06-01 stsp mode, 0, repo);
399 2acfca77 2018-04-01 stsp got_object_close(obj);
400 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
401 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
403 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
404 2acfca77 2018-04-01 stsp return err;
407 474b4f94 2017-11-30 stsp static const struct got_error *
408 b72706c3 2022-06-01 stsp diff_added_tree(struct got_object_id *id, FILE *f, const char *label,
409 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
410 31b4484f 2019-07-27 stsp int diff_content)
412 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
413 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
414 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
416 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
420 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
421 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
425 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
429 b72706c3 2022-06-01 stsp err = got_diff_tree(NULL, tree, NULL, f, NULL, label,
430 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
433 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
434 9c70d4c3 2017-11-30 stsp if (treeobj)
435 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
436 9c70d4c3 2017-11-30 stsp return err;
439 474b4f94 2017-11-30 stsp static const struct got_error *
440 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
441 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, const char *label1, const char *label2,
442 b72706c3 2022-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
443 b72706c3 2022-06-01 stsp int diff_content)
445 f6861a81 2018-09-13 stsp const struct got_error *err;
446 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
447 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
448 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
449 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
451 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
455 15a94983 2018-12-23 stsp if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
456 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
460 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
464 15a94983 2018-12-23 stsp if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
465 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
469 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
473 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
477 b72706c3 2022-06-01 stsp err = got_diff_tree(tree1, tree2, f1, f2, label1, label2,
478 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
482 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
484 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
485 789689b5 2017-11-30 stsp if (treeobj1)
486 789689b5 2017-11-30 stsp got_object_close(treeobj1);
487 789689b5 2017-11-30 stsp if (treeobj2)
488 789689b5 2017-11-30 stsp got_object_close(treeobj2);
489 789689b5 2017-11-30 stsp return err;
492 474b4f94 2017-11-30 stsp static const struct got_error *
493 b72706c3 2022-06-01 stsp diff_deleted_tree(struct got_object_id *id, FILE *f, const char *label,
494 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
495 31b4484f 2019-07-27 stsp int diff_content)
497 f6861a81 2018-09-13 stsp const struct got_error *err;
498 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
499 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
501 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
505 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
506 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
510 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
514 b72706c3 2022-06-01 stsp err = got_diff_tree(tree, NULL, f, NULL, label, NULL,
515 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
518 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
519 2c56f2ce 2017-11-30 stsp if (treeobj)
520 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
521 2c56f2ce 2017-11-30 stsp return err;
524 474b4f94 2017-11-30 stsp static const struct got_error *
525 74671950 2018-02-11 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
526 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
527 aaa13589 2019-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
529 013404a9 2017-11-30 stsp /* XXX TODO */
530 474b4f94 2017-11-30 stsp return NULL;
533 474b4f94 2017-11-30 stsp static const struct got_error *
534 b72706c3 2022-06-01 stsp diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
535 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, const char *label1, const char *label2,
536 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
537 31b4484f 2019-07-27 stsp int diff_content)
539 f6861a81 2018-09-13 stsp const struct got_error *err = NULL;
540 19ae6da1 2018-11-05 stsp int id_match;
542 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te1))
543 63c5ca5d 2019-08-24 stsp return NULL;
545 474b4f94 2017-11-30 stsp if (te2 == NULL) {
546 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
547 b72706c3 2022-06-01 stsp err = diff_deleted_tree(&te1->id, f1, label1, repo,
548 31b4484f 2019-07-27 stsp cb, cb_arg, diff_content);
550 31b4484f 2019-07-27 stsp if (diff_content)
551 b72706c3 2022-06-01 stsp err = diff_deleted_blob(&te1->id, f1, label1,
552 46f68b20 2019-10-19 stsp te1->mode, repo, cb, cb_arg);
554 b72706c3 2022-06-01 stsp err = cb(cb_arg, NULL, NULL, NULL, NULL,
555 b72706c3 2022-06-01 stsp &te1->id, NULL, label1, NULL,
556 b72706c3 2022-06-01 stsp te1->mode, 0, repo);
558 f6861a81 2018-09-13 stsp return err;
559 63c5ca5d 2019-08-24 stsp } else if (got_object_tree_entry_is_submodule(te2))
560 63c5ca5d 2019-08-24 stsp return NULL;
562 56e0773d 2019-11-28 stsp id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
563 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
564 19ae6da1 2018-11-05 stsp if (!id_match)
565 b72706c3 2022-06-01 stsp return diff_modified_tree(&te1->id, &te2->id, f1, f2,
566 31b4484f 2019-07-27 stsp label1, label2, repo, cb, cb_arg, diff_content);
567 40dde666 2020-07-23 stsp } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
568 40dde666 2020-07-23 stsp (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
569 46f68b20 2019-10-19 stsp if (!id_match ||
570 40dde666 2020-07-23 stsp ((te1->mode & (S_IFLNK | S_IXUSR))) !=
571 40dde666 2020-07-23 stsp (te2->mode & (S_IFLNK | S_IXUSR))) {
572 31b4484f 2019-07-27 stsp if (diff_content)
573 56e0773d 2019-11-28 stsp return diff_modified_blob(&te1->id, &te2->id,
574 b72706c3 2022-06-01 stsp f1, f2, label1, label2, te1->mode,
575 b72706c3 2022-06-01 stsp te2->mode, repo, cb, cb_arg);
577 b72706c3 2022-06-01 stsp return cb(cb_arg, NULL, NULL, NULL, NULL,
578 b72706c3 2022-06-01 stsp &te1->id, &te2->id, label1, label2,
579 b72706c3 2022-06-01 stsp te1->mode, te2->mode, repo);
583 19ae6da1 2018-11-05 stsp if (id_match)
584 f6861a81 2018-09-13 stsp return NULL;
586 56e0773d 2019-11-28 stsp return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
587 aaa13589 2019-06-01 stsp cb, cb_arg);
590 474b4f94 2017-11-30 stsp static const struct got_error *
591 56e0773d 2019-11-28 stsp diff_entry_new_old(struct got_tree_entry *te2,
592 b72706c3 2022-06-01 stsp struct got_tree_entry *te1, FILE *f2, const char *label2,
593 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
594 31b4484f 2019-07-27 stsp int diff_content)
596 f6861a81 2018-09-13 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
597 63c5ca5d 2019-08-24 stsp return NULL;
599 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te2))
600 f6861a81 2018-09-13 stsp return NULL;
602 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
603 b72706c3 2022-06-01 stsp return diff_added_tree(&te2->id, f2, label2,
604 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
606 31b4484f 2019-07-27 stsp if (diff_content)
607 b72706c3 2022-06-01 stsp return diff_added_blob(&te2->id, f2, label2, te2->mode,
608 b72706c3 2022-06-01 stsp repo, cb, cb_arg);
610 b72706c3 2022-06-01 stsp return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
611 b72706c3 2022-06-01 stsp NULL, label2, 0, te2->mode, repo);
614 0208f208 2020-05-05 stsp const struct got_error *
615 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
616 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
617 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
618 b72706c3 2022-06-01 stsp const char *label1, const char *label2,
619 0208f208 2020-05-05 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
621 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
622 0208f208 2020-05-05 stsp struct got_pathlist_head *paths = arg;
623 0208f208 2020-05-05 stsp struct got_diff_changed_path *change = NULL;
624 0208f208 2020-05-05 stsp char *path = NULL;
626 0208f208 2020-05-05 stsp path = strdup(label2 ? label2 : label1);
627 0208f208 2020-05-05 stsp if (path == NULL)
628 0208f208 2020-05-05 stsp return got_error_from_errno("malloc");
630 0208f208 2020-05-05 stsp change = malloc(sizeof(*change));
631 0208f208 2020-05-05 stsp if (change == NULL) {
632 0208f208 2020-05-05 stsp err = got_error_from_errno("malloc");
636 0208f208 2020-05-05 stsp change->status = GOT_STATUS_NO_CHANGE;
637 0208f208 2020-05-05 stsp if (id1 == NULL)
638 0208f208 2020-05-05 stsp change->status = GOT_STATUS_ADD;
639 0208f208 2020-05-05 stsp else if (id2 == NULL)
640 0208f208 2020-05-05 stsp change->status = GOT_STATUS_DELETE;
642 0208f208 2020-05-05 stsp if (got_object_id_cmp(id1, id2) != 0)
643 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODIFY;
644 0208f208 2020-05-05 stsp else if (mode1 != mode2)
645 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODE_CHANGE;
648 46ea77db 2021-12-20 stsp err = got_pathlist_append(paths, path, change);
651 0208f208 2020-05-05 stsp free(path);
652 0208f208 2020-05-05 stsp free(change);
654 0208f208 2020-05-05 stsp return err;
657 474b4f94 2017-11-30 stsp const struct got_error *
658 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
659 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, const char *label1, const char *label2,
660 b72706c3 2022-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
661 b72706c3 2022-06-01 stsp int diff_content)
663 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
664 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
665 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
666 f6861a81 2018-09-13 stsp char *l1 = NULL, *l2 = NULL;
667 56e0773d 2019-11-28 stsp int tidx1 = 0, tidx2 = 0;
669 883f0469 2018-06-23 stsp if (tree1) {
670 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, 0);
671 60f50a58 2018-09-15 stsp if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
672 f6861a81 2018-09-13 stsp te1->name) == -1)
673 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
675 883f0469 2018-06-23 stsp if (tree2) {
676 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, 0);
677 60f50a58 2018-09-15 stsp if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
678 f6861a81 2018-09-13 stsp te2->name) == -1)
679 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
684 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
686 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree2,
687 1de5e065 2019-06-01 stsp te1->name);
691 f6861a81 2018-09-13 stsp if (te && asprintf(&l2, "%s%s%s", label2,
692 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te->name) == -1)
694 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
696 b72706c3 2022-06-01 stsp err = diff_entry_old_new(te1, te, f1, f2, l1, l2,
697 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
703 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
705 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree1,
706 1de5e065 2019-06-01 stsp te2->name);
709 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
710 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te->name) == -1)
712 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
714 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
715 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te2->name) == -1)
717 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
719 b72706c3 2022-06-01 stsp err = diff_entry_new_old(te2, te, f2, l2, repo,
720 31b4484f 2019-07-27 stsp cb, cb_arg, diff_content);
729 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, tidx1);
731 f6861a81 2018-09-13 stsp asprintf(&l1, "%s%s%s", label1,
732 f6861a81 2018-09-13 stsp label1[0] ? "/" : "", te1->name) == -1)
733 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
739 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, tidx2);
741 f6861a81 2018-09-13 stsp asprintf(&l2, "%s%s%s", label2,
742 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te2->name) == -1)
743 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
745 474b4f94 2017-11-30 stsp } while (te1 || te2);
747 11528a82 2018-05-19 stsp return err;
750 11528a82 2018-05-19 stsp const struct got_error *
751 fe621944 2020-11-10 stsp got_diff_objects_as_blobs(off_t **line_offsets, size_t *nlines,
752 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
753 54156555 2018-12-24 stsp const char *label1, const char *label2, int diff_context,
754 64453f7e 2020-11-21 stsp int ignore_whitespace, int force_text_diff,
755 64453f7e 2020-11-21 stsp struct got_repository *repo, FILE *outfile)
757 11528a82 2018-05-19 stsp const struct got_error *err;
758 11528a82 2018-05-19 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
759 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
761 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
762 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
764 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
765 eb81bc23 2022-06-28 tracey if (fd1 == -1)
766 eb81bc23 2022-06-28 tracey return got_error_from_errno("got_opentempfd");
767 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
768 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
769 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
770 eb81bc23 2022-06-28 tracey goto done;
774 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
779 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
783 b72706c3 2022-06-01 stsp err = got_diff_blob(line_offsets, nlines, blob1, blob2, f1, f2,
784 64453f7e 2020-11-21 stsp label1, label2, diff_context, ignore_whitespace, force_text_diff,
787 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
788 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
790 67b631c9 2021-10-10 stsp got_object_blob_close(blob1);
791 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
792 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
794 67b631c9 2021-10-10 stsp got_object_blob_close(blob2);
795 67b631c9 2021-10-10 stsp return err;
798 67b631c9 2021-10-10 stsp static const struct got_error *
799 67b631c9 2021-10-10 stsp diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
800 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, struct got_pathlist_head *paths,
801 b72706c3 2022-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
803 67b631c9 2021-10-10 stsp const struct got_error *err = NULL;
804 67b631c9 2021-10-10 stsp struct got_pathlist_entry *pe;
805 67b631c9 2021-10-10 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
806 67b631c9 2021-10-10 stsp struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
807 67b631c9 2021-10-10 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
808 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
810 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
811 eb81bc23 2022-06-28 tracey if (fd1 == -1)
812 eb81bc23 2022-06-28 tracey return got_error_from_errno("got_opentempfd");
813 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
814 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
815 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
816 eb81bc23 2022-06-28 tracey goto done;
818 67b631c9 2021-10-10 stsp TAILQ_FOREACH(pe, paths, entry) {
819 67b631c9 2021-10-10 stsp int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
820 67b631c9 2021-10-10 stsp mode_t mode1 = 0, mode2 = 0;
823 67b631c9 2021-10-10 stsp id1 = NULL;
825 67b631c9 2021-10-10 stsp id2 = NULL;
826 67b631c9 2021-10-10 stsp if (subtree1) {
827 67b631c9 2021-10-10 stsp got_object_tree_close(subtree1);
828 67b631c9 2021-10-10 stsp subtree1 = NULL;
830 67b631c9 2021-10-10 stsp if (subtree2) {
831 67b631c9 2021-10-10 stsp got_object_tree_close(subtree2);
832 67b631c9 2021-10-10 stsp subtree2 = NULL;
834 67b631c9 2021-10-10 stsp if (blob1) {
835 67b631c9 2021-10-10 stsp got_object_blob_close(blob1);
836 67b631c9 2021-10-10 stsp blob1 = NULL;
838 67b631c9 2021-10-10 stsp if (blob2) {
839 67b631c9 2021-10-10 stsp got_object_blob_close(blob2);
840 67b631c9 2021-10-10 stsp blob2 = NULL;
843 67b631c9 2021-10-10 stsp err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
845 67b631c9 2021-10-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
847 67b631c9 2021-10-10 stsp err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
849 67b631c9 2021-10-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
851 67b631c9 2021-10-10 stsp if (id1 == NULL && id2 == NULL) {
852 67b631c9 2021-10-10 stsp err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
856 67b631c9 2021-10-10 stsp err = got_object_get_type(&type1, repo, id1);
861 67b631c9 2021-10-10 stsp err = got_object_get_type(&type2, repo, id2);
865 67b631c9 2021-10-10 stsp if (type1 == GOT_OBJ_TYPE_ANY &&
866 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_ANY) {
867 67b631c9 2021-10-10 stsp err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
869 67b631c9 2021-10-10 stsp } else if (type1 != GOT_OBJ_TYPE_ANY &&
870 67b631c9 2021-10-10 stsp type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
871 67b631c9 2021-10-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
875 67b631c9 2021-10-10 stsp if (type1 == GOT_OBJ_TYPE_BLOB ||
876 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_BLOB) {
878 67b631c9 2021-10-10 stsp err = got_object_open_as_blob(&blob1, repo,
879 eb81bc23 2022-06-28 tracey id1, 8192, fd1);
884 67b631c9 2021-10-10 stsp err = got_object_open_as_blob(&blob2, repo,
885 eb81bc23 2022-06-28 tracey id2, 8192, fd2);
889 b72706c3 2022-06-01 stsp err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
890 67b631c9 2021-10-10 stsp id1 ? pe->path : "/dev/null",
891 67b631c9 2021-10-10 stsp id2 ? pe->path : "/dev/null",
892 67b631c9 2021-10-10 stsp mode1, mode2, repo);
895 67b631c9 2021-10-10 stsp } else if (type1 == GOT_OBJ_TYPE_TREE ||
896 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_TREE) {
898 67b631c9 2021-10-10 stsp err = got_object_open_as_tree(&subtree1, repo,
904 67b631c9 2021-10-10 stsp err = got_object_open_as_tree(&subtree2, repo,
909 b72706c3 2022-06-01 stsp err = got_diff_tree(subtree1, subtree2, f1, f2,
910 67b631c9 2021-10-10 stsp id1 ? pe->path : "/dev/null",
911 67b631c9 2021-10-10 stsp id2 ? pe->path : "/dev/null",
912 67b631c9 2021-10-10 stsp repo, cb, cb_arg, 1);
916 67b631c9 2021-10-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
919 eb81bc23 2022-06-28 tracey if (ftruncate(fd1, 0L) == -1)
920 eb81bc23 2022-06-28 tracey return got_error_from_errno("ftruncate");
921 eb81bc23 2022-06-28 tracey if (ftruncate(fd2, 0L) == -1)
922 eb81bc23 2022-06-28 tracey return got_error_from_errno("ftruncate");
927 67b631c9 2021-10-10 stsp if (subtree1)
928 67b631c9 2021-10-10 stsp got_object_tree_close(subtree1);
929 67b631c9 2021-10-10 stsp if (subtree2)
930 67b631c9 2021-10-10 stsp got_object_tree_close(subtree2);
931 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
932 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
934 11528a82 2018-05-19 stsp got_object_blob_close(blob1);
935 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
936 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
938 11528a82 2018-05-19 stsp got_object_blob_close(blob2);
939 474b4f94 2017-11-30 stsp return err;
942 8469d821 2022-06-25 stsp static const struct got_error *
943 8469d821 2022-06-25 stsp show_object_id(off_t **line_offsets, size_t *nlines, const char *obj_typestr,
944 8469d821 2022-06-25 stsp int ch, const char *id_str, FILE *outfile)
946 8469d821 2022-06-25 stsp const struct got_error *err;
948 8469d821 2022-06-25 stsp off_t outoff = 0;
950 8469d821 2022-06-25 stsp n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
951 8469d821 2022-06-25 stsp if (line_offsets != NULL && *line_offsets != NULL) {
952 8469d821 2022-06-25 stsp if (*nlines == 0) {
953 8469d821 2022-06-25 stsp err = add_line_offset(line_offsets, nlines, 0);
955 8469d821 2022-06-25 stsp return err;
957 8469d821 2022-06-25 stsp outoff = (*line_offsets)[*nlines - 1];
959 8469d821 2022-06-25 stsp outoff += n;
960 8469d821 2022-06-25 stsp err = add_line_offset(line_offsets, nlines, outoff);
962 8469d821 2022-06-25 stsp return err;
965 8469d821 2022-06-25 stsp return NULL;
968 8469d821 2022-06-25 stsp static const struct got_error *
969 8469d821 2022-06-25 stsp diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
970 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
971 58e31a80 2022-06-27 op struct got_pathlist_head *paths, const char *label1, const char *label2,
972 58e31a80 2022-06-27 op int diff_context, int ignore_whitespace, int force_text_diff,
973 58e31a80 2022-06-27 op struct got_repository *repo, FILE *outfile)
975 11528a82 2018-05-19 stsp const struct got_error *err;
976 11528a82 2018-05-19 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
977 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
978 fe621944 2020-11-10 stsp int want_lineoffsets = (line_offsets != NULL && *line_offsets != NULL);
980 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
981 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
984 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo, id1);
989 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo, id2);
994 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
995 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
996 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
997 aaa13589 2019-06-01 stsp arg.outfile = outfile;
998 fe621944 2020-11-10 stsp if (want_lineoffsets) {
999 fe621944 2020-11-10 stsp arg.line_offsets = *line_offsets;
1000 fe621944 2020-11-10 stsp arg.nlines = *nlines;
1002 fe621944 2020-11-10 stsp arg.line_offsets = NULL;
1003 fe621944 2020-11-10 stsp arg.nlines = 0;
1005 67b631c9 2021-10-10 stsp if (paths == NULL || TAILQ_EMPTY(paths)) {
1006 b72706c3 2022-06-01 stsp err = got_diff_tree(tree1, tree2, f1, f2, label1, label2,
1007 b72706c3 2022-06-01 stsp repo, got_diff_blob_output_unidiff, &arg, 1);
1009 b72706c3 2022-06-01 stsp err = diff_paths(tree1, tree2, f1, f2, paths, repo,
1010 67b631c9 2021-10-10 stsp got_diff_blob_output_unidiff, &arg);
1012 fe621944 2020-11-10 stsp if (want_lineoffsets) {
1013 fe621944 2020-11-10 stsp *line_offsets = arg.line_offsets; /* was likely re-allocated */
1014 fe621944 2020-11-10 stsp *nlines = arg.nlines;
1017 11528a82 2018-05-19 stsp if (tree1)
1018 11528a82 2018-05-19 stsp got_object_tree_close(tree1);
1019 11528a82 2018-05-19 stsp if (tree2)
1020 11528a82 2018-05-19 stsp got_object_tree_close(tree2);
1021 11528a82 2018-05-19 stsp return err;
1024 11528a82 2018-05-19 stsp const struct got_error *
1025 8469d821 2022-06-25 stsp got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
1026 8469d821 2022-06-25 stsp FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
1027 58e31a80 2022-06-27 op struct got_pathlist_head *paths, const char *label1, const char *label2,
1028 58e31a80 2022-06-27 op int diff_context, int ignore_whitespace, int force_text_diff,
1029 58e31a80 2022-06-27 op struct got_repository *repo, FILE *outfile)
1031 8469d821 2022-06-25 stsp const struct got_error *err;
1032 8469d821 2022-06-25 stsp char *idstr = NULL;
1034 8469d821 2022-06-25 stsp if (id1 == NULL && id2 == NULL)
1035 8469d821 2022-06-25 stsp return got_error(GOT_ERR_NO_OBJ);
1037 8469d821 2022-06-25 stsp if (id1) {
1038 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id1);
1040 8469d821 2022-06-25 stsp goto done;
1041 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "tree", '-',
1042 8469d821 2022-06-25 stsp idstr, outfile);
1044 8469d821 2022-06-25 stsp goto done;
1045 8469d821 2022-06-25 stsp free(idstr);
1046 8469d821 2022-06-25 stsp idstr = NULL;
1048 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "tree", '-',
1049 8469d821 2022-06-25 stsp "/dev/null", outfile);
1051 8469d821 2022-06-25 stsp goto done;
1054 8469d821 2022-06-25 stsp if (id2) {
1055 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id2);
1057 8469d821 2022-06-25 stsp goto done;
1058 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "tree", '+',
1059 8469d821 2022-06-25 stsp idstr, outfile);
1061 8469d821 2022-06-25 stsp goto done;
1062 8469d821 2022-06-25 stsp free(idstr);
1063 8469d821 2022-06-25 stsp idstr = NULL;
1065 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "tree", '+',
1066 8469d821 2022-06-25 stsp "/dev/null", outfile);
1068 8469d821 2022-06-25 stsp goto done;
1071 8469d821 2022-06-25 stsp err = diff_objects_as_trees(line_offsets, nlines, f1, f2, id1, id2,
1072 8469d821 2022-06-25 stsp paths, label1, label2, diff_context, ignore_whitespace,
1073 8469d821 2022-06-25 stsp force_text_diff, repo, outfile);
1075 8469d821 2022-06-25 stsp free(idstr);
1076 8469d821 2022-06-25 stsp return err;
1079 8469d821 2022-06-25 stsp const struct got_error *
1080 fe621944 2020-11-10 stsp got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
1081 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
1082 67b631c9 2021-10-10 stsp struct got_pathlist_head *paths,
1083 64453f7e 2020-11-21 stsp int diff_context, int ignore_whitespace, int force_text_diff,
1084 15a94983 2018-12-23 stsp struct got_repository *repo, FILE *outfile)
1086 11528a82 2018-05-19 stsp const struct got_error *err;
1087 11528a82 2018-05-19 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1088 8469d821 2022-06-25 stsp char *idstr = NULL;
1090 15a94983 2018-12-23 stsp if (id2 == NULL)
1091 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
1093 15a94983 2018-12-23 stsp if (id1) {
1094 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit1, repo, id1);
1096 cd0acaa7 2018-05-20 stsp goto done;
1097 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id1);
1099 8469d821 2022-06-25 stsp goto done;
1100 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "commit", '-',
1101 8469d821 2022-06-25 stsp idstr, outfile);
1103 8469d821 2022-06-25 stsp goto done;
1104 8469d821 2022-06-25 stsp free(idstr);
1105 8469d821 2022-06-25 stsp idstr = NULL;
1107 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "commit", '-',
1108 8469d821 2022-06-25 stsp "/dev/null", outfile);
1110 8469d821 2022-06-25 stsp goto done;
1113 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, repo, id2);
1115 9b697879 2018-05-20 stsp goto done;
1117 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id2);
1119 8469d821 2022-06-25 stsp goto done;
1120 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "commit", '+',
1121 8469d821 2022-06-25 stsp idstr, outfile);
1123 8469d821 2022-06-25 stsp goto done;
1125 8469d821 2022-06-25 stsp err = diff_objects_as_trees(line_offsets, nlines, f1, f2,
1126 15a94983 2018-12-23 stsp commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1127 67b631c9 2021-10-10 stsp got_object_commit_get_tree_id(commit2), paths, "", "",
1128 67b631c9 2021-10-10 stsp diff_context, ignore_whitespace, force_text_diff, repo, outfile);
1130 11528a82 2018-05-19 stsp if (commit1)
1131 11528a82 2018-05-19 stsp got_object_commit_close(commit1);
1132 11528a82 2018-05-19 stsp if (commit2)
1133 11528a82 2018-05-19 stsp got_object_commit_close(commit2);
1134 8469d821 2022-06-25 stsp free(idstr);
1135 11528a82 2018-05-19 stsp return err;
1138 dc424a06 2019-08-07 stsp const struct got_error *
1139 fe621944 2020-11-10 stsp got_diff_files(struct got_diffreg_result **resultp,
1140 fe621944 2020-11-10 stsp FILE *f1, const char *label1, FILE *f2, const char *label2,
1141 64453f7e 2020-11-21 stsp int diff_context, int ignore_whitespace, int force_text_diff,
1142 64453f7e 2020-11-21 stsp FILE *outfile)
1144 dc424a06 2019-08-07 stsp const struct got_error *err = NULL;
1145 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
1147 fe621944 2020-11-10 stsp if (resultp)
1148 fe621944 2020-11-10 stsp *resultp = NULL;
1150 dc424a06 2019-08-07 stsp if (outfile) {
1151 dc424a06 2019-08-07 stsp fprintf(outfile, "file - %s\n",
1152 dc424a06 2019-08-07 stsp f1 == NULL ? "/dev/null" : label1);
1153 dc424a06 2019-08-07 stsp fprintf(outfile, "file + %s\n",
1154 dc424a06 2019-08-07 stsp f2 == NULL ? "/dev/null" : label2);
1157 fe621944 2020-11-10 stsp err = got_diffreg(&diffreg_result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
1158 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
1160 fe621944 2020-11-10 stsp goto done;
1162 fe621944 2020-11-10 stsp if (outfile) {
1163 fe621944 2020-11-10 stsp err = got_diffreg_output(NULL, NULL, diffreg_result,
1164 1cb46f00 2020-11-21 stsp f1 != NULL, f2 != NULL, label1, label2,
1165 1cb46f00 2020-11-21 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1167 dc424a06 2019-08-07 stsp goto done;
1171 fe621944 2020-11-10 stsp if (resultp && err == NULL)
1172 fe621944 2020-11-10 stsp *resultp = diffreg_result;
1173 fe621944 2020-11-10 stsp else if (diffreg_result) {
1174 fe621944 2020-11-10 stsp const struct got_error *free_err;
1175 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
1176 fe621944 2020-11-10 stsp if (free_err && err == NULL)
1177 fe621944 2020-11-10 stsp err = free_err;
1180 dc424a06 2019-08-07 stsp return err;