2 7d283eee 2017-11-29 stsp * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
4 7d283eee 2017-11-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 7d283eee 2017-11-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 7d283eee 2017-11-29 stsp * copyright notice and this permission notice appear in all copies.
8 7d283eee 2017-11-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 7d283eee 2017-11-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 7d283eee 2017-11-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 7d283eee 2017-11-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 7d283eee 2017-11-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 7d283eee 2017-11-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 7d283eee 2017-11-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
18 1c7f0520 2017-11-29 stsp #include <sys/stat.h>
20 7d283eee 2017-11-29 stsp #include <stdio.h>
21 7d283eee 2017-11-29 stsp #include <stdlib.h>
22 7d283eee 2017-11-29 stsp #include <string.h>
23 f9d67749 2017-11-30 stsp #include <limits.h>
24 7d283eee 2017-11-29 stsp #include <zlib.h>
26 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
28 7d283eee 2017-11-29 stsp #include "got_object.h"
29 e09a504c 2019-06-28 stsp #include "got_repository.h"
30 7d283eee 2017-11-29 stsp #include "got_error.h"
31 789689b5 2017-11-30 stsp #include "got_diff.h"
32 324d37e7 2019-05-11 stsp #include "got_path.h"
33 0208f208 2020-05-05 stsp #include "got_cancel.h"
34 0208f208 2020-05-05 stsp #include "got_worktree.h"
35 1758cce7 2022-06-13 thomas #include "got_opentemp.h"
37 718b3ab0 2018-03-17 stsp #include "got_lib_diff.h"
38 15a94983 2018-12-23 stsp #include "got_lib_delta.h"
39 15a94983 2018-12-23 stsp #include "got_lib_inflate.h"
40 15a94983 2018-12-23 stsp #include "got_lib_object.h"
42 772fcad5 2023-01-07 thomas #ifndef MAX
43 772fcad5 2023-01-07 thomas #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
46 404c43c4 2018-06-21 stsp static const struct got_error *
47 82c78e96 2022-08-06 thomas add_line_metadata(struct got_diff_line **lines, size_t *nlines,
48 82c78e96 2022-08-06 thomas off_t off, uint8_t type)
50 82c78e96 2022-08-06 thomas struct got_diff_line *p;
52 82c78e96 2022-08-06 thomas p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
53 fe621944 2020-11-10 stsp if (p == NULL)
54 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
55 82c78e96 2022-08-06 thomas *lines = p;
56 82c78e96 2022-08-06 thomas (*lines)[*nlines].offset = off;
57 82c78e96 2022-08-06 thomas (*lines)[*nlines].type = type;
58 fe621944 2020-11-10 stsp (*nlines)++;
60 fe621944 2020-11-10 stsp return NULL;
63 fe621944 2020-11-10 stsp static const struct got_error *
64 82c78e96 2022-08-06 thomas diff_blobs(struct got_diff_line **lines, size_t *nlines,
65 fe621944 2020-11-10 stsp struct got_diffreg_result **resultp, struct got_blob_object *blob1,
66 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
67 46f68b20 2019-10-19 stsp const char *label1, const char *label2, mode_t mode1, mode_t mode2,
68 25ec7006 2022-07-01 thomas int diff_context, int ignore_whitespace, int force_text_diff, FILE *outfile,
69 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo)
71 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
72 f78b0693 2017-11-29 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
73 f78b0693 2017-11-29 stsp char hex2[SHA1_DIGEST_STRING_LENGTH];
74 0c6f49ba 2022-07-01 thomas const char *idstr1 = NULL, *idstr2 = NULL;
75 be659d10 2020-11-18 stsp off_t size1, size2;
76 a6d62598 2023-01-07 thomas struct got_diffreg_result *result = NULL;
77 fe621944 2020-11-10 stsp off_t outoff = 0;
80 3387ec82 2022-08-30 thomas if (lines && *lines && *nlines > 0)
81 82c78e96 2022-08-06 thomas outoff = (*lines)[*nlines - 1].offset;
82 82c78e96 2022-08-06 thomas else if (lines) {
83 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
88 fe621944 2020-11-10 stsp if (resultp)
89 fe621944 2020-11-10 stsp *resultp = NULL;
92 1758cce7 2022-06-13 thomas err = got_opentemp_truncate(f1);
94 a0f32f33 2022-06-13 thomas goto done;
97 1758cce7 2022-06-13 thomas err = got_opentemp_truncate(f2);
99 a0f32f33 2022-06-13 thomas goto done;
103 98abbc84 2017-11-30 stsp if (blob1) {
104 f934cf2c 2018-02-12 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
105 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
110 98abbc84 2017-11-30 stsp idstr1 = "/dev/null";
113 98abbc84 2017-11-30 stsp if (blob2) {
114 f934cf2c 2018-02-12 stsp idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
115 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
120 98abbc84 2017-11-30 stsp idstr2 = "/dev/null";
122 09de383e 2018-12-24 stsp if (outfile) {
123 46f68b20 2019-10-19 stsp char *modestr1 = NULL, *modestr2 = NULL;
124 40dde666 2020-07-23 stsp int modebits;
125 46f68b20 2019-10-19 stsp if (mode1 && mode1 != mode2) {
126 40dde666 2020-07-23 stsp if (S_ISLNK(mode1))
127 40dde666 2020-07-23 stsp modebits = S_IFLNK;
129 40dde666 2020-07-23 stsp modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
130 46f68b20 2019-10-19 stsp if (asprintf(&modestr1, " (mode %o)",
131 40dde666 2020-07-23 stsp mode1 & modebits) == -1) {
132 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
136 46f68b20 2019-10-19 stsp if (mode2 && mode1 != mode2) {
137 40dde666 2020-07-23 stsp if (S_ISLNK(mode2))
138 40dde666 2020-07-23 stsp modebits = S_IFLNK;
140 40dde666 2020-07-23 stsp modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
141 46f68b20 2019-10-19 stsp if (asprintf(&modestr2, " (mode %o)",
142 40dde666 2020-07-23 stsp mode2 & modebits) == -1) {
143 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
147 fe621944 2020-11-10 stsp n = fprintf(outfile, "blob - %s%s\n", idstr1,
148 46f68b20 2019-10-19 stsp modestr1 ? modestr1 : "");
151 fe621944 2020-11-10 stsp outoff += n;
152 82c78e96 2022-08-06 thomas if (lines) {
153 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
154 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_BLOB_MIN);
159 fe621944 2020-11-10 stsp n = fprintf(outfile, "blob + %s%s\n", idstr2,
160 46f68b20 2019-10-19 stsp modestr2 ? modestr2 : "");
163 fe621944 2020-11-10 stsp outoff += n;
164 82c78e96 2022-08-06 thomas if (lines) {
165 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
166 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_BLOB_PLUS);
171 46f68b20 2019-10-19 stsp free(modestr1);
172 46f68b20 2019-10-19 stsp free(modestr2);
174 25ec7006 2022-07-01 thomas err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
175 25ec7006 2022-07-01 thomas force_text_diff);
179 fe621944 2020-11-10 stsp if (outfile) {
180 82c78e96 2022-08-06 thomas err = got_diffreg_output(lines, nlines, result,
181 1cb46f00 2020-11-21 stsp blob1 != NULL, blob2 != NULL,
182 fe621944 2020-11-10 stsp label1 ? label1 : idstr1,
183 fe621944 2020-11-10 stsp label2 ? label2 : idstr2,
184 fe621944 2020-11-10 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
190 fe621944 2020-11-10 stsp if (resultp && err == NULL)
191 fe621944 2020-11-10 stsp *resultp = result;
192 a6d62598 2023-01-07 thomas else if (result) {
193 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
194 fe621944 2020-11-10 stsp if (free_err && err == NULL)
195 fe621944 2020-11-10 stsp err = free_err;
198 7d283eee 2017-11-29 stsp return err;
201 aaa13589 2019-06-01 stsp const struct got_error *
202 aaa13589 2019-06-01 stsp got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
203 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
204 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
205 a0f32f33 2022-06-13 thomas const char *label1, const char *label2, mode_t mode1, mode_t mode2,
206 a0f32f33 2022-06-13 thomas struct got_repository *repo)
208 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg *a = arg;
210 82c78e96 2022-08-06 thomas return diff_blobs(&a->lines, &a->nlines, NULL,
211 a0f32f33 2022-06-13 thomas blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
212 25ec7006 2022-07-01 thomas a->ignore_whitespace, a->force_text_diff, a->outfile, a->diff_algo);
215 404c43c4 2018-06-21 stsp const struct got_error *
216 82c78e96 2022-08-06 thomas got_diff_blob(struct got_diff_line **lines, size_t*nlines,
217 fe621944 2020-11-10 stsp struct got_blob_object *blob1, struct got_blob_object *blob2,
218 a0f32f33 2022-06-13 thomas FILE *f1, FILE *f2, const char *label1, const char *label2,
219 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo, int diff_context,
220 25ec7006 2022-07-01 thomas int ignore_whitespace, int force_text_diff, FILE *outfile)
222 82c78e96 2022-08-06 thomas return diff_blobs(lines, nlines, NULL, blob1, blob2, f1, f2,
223 64453f7e 2020-11-21 stsp label1, label2, 0, 0, diff_context, ignore_whitespace,
224 25ec7006 2022-07-01 thomas force_text_diff, outfile, diff_algo);
227 7f1f93af 2019-08-06 stsp static const struct got_error *
228 fe621944 2020-11-10 stsp diff_blob_file(struct got_diffreg_result **resultp,
229 a0f32f33 2022-06-13 thomas struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
230 6d054bb9 2022-09-23 thomas FILE *f2, int f2_exists, struct stat *sb2, const char *label2,
231 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
232 25ec7006 2022-07-01 thomas int force_text_diff, FILE *outfile)
234 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
235 b72f483a 2019-02-05 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
236 0c6f49ba 2022-07-01 thomas const char *idstr1 = NULL;
237 fe621944 2020-11-10 stsp struct got_diffreg_result *result = NULL;
239 fe621944 2020-11-10 stsp if (resultp)
240 fe621944 2020-11-10 stsp *resultp = NULL;
242 a0f32f33 2022-06-13 thomas if (blob1)
243 b72f483a 2019-02-05 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
245 b72f483a 2019-02-05 stsp idstr1 = "/dev/null";
247 7f1f93af 2019-08-06 stsp if (outfile) {
248 6d054bb9 2022-09-23 thomas char *mode = NULL;
250 6d054bb9 2022-09-23 thomas /* display file mode for new added files only */
251 6d054bb9 2022-09-23 thomas if (f2_exists && blob1 == NULL) {
252 6d054bb9 2022-09-23 thomas int mmask = (S_IRWXU | S_IRWXG | S_IRWXO);
254 6d054bb9 2022-09-23 thomas if (S_ISLNK(sb2->st_mode))
255 6d054bb9 2022-09-23 thomas mmask = S_IFLNK;
256 6d054bb9 2022-09-23 thomas if (asprintf(&mode, " (mode %o)",
257 6d054bb9 2022-09-23 thomas sb2->st_mode & mmask) == -1)
258 6d054bb9 2022-09-23 thomas return got_error_from_errno("asprintf");
260 4ce46740 2019-08-08 stsp fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
261 6d054bb9 2022-09-23 thomas fprintf(outfile, "file + %s%s\n",
262 6d054bb9 2022-09-23 thomas f2_exists ? label2 : "/dev/null", mode ? mode : "");
263 6d054bb9 2022-09-23 thomas free(mode);
266 25ec7006 2022-07-01 thomas err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
267 25ec7006 2022-07-01 thomas force_text_diff);
271 fe621944 2020-11-10 stsp if (outfile) {
272 1cb46f00 2020-11-21 stsp err = got_diffreg_output(NULL, NULL, result,
273 dd2e2f52 2022-07-01 thomas blob1 != NULL, f2_exists,
274 1cb46f00 2020-11-21 stsp label2, /* show local file's path, not a blob ID */
275 1cb46f00 2020-11-21 stsp label2, GOT_DIFF_OUTPUT_UNIDIFF,
276 1cb46f00 2020-11-21 stsp diff_context, outfile);
282 fe621944 2020-11-10 stsp if (resultp && err == NULL)
283 fe621944 2020-11-10 stsp *resultp = result;
284 fe621944 2020-11-10 stsp else if (result) {
285 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
286 fe621944 2020-11-10 stsp if (free_err && err == NULL)
287 fe621944 2020-11-10 stsp err = free_err;
289 b72f483a 2019-02-05 stsp return err;
292 7f1f93af 2019-08-06 stsp const struct got_error *
293 a0f32f33 2022-06-13 thomas got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
294 6d054bb9 2022-09-23 thomas const char *label1, FILE *f2, int f2_exists, struct stat *sb2,
295 25ec7006 2022-07-01 thomas const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
296 25ec7006 2022-07-01 thomas int ignore_whitespace, int force_text_diff, FILE *outfile)
298 dd2e2f52 2022-07-01 thomas return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
299 6d054bb9 2022-09-23 thomas sb2, label2, diff_algo, diff_context, ignore_whitespace,
300 6d054bb9 2022-09-23 thomas force_text_diff, outfile);
303 474b4f94 2017-11-30 stsp static const struct got_error *
304 dd2e2f52 2022-07-01 thomas diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
305 19a6a6b5 2022-07-01 thomas const char *label, mode_t mode, struct got_repository *repo,
306 a0f32f33 2022-06-13 thomas got_diff_blob_cb cb, void *cb_arg)
308 4e22badc 2017-11-30 stsp const struct got_error *err;
309 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
310 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
312 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
314 4e22badc 2017-11-30 stsp return err;
316 dd2e2f52 2022-07-01 thomas err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
319 dd2e2f52 2022-07-01 thomas err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
320 a0f32f33 2022-06-13 thomas NULL, label, 0, mode, repo);
322 2acfca77 2018-04-01 stsp got_object_close(obj);
324 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
325 2acfca77 2018-04-01 stsp return err;
328 474b4f94 2017-11-30 stsp static const struct got_error *
329 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
330 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
331 19a6a6b5 2022-07-01 thomas const char *label1, const char *label2,
332 a0f32f33 2022-06-13 thomas mode_t mode1, mode_t mode2, struct got_repository *repo,
333 a0f32f33 2022-06-13 thomas got_diff_blob_cb cb, void *cb_arg)
335 6a213ccb 2017-11-30 stsp const struct got_error *err;
336 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
337 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
338 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
339 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
341 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
343 730a8aa0 2018-04-24 stsp return err;
345 15a94983 2018-12-23 stsp if (obj1->type != GOT_OBJ_TYPE_BLOB) {
346 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
350 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
353 15a94983 2018-12-23 stsp if (obj2->type != GOT_OBJ_TYPE_BLOB) {
354 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
358 f4ae6ddb 2022-07-01 thomas err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
362 f4ae6ddb 2022-07-01 thomas err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
366 a0f32f33 2022-06-13 thomas err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
367 a0f32f33 2022-06-13 thomas mode1, mode2, repo);
370 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
372 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
374 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
376 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
377 6a213ccb 2017-11-30 stsp return err;
380 474b4f94 2017-11-30 stsp static const struct got_error *
381 dd2e2f52 2022-07-01 thomas diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
382 dd2e2f52 2022-07-01 thomas FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
383 19a6a6b5 2022-07-01 thomas got_diff_blob_cb cb, void *cb_arg)
385 365fb436 2017-11-30 stsp const struct got_error *err;
386 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
387 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
389 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
391 365fb436 2017-11-30 stsp return err;
393 dd2e2f52 2022-07-01 thomas err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
396 dd2e2f52 2022-07-01 thomas err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
397 a0f32f33 2022-06-13 thomas mode, 0, repo);
399 2acfca77 2018-04-01 stsp got_object_close(obj);
401 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
402 2acfca77 2018-04-01 stsp return err;
405 474b4f94 2017-11-30 stsp static const struct got_error *
406 dd2e2f52 2022-07-01 thomas diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
407 dd2e2f52 2022-07-01 thomas const char *label, struct got_repository *repo, got_diff_blob_cb cb,
408 dd2e2f52 2022-07-01 thomas void *cb_arg, int diff_content)
410 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
411 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
412 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
414 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
418 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
419 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
423 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
427 dd2e2f52 2022-07-01 thomas err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
428 a0f32f33 2022-06-13 thomas repo, cb, cb_arg, diff_content);
431 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
432 9c70d4c3 2017-11-30 stsp if (treeobj)
433 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
434 9c70d4c3 2017-11-30 stsp return err;
437 474b4f94 2017-11-30 stsp static const struct got_error *
438 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
439 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
440 19a6a6b5 2022-07-01 thomas const char *label1, const char *label2,
441 a0f32f33 2022-06-13 thomas struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
442 a0f32f33 2022-06-13 thomas int diff_content)
444 f6861a81 2018-09-13 stsp const struct got_error *err;
445 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
446 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
447 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
448 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
450 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
454 15a94983 2018-12-23 stsp if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
455 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
459 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
463 15a94983 2018-12-23 stsp if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
464 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
468 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
472 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
476 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
477 19a6a6b5 2022-07-01 thomas label1, label2, repo, cb, cb_arg, diff_content);
481 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
483 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
484 789689b5 2017-11-30 stsp if (treeobj1)
485 789689b5 2017-11-30 stsp got_object_close(treeobj1);
486 789689b5 2017-11-30 stsp if (treeobj2)
487 789689b5 2017-11-30 stsp got_object_close(treeobj2);
488 789689b5 2017-11-30 stsp return err;
491 474b4f94 2017-11-30 stsp static const struct got_error *
492 dd2e2f52 2022-07-01 thomas diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
493 dd2e2f52 2022-07-01 thomas FILE *f2, const char *label, struct got_repository *repo,
494 19a6a6b5 2022-07-01 thomas got_diff_blob_cb cb, void *cb_arg, int diff_content)
496 f6861a81 2018-09-13 stsp const struct got_error *err;
497 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
498 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
500 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
504 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
505 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
509 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
513 dd2e2f52 2022-07-01 thomas err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
514 a0f32f33 2022-06-13 thomas repo, cb, cb_arg, diff_content);
517 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
518 2c56f2ce 2017-11-30 stsp if (treeobj)
519 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
520 2c56f2ce 2017-11-30 stsp return err;
523 474b4f94 2017-11-30 stsp static const struct got_error *
524 74671950 2018-02-11 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
525 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
526 aaa13589 2019-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
528 013404a9 2017-11-30 stsp /* XXX TODO */
529 474b4f94 2017-11-30 stsp return NULL;
532 474b4f94 2017-11-30 stsp static const struct got_error *
533 a0f32f33 2022-06-13 thomas diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
534 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
535 19a6a6b5 2022-07-01 thomas 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 dd2e2f52 2022-07-01 thomas err = diff_deleted_tree(&te1->id, f1, fd1, f2,
548 dd2e2f52 2022-07-01 thomas label1, repo, cb, cb_arg, diff_content);
550 31b4484f 2019-07-27 stsp if (diff_content)
551 19a6a6b5 2022-07-01 thomas err = diff_deleted_blob(&te1->id, f1, fd1,
552 dd2e2f52 2022-07-01 thomas f2, label1, te1->mode, repo, cb, cb_arg);
554 a0f32f33 2022-06-13 thomas err = cb(cb_arg, NULL, NULL, NULL, NULL,
555 a0f32f33 2022-06-13 thomas &te1->id, NULL, label1, NULL,
556 a0f32f33 2022-06-13 thomas 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 a0f32f33 2022-06-13 thomas return diff_modified_tree(&te1->id, &te2->id, f1, f2,
566 19a6a6b5 2022-07-01 thomas fd1, fd2, label1, label2, repo, cb, cb_arg,
567 19a6a6b5 2022-07-01 thomas diff_content);
568 40dde666 2020-07-23 stsp } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
569 40dde666 2020-07-23 stsp (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
570 46f68b20 2019-10-19 stsp if (!id_match ||
571 40dde666 2020-07-23 stsp ((te1->mode & (S_IFLNK | S_IXUSR))) !=
572 40dde666 2020-07-23 stsp (te2->mode & (S_IFLNK | S_IXUSR))) {
573 31b4484f 2019-07-27 stsp if (diff_content)
574 56e0773d 2019-11-28 stsp return diff_modified_blob(&te1->id, &te2->id,
575 19a6a6b5 2022-07-01 thomas f1, f2, fd1, fd2, label1, label2,
576 19a6a6b5 2022-07-01 thomas te1->mode, te2->mode, repo, cb, cb_arg);
578 a0f32f33 2022-06-13 thomas return cb(cb_arg, NULL, NULL, NULL, NULL,
579 a0f32f33 2022-06-13 thomas &te1->id, &te2->id, label1, label2,
580 a0f32f33 2022-06-13 thomas te1->mode, te2->mode, repo);
584 19ae6da1 2018-11-05 stsp if (id_match)
585 f6861a81 2018-09-13 stsp return NULL;
587 56e0773d 2019-11-28 stsp return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
588 aaa13589 2019-06-01 stsp cb, cb_arg);
591 474b4f94 2017-11-30 stsp static const struct got_error *
592 56e0773d 2019-11-28 stsp diff_entry_new_old(struct got_tree_entry *te2,
593 dd2e2f52 2022-07-01 thomas struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
594 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
595 31b4484f 2019-07-27 stsp int diff_content)
597 f6861a81 2018-09-13 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
598 63c5ca5d 2019-08-24 stsp return NULL;
600 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te2))
601 f6861a81 2018-09-13 stsp return NULL;
603 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
604 dd2e2f52 2022-07-01 thomas return diff_added_tree(&te2->id, f1, f2, fd2, label2,
605 a0f32f33 2022-06-13 thomas repo, cb, cb_arg, diff_content);
607 31b4484f 2019-07-27 stsp if (diff_content)
608 dd2e2f52 2022-07-01 thomas return diff_added_blob(&te2->id, f1, f2, fd2,
609 19a6a6b5 2022-07-01 thomas label2, te2->mode, repo, cb, cb_arg);
611 a0f32f33 2022-06-13 thomas return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
612 a0f32f33 2022-06-13 thomas NULL, label2, 0, te2->mode, repo);
615 772fcad5 2023-01-07 thomas static void
616 772fcad5 2023-01-07 thomas diffstat_field_width(size_t *maxlen, int *add_cols, int *rm_cols, size_t len,
617 772fcad5 2023-01-07 thomas uint32_t add, uint32_t rm)
619 772fcad5 2023-01-07 thomas int d1 = 1, d2 = 1;
621 772fcad5 2023-01-07 thomas *maxlen = MAX(*maxlen, len);
623 772fcad5 2023-01-07 thomas while (add /= 10)
625 772fcad5 2023-01-07 thomas *add_cols = MAX(*add_cols, d1);
627 772fcad5 2023-01-07 thomas while (rm /= 10)
629 772fcad5 2023-01-07 thomas *rm_cols = MAX(*rm_cols, d2);
632 0208f208 2020-05-05 stsp const struct got_error *
633 772fcad5 2023-01-07 thomas got_diff_tree_compute_diffstat(void *arg, struct got_blob_object *blob1,
634 772fcad5 2023-01-07 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
635 772fcad5 2023-01-07 thomas struct got_object_id *id1, struct got_object_id *id2,
636 772fcad5 2023-01-07 thomas const char *label1, const char *label2,
637 772fcad5 2023-01-07 thomas mode_t mode1, mode_t mode2, struct got_repository *repo)
639 772fcad5 2023-01-07 thomas const struct got_error *err = NULL;
640 772fcad5 2023-01-07 thomas struct got_diffreg_result *result = NULL;
641 772fcad5 2023-01-07 thomas struct diff_result *r;
642 772fcad5 2023-01-07 thomas struct got_diff_changed_path *change = NULL;
643 772fcad5 2023-01-07 thomas struct got_diffstat_cb_arg *a = arg;
644 772fcad5 2023-01-07 thomas struct got_pathlist_entry *pe;
645 772fcad5 2023-01-07 thomas char *path = NULL;
648 772fcad5 2023-01-07 thomas path = strdup(label2 ? label2 : label1);
649 772fcad5 2023-01-07 thomas if (path == NULL)
650 772fcad5 2023-01-07 thomas return got_error_from_errno("malloc");
652 772fcad5 2023-01-07 thomas change = malloc(sizeof(*change));
653 772fcad5 2023-01-07 thomas if (change == NULL) {
654 772fcad5 2023-01-07 thomas err = got_error_from_errno("malloc");
655 772fcad5 2023-01-07 thomas goto done;
658 772fcad5 2023-01-07 thomas change->add = 0;
659 772fcad5 2023-01-07 thomas change->rm = 0;
660 772fcad5 2023-01-07 thomas change->status = GOT_STATUS_NO_CHANGE;
661 772fcad5 2023-01-07 thomas if (id1 == NULL)
662 772fcad5 2023-01-07 thomas change->status = GOT_STATUS_ADD;
663 772fcad5 2023-01-07 thomas else if (id2 == NULL)
664 772fcad5 2023-01-07 thomas change->status = GOT_STATUS_DELETE;
666 772fcad5 2023-01-07 thomas if (got_object_id_cmp(id1, id2) != 0)
667 772fcad5 2023-01-07 thomas change->status = GOT_STATUS_MODIFY;
668 772fcad5 2023-01-07 thomas else if (mode1 != mode2)
669 772fcad5 2023-01-07 thomas change->status = GOT_STATUS_MODE_CHANGE;
672 772fcad5 2023-01-07 thomas if (f1) {
673 772fcad5 2023-01-07 thomas err = got_opentemp_truncate(f1);
675 772fcad5 2023-01-07 thomas goto done;
677 772fcad5 2023-01-07 thomas if (f2) {
678 772fcad5 2023-01-07 thomas err = got_opentemp_truncate(f2);
680 772fcad5 2023-01-07 thomas goto done;
683 772fcad5 2023-01-07 thomas if (blob1) {
684 772fcad5 2023-01-07 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1,
687 772fcad5 2023-01-07 thomas goto done;
689 772fcad5 2023-01-07 thomas if (blob2) {
690 772fcad5 2023-01-07 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2,
693 772fcad5 2023-01-07 thomas goto done;
696 772fcad5 2023-01-07 thomas err = got_diffreg(&result, f1, f2, a->diff_algo, a->ignore_ws,
697 772fcad5 2023-01-07 thomas a->force_text);
699 772fcad5 2023-01-07 thomas goto done;
701 772fcad5 2023-01-07 thomas for (i = 0, r = result->result; i < r->chunks.len; ++i) {
702 772fcad5 2023-01-07 thomas int flags = (r->left->atomizer_flags | r->right->atomizer_flags);
703 772fcad5 2023-01-07 thomas int isbin = (flags & DIFF_ATOMIZER_FOUND_BINARY_DATA);
705 772fcad5 2023-01-07 thomas if (!isbin || a->force_text) {
706 772fcad5 2023-01-07 thomas struct diff_chunk *c;
707 772fcad5 2023-01-07 thomas int clc, crc;
709 772fcad5 2023-01-07 thomas c = diff_chunk_get(r, i);
710 772fcad5 2023-01-07 thomas clc = diff_chunk_get_left_count(c);
711 772fcad5 2023-01-07 thomas crc = diff_chunk_get_right_count(c);
713 772fcad5 2023-01-07 thomas if (clc && !crc)
714 772fcad5 2023-01-07 thomas change->rm += clc;
715 772fcad5 2023-01-07 thomas else if (crc && !clc)
716 772fcad5 2023-01-07 thomas change->add += crc;
720 772fcad5 2023-01-07 thomas err = got_pathlist_append(a->paths, path, change);
722 772fcad5 2023-01-07 thomas goto done;
724 772fcad5 2023-01-07 thomas pe = TAILQ_LAST(a->paths, got_pathlist_head);
725 772fcad5 2023-01-07 thomas diffstat_field_width(&a->max_path_len, &a->add_cols, &a->rm_cols,
726 772fcad5 2023-01-07 thomas pe->path_len, change->add, change->rm);
727 772fcad5 2023-01-07 thomas a->ins += change->add;
728 772fcad5 2023-01-07 thomas a->del += change->rm;
729 772fcad5 2023-01-07 thomas ++a->nfiles;
732 772fcad5 2023-01-07 thomas if (result) {
733 772fcad5 2023-01-07 thomas const struct got_error *free_err;
735 772fcad5 2023-01-07 thomas free_err = got_diffreg_result_free(result);
736 772fcad5 2023-01-07 thomas if (free_err && err == NULL)
737 772fcad5 2023-01-07 thomas err = free_err;
739 772fcad5 2023-01-07 thomas if (err) {
740 772fcad5 2023-01-07 thomas free(path);
741 772fcad5 2023-01-07 thomas free(change);
743 772fcad5 2023-01-07 thomas return err;
746 772fcad5 2023-01-07 thomas const struct got_error *
747 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
748 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
749 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
750 a0f32f33 2022-06-13 thomas const char *label1, const char *label2,
751 0208f208 2020-05-05 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
753 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
754 0208f208 2020-05-05 stsp struct got_pathlist_head *paths = arg;
755 0208f208 2020-05-05 stsp struct got_diff_changed_path *change = NULL;
756 0208f208 2020-05-05 stsp char *path = NULL;
758 0208f208 2020-05-05 stsp path = strdup(label2 ? label2 : label1);
759 0208f208 2020-05-05 stsp if (path == NULL)
760 0208f208 2020-05-05 stsp return got_error_from_errno("malloc");
762 0208f208 2020-05-05 stsp change = malloc(sizeof(*change));
763 0208f208 2020-05-05 stsp if (change == NULL) {
764 0208f208 2020-05-05 stsp err = got_error_from_errno("malloc");
768 0208f208 2020-05-05 stsp change->status = GOT_STATUS_NO_CHANGE;
769 0208f208 2020-05-05 stsp if (id1 == NULL)
770 0208f208 2020-05-05 stsp change->status = GOT_STATUS_ADD;
771 0208f208 2020-05-05 stsp else if (id2 == NULL)
772 0208f208 2020-05-05 stsp change->status = GOT_STATUS_DELETE;
774 0208f208 2020-05-05 stsp if (got_object_id_cmp(id1, id2) != 0)
775 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODIFY;
776 0208f208 2020-05-05 stsp else if (mode1 != mode2)
777 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODE_CHANGE;
780 4c71f93b 2021-12-31 thomas err = got_pathlist_append(paths, path, change);
783 0208f208 2020-05-05 stsp free(path);
784 0208f208 2020-05-05 stsp free(change);
786 0208f208 2020-05-05 stsp return err;
789 474b4f94 2017-11-30 stsp const struct got_error *
790 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
791 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
792 19a6a6b5 2022-07-01 thomas const char *label1, const char *label2,
793 a0f32f33 2022-06-13 thomas struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
794 a0f32f33 2022-06-13 thomas int diff_content)
796 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
797 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
798 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
799 f6861a81 2018-09-13 stsp char *l1 = NULL, *l2 = NULL;
800 56e0773d 2019-11-28 stsp int tidx1 = 0, tidx2 = 0;
802 883f0469 2018-06-23 stsp if (tree1) {
803 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, 0);
804 60f50a58 2018-09-15 stsp if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
805 f6861a81 2018-09-13 stsp te1->name) == -1)
806 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
808 883f0469 2018-06-23 stsp if (tree2) {
809 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, 0);
810 60f50a58 2018-09-15 stsp if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
811 f6861a81 2018-09-13 stsp te2->name) == -1)
812 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
817 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
819 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree2,
820 1de5e065 2019-06-01 stsp te1->name);
824 f6861a81 2018-09-13 stsp if (te && asprintf(&l2, "%s%s%s", label2,
825 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te->name) == -1)
827 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
829 19a6a6b5 2022-07-01 thomas err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
830 19a6a6b5 2022-07-01 thomas l1, l2, repo, cb, cb_arg, diff_content);
836 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
838 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree1,
839 1de5e065 2019-06-01 stsp te2->name);
842 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
843 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te->name) == -1)
845 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
847 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
848 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te2->name) == -1)
850 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
852 dd2e2f52 2022-07-01 thomas err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
853 dd2e2f52 2022-07-01 thomas repo, cb, cb_arg, diff_content);
862 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, tidx1);
864 f6861a81 2018-09-13 stsp asprintf(&l1, "%s%s%s", label1,
865 f6861a81 2018-09-13 stsp label1[0] ? "/" : "", te1->name) == -1)
866 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
872 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, tidx2);
874 f6861a81 2018-09-13 stsp asprintf(&l2, "%s%s%s", label2,
875 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te2->name) == -1)
876 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
878 474b4f94 2017-11-30 stsp } while (te1 || te2);
880 11528a82 2018-05-19 stsp return err;
883 11528a82 2018-05-19 stsp const struct got_error *
884 82c78e96 2022-08-06 thomas got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
885 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
886 19a6a6b5 2022-07-01 thomas struct got_object_id *id1, struct got_object_id *id2,
887 25ec7006 2022-07-01 thomas const char *label1, const char *label2,
888 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo, int diff_context,
889 64453f7e 2020-11-21 stsp int ignore_whitespace, int force_text_diff,
890 64453f7e 2020-11-21 stsp struct got_repository *repo, FILE *outfile)
892 11528a82 2018-05-19 stsp const struct got_error *err;
893 11528a82 2018-05-19 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
895 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
896 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
899 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
904 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
908 82c78e96 2022-08-06 thomas err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
909 82c78e96 2022-08-06 thomas diff_algo, diff_context, ignore_whitespace, force_text_diff,
910 82c78e96 2022-08-06 thomas outfile);
912 cc8021af 2021-10-12 thomas if (blob1)
913 cc8021af 2021-10-12 thomas got_object_blob_close(blob1);
914 cc8021af 2021-10-12 thomas if (blob2)
915 cc8021af 2021-10-12 thomas got_object_blob_close(blob2);
916 cc8021af 2021-10-12 thomas return err;
919 cc8021af 2021-10-12 thomas static const struct got_error *
920 cc8021af 2021-10-12 thomas diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
921 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
922 a0f32f33 2022-06-13 thomas struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
924 cc8021af 2021-10-12 thomas const struct got_error *err = NULL;
925 cc8021af 2021-10-12 thomas struct got_pathlist_entry *pe;
926 cc8021af 2021-10-12 thomas struct got_object_id *id1 = NULL, *id2 = NULL;
927 cc8021af 2021-10-12 thomas struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
928 cc8021af 2021-10-12 thomas struct got_blob_object *blob1 = NULL, *blob2 = NULL;
930 cc8021af 2021-10-12 thomas TAILQ_FOREACH(pe, paths, entry) {
931 cc8021af 2021-10-12 thomas int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
932 cc8021af 2021-10-12 thomas mode_t mode1 = 0, mode2 = 0;
934 cc8021af 2021-10-12 thomas free(id1);
935 cc8021af 2021-10-12 thomas id1 = NULL;
936 cc8021af 2021-10-12 thomas free(id2);
937 cc8021af 2021-10-12 thomas id2 = NULL;
938 cc8021af 2021-10-12 thomas if (subtree1) {
939 cc8021af 2021-10-12 thomas got_object_tree_close(subtree1);
940 cc8021af 2021-10-12 thomas subtree1 = NULL;
942 cc8021af 2021-10-12 thomas if (subtree2) {
943 cc8021af 2021-10-12 thomas got_object_tree_close(subtree2);
944 cc8021af 2021-10-12 thomas subtree2 = NULL;
946 cc8021af 2021-10-12 thomas if (blob1) {
947 cc8021af 2021-10-12 thomas got_object_blob_close(blob1);
948 cc8021af 2021-10-12 thomas blob1 = NULL;
950 cc8021af 2021-10-12 thomas if (blob2) {
951 cc8021af 2021-10-12 thomas got_object_blob_close(blob2);
952 cc8021af 2021-10-12 thomas blob2 = NULL;
955 cc8021af 2021-10-12 thomas err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
956 cc8021af 2021-10-12 thomas pe->path);
957 cc8021af 2021-10-12 thomas if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
958 cc8021af 2021-10-12 thomas goto done;
959 cc8021af 2021-10-12 thomas err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
960 cc8021af 2021-10-12 thomas pe->path);
961 cc8021af 2021-10-12 thomas if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
962 cc8021af 2021-10-12 thomas goto done;
963 cc8021af 2021-10-12 thomas if (id1 == NULL && id2 == NULL) {
964 cc8021af 2021-10-12 thomas err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
965 cc8021af 2021-10-12 thomas goto done;
967 cc8021af 2021-10-12 thomas if (id1) {
968 cc8021af 2021-10-12 thomas err = got_object_get_type(&type1, repo, id1);
970 cc8021af 2021-10-12 thomas goto done;
972 cc8021af 2021-10-12 thomas if (id2) {
973 cc8021af 2021-10-12 thomas err = got_object_get_type(&type2, repo, id2);
975 cc8021af 2021-10-12 thomas goto done;
977 cc8021af 2021-10-12 thomas if (type1 == GOT_OBJ_TYPE_ANY &&
978 cc8021af 2021-10-12 thomas type2 == GOT_OBJ_TYPE_ANY) {
979 cc8021af 2021-10-12 thomas err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
980 cc8021af 2021-10-12 thomas goto done;
981 cc8021af 2021-10-12 thomas } else if (type1 != GOT_OBJ_TYPE_ANY &&
982 cc8021af 2021-10-12 thomas type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
983 cc8021af 2021-10-12 thomas err = got_error(GOT_ERR_OBJ_TYPE);
984 cc8021af 2021-10-12 thomas goto done;
987 cc8021af 2021-10-12 thomas if (type1 == GOT_OBJ_TYPE_BLOB ||
988 cc8021af 2021-10-12 thomas type2 == GOT_OBJ_TYPE_BLOB) {
989 cc8021af 2021-10-12 thomas if (id1) {
990 cc8021af 2021-10-12 thomas err = got_object_open_as_blob(&blob1, repo,
991 f4ae6ddb 2022-07-01 thomas id1, 8192, fd1);
993 cc8021af 2021-10-12 thomas goto done;
995 cc8021af 2021-10-12 thomas if (id2) {
996 cc8021af 2021-10-12 thomas err = got_object_open_as_blob(&blob2, repo,
997 f4ae6ddb 2022-07-01 thomas id2, 8192, fd2);
999 cc8021af 2021-10-12 thomas goto done;
1001 a0f32f33 2022-06-13 thomas err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
1002 cc8021af 2021-10-12 thomas id1 ? pe->path : "/dev/null",
1003 cc8021af 2021-10-12 thomas id2 ? pe->path : "/dev/null",
1004 cc8021af 2021-10-12 thomas mode1, mode2, repo);
1005 cc8021af 2021-10-12 thomas if (err)
1006 cc8021af 2021-10-12 thomas goto done;
1007 cc8021af 2021-10-12 thomas } else if (type1 == GOT_OBJ_TYPE_TREE ||
1008 cc8021af 2021-10-12 thomas type2 == GOT_OBJ_TYPE_TREE) {
1009 cc8021af 2021-10-12 thomas if (id1) {
1010 cc8021af 2021-10-12 thomas err = got_object_open_as_tree(&subtree1, repo,
1012 cc8021af 2021-10-12 thomas if (err)
1013 cc8021af 2021-10-12 thomas goto done;
1015 cc8021af 2021-10-12 thomas if (id2) {
1016 cc8021af 2021-10-12 thomas err = got_object_open_as_tree(&subtree2, repo,
1018 cc8021af 2021-10-12 thomas if (err)
1019 cc8021af 2021-10-12 thomas goto done;
1021 a0f32f33 2022-06-13 thomas err = got_diff_tree(subtree1, subtree2, f1, f2,
1022 19a6a6b5 2022-07-01 thomas fd1, fd2,
1023 cc8021af 2021-10-12 thomas id1 ? pe->path : "/dev/null",
1024 cc8021af 2021-10-12 thomas id2 ? pe->path : "/dev/null",
1025 cc8021af 2021-10-12 thomas repo, cb, cb_arg, 1);
1026 cc8021af 2021-10-12 thomas if (err)
1027 cc8021af 2021-10-12 thomas goto done;
1028 cc8021af 2021-10-12 thomas } else {
1029 cc8021af 2021-10-12 thomas err = got_error(GOT_ERR_OBJ_TYPE);
1030 cc8021af 2021-10-12 thomas goto done;
1034 cc8021af 2021-10-12 thomas free(id1);
1035 cc8021af 2021-10-12 thomas free(id2);
1036 cc8021af 2021-10-12 thomas if (subtree1)
1037 cc8021af 2021-10-12 thomas got_object_tree_close(subtree1);
1038 cc8021af 2021-10-12 thomas if (subtree2)
1039 cc8021af 2021-10-12 thomas got_object_tree_close(subtree2);
1040 11528a82 2018-05-19 stsp if (blob1)
1041 11528a82 2018-05-19 stsp got_object_blob_close(blob1);
1042 11528a82 2018-05-19 stsp if (blob2)
1043 11528a82 2018-05-19 stsp got_object_blob_close(blob2);
1044 474b4f94 2017-11-30 stsp return err;
1047 9b4458b4 2022-06-26 thomas static const struct got_error *
1048 82c78e96 2022-08-06 thomas show_object_id(struct got_diff_line **lines, size_t *nlines,
1049 82c78e96 2022-08-06 thomas const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
1051 9b4458b4 2022-06-26 thomas const struct got_error *err;
1053 9b4458b4 2022-06-26 thomas off_t outoff = 0;
1055 9b4458b4 2022-06-26 thomas n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
1056 7e1f3a3d 2022-08-16 thomas if (n < 0)
1057 7e1f3a3d 2022-08-16 thomas return got_error_from_errno("fprintf");
1059 82c78e96 2022-08-06 thomas if (lines != NULL && *lines != NULL) {
1060 9b4458b4 2022-06-26 thomas if (*nlines == 0) {
1061 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0,
1062 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
1063 9b4458b4 2022-06-26 thomas if (err)
1064 9b4458b4 2022-06-26 thomas return err;
1066 82c78e96 2022-08-06 thomas outoff = (*lines)[*nlines - 1].offset;
1068 9b4458b4 2022-06-26 thomas outoff += n;
1069 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
1070 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
1071 9b4458b4 2022-06-26 thomas if (err)
1072 9b4458b4 2022-06-26 thomas return err;
1075 9b4458b4 2022-06-26 thomas return NULL;
1078 9b4458b4 2022-06-26 thomas static const struct got_error *
1079 82c78e96 2022-08-06 thomas diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1080 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
1081 19a6a6b5 2022-07-01 thomas struct got_object_id *id1, struct got_object_id *id2,
1082 0c6f49ba 2022-07-01 thomas struct got_pathlist_head *paths, const char *label1, const char *label2,
1083 0c6f49ba 2022-07-01 thomas int diff_context, int ignore_whitespace, int force_text_diff,
1084 25ec7006 2022-07-01 thomas struct got_repository *repo, FILE *outfile,
1085 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo)
1087 11528a82 2018-05-19 stsp const struct got_error *err;
1088 11528a82 2018-05-19 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1089 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
1090 82c78e96 2022-08-06 thomas int want_linemeta = (lines != NULL && *lines != NULL);
1092 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
1093 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
1095 15a94983 2018-12-23 stsp if (id1) {
1096 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo, id1);
1098 cd0acaa7 2018-05-20 stsp goto done;
1100 15a94983 2018-12-23 stsp if (id2) {
1101 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo, id2);
1103 cd0acaa7 2018-05-20 stsp goto done;
1106 25ec7006 2022-07-01 thomas arg.diff_algo = diff_algo;
1107 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1108 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
1109 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
1110 aaa13589 2019-06-01 stsp arg.outfile = outfile;
1111 82c78e96 2022-08-06 thomas if (want_linemeta) {
1112 82c78e96 2022-08-06 thomas arg.lines = *lines;
1113 fe621944 2020-11-10 stsp arg.nlines = *nlines;
1115 82c78e96 2022-08-06 thomas arg.lines = NULL;
1116 fe621944 2020-11-10 stsp arg.nlines = 0;
1118 cc8021af 2021-10-12 thomas if (paths == NULL || TAILQ_EMPTY(paths)) {
1119 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
1120 19a6a6b5 2022-07-01 thomas label1, label2, repo,
1121 19a6a6b5 2022-07-01 thomas got_diff_blob_output_unidiff, &arg, 1);
1122 cc8021af 2021-10-12 thomas } else {
1123 19a6a6b5 2022-07-01 thomas err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
1124 cc8021af 2021-10-12 thomas got_diff_blob_output_unidiff, &arg);
1126 82c78e96 2022-08-06 thomas if (want_linemeta) {
1127 82c78e96 2022-08-06 thomas *lines = arg.lines; /* was likely re-allocated */
1128 fe621944 2020-11-10 stsp *nlines = arg.nlines;
1131 11528a82 2018-05-19 stsp if (tree1)
1132 11528a82 2018-05-19 stsp got_object_tree_close(tree1);
1133 11528a82 2018-05-19 stsp if (tree2)
1134 11528a82 2018-05-19 stsp got_object_tree_close(tree2);
1135 11528a82 2018-05-19 stsp return err;
1138 11528a82 2018-05-19 stsp const struct got_error *
1139 82c78e96 2022-08-06 thomas got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1140 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
1141 19a6a6b5 2022-07-01 thomas struct got_object_id *id1, struct got_object_id *id2,
1142 0c6f49ba 2022-07-01 thomas struct got_pathlist_head *paths, const char *label1, const char *label2,
1143 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1144 25ec7006 2022-07-01 thomas int force_text_diff, struct got_repository *repo, FILE *outfile)
1146 9b4458b4 2022-06-26 thomas const struct got_error *err;
1147 9b4458b4 2022-06-26 thomas char *idstr = NULL;
1149 9b4458b4 2022-06-26 thomas if (id1 == NULL && id2 == NULL)
1150 9b4458b4 2022-06-26 thomas return got_error(GOT_ERR_NO_OBJ);
1152 9b4458b4 2022-06-26 thomas if (id1) {
1153 9b4458b4 2022-06-26 thomas err = got_object_id_str(&idstr, id1);
1154 9b4458b4 2022-06-26 thomas if (err)
1155 9b4458b4 2022-06-26 thomas goto done;
1156 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1157 9b4458b4 2022-06-26 thomas if (err)
1158 9b4458b4 2022-06-26 thomas goto done;
1159 9b4458b4 2022-06-26 thomas free(idstr);
1160 9b4458b4 2022-06-26 thomas idstr = NULL;
1161 9b4458b4 2022-06-26 thomas } else {
1162 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1163 82c78e96 2022-08-06 thomas outfile);
1164 9b4458b4 2022-06-26 thomas if (err)
1165 9b4458b4 2022-06-26 thomas goto done;
1168 9b4458b4 2022-06-26 thomas if (id2) {
1169 9b4458b4 2022-06-26 thomas err = got_object_id_str(&idstr, id2);
1170 9b4458b4 2022-06-26 thomas if (err)
1171 9b4458b4 2022-06-26 thomas goto done;
1172 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1173 9b4458b4 2022-06-26 thomas if (err)
1174 9b4458b4 2022-06-26 thomas goto done;
1175 9b4458b4 2022-06-26 thomas free(idstr);
1176 9b4458b4 2022-06-26 thomas idstr = NULL;
1177 9b4458b4 2022-06-26 thomas } else {
1178 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1179 82c78e96 2022-08-06 thomas outfile);
1180 9b4458b4 2022-06-26 thomas if (err)
1181 9b4458b4 2022-06-26 thomas goto done;
1184 82c78e96 2022-08-06 thomas err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1185 82c78e96 2022-08-06 thomas paths, label1, label2, diff_context, ignore_whitespace,
1186 25ec7006 2022-07-01 thomas force_text_diff, repo, outfile, diff_algo);
1188 9b4458b4 2022-06-26 thomas free(idstr);
1189 9b4458b4 2022-06-26 thomas return err;
1192 9b4458b4 2022-06-26 thomas const struct got_error *
1193 82c78e96 2022-08-06 thomas got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1194 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
1195 19a6a6b5 2022-07-01 thomas struct got_object_id *id1, struct got_object_id *id2,
1196 25ec7006 2022-07-01 thomas struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1197 64453f7e 2020-11-21 stsp int diff_context, int ignore_whitespace, int force_text_diff,
1198 15a94983 2018-12-23 stsp struct got_repository *repo, FILE *outfile)
1200 11528a82 2018-05-19 stsp const struct got_error *err;
1201 11528a82 2018-05-19 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1202 9b4458b4 2022-06-26 thomas char *idstr = NULL;
1204 15a94983 2018-12-23 stsp if (id2 == NULL)
1205 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
1207 15a94983 2018-12-23 stsp if (id1) {
1208 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit1, repo, id1);
1210 cd0acaa7 2018-05-20 stsp goto done;
1211 9b4458b4 2022-06-26 thomas err = got_object_id_str(&idstr, id1);
1212 9b4458b4 2022-06-26 thomas if (err)
1213 9b4458b4 2022-06-26 thomas goto done;
1214 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "commit", '-', idstr,
1215 82c78e96 2022-08-06 thomas outfile);
1216 9b4458b4 2022-06-26 thomas if (err)
1217 9b4458b4 2022-06-26 thomas goto done;
1218 9b4458b4 2022-06-26 thomas free(idstr);
1219 9b4458b4 2022-06-26 thomas idstr = NULL;
1220 9b4458b4 2022-06-26 thomas } else {
1221 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1222 82c78e96 2022-08-06 thomas outfile);
1223 9b4458b4 2022-06-26 thomas if (err)
1224 9b4458b4 2022-06-26 thomas goto done;
1227 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, repo, id2);
1229 9b697879 2018-05-20 stsp goto done;
1231 9b4458b4 2022-06-26 thomas err = got_object_id_str(&idstr, id2);
1232 9b4458b4 2022-06-26 thomas if (err)
1233 9b4458b4 2022-06-26 thomas goto done;
1234 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1235 9b4458b4 2022-06-26 thomas if (err)
1236 9b4458b4 2022-06-26 thomas goto done;
1238 82c78e96 2022-08-06 thomas err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1239 15a94983 2018-12-23 stsp commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1240 cc8021af 2021-10-12 thomas got_object_commit_get_tree_id(commit2), paths, "", "",
1241 25ec7006 2022-07-01 thomas diff_context, ignore_whitespace, force_text_diff, repo, outfile,
1242 25ec7006 2022-07-01 thomas diff_algo);
1244 11528a82 2018-05-19 stsp if (commit1)
1245 11528a82 2018-05-19 stsp got_object_commit_close(commit1);
1246 11528a82 2018-05-19 stsp if (commit2)
1247 11528a82 2018-05-19 stsp got_object_commit_close(commit2);
1248 9b4458b4 2022-06-26 thomas free(idstr);
1249 11528a82 2018-05-19 stsp return err;
1252 dc424a06 2019-08-07 stsp const struct got_error *
1253 fe621944 2020-11-10 stsp got_diff_files(struct got_diffreg_result **resultp,
1254 dd2e2f52 2022-07-01 thomas FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1255 dd2e2f52 2022-07-01 thomas const char *label2, int diff_context, int ignore_whitespace,
1256 25ec7006 2022-07-01 thomas int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1258 dc424a06 2019-08-07 stsp const struct got_error *err = NULL;
1259 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
1261 fe621944 2020-11-10 stsp if (resultp)
1262 fe621944 2020-11-10 stsp *resultp = NULL;
1264 dc424a06 2019-08-07 stsp if (outfile) {
1265 dc424a06 2019-08-07 stsp fprintf(outfile, "file - %s\n",
1266 dd2e2f52 2022-07-01 thomas f1_exists ? label1 : "/dev/null");
1267 dc424a06 2019-08-07 stsp fprintf(outfile, "file + %s\n",
1268 dd2e2f52 2022-07-01 thomas f2_exists ? label2 : "/dev/null");
1271 25ec7006 2022-07-01 thomas err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1272 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
1274 fe621944 2020-11-10 stsp goto done;
1276 fe621944 2020-11-10 stsp if (outfile) {
1277 fe621944 2020-11-10 stsp err = got_diffreg_output(NULL, NULL, diffreg_result,
1278 dd2e2f52 2022-07-01 thomas f1_exists, f2_exists, label1, label2,
1279 1cb46f00 2020-11-21 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1281 dc424a06 2019-08-07 stsp goto done;
1285 fe621944 2020-11-10 stsp if (resultp && err == NULL)
1286 fe621944 2020-11-10 stsp *resultp = diffreg_result;
1287 fe621944 2020-11-10 stsp else if (diffreg_result) {
1288 fe621944 2020-11-10 stsp const struct got_error *free_err;
1289 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
1290 fe621944 2020-11-10 stsp if (free_err && err == NULL)
1291 fe621944 2020-11-10 stsp err = free_err;
1294 dc424a06 2019-08-07 stsp return err;