Blame


1 7d283eee 2017-11-29 stsp /*
2 7d283eee 2017-11-29 stsp * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
3 7d283eee 2017-11-29 stsp *
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.
7 7d283eee 2017-11-29 stsp *
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.
15 7d283eee 2017-11-29 stsp */
16 4fccd2fe 2023-03-08 thomas
17 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
18 7d283eee 2017-11-29 stsp
19 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
20 1c7f0520 2017-11-29 stsp #include <sys/stat.h>
21 7d283eee 2017-11-29 stsp
22 7d283eee 2017-11-29 stsp #include <stdio.h>
23 7d283eee 2017-11-29 stsp #include <stdlib.h>
24 7d283eee 2017-11-29 stsp #include <string.h>
25 f9d67749 2017-11-30 stsp #include <limits.h>
26 7d283eee 2017-11-29 stsp #include <zlib.h>
27 dd038bc6 2021-09-21 thomas.ad
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"
36 7d283eee 2017-11-29 stsp
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"
41 772fcad5 2023-01-07 thomas
42 772fcad5 2023-01-07 thomas #ifndef MAX
43 772fcad5 2023-01-07 thomas #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
44 772fcad5 2023-01-07 thomas #endif
45 a7852263 2017-11-30 stsp
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)
49 fe621944 2020-11-10 stsp {
50 82c78e96 2022-08-06 thomas struct got_diff_line *p;
51 fe621944 2020-11-10 stsp
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)++;
59 82c78e96 2022-08-06 thomas
60 fe621944 2020-11-10 stsp return NULL;
61 fe621944 2020-11-10 stsp }
62 fe621944 2020-11-10 stsp
63 53d03f97 2023-01-10 thomas static void
64 53d03f97 2023-01-10 thomas diffstat_field_width(size_t *maxlen, int *add_cols, int *rm_cols, size_t len,
65 53d03f97 2023-01-10 thomas uint32_t add, uint32_t rm)
66 53d03f97 2023-01-10 thomas {
67 53d03f97 2023-01-10 thomas int d1 = 1, d2 = 1;
68 53d03f97 2023-01-10 thomas
69 53d03f97 2023-01-10 thomas if (maxlen)
70 53d03f97 2023-01-10 thomas *maxlen = MAX(*maxlen, len);
71 53d03f97 2023-01-10 thomas
72 53d03f97 2023-01-10 thomas while (add /= 10)
73 53d03f97 2023-01-10 thomas ++d1;
74 53d03f97 2023-01-10 thomas *add_cols = MAX(*add_cols, d1);
75 53d03f97 2023-01-10 thomas
76 53d03f97 2023-01-10 thomas while (rm /= 10)
77 53d03f97 2023-01-10 thomas ++d2;
78 53d03f97 2023-01-10 thomas *rm_cols = MAX(*rm_cols, d2);
79 53d03f97 2023-01-10 thomas }
80 53d03f97 2023-01-10 thomas
81 fe621944 2020-11-10 stsp static const struct got_error *
82 53d03f97 2023-01-10 thomas get_diffstat(struct got_diffstat_cb_arg *ds, const char *path,
83 53d03f97 2023-01-10 thomas struct diff_result *r, int force_text, int status)
84 53d03f97 2023-01-10 thomas {
85 53d03f97 2023-01-10 thomas const struct got_error *err;
86 53d03f97 2023-01-10 thomas struct got_pathlist_entry *pe;
87 53d03f97 2023-01-10 thomas struct got_diff_changed_path *change = NULL;
88 53d03f97 2023-01-10 thomas int flags = (r->left->atomizer_flags | r->right->atomizer_flags);
89 53d03f97 2023-01-10 thomas int isbin = (flags & DIFF_ATOMIZER_FOUND_BINARY_DATA);
90 53d03f97 2023-01-10 thomas int i;
91 53d03f97 2023-01-10 thomas
92 53d03f97 2023-01-10 thomas change = calloc(1, sizeof(*change));
93 53d03f97 2023-01-10 thomas if (change == NULL)
94 e80ef83e 2023-01-10 thomas return got_error_from_errno("calloc");
95 53d03f97 2023-01-10 thomas
96 53d03f97 2023-01-10 thomas if (!isbin || force_text) {
97 53d03f97 2023-01-10 thomas for (i = 0; i < r->chunks.len; ++i) {
98 53d03f97 2023-01-10 thomas struct diff_chunk *c;
99 53d03f97 2023-01-10 thomas int clc, crc;
100 53d03f97 2023-01-10 thomas
101 53d03f97 2023-01-10 thomas c = diff_chunk_get(r, i);
102 53d03f97 2023-01-10 thomas clc = diff_chunk_get_left_count(c);
103 53d03f97 2023-01-10 thomas crc = diff_chunk_get_right_count(c);
104 53d03f97 2023-01-10 thomas
105 53d03f97 2023-01-10 thomas if (crc && !clc)
106 53d03f97 2023-01-10 thomas change->add += crc;
107 53d03f97 2023-01-10 thomas if (clc && !crc)
108 53d03f97 2023-01-10 thomas change->rm += clc;
109 53d03f97 2023-01-10 thomas }
110 53d03f97 2023-01-10 thomas }
111 53d03f97 2023-01-10 thomas
112 53d03f97 2023-01-10 thomas change->status = status;
113 53d03f97 2023-01-10 thomas ds->ins += change->add;
114 53d03f97 2023-01-10 thomas ds->del += change->rm;
115 53d03f97 2023-01-10 thomas ++ds->nfiles;
116 53d03f97 2023-01-10 thomas
117 53d03f97 2023-01-10 thomas err = got_pathlist_append(ds->paths, path, change);
118 92a56907 2023-01-14 thomas if (err) {
119 92a56907 2023-01-14 thomas free(change);
120 53d03f97 2023-01-10 thomas return err;
121 92a56907 2023-01-14 thomas }
122 53d03f97 2023-01-10 thomas
123 53d03f97 2023-01-10 thomas pe = TAILQ_LAST(ds->paths, got_pathlist_head);
124 53d03f97 2023-01-10 thomas diffstat_field_width(&ds->max_path_len, &ds->add_cols, &ds->rm_cols,
125 53d03f97 2023-01-10 thomas pe->path_len, change->add, change->rm);
126 53d03f97 2023-01-10 thomas
127 53d03f97 2023-01-10 thomas return NULL;
128 53d03f97 2023-01-10 thomas }
129 53d03f97 2023-01-10 thomas
130 53d03f97 2023-01-10 thomas static const struct got_error *
131 82c78e96 2022-08-06 thomas diff_blobs(struct got_diff_line **lines, size_t *nlines,
132 fe621944 2020-11-10 stsp struct got_diffreg_result **resultp, struct got_blob_object *blob1,
133 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
134 46f68b20 2019-10-19 stsp const char *label1, const char *label2, mode_t mode1, mode_t mode2,
135 53d03f97 2023-01-10 thomas int diff_context, int ignore_whitespace, int force_text_diff,
136 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg *diffstat, FILE *outfile,
137 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo)
138 7d283eee 2017-11-29 stsp {
139 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
140 01986ce9 2023-02-07 thomas char hex1[GOT_OBJECT_ID_HEX_MAXLEN];
141 01986ce9 2023-02-07 thomas char hex2[GOT_OBJECT_ID_HEX_MAXLEN];
142 0c6f49ba 2022-07-01 thomas const char *idstr1 = NULL, *idstr2 = NULL;
143 92a56907 2023-01-14 thomas char *modestr1 = NULL, *modestr2 = NULL;
144 be659d10 2020-11-18 stsp off_t size1, size2;
145 a6d62598 2023-01-07 thomas struct got_diffreg_result *result = NULL;
146 fe621944 2020-11-10 stsp off_t outoff = 0;
147 fe621944 2020-11-10 stsp int n;
148 7d283eee 2017-11-29 stsp
149 3387ec82 2022-08-30 thomas if (lines && *lines && *nlines > 0)
150 82c78e96 2022-08-06 thomas outoff = (*lines)[*nlines - 1].offset;
151 82c78e96 2022-08-06 thomas else if (lines) {
152 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
153 9c659ea0 2020-11-22 stsp if (err)
154 9c659ea0 2020-11-22 stsp goto done;
155 9c659ea0 2020-11-22 stsp }
156 fe621944 2020-11-10 stsp
157 fe621944 2020-11-10 stsp if (resultp)
158 fe621944 2020-11-10 stsp *resultp = NULL;
159 fe621944 2020-11-10 stsp
160 a0f32f33 2022-06-13 thomas if (f1) {
161 1758cce7 2022-06-13 thomas err = got_opentemp_truncate(f1);
162 a0f32f33 2022-06-13 thomas if (err)
163 a0f32f33 2022-06-13 thomas goto done;
164 fe621944 2020-11-10 stsp }
165 a0f32f33 2022-06-13 thomas if (f2) {
166 1758cce7 2022-06-13 thomas err = got_opentemp_truncate(f2);
167 a0f32f33 2022-06-13 thomas if (err)
168 a0f32f33 2022-06-13 thomas goto done;
169 fe621944 2020-11-10 stsp }
170 7d283eee 2017-11-29 stsp
171 f934cf2c 2018-02-12 stsp size1 = 0;
172 98abbc84 2017-11-30 stsp if (blob1) {
173 f934cf2c 2018-02-12 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
174 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
175 6c4c42e0 2019-06-24 stsp blob1);
176 35e9ba5d 2018-06-21 stsp if (err)
177 35e9ba5d 2018-06-21 stsp goto done;
178 98abbc84 2017-11-30 stsp } else
179 98abbc84 2017-11-30 stsp idstr1 = "/dev/null";
180 7d283eee 2017-11-29 stsp
181 f934cf2c 2018-02-12 stsp size2 = 0;
182 98abbc84 2017-11-30 stsp if (blob2) {
183 f934cf2c 2018-02-12 stsp idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
184 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
185 6c4c42e0 2019-06-24 stsp blob2);
186 35e9ba5d 2018-06-21 stsp if (err)
187 35e9ba5d 2018-06-21 stsp goto done;
188 98abbc84 2017-11-30 stsp } else
189 98abbc84 2017-11-30 stsp idstr2 = "/dev/null";
190 7d283eee 2017-11-29 stsp
191 09de383e 2018-12-24 stsp if (outfile) {
192 40dde666 2020-07-23 stsp int modebits;
193 92a56907 2023-01-14 thomas
194 46f68b20 2019-10-19 stsp if (mode1 && mode1 != mode2) {
195 40dde666 2020-07-23 stsp if (S_ISLNK(mode1))
196 40dde666 2020-07-23 stsp modebits = S_IFLNK;
197 40dde666 2020-07-23 stsp else
198 40dde666 2020-07-23 stsp modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
199 46f68b20 2019-10-19 stsp if (asprintf(&modestr1, " (mode %o)",
200 40dde666 2020-07-23 stsp mode1 & modebits) == -1) {
201 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
202 46f68b20 2019-10-19 stsp goto done;
203 46f68b20 2019-10-19 stsp }
204 46f68b20 2019-10-19 stsp }
205 46f68b20 2019-10-19 stsp if (mode2 && mode1 != mode2) {
206 40dde666 2020-07-23 stsp if (S_ISLNK(mode2))
207 40dde666 2020-07-23 stsp modebits = S_IFLNK;
208 40dde666 2020-07-23 stsp else
209 40dde666 2020-07-23 stsp modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
210 46f68b20 2019-10-19 stsp if (asprintf(&modestr2, " (mode %o)",
211 40dde666 2020-07-23 stsp mode2 & modebits) == -1) {
212 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
213 46f68b20 2019-10-19 stsp goto done;
214 46f68b20 2019-10-19 stsp }
215 46f68b20 2019-10-19 stsp }
216 fe621944 2020-11-10 stsp n = fprintf(outfile, "blob - %s%s\n", idstr1,
217 46f68b20 2019-10-19 stsp modestr1 ? modestr1 : "");
218 fe621944 2020-11-10 stsp if (n < 0)
219 fe621944 2020-11-10 stsp goto done;
220 fe621944 2020-11-10 stsp outoff += n;
221 82c78e96 2022-08-06 thomas if (lines) {
222 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
223 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_BLOB_MIN);
224 fe621944 2020-11-10 stsp if (err)
225 fe621944 2020-11-10 stsp goto done;
226 fe621944 2020-11-10 stsp }
227 fe621944 2020-11-10 stsp
228 fe621944 2020-11-10 stsp n = fprintf(outfile, "blob + %s%s\n", idstr2,
229 46f68b20 2019-10-19 stsp modestr2 ? modestr2 : "");
230 fe621944 2020-11-10 stsp if (n < 0)
231 fe621944 2020-11-10 stsp goto done;
232 fe621944 2020-11-10 stsp outoff += n;
233 82c78e96 2022-08-06 thomas if (lines) {
234 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
235 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_BLOB_PLUS);
236 fe621944 2020-11-10 stsp if (err)
237 fe621944 2020-11-10 stsp goto done;
238 fe621944 2020-11-10 stsp }
239 09de383e 2018-12-24 stsp }
240 53d03f97 2023-01-10 thomas
241 25ec7006 2022-07-01 thomas err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
242 25ec7006 2022-07-01 thomas force_text_diff);
243 fe621944 2020-11-10 stsp if (err)
244 fe621944 2020-11-10 stsp goto done;
245 fe621944 2020-11-10 stsp
246 be97ab03 2023-01-19 thomas if (diffstat) {
247 53d03f97 2023-01-10 thomas char *path = NULL;
248 53d03f97 2023-01-10 thomas int status = GOT_STATUS_NO_CHANGE;
249 e80ef83e 2023-01-10 thomas
250 e80ef83e 2023-01-10 thomas if (blob1 == NULL)
251 e80ef83e 2023-01-10 thomas status = GOT_STATUS_ADD;
252 e80ef83e 2023-01-10 thomas else if (blob2 == NULL)
253 e80ef83e 2023-01-10 thomas status = GOT_STATUS_DELETE;
254 be97ab03 2023-01-19 thomas else {
255 be97ab03 2023-01-19 thomas if (strcmp(idstr1, idstr2) != 0)
256 be97ab03 2023-01-19 thomas status = GOT_STATUS_MODIFY;
257 be97ab03 2023-01-19 thomas else if (mode1 != mode2)
258 be97ab03 2023-01-19 thomas status = GOT_STATUS_MODE_CHANGE;
259 be97ab03 2023-01-19 thomas }
260 53d03f97 2023-01-10 thomas
261 53d03f97 2023-01-10 thomas if (label1 == NULL && label2 == NULL) {
262 53d03f97 2023-01-10 thomas /* diffstat of blobs, show hash instead of path */
263 53d03f97 2023-01-10 thomas if (asprintf(&path, "%.10s -> %.10s",
264 53d03f97 2023-01-10 thomas idstr1, idstr2) == -1) {
265 53d03f97 2023-01-10 thomas err = got_error_from_errno("asprintf");
266 53d03f97 2023-01-10 thomas goto done;
267 53d03f97 2023-01-10 thomas }
268 53d03f97 2023-01-10 thomas } else {
269 e80ef83e 2023-01-10 thomas if (label2 != NULL &&
270 e80ef83e 2023-01-10 thomas (status != GOT_STATUS_DELETE || label1 == NULL))
271 e80ef83e 2023-01-10 thomas path = strdup(label2);
272 e80ef83e 2023-01-10 thomas else
273 e80ef83e 2023-01-10 thomas path = strdup(label1);
274 53d03f97 2023-01-10 thomas if (path == NULL) {
275 e80ef83e 2023-01-10 thomas err = got_error_from_errno("strdup");
276 53d03f97 2023-01-10 thomas goto done;
277 53d03f97 2023-01-10 thomas }
278 53d03f97 2023-01-10 thomas }
279 53d03f97 2023-01-10 thomas
280 be97ab03 2023-01-19 thomas err = get_diffstat(diffstat, path, result->result,
281 be97ab03 2023-01-19 thomas force_text_diff, status);
282 53d03f97 2023-01-10 thomas if (err) {
283 53d03f97 2023-01-10 thomas free(path);
284 53d03f97 2023-01-10 thomas goto done;
285 53d03f97 2023-01-10 thomas }
286 53d03f97 2023-01-10 thomas }
287 53d03f97 2023-01-10 thomas
288 fe621944 2020-11-10 stsp if (outfile) {
289 82c78e96 2022-08-06 thomas err = got_diffreg_output(lines, nlines, result,
290 1cb46f00 2020-11-21 stsp blob1 != NULL, blob2 != NULL,
291 fe621944 2020-11-10 stsp label1 ? label1 : idstr1,
292 fe621944 2020-11-10 stsp label2 ? label2 : idstr2,
293 fe621944 2020-11-10 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
294 fe621944 2020-11-10 stsp if (err)
295 fe621944 2020-11-10 stsp goto done;
296 fe621944 2020-11-10 stsp }
297 fe621944 2020-11-10 stsp
298 a6d62598 2023-01-07 thomas done:
299 92a56907 2023-01-14 thomas free(modestr1);
300 92a56907 2023-01-14 thomas free(modestr2);
301 fe621944 2020-11-10 stsp if (resultp && err == NULL)
302 fe621944 2020-11-10 stsp *resultp = result;
303 a6d62598 2023-01-07 thomas else if (result) {
304 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
305 fe621944 2020-11-10 stsp if (free_err && err == NULL)
306 fe621944 2020-11-10 stsp err = free_err;
307 fe621944 2020-11-10 stsp }
308 a6d62598 2023-01-07 thomas
309 7d283eee 2017-11-29 stsp return err;
310 aaa13589 2019-06-01 stsp }
311 aaa13589 2019-06-01 stsp
312 aaa13589 2019-06-01 stsp const struct got_error *
313 aaa13589 2019-06-01 stsp got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
314 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
315 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
316 a0f32f33 2022-06-13 thomas const char *label1, const char *label2, mode_t mode1, mode_t mode2,
317 a0f32f33 2022-06-13 thomas struct got_repository *repo)
318 aaa13589 2019-06-01 stsp {
319 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg *a = arg;
320 aaa13589 2019-06-01 stsp
321 82c78e96 2022-08-06 thomas return diff_blobs(&a->lines, &a->nlines, NULL,
322 a0f32f33 2022-06-13 thomas blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
323 be97ab03 2023-01-19 thomas a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile,
324 be97ab03 2023-01-19 thomas a->diff_algo);
325 7d283eee 2017-11-29 stsp }
326 474b4f94 2017-11-30 stsp
327 404c43c4 2018-06-21 stsp const struct got_error *
328 82c78e96 2022-08-06 thomas got_diff_blob(struct got_diff_line **lines, size_t*nlines,
329 fe621944 2020-11-10 stsp struct got_blob_object *blob1, struct got_blob_object *blob2,
330 a0f32f33 2022-06-13 thomas FILE *f1, FILE *f2, const char *label1, const char *label2,
331 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo, int diff_context,
332 be97ab03 2023-01-19 thomas int ignore_whitespace, int force_text_diff,
333 53d03f97 2023-01-10 thomas struct got_diffstat_cb_arg *ds, FILE *outfile)
334 404c43c4 2018-06-21 stsp {
335 82c78e96 2022-08-06 thomas return diff_blobs(lines, nlines, NULL, blob1, blob2, f1, f2,
336 64453f7e 2020-11-21 stsp label1, label2, 0, 0, diff_context, ignore_whitespace,
337 be97ab03 2023-01-19 thomas force_text_diff, ds, outfile, diff_algo);
338 404c43c4 2018-06-21 stsp }
339 404c43c4 2018-06-21 stsp
340 7f1f93af 2019-08-06 stsp static const struct got_error *
341 fe621944 2020-11-10 stsp diff_blob_file(struct got_diffreg_result **resultp,
342 a0f32f33 2022-06-13 thomas struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
343 6d054bb9 2022-09-23 thomas FILE *f2, int f2_exists, struct stat *sb2, const char *label2,
344 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
345 be97ab03 2023-01-19 thomas int force_text_diff, struct got_diffstat_cb_arg *diffstat, FILE *outfile)
346 b72f483a 2019-02-05 stsp {
347 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
348 01986ce9 2023-02-07 thomas char hex1[GOT_OBJECT_ID_HEX_MAXLEN];
349 0c6f49ba 2022-07-01 thomas const char *idstr1 = NULL;
350 fe621944 2020-11-10 stsp struct got_diffreg_result *result = NULL;
351 b72f483a 2019-02-05 stsp
352 fe621944 2020-11-10 stsp if (resultp)
353 fe621944 2020-11-10 stsp *resultp = NULL;
354 7f1f93af 2019-08-06 stsp
355 a0f32f33 2022-06-13 thomas if (blob1)
356 b72f483a 2019-02-05 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
357 a0f32f33 2022-06-13 thomas else
358 b72f483a 2019-02-05 stsp idstr1 = "/dev/null";
359 b72f483a 2019-02-05 stsp
360 7f1f93af 2019-08-06 stsp if (outfile) {
361 6d054bb9 2022-09-23 thomas char *mode = NULL;
362 6d054bb9 2022-09-23 thomas
363 6d054bb9 2022-09-23 thomas /* display file mode for new added files only */
364 6d054bb9 2022-09-23 thomas if (f2_exists && blob1 == NULL) {
365 6d054bb9 2022-09-23 thomas int mmask = (S_IRWXU | S_IRWXG | S_IRWXO);
366 6d054bb9 2022-09-23 thomas
367 6d054bb9 2022-09-23 thomas if (S_ISLNK(sb2->st_mode))
368 6d054bb9 2022-09-23 thomas mmask = S_IFLNK;
369 6d054bb9 2022-09-23 thomas if (asprintf(&mode, " (mode %o)",
370 6d054bb9 2022-09-23 thomas sb2->st_mode & mmask) == -1)
371 6d054bb9 2022-09-23 thomas return got_error_from_errno("asprintf");
372 6d054bb9 2022-09-23 thomas }
373 4ce46740 2019-08-08 stsp fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
374 6d054bb9 2022-09-23 thomas fprintf(outfile, "file + %s%s\n",
375 6d054bb9 2022-09-23 thomas f2_exists ? label2 : "/dev/null", mode ? mode : "");
376 6d054bb9 2022-09-23 thomas free(mode);
377 7f1f93af 2019-08-06 stsp }
378 fe621944 2020-11-10 stsp
379 25ec7006 2022-07-01 thomas err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
380 25ec7006 2022-07-01 thomas force_text_diff);
381 cb63de52 2023-09-21 thomas if (err) {
382 cb63de52 2023-09-21 thomas char msg[GOT_ERR_MAX_MSG_SIZE];
383 cb63de52 2023-09-21 thomas if (snprintf(msg, sizeof(msg), "%s vs %s: %s",
384 cb63de52 2023-09-21 thomas label1 ? label1 : idstr1,
385 cb63de52 2023-09-21 thomas f2_exists ? label2 : "/dev/null", err->msg) >= 0) {
386 cb63de52 2023-09-21 thomas err = got_error_msg(err->code, msg);
387 cb63de52 2023-09-21 thomas }
388 fe621944 2020-11-10 stsp goto done;
389 cb63de52 2023-09-21 thomas }
390 fe621944 2020-11-10 stsp
391 fe621944 2020-11-10 stsp if (outfile) {
392 1cb46f00 2020-11-21 stsp err = got_diffreg_output(NULL, NULL, result,
393 dd2e2f52 2022-07-01 thomas blob1 != NULL, f2_exists,
394 1cb46f00 2020-11-21 stsp label2, /* show local file's path, not a blob ID */
395 1cb46f00 2020-11-21 stsp label2, GOT_DIFF_OUTPUT_UNIDIFF,
396 1cb46f00 2020-11-21 stsp diff_context, outfile);
397 7f1f93af 2019-08-06 stsp if (err)
398 53d03f97 2023-01-10 thomas goto done;
399 53d03f97 2023-01-10 thomas }
400 53d03f97 2023-01-10 thomas
401 be97ab03 2023-01-19 thomas if (diffstat) {
402 53d03f97 2023-01-10 thomas char *path = NULL;
403 53d03f97 2023-01-10 thomas int status = GOT_STATUS_NO_CHANGE;
404 53d03f97 2023-01-10 thomas
405 53d03f97 2023-01-10 thomas /*
406 53d03f97 2023-01-10 thomas * Ignore 'm'ode status change: if there's no accompanying
407 53d03f97 2023-01-10 thomas * content change, there'll be no diffstat, and if there
408 53d03f97 2023-01-10 thomas * are actual changes, 'M'odified takes precedence.
409 53d03f97 2023-01-10 thomas */
410 53d03f97 2023-01-10 thomas if (blob1 == NULL)
411 53d03f97 2023-01-10 thomas status = GOT_STATUS_ADD;
412 53d03f97 2023-01-10 thomas else if (!f2_exists)
413 53d03f97 2023-01-10 thomas status = GOT_STATUS_DELETE;
414 53d03f97 2023-01-10 thomas else
415 53d03f97 2023-01-10 thomas status = GOT_STATUS_MODIFY;
416 e80ef83e 2023-01-10 thomas
417 e80ef83e 2023-01-10 thomas if (label2 != NULL &&
418 e80ef83e 2023-01-10 thomas (status != GOT_STATUS_DELETE || label1 == NULL))
419 e80ef83e 2023-01-10 thomas path = strdup(label2);
420 e80ef83e 2023-01-10 thomas else
421 e80ef83e 2023-01-10 thomas path = strdup(label1);
422 e80ef83e 2023-01-10 thomas if (path == NULL) {
423 e80ef83e 2023-01-10 thomas err = got_error_from_errno("strdup");
424 e80ef83e 2023-01-10 thomas goto done;
425 e80ef83e 2023-01-10 thomas }
426 53d03f97 2023-01-10 thomas
427 be97ab03 2023-01-19 thomas err = get_diffstat(diffstat, path, result->result,
428 be97ab03 2023-01-19 thomas force_text_diff, status);
429 53d03f97 2023-01-10 thomas if (err) {
430 53d03f97 2023-01-10 thomas free(path);
431 fe621944 2020-11-10 stsp goto done;
432 53d03f97 2023-01-10 thomas }
433 7f1f93af 2019-08-06 stsp }
434 fe621944 2020-11-10 stsp
435 a6d62598 2023-01-07 thomas done:
436 fe621944 2020-11-10 stsp if (resultp && err == NULL)
437 fe621944 2020-11-10 stsp *resultp = result;
438 fe621944 2020-11-10 stsp else if (result) {
439 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
440 fe621944 2020-11-10 stsp if (free_err && err == NULL)
441 fe621944 2020-11-10 stsp err = free_err;
442 fe621944 2020-11-10 stsp }
443 b72f483a 2019-02-05 stsp return err;
444 7f1f93af 2019-08-06 stsp }
445 7f1f93af 2019-08-06 stsp
446 7f1f93af 2019-08-06 stsp const struct got_error *
447 a0f32f33 2022-06-13 thomas got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
448 6d054bb9 2022-09-23 thomas const char *label1, FILE *f2, int f2_exists, struct stat *sb2,
449 25ec7006 2022-07-01 thomas const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
450 be97ab03 2023-01-19 thomas int ignore_whitespace, int force_text_diff,
451 53d03f97 2023-01-10 thomas struct got_diffstat_cb_arg *ds, FILE *outfile)
452 7f1f93af 2019-08-06 stsp {
453 dd2e2f52 2022-07-01 thomas return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
454 6d054bb9 2022-09-23 thomas sb2, label2, diff_algo, diff_context, ignore_whitespace,
455 be97ab03 2023-01-19 thomas force_text_diff, ds, outfile);
456 474b4f94 2017-11-30 stsp }
457 474b4f94 2017-11-30 stsp
458 474b4f94 2017-11-30 stsp static const struct got_error *
459 dd2e2f52 2022-07-01 thomas diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
460 19a6a6b5 2022-07-01 thomas const char *label, mode_t mode, struct got_repository *repo,
461 a0f32f33 2022-06-13 thomas got_diff_blob_cb cb, void *cb_arg)
462 474b4f94 2017-11-30 stsp {
463 4e22badc 2017-11-30 stsp const struct got_error *err;
464 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
465 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
466 4e22badc 2017-11-30 stsp
467 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
468 4e22badc 2017-11-30 stsp if (err)
469 4e22badc 2017-11-30 stsp return err;
470 4e22badc 2017-11-30 stsp
471 dd2e2f52 2022-07-01 thomas err = got_object_blob_open(&blob, repo, obj, 8192, fd2);
472 2acfca77 2018-04-01 stsp if (err)
473 2acfca77 2018-04-01 stsp goto done;
474 dd2e2f52 2022-07-01 thomas err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
475 a0f32f33 2022-06-13 thomas NULL, label, 0, mode, repo);
476 2acfca77 2018-04-01 stsp done:
477 2acfca77 2018-04-01 stsp got_object_close(obj);
478 2acfca77 2018-04-01 stsp if (blob)
479 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
480 2acfca77 2018-04-01 stsp return err;
481 474b4f94 2017-11-30 stsp }
482 474b4f94 2017-11-30 stsp
483 474b4f94 2017-11-30 stsp static const struct got_error *
484 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
485 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
486 19a6a6b5 2022-07-01 thomas const char *label1, const char *label2,
487 a0f32f33 2022-06-13 thomas mode_t mode1, mode_t mode2, struct got_repository *repo,
488 a0f32f33 2022-06-13 thomas got_diff_blob_cb cb, void *cb_arg)
489 474b4f94 2017-11-30 stsp {
490 6a213ccb 2017-11-30 stsp const struct got_error *err;
491 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
492 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
493 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
494 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
495 6a213ccb 2017-11-30 stsp
496 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
497 6a213ccb 2017-11-30 stsp if (err)
498 730a8aa0 2018-04-24 stsp return err;
499 f4ae6ddb 2022-07-01 thomas
500 15a94983 2018-12-23 stsp if (obj1->type != GOT_OBJ_TYPE_BLOB) {
501 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
502 6a213ccb 2017-11-30 stsp goto done;
503 6a213ccb 2017-11-30 stsp }
504 6a213ccb 2017-11-30 stsp
505 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
506 730a8aa0 2018-04-24 stsp if (err)
507 6a213ccb 2017-11-30 stsp goto done;
508 15a94983 2018-12-23 stsp if (obj2->type != GOT_OBJ_TYPE_BLOB) {
509 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
510 6a213ccb 2017-11-30 stsp goto done;
511 6a213ccb 2017-11-30 stsp }
512 6a213ccb 2017-11-30 stsp
513 f4ae6ddb 2022-07-01 thomas err = got_object_blob_open(&blob1, repo, obj1, 8192, fd1);
514 2acfca77 2018-04-01 stsp if (err)
515 6a213ccb 2017-11-30 stsp goto done;
516 6a213ccb 2017-11-30 stsp
517 f4ae6ddb 2022-07-01 thomas err = got_object_blob_open(&blob2, repo, obj2, 8192, fd2);
518 2acfca77 2018-04-01 stsp if (err)
519 6a213ccb 2017-11-30 stsp goto done;
520 6a213ccb 2017-11-30 stsp
521 a0f32f33 2022-06-13 thomas err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
522 a0f32f33 2022-06-13 thomas mode1, mode2, repo);
523 6a213ccb 2017-11-30 stsp done:
524 a3e2cbea 2017-12-01 stsp if (obj1)
525 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
526 a3e2cbea 2017-12-01 stsp if (obj2)
527 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
528 a3e2cbea 2017-12-01 stsp if (blob1)
529 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
530 a3e2cbea 2017-12-01 stsp if (blob2)
531 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
532 6a213ccb 2017-11-30 stsp return err;
533 474b4f94 2017-11-30 stsp }
534 474b4f94 2017-11-30 stsp
535 474b4f94 2017-11-30 stsp static const struct got_error *
536 dd2e2f52 2022-07-01 thomas diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
537 dd2e2f52 2022-07-01 thomas FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
538 19a6a6b5 2022-07-01 thomas got_diff_blob_cb cb, void *cb_arg)
539 474b4f94 2017-11-30 stsp {
540 365fb436 2017-11-30 stsp const struct got_error *err;
541 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
542 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
543 365fb436 2017-11-30 stsp
544 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
545 365fb436 2017-11-30 stsp if (err)
546 365fb436 2017-11-30 stsp return err;
547 365fb436 2017-11-30 stsp
548 dd2e2f52 2022-07-01 thomas err = got_object_blob_open(&blob, repo, obj, 8192, fd1);
549 2acfca77 2018-04-01 stsp if (err)
550 2acfca77 2018-04-01 stsp goto done;
551 dd2e2f52 2022-07-01 thomas err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
552 a0f32f33 2022-06-13 thomas mode, 0, repo);
553 2acfca77 2018-04-01 stsp done:
554 2acfca77 2018-04-01 stsp got_object_close(obj);
555 2acfca77 2018-04-01 stsp if (blob)
556 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
557 2acfca77 2018-04-01 stsp return err;
558 474b4f94 2017-11-30 stsp }
559 474b4f94 2017-11-30 stsp
560 474b4f94 2017-11-30 stsp static const struct got_error *
561 dd2e2f52 2022-07-01 thomas diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
562 dd2e2f52 2022-07-01 thomas const char *label, struct got_repository *repo, got_diff_blob_cb cb,
563 dd2e2f52 2022-07-01 thomas void *cb_arg, int diff_content)
564 474b4f94 2017-11-30 stsp {
565 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
566 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
567 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
568 9c70d4c3 2017-11-30 stsp
569 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
570 9c70d4c3 2017-11-30 stsp if (err)
571 9c70d4c3 2017-11-30 stsp goto done;
572 9c70d4c3 2017-11-30 stsp
573 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
574 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
575 9c70d4c3 2017-11-30 stsp goto done;
576 9c70d4c3 2017-11-30 stsp }
577 9c70d4c3 2017-11-30 stsp
578 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
579 9c70d4c3 2017-11-30 stsp if (err)
580 9c70d4c3 2017-11-30 stsp goto done;
581 9c70d4c3 2017-11-30 stsp
582 dd2e2f52 2022-07-01 thomas err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
583 a0f32f33 2022-06-13 thomas repo, cb, cb_arg, diff_content);
584 9c70d4c3 2017-11-30 stsp done:
585 9c70d4c3 2017-11-30 stsp if (tree)
586 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
587 9c70d4c3 2017-11-30 stsp if (treeobj)
588 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
589 9c70d4c3 2017-11-30 stsp return err;
590 474b4f94 2017-11-30 stsp }
591 474b4f94 2017-11-30 stsp
592 474b4f94 2017-11-30 stsp static const struct got_error *
593 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
594 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
595 19a6a6b5 2022-07-01 thomas const char *label1, const char *label2,
596 a0f32f33 2022-06-13 thomas struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
597 a0f32f33 2022-06-13 thomas int diff_content)
598 474b4f94 2017-11-30 stsp {
599 f6861a81 2018-09-13 stsp const struct got_error *err;
600 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
601 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
602 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
603 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
604 789689b5 2017-11-30 stsp
605 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
606 789689b5 2017-11-30 stsp if (err)
607 789689b5 2017-11-30 stsp goto done;
608 789689b5 2017-11-30 stsp
609 15a94983 2018-12-23 stsp if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
610 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
611 789689b5 2017-11-30 stsp goto done;
612 789689b5 2017-11-30 stsp }
613 789689b5 2017-11-30 stsp
614 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
615 789689b5 2017-11-30 stsp if (err)
616 789689b5 2017-11-30 stsp goto done;
617 789689b5 2017-11-30 stsp
618 15a94983 2018-12-23 stsp if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
619 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
620 789689b5 2017-11-30 stsp goto done;
621 789689b5 2017-11-30 stsp }
622 789689b5 2017-11-30 stsp
623 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
624 789689b5 2017-11-30 stsp if (err)
625 789689b5 2017-11-30 stsp goto done;
626 789689b5 2017-11-30 stsp
627 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
628 789689b5 2017-11-30 stsp if (err)
629 789689b5 2017-11-30 stsp goto done;
630 789689b5 2017-11-30 stsp
631 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
632 19a6a6b5 2022-07-01 thomas label1, label2, repo, cb, cb_arg, diff_content);
633 789689b5 2017-11-30 stsp
634 789689b5 2017-11-30 stsp done:
635 789689b5 2017-11-30 stsp if (tree1)
636 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
637 789689b5 2017-11-30 stsp if (tree2)
638 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
639 789689b5 2017-11-30 stsp if (treeobj1)
640 789689b5 2017-11-30 stsp got_object_close(treeobj1);
641 789689b5 2017-11-30 stsp if (treeobj2)
642 789689b5 2017-11-30 stsp got_object_close(treeobj2);
643 789689b5 2017-11-30 stsp return err;
644 474b4f94 2017-11-30 stsp }
645 474b4f94 2017-11-30 stsp
646 474b4f94 2017-11-30 stsp static const struct got_error *
647 dd2e2f52 2022-07-01 thomas diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
648 dd2e2f52 2022-07-01 thomas FILE *f2, const char *label, struct got_repository *repo,
649 19a6a6b5 2022-07-01 thomas got_diff_blob_cb cb, void *cb_arg, int diff_content)
650 474b4f94 2017-11-30 stsp {
651 f6861a81 2018-09-13 stsp const struct got_error *err;
652 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
653 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
654 2c56f2ce 2017-11-30 stsp
655 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
656 2c56f2ce 2017-11-30 stsp if (err)
657 2c56f2ce 2017-11-30 stsp goto done;
658 2c56f2ce 2017-11-30 stsp
659 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
660 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
661 2c56f2ce 2017-11-30 stsp goto done;
662 2c56f2ce 2017-11-30 stsp }
663 2c56f2ce 2017-11-30 stsp
664 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
665 2c56f2ce 2017-11-30 stsp if (err)
666 2c56f2ce 2017-11-30 stsp goto done;
667 2c56f2ce 2017-11-30 stsp
668 dd2e2f52 2022-07-01 thomas err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
669 a0f32f33 2022-06-13 thomas repo, cb, cb_arg, diff_content);
670 2c56f2ce 2017-11-30 stsp done:
671 2c56f2ce 2017-11-30 stsp if (tree)
672 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
673 2c56f2ce 2017-11-30 stsp if (treeobj)
674 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
675 2c56f2ce 2017-11-30 stsp return err;
676 474b4f94 2017-11-30 stsp }
677 474b4f94 2017-11-30 stsp
678 474b4f94 2017-11-30 stsp static const struct got_error *
679 870ddae5 2023-03-10 thomas diff_kind_mismatch(struct got_tree_entry *te1, struct got_tree_entry *te2,
680 870ddae5 2023-03-10 thomas FILE *f1, FILE *f2, int fd1, int fd2,
681 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
682 870ddae5 2023-03-10 thomas got_diff_blob_cb cb, void *cb_arg, int diff_content)
683 474b4f94 2017-11-30 stsp {
684 870ddae5 2023-03-10 thomas const struct got_error *err = NULL;
685 870ddae5 2023-03-10 thomas
686 870ddae5 2023-03-10 thomas /*
687 870ddae5 2023-03-10 thomas * Handle files changing into directories and vice-versa.
688 870ddae5 2023-03-10 thomas * Disregard edge cases with FIFOs, device nodes, etc for now.
689 870ddae5 2023-03-10 thomas */
690 870ddae5 2023-03-10 thomas if (!S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
691 870ddae5 2023-03-10 thomas if (S_ISREG(te1->mode)) {
692 870ddae5 2023-03-10 thomas if (diff_content) {
693 870ddae5 2023-03-10 thomas err = diff_deleted_blob(&te1->id, f1, fd1,
694 870ddae5 2023-03-10 thomas f2, label1, te1->mode, repo, cb, cb_arg);
695 870ddae5 2023-03-10 thomas } else {
696 870ddae5 2023-03-10 thomas err = cb(cb_arg, NULL, NULL, NULL, NULL,
697 870ddae5 2023-03-10 thomas &te1->id, NULL, label1, NULL,
698 870ddae5 2023-03-10 thomas te1->mode, 0, repo);
699 870ddae5 2023-03-10 thomas }
700 870ddae5 2023-03-10 thomas if (err)
701 870ddae5 2023-03-10 thomas return err;
702 870ddae5 2023-03-10 thomas }
703 870ddae5 2023-03-10 thomas return diff_added_tree(&te2->id, f1, f2, fd2, label2,
704 870ddae5 2023-03-10 thomas repo, cb, cb_arg, diff_content);
705 870ddae5 2023-03-10 thomas } else if (S_ISDIR(te1->mode) && !S_ISDIR(te2->mode)) {
706 870ddae5 2023-03-10 thomas err = diff_deleted_tree(&te1->id, f1, fd1, f2,
707 870ddae5 2023-03-10 thomas label1, repo, cb, cb_arg, diff_content);
708 870ddae5 2023-03-10 thomas if (err)
709 870ddae5 2023-03-10 thomas return err;
710 870ddae5 2023-03-10 thomas if (S_ISREG(te2->mode)) {
711 870ddae5 2023-03-10 thomas if (diff_content) {
712 870ddae5 2023-03-10 thomas err = diff_added_blob(&te2->id, f1, f2, fd2,
713 870ddae5 2023-03-10 thomas label2, te2->mode, repo, cb, cb_arg);
714 870ddae5 2023-03-10 thomas } else {
715 870ddae5 2023-03-10 thomas err = cb(cb_arg, NULL, NULL, NULL, NULL, NULL,
716 870ddae5 2023-03-10 thomas &te2->id, NULL, label2, 0, te2->mode, repo);
717 870ddae5 2023-03-10 thomas }
718 870ddae5 2023-03-10 thomas if (err)
719 870ddae5 2023-03-10 thomas return err;
720 870ddae5 2023-03-10 thomas }
721 870ddae5 2023-03-10 thomas }
722 870ddae5 2023-03-10 thomas
723 474b4f94 2017-11-30 stsp return NULL;
724 474b4f94 2017-11-30 stsp }
725 474b4f94 2017-11-30 stsp
726 474b4f94 2017-11-30 stsp static const struct got_error *
727 a0f32f33 2022-06-13 thomas diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
728 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
729 19a6a6b5 2022-07-01 thomas const char *label1, const char *label2,
730 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
731 31b4484f 2019-07-27 stsp int diff_content)
732 474b4f94 2017-11-30 stsp {
733 f6861a81 2018-09-13 stsp const struct got_error *err = NULL;
734 19ae6da1 2018-11-05 stsp int id_match;
735 63c5ca5d 2019-08-24 stsp
736 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te1))
737 63c5ca5d 2019-08-24 stsp return NULL;
738 474b4f94 2017-11-30 stsp
739 474b4f94 2017-11-30 stsp if (te2 == NULL) {
740 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
741 dd2e2f52 2022-07-01 thomas err = diff_deleted_tree(&te1->id, f1, fd1, f2,
742 dd2e2f52 2022-07-01 thomas label1, repo, cb, cb_arg, diff_content);
743 31b4484f 2019-07-27 stsp else {
744 31b4484f 2019-07-27 stsp if (diff_content)
745 19a6a6b5 2022-07-01 thomas err = diff_deleted_blob(&te1->id, f1, fd1,
746 dd2e2f52 2022-07-01 thomas f2, label1, te1->mode, repo, cb, cb_arg);
747 31b4484f 2019-07-27 stsp else
748 a0f32f33 2022-06-13 thomas err = cb(cb_arg, NULL, NULL, NULL, NULL,
749 a0f32f33 2022-06-13 thomas &te1->id, NULL, label1, NULL,
750 a0f32f33 2022-06-13 thomas te1->mode, 0, repo);
751 31b4484f 2019-07-27 stsp }
752 f6861a81 2018-09-13 stsp return err;
753 63c5ca5d 2019-08-24 stsp } else if (got_object_tree_entry_is_submodule(te2))
754 63c5ca5d 2019-08-24 stsp return NULL;
755 474b4f94 2017-11-30 stsp
756 56e0773d 2019-11-28 stsp id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
757 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
758 19ae6da1 2018-11-05 stsp if (!id_match)
759 a0f32f33 2022-06-13 thomas return diff_modified_tree(&te1->id, &te2->id, f1, f2,
760 19a6a6b5 2022-07-01 thomas fd1, fd2, label1, label2, repo, cb, cb_arg,
761 19a6a6b5 2022-07-01 thomas diff_content);
762 40dde666 2020-07-23 stsp } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
763 40dde666 2020-07-23 stsp (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
764 46f68b20 2019-10-19 stsp if (!id_match ||
765 40dde666 2020-07-23 stsp ((te1->mode & (S_IFLNK | S_IXUSR))) !=
766 40dde666 2020-07-23 stsp (te2->mode & (S_IFLNK | S_IXUSR))) {
767 31b4484f 2019-07-27 stsp if (diff_content)
768 56e0773d 2019-11-28 stsp return diff_modified_blob(&te1->id, &te2->id,
769 19a6a6b5 2022-07-01 thomas f1, f2, fd1, fd2, label1, label2,
770 19a6a6b5 2022-07-01 thomas te1->mode, te2->mode, repo, cb, cb_arg);
771 31b4484f 2019-07-27 stsp else
772 a0f32f33 2022-06-13 thomas return cb(cb_arg, NULL, NULL, NULL, NULL,
773 a0f32f33 2022-06-13 thomas &te1->id, &te2->id, label1, label2,
774 a0f32f33 2022-06-13 thomas te1->mode, te2->mode, repo);
775 31b4484f 2019-07-27 stsp }
776 413ea19d 2017-11-30 stsp }
777 474b4f94 2017-11-30 stsp
778 19ae6da1 2018-11-05 stsp if (id_match)
779 f6861a81 2018-09-13 stsp return NULL;
780 f6861a81 2018-09-13 stsp
781 870ddae5 2023-03-10 thomas return diff_kind_mismatch(te1, te2, f1, f2, fd1, fd2,
782 870ddae5 2023-03-10 thomas label1, label2, repo, cb, cb_arg, diff_content);
783 474b4f94 2017-11-30 stsp }
784 474b4f94 2017-11-30 stsp
785 474b4f94 2017-11-30 stsp static const struct got_error *
786 56e0773d 2019-11-28 stsp diff_entry_new_old(struct got_tree_entry *te2,
787 dd2e2f52 2022-07-01 thomas struct got_tree_entry *te1, FILE *f1, FILE *f2, int fd2, const char *label2,
788 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
789 31b4484f 2019-07-27 stsp int diff_content)
790 474b4f94 2017-11-30 stsp {
791 f6861a81 2018-09-13 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
792 63c5ca5d 2019-08-24 stsp return NULL;
793 63c5ca5d 2019-08-24 stsp
794 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te2))
795 f6861a81 2018-09-13 stsp return NULL;
796 474b4f94 2017-11-30 stsp
797 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
798 dd2e2f52 2022-07-01 thomas return diff_added_tree(&te2->id, f1, f2, fd2, label2,
799 a0f32f33 2022-06-13 thomas repo, cb, cb_arg, diff_content);
800 f6861a81 2018-09-13 stsp
801 31b4484f 2019-07-27 stsp if (diff_content)
802 dd2e2f52 2022-07-01 thomas return diff_added_blob(&te2->id, f1, f2, fd2,
803 19a6a6b5 2022-07-01 thomas label2, te2->mode, repo, cb, cb_arg);
804 31b4484f 2019-07-27 stsp
805 a0f32f33 2022-06-13 thomas return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
806 a0f32f33 2022-06-13 thomas NULL, label2, 0, te2->mode, repo);
807 0208f208 2020-05-05 stsp }
808 0208f208 2020-05-05 stsp
809 0208f208 2020-05-05 stsp const struct got_error *
810 772fcad5 2023-01-07 thomas got_diff_tree_compute_diffstat(void *arg, struct got_blob_object *blob1,
811 772fcad5 2023-01-07 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
812 772fcad5 2023-01-07 thomas struct got_object_id *id1, struct got_object_id *id2,
813 772fcad5 2023-01-07 thomas const char *label1, const char *label2,
814 772fcad5 2023-01-07 thomas mode_t mode1, mode_t mode2, struct got_repository *repo)
815 772fcad5 2023-01-07 thomas {
816 772fcad5 2023-01-07 thomas const struct got_error *err = NULL;
817 772fcad5 2023-01-07 thomas struct got_diffreg_result *result = NULL;
818 772fcad5 2023-01-07 thomas struct got_diffstat_cb_arg *a = arg;
819 772fcad5 2023-01-07 thomas char *path = NULL;
820 53d03f97 2023-01-10 thomas int status = GOT_STATUS_NO_CHANGE;
821 772fcad5 2023-01-07 thomas
822 772fcad5 2023-01-07 thomas path = strdup(label2 ? label2 : label1);
823 772fcad5 2023-01-07 thomas if (path == NULL)
824 e80ef83e 2023-01-10 thomas return got_error_from_errno("strdup");
825 772fcad5 2023-01-07 thomas
826 772fcad5 2023-01-07 thomas if (id1 == NULL)
827 53d03f97 2023-01-10 thomas status = GOT_STATUS_ADD;
828 772fcad5 2023-01-07 thomas else if (id2 == NULL)
829 53d03f97 2023-01-10 thomas status = GOT_STATUS_DELETE;
830 772fcad5 2023-01-07 thomas else {
831 772fcad5 2023-01-07 thomas if (got_object_id_cmp(id1, id2) != 0)
832 53d03f97 2023-01-10 thomas status = GOT_STATUS_MODIFY;
833 772fcad5 2023-01-07 thomas else if (mode1 != mode2)
834 53d03f97 2023-01-10 thomas status = GOT_STATUS_MODE_CHANGE;
835 772fcad5 2023-01-07 thomas }
836 772fcad5 2023-01-07 thomas
837 772fcad5 2023-01-07 thomas if (f1) {
838 772fcad5 2023-01-07 thomas err = got_opentemp_truncate(f1);
839 772fcad5 2023-01-07 thomas if (err)
840 772fcad5 2023-01-07 thomas goto done;
841 772fcad5 2023-01-07 thomas }
842 772fcad5 2023-01-07 thomas if (f2) {
843 772fcad5 2023-01-07 thomas err = got_opentemp_truncate(f2);
844 772fcad5 2023-01-07 thomas if (err)
845 772fcad5 2023-01-07 thomas goto done;
846 772fcad5 2023-01-07 thomas }
847 772fcad5 2023-01-07 thomas
848 772fcad5 2023-01-07 thomas if (blob1) {
849 772fcad5 2023-01-07 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1,
850 772fcad5 2023-01-07 thomas blob1);
851 772fcad5 2023-01-07 thomas if (err)
852 772fcad5 2023-01-07 thomas goto done;
853 772fcad5 2023-01-07 thomas }
854 772fcad5 2023-01-07 thomas if (blob2) {
855 772fcad5 2023-01-07 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2,
856 772fcad5 2023-01-07 thomas blob2);
857 772fcad5 2023-01-07 thomas if (err)
858 772fcad5 2023-01-07 thomas goto done;
859 772fcad5 2023-01-07 thomas }
860 772fcad5 2023-01-07 thomas
861 772fcad5 2023-01-07 thomas err = got_diffreg(&result, f1, f2, a->diff_algo, a->ignore_ws,
862 772fcad5 2023-01-07 thomas a->force_text);
863 772fcad5 2023-01-07 thomas if (err)
864 772fcad5 2023-01-07 thomas goto done;
865 772fcad5 2023-01-07 thomas
866 53d03f97 2023-01-10 thomas err = get_diffstat(a, path, result->result, a->force_text, status);
867 772fcad5 2023-01-07 thomas
868 772fcad5 2023-01-07 thomas done:
869 772fcad5 2023-01-07 thomas if (result) {
870 772fcad5 2023-01-07 thomas const struct got_error *free_err;
871 772fcad5 2023-01-07 thomas
872 772fcad5 2023-01-07 thomas free_err = got_diffreg_result_free(result);
873 772fcad5 2023-01-07 thomas if (free_err && err == NULL)
874 772fcad5 2023-01-07 thomas err = free_err;
875 772fcad5 2023-01-07 thomas }
876 53d03f97 2023-01-10 thomas if (err)
877 772fcad5 2023-01-07 thomas free(path);
878 772fcad5 2023-01-07 thomas return err;
879 772fcad5 2023-01-07 thomas }
880 772fcad5 2023-01-07 thomas
881 772fcad5 2023-01-07 thomas const struct got_error *
882 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
883 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
884 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
885 a0f32f33 2022-06-13 thomas const char *label1, const char *label2,
886 0208f208 2020-05-05 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
887 0208f208 2020-05-05 stsp {
888 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
889 0208f208 2020-05-05 stsp struct got_pathlist_head *paths = arg;
890 0208f208 2020-05-05 stsp struct got_diff_changed_path *change = NULL;
891 0208f208 2020-05-05 stsp char *path = NULL;
892 0208f208 2020-05-05 stsp
893 0208f208 2020-05-05 stsp path = strdup(label2 ? label2 : label1);
894 0208f208 2020-05-05 stsp if (path == NULL)
895 e80ef83e 2023-01-10 thomas return got_error_from_errno("strdup");
896 0208f208 2020-05-05 stsp
897 0208f208 2020-05-05 stsp change = malloc(sizeof(*change));
898 0208f208 2020-05-05 stsp if (change == NULL) {
899 0208f208 2020-05-05 stsp err = got_error_from_errno("malloc");
900 0208f208 2020-05-05 stsp goto done;
901 0208f208 2020-05-05 stsp }
902 0208f208 2020-05-05 stsp
903 0208f208 2020-05-05 stsp change->status = GOT_STATUS_NO_CHANGE;
904 0208f208 2020-05-05 stsp if (id1 == NULL)
905 0208f208 2020-05-05 stsp change->status = GOT_STATUS_ADD;
906 0208f208 2020-05-05 stsp else if (id2 == NULL)
907 0208f208 2020-05-05 stsp change->status = GOT_STATUS_DELETE;
908 0208f208 2020-05-05 stsp else {
909 0208f208 2020-05-05 stsp if (got_object_id_cmp(id1, id2) != 0)
910 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODIFY;
911 0208f208 2020-05-05 stsp else if (mode1 != mode2)
912 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODE_CHANGE;
913 0208f208 2020-05-05 stsp }
914 0208f208 2020-05-05 stsp
915 4c71f93b 2021-12-31 thomas err = got_pathlist_append(paths, path, change);
916 0208f208 2020-05-05 stsp done:
917 0208f208 2020-05-05 stsp if (err) {
918 0208f208 2020-05-05 stsp free(path);
919 0208f208 2020-05-05 stsp free(change);
920 0208f208 2020-05-05 stsp }
921 0208f208 2020-05-05 stsp return err;
922 474b4f94 2017-11-30 stsp }
923 474b4f94 2017-11-30 stsp
924 474b4f94 2017-11-30 stsp const struct got_error *
925 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
926 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
927 19a6a6b5 2022-07-01 thomas const char *label1, const char *label2,
928 a0f32f33 2022-06-13 thomas struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
929 a0f32f33 2022-06-13 thomas int diff_content)
930 474b4f94 2017-11-30 stsp {
931 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
932 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
933 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
934 f6861a81 2018-09-13 stsp char *l1 = NULL, *l2 = NULL;
935 56e0773d 2019-11-28 stsp int tidx1 = 0, tidx2 = 0;
936 474b4f94 2017-11-30 stsp
937 883f0469 2018-06-23 stsp if (tree1) {
938 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, 0);
939 60f50a58 2018-09-15 stsp if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
940 f6861a81 2018-09-13 stsp te1->name) == -1)
941 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
942 883f0469 2018-06-23 stsp }
943 883f0469 2018-06-23 stsp if (tree2) {
944 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, 0);
945 60f50a58 2018-09-15 stsp if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
946 92a56907 2023-01-14 thomas te2->name) == -1) {
947 92a56907 2023-01-14 thomas err = got_error_from_errno("asprintf");
948 92a56907 2023-01-14 thomas goto done;
949 92a56907 2023-01-14 thomas }
950 883f0469 2018-06-23 stsp }
951 474b4f94 2017-11-30 stsp
952 474b4f94 2017-11-30 stsp do {
953 474b4f94 2017-11-30 stsp if (te1) {
954 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
955 92a56907 2023-01-14 thomas
956 f6861a81 2018-09-13 stsp if (tree2)
957 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree2,
958 1de5e065 2019-06-01 stsp te1->name);
959 f6861a81 2018-09-13 stsp if (te) {
960 f6861a81 2018-09-13 stsp free(l2);
961 f6861a81 2018-09-13 stsp l2 = NULL;
962 f6861a81 2018-09-13 stsp if (te && asprintf(&l2, "%s%s%s", label2,
963 92a56907 2023-01-14 thomas label2[0] ? "/" : "", te->name) == -1) {
964 92a56907 2023-01-14 thomas err = got_error_from_errno("asprintf");
965 92a56907 2023-01-14 thomas goto done;
966 92a56907 2023-01-14 thomas }
967 f6861a81 2018-09-13 stsp }
968 92a56907 2023-01-14 thomas
969 19a6a6b5 2022-07-01 thomas err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
970 19a6a6b5 2022-07-01 thomas l1, l2, repo, cb, cb_arg, diff_content);
971 474b4f94 2017-11-30 stsp if (err)
972 474b4f94 2017-11-30 stsp break;
973 474b4f94 2017-11-30 stsp }
974 474b4f94 2017-11-30 stsp
975 474b4f94 2017-11-30 stsp if (te2) {
976 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
977 92a56907 2023-01-14 thomas
978 f6861a81 2018-09-13 stsp if (tree1)
979 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree1,
980 1de5e065 2019-06-01 stsp te2->name);
981 92a56907 2023-01-14 thomas
982 d6ce02f1 2018-11-17 stsp free(l2);
983 92a56907 2023-01-14 thomas l2 = NULL;
984 d6ce02f1 2018-11-17 stsp if (te) {
985 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
986 92a56907 2023-01-14 thomas label2[0] ? "/" : "", te->name) == -1) {
987 92a56907 2023-01-14 thomas err = got_error_from_errno("asprintf");
988 92a56907 2023-01-14 thomas goto done;
989 92a56907 2023-01-14 thomas }
990 d6ce02f1 2018-11-17 stsp } else {
991 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
992 92a56907 2023-01-14 thomas label2[0] ? "/" : "", te2->name) == -1) {
993 92a56907 2023-01-14 thomas err = got_error_from_errno("asprintf");
994 92a56907 2023-01-14 thomas goto done;
995 92a56907 2023-01-14 thomas }
996 d6ce02f1 2018-11-17 stsp }
997 92a56907 2023-01-14 thomas
998 dd2e2f52 2022-07-01 thomas err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
999 dd2e2f52 2022-07-01 thomas repo, cb, cb_arg, diff_content);
1000 474b4f94 2017-11-30 stsp if (err)
1001 474b4f94 2017-11-30 stsp break;
1002 474b4f94 2017-11-30 stsp }
1003 474b4f94 2017-11-30 stsp
1004 f6861a81 2018-09-13 stsp free(l1);
1005 f6861a81 2018-09-13 stsp l1 = NULL;
1006 f6861a81 2018-09-13 stsp if (te1) {
1007 56e0773d 2019-11-28 stsp tidx1++;
1008 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, tidx1);
1009 f6861a81 2018-09-13 stsp if (te1 &&
1010 f6861a81 2018-09-13 stsp asprintf(&l1, "%s%s%s", label1,
1011 92a56907 2023-01-14 thomas label1[0] ? "/" : "", te1->name) == -1) {
1012 92a56907 2023-01-14 thomas err = got_error_from_errno("asprintf");
1013 92a56907 2023-01-14 thomas goto done;
1014 92a56907 2023-01-14 thomas }
1015 f6861a81 2018-09-13 stsp }
1016 92a56907 2023-01-14 thomas
1017 f6861a81 2018-09-13 stsp free(l2);
1018 f6861a81 2018-09-13 stsp l2 = NULL;
1019 f6861a81 2018-09-13 stsp if (te2) {
1020 56e0773d 2019-11-28 stsp tidx2++;
1021 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, tidx2);
1022 f6861a81 2018-09-13 stsp if (te2 &&
1023 f6861a81 2018-09-13 stsp asprintf(&l2, "%s%s%s", label2,
1024 92a56907 2023-01-14 thomas label2[0] ? "/" : "", te2->name) == -1) {
1025 92a56907 2023-01-14 thomas err = got_error_from_errno("asprintf");
1026 92a56907 2023-01-14 thomas goto done;
1027 92a56907 2023-01-14 thomas }
1028 f6861a81 2018-09-13 stsp }
1029 474b4f94 2017-11-30 stsp } while (te1 || te2);
1030 11528a82 2018-05-19 stsp
1031 92a56907 2023-01-14 thomas done:
1032 92a56907 2023-01-14 thomas free(l1);
1033 92a56907 2023-01-14 thomas free(l2);
1034 11528a82 2018-05-19 stsp return err;
1035 11528a82 2018-05-19 stsp }
1036 11528a82 2018-05-19 stsp
1037 11528a82 2018-05-19 stsp const struct got_error *
1038 82c78e96 2022-08-06 thomas got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
1039 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
1040 19a6a6b5 2022-07-01 thomas struct got_object_id *id1, struct got_object_id *id2,
1041 25ec7006 2022-07-01 thomas const char *label1, const char *label2,
1042 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo, int diff_context,
1043 be97ab03 2023-01-19 thomas int ignore_whitespace, int force_text_diff, struct got_diffstat_cb_arg *ds,
1044 be97ab03 2023-01-19 thomas struct got_repository *repo, FILE *outfile)
1045 11528a82 2018-05-19 stsp {
1046 11528a82 2018-05-19 stsp const struct got_error *err;
1047 11528a82 2018-05-19 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1048 b74c7625 2018-05-20 stsp
1049 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
1050 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
1051 f4ae6ddb 2022-07-01 thomas
1052 15a94983 2018-12-23 stsp if (id1) {
1053 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
1054 cd0acaa7 2018-05-20 stsp if (err)
1055 cd0acaa7 2018-05-20 stsp goto done;
1056 cd0acaa7 2018-05-20 stsp }
1057 15a94983 2018-12-23 stsp if (id2) {
1058 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob2, repo, id2, 8192, fd2);
1059 cd0acaa7 2018-05-20 stsp if (err)
1060 cd0acaa7 2018-05-20 stsp goto done;
1061 cd0acaa7 2018-05-20 stsp }
1062 82c78e96 2022-08-06 thomas err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
1063 82c78e96 2022-08-06 thomas diff_algo, diff_context, ignore_whitespace, force_text_diff,
1064 be97ab03 2023-01-19 thomas ds, outfile);
1065 cc8021af 2021-10-12 thomas done:
1066 cc8021af 2021-10-12 thomas if (blob1)
1067 cc8021af 2021-10-12 thomas got_object_blob_close(blob1);
1068 cc8021af 2021-10-12 thomas if (blob2)
1069 cc8021af 2021-10-12 thomas got_object_blob_close(blob2);
1070 cc8021af 2021-10-12 thomas return err;
1071 cc8021af 2021-10-12 thomas }
1072 cc8021af 2021-10-12 thomas
1073 cc8021af 2021-10-12 thomas static const struct got_error *
1074 cc8021af 2021-10-12 thomas diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
1075 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
1076 a0f32f33 2022-06-13 thomas struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
1077 cc8021af 2021-10-12 thomas {
1078 cc8021af 2021-10-12 thomas const struct got_error *err = NULL;
1079 cc8021af 2021-10-12 thomas struct got_pathlist_entry *pe;
1080 cc8021af 2021-10-12 thomas struct got_object_id *id1 = NULL, *id2 = NULL;
1081 cc8021af 2021-10-12 thomas struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
1082 cc8021af 2021-10-12 thomas struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1083 cc8021af 2021-10-12 thomas
1084 cc8021af 2021-10-12 thomas TAILQ_FOREACH(pe, paths, entry) {
1085 cc8021af 2021-10-12 thomas int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
1086 cc8021af 2021-10-12 thomas mode_t mode1 = 0, mode2 = 0;
1087 cc8021af 2021-10-12 thomas
1088 cc8021af 2021-10-12 thomas free(id1);
1089 cc8021af 2021-10-12 thomas id1 = NULL;
1090 cc8021af 2021-10-12 thomas free(id2);
1091 cc8021af 2021-10-12 thomas id2 = NULL;
1092 cc8021af 2021-10-12 thomas if (subtree1) {
1093 cc8021af 2021-10-12 thomas got_object_tree_close(subtree1);
1094 cc8021af 2021-10-12 thomas subtree1 = NULL;
1095 cc8021af 2021-10-12 thomas }
1096 cc8021af 2021-10-12 thomas if (subtree2) {
1097 cc8021af 2021-10-12 thomas got_object_tree_close(subtree2);
1098 cc8021af 2021-10-12 thomas subtree2 = NULL;
1099 cc8021af 2021-10-12 thomas }
1100 cc8021af 2021-10-12 thomas if (blob1) {
1101 cc8021af 2021-10-12 thomas got_object_blob_close(blob1);
1102 cc8021af 2021-10-12 thomas blob1 = NULL;
1103 cc8021af 2021-10-12 thomas }
1104 cc8021af 2021-10-12 thomas if (blob2) {
1105 cc8021af 2021-10-12 thomas got_object_blob_close(blob2);
1106 cc8021af 2021-10-12 thomas blob2 = NULL;
1107 cc8021af 2021-10-12 thomas }
1108 64592dff 2023-06-08 thomas if (tree1) {
1109 64592dff 2023-06-08 thomas err = got_object_tree_find_path(&id1, &mode1, repo,
1110 64592dff 2023-06-08 thomas tree1, pe->path);
1111 64592dff 2023-06-08 thomas if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1112 64592dff 2023-06-08 thomas goto done;
1113 64592dff 2023-06-08 thomas }
1114 64592dff 2023-06-08 thomas if (tree2) {
1115 64592dff 2023-06-08 thomas err = got_object_tree_find_path(&id2, &mode2, repo,
1116 64592dff 2023-06-08 thomas tree2, pe->path);
1117 64592dff 2023-06-08 thomas if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1118 64592dff 2023-06-08 thomas goto done;
1119 64592dff 2023-06-08 thomas }
1120 cc8021af 2021-10-12 thomas if (id1 == NULL && id2 == NULL) {
1121 cc8021af 2021-10-12 thomas err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
1122 cc8021af 2021-10-12 thomas goto done;
1123 cc8021af 2021-10-12 thomas }
1124 cc8021af 2021-10-12 thomas if (id1) {
1125 cc8021af 2021-10-12 thomas err = got_object_get_type(&type1, repo, id1);
1126 cc8021af 2021-10-12 thomas if (err)
1127 cc8021af 2021-10-12 thomas goto done;
1128 cc8021af 2021-10-12 thomas }
1129 cc8021af 2021-10-12 thomas if (id2) {
1130 cc8021af 2021-10-12 thomas err = got_object_get_type(&type2, repo, id2);
1131 cc8021af 2021-10-12 thomas if (err)
1132 cc8021af 2021-10-12 thomas goto done;
1133 cc8021af 2021-10-12 thomas }
1134 cc8021af 2021-10-12 thomas if (type1 == GOT_OBJ_TYPE_ANY &&
1135 cc8021af 2021-10-12 thomas type2 == GOT_OBJ_TYPE_ANY) {
1136 cc8021af 2021-10-12 thomas err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
1137 cc8021af 2021-10-12 thomas goto done;
1138 cc8021af 2021-10-12 thomas } else if (type1 != GOT_OBJ_TYPE_ANY &&
1139 cc8021af 2021-10-12 thomas type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
1140 cc8021af 2021-10-12 thomas err = got_error(GOT_ERR_OBJ_TYPE);
1141 cc8021af 2021-10-12 thomas goto done;
1142 cc8021af 2021-10-12 thomas }
1143 cc8021af 2021-10-12 thomas
1144 cc8021af 2021-10-12 thomas if (type1 == GOT_OBJ_TYPE_BLOB ||
1145 cc8021af 2021-10-12 thomas type2 == GOT_OBJ_TYPE_BLOB) {
1146 cc8021af 2021-10-12 thomas if (id1) {
1147 cc8021af 2021-10-12 thomas err = got_object_open_as_blob(&blob1, repo,
1148 f4ae6ddb 2022-07-01 thomas id1, 8192, fd1);
1149 cc8021af 2021-10-12 thomas if (err)
1150 cc8021af 2021-10-12 thomas goto done;
1151 cc8021af 2021-10-12 thomas }
1152 cc8021af 2021-10-12 thomas if (id2) {
1153 cc8021af 2021-10-12 thomas err = got_object_open_as_blob(&blob2, repo,
1154 f4ae6ddb 2022-07-01 thomas id2, 8192, fd2);
1155 cc8021af 2021-10-12 thomas if (err)
1156 cc8021af 2021-10-12 thomas goto done;
1157 cc8021af 2021-10-12 thomas }
1158 a0f32f33 2022-06-13 thomas err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
1159 cc8021af 2021-10-12 thomas id1 ? pe->path : "/dev/null",
1160 cc8021af 2021-10-12 thomas id2 ? pe->path : "/dev/null",
1161 cc8021af 2021-10-12 thomas mode1, mode2, repo);
1162 cc8021af 2021-10-12 thomas if (err)
1163 cc8021af 2021-10-12 thomas goto done;
1164 cc8021af 2021-10-12 thomas } else if (type1 == GOT_OBJ_TYPE_TREE ||
1165 cc8021af 2021-10-12 thomas type2 == GOT_OBJ_TYPE_TREE) {
1166 cc8021af 2021-10-12 thomas if (id1) {
1167 cc8021af 2021-10-12 thomas err = got_object_open_as_tree(&subtree1, repo,
1168 cc8021af 2021-10-12 thomas id1);
1169 cc8021af 2021-10-12 thomas if (err)
1170 cc8021af 2021-10-12 thomas goto done;
1171 cc8021af 2021-10-12 thomas }
1172 cc8021af 2021-10-12 thomas if (id2) {
1173 cc8021af 2021-10-12 thomas err = got_object_open_as_tree(&subtree2, repo,
1174 cc8021af 2021-10-12 thomas id2);
1175 cc8021af 2021-10-12 thomas if (err)
1176 cc8021af 2021-10-12 thomas goto done;
1177 cc8021af 2021-10-12 thomas }
1178 a0f32f33 2022-06-13 thomas err = got_diff_tree(subtree1, subtree2, f1, f2,
1179 19a6a6b5 2022-07-01 thomas fd1, fd2,
1180 cc8021af 2021-10-12 thomas id1 ? pe->path : "/dev/null",
1181 cc8021af 2021-10-12 thomas id2 ? pe->path : "/dev/null",
1182 cc8021af 2021-10-12 thomas repo, cb, cb_arg, 1);
1183 cc8021af 2021-10-12 thomas if (err)
1184 cc8021af 2021-10-12 thomas goto done;
1185 cc8021af 2021-10-12 thomas } else {
1186 cc8021af 2021-10-12 thomas err = got_error(GOT_ERR_OBJ_TYPE);
1187 cc8021af 2021-10-12 thomas goto done;
1188 cc8021af 2021-10-12 thomas }
1189 cc8021af 2021-10-12 thomas }
1190 11528a82 2018-05-19 stsp done:
1191 cc8021af 2021-10-12 thomas free(id1);
1192 cc8021af 2021-10-12 thomas free(id2);
1193 cc8021af 2021-10-12 thomas if (subtree1)
1194 cc8021af 2021-10-12 thomas got_object_tree_close(subtree1);
1195 cc8021af 2021-10-12 thomas if (subtree2)
1196 cc8021af 2021-10-12 thomas got_object_tree_close(subtree2);
1197 11528a82 2018-05-19 stsp if (blob1)
1198 11528a82 2018-05-19 stsp got_object_blob_close(blob1);
1199 11528a82 2018-05-19 stsp if (blob2)
1200 11528a82 2018-05-19 stsp got_object_blob_close(blob2);
1201 474b4f94 2017-11-30 stsp return err;
1202 474b4f94 2017-11-30 stsp }
1203 11528a82 2018-05-19 stsp
1204 9b4458b4 2022-06-26 thomas static const struct got_error *
1205 82c78e96 2022-08-06 thomas show_object_id(struct got_diff_line **lines, size_t *nlines,
1206 82c78e96 2022-08-06 thomas const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
1207 9b4458b4 2022-06-26 thomas {
1208 9b4458b4 2022-06-26 thomas const struct got_error *err;
1209 9b4458b4 2022-06-26 thomas int n;
1210 9b4458b4 2022-06-26 thomas off_t outoff = 0;
1211 9b4458b4 2022-06-26 thomas
1212 9b4458b4 2022-06-26 thomas n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
1213 7e1f3a3d 2022-08-16 thomas if (n < 0)
1214 7e1f3a3d 2022-08-16 thomas return got_error_from_errno("fprintf");
1215 7e1f3a3d 2022-08-16 thomas
1216 82c78e96 2022-08-06 thomas if (lines != NULL && *lines != NULL) {
1217 9b4458b4 2022-06-26 thomas if (*nlines == 0) {
1218 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0,
1219 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
1220 9b4458b4 2022-06-26 thomas if (err)
1221 9b4458b4 2022-06-26 thomas return err;
1222 9b4458b4 2022-06-26 thomas } else
1223 82c78e96 2022-08-06 thomas outoff = (*lines)[*nlines - 1].offset;
1224 9b4458b4 2022-06-26 thomas
1225 9b4458b4 2022-06-26 thomas outoff += n;
1226 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
1227 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
1228 9b4458b4 2022-06-26 thomas if (err)
1229 9b4458b4 2022-06-26 thomas return err;
1230 9b4458b4 2022-06-26 thomas }
1231 9b4458b4 2022-06-26 thomas
1232 9b4458b4 2022-06-26 thomas return NULL;
1233 9b4458b4 2022-06-26 thomas }
1234 9b4458b4 2022-06-26 thomas
1235 9b4458b4 2022-06-26 thomas static const struct got_error *
1236 82c78e96 2022-08-06 thomas diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1237 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
1238 19a6a6b5 2022-07-01 thomas struct got_object_id *id1, struct got_object_id *id2,
1239 0c6f49ba 2022-07-01 thomas struct got_pathlist_head *paths, const char *label1, const char *label2,
1240 0c6f49ba 2022-07-01 thomas int diff_context, int ignore_whitespace, int force_text_diff,
1241 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg *dsa, struct got_repository *repo,
1242 be97ab03 2023-01-19 thomas FILE *outfile, enum got_diff_algorithm diff_algo)
1243 11528a82 2018-05-19 stsp {
1244 11528a82 2018-05-19 stsp const struct got_error *err;
1245 11528a82 2018-05-19 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1246 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
1247 82c78e96 2022-08-06 thomas int want_linemeta = (lines != NULL && *lines != NULL);
1248 11528a82 2018-05-19 stsp
1249 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
1250 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
1251 b74c7625 2018-05-20 stsp
1252 15a94983 2018-12-23 stsp if (id1) {
1253 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo, id1);
1254 cd0acaa7 2018-05-20 stsp if (err)
1255 cd0acaa7 2018-05-20 stsp goto done;
1256 cd0acaa7 2018-05-20 stsp }
1257 15a94983 2018-12-23 stsp if (id2) {
1258 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo, id2);
1259 cd0acaa7 2018-05-20 stsp if (err)
1260 cd0acaa7 2018-05-20 stsp goto done;
1261 cd0acaa7 2018-05-20 stsp }
1262 cc8021af 2021-10-12 thomas
1263 25ec7006 2022-07-01 thomas arg.diff_algo = diff_algo;
1264 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1265 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
1266 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
1267 53d03f97 2023-01-10 thomas arg.diffstat = dsa;
1268 aaa13589 2019-06-01 stsp arg.outfile = outfile;
1269 82c78e96 2022-08-06 thomas if (want_linemeta) {
1270 82c78e96 2022-08-06 thomas arg.lines = *lines;
1271 fe621944 2020-11-10 stsp arg.nlines = *nlines;
1272 fe621944 2020-11-10 stsp } else {
1273 82c78e96 2022-08-06 thomas arg.lines = NULL;
1274 fe621944 2020-11-10 stsp arg.nlines = 0;
1275 fe621944 2020-11-10 stsp }
1276 53d03f97 2023-01-10 thomas if (paths == NULL || TAILQ_EMPTY(paths))
1277 53d03f97 2023-01-10 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, label1,
1278 53d03f97 2023-01-10 thomas label2, repo, got_diff_blob_output_unidiff, &arg, 1);
1279 53d03f97 2023-01-10 thomas else
1280 19a6a6b5 2022-07-01 thomas err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
1281 cc8021af 2021-10-12 thomas got_diff_blob_output_unidiff, &arg);
1282 82c78e96 2022-08-06 thomas if (want_linemeta) {
1283 82c78e96 2022-08-06 thomas *lines = arg.lines; /* was likely re-allocated */
1284 fe621944 2020-11-10 stsp *nlines = arg.nlines;
1285 fe621944 2020-11-10 stsp }
1286 11528a82 2018-05-19 stsp done:
1287 11528a82 2018-05-19 stsp if (tree1)
1288 11528a82 2018-05-19 stsp got_object_tree_close(tree1);
1289 11528a82 2018-05-19 stsp if (tree2)
1290 11528a82 2018-05-19 stsp got_object_tree_close(tree2);
1291 11528a82 2018-05-19 stsp return err;
1292 11528a82 2018-05-19 stsp }
1293 11528a82 2018-05-19 stsp
1294 11528a82 2018-05-19 stsp const struct got_error *
1295 82c78e96 2022-08-06 thomas got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1296 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
1297 19a6a6b5 2022-07-01 thomas struct got_object_id *id1, struct got_object_id *id2,
1298 0c6f49ba 2022-07-01 thomas struct got_pathlist_head *paths, const char *label1, const char *label2,
1299 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1300 be97ab03 2023-01-19 thomas int force_text_diff, struct got_diffstat_cb_arg *dsa,
1301 53d03f97 2023-01-10 thomas struct got_repository *repo, FILE *outfile)
1302 9b4458b4 2022-06-26 thomas {
1303 9b4458b4 2022-06-26 thomas const struct got_error *err;
1304 9b4458b4 2022-06-26 thomas char *idstr = NULL;
1305 9b4458b4 2022-06-26 thomas
1306 9b4458b4 2022-06-26 thomas if (id1 == NULL && id2 == NULL)
1307 9b4458b4 2022-06-26 thomas return got_error(GOT_ERR_NO_OBJ);
1308 9b4458b4 2022-06-26 thomas
1309 9b4458b4 2022-06-26 thomas if (id1) {
1310 9b4458b4 2022-06-26 thomas err = got_object_id_str(&idstr, id1);
1311 9b4458b4 2022-06-26 thomas if (err)
1312 9b4458b4 2022-06-26 thomas goto done;
1313 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1314 9b4458b4 2022-06-26 thomas if (err)
1315 9b4458b4 2022-06-26 thomas goto done;
1316 9b4458b4 2022-06-26 thomas free(idstr);
1317 9b4458b4 2022-06-26 thomas idstr = NULL;
1318 9b4458b4 2022-06-26 thomas } else {
1319 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1320 82c78e96 2022-08-06 thomas outfile);
1321 9b4458b4 2022-06-26 thomas if (err)
1322 9b4458b4 2022-06-26 thomas goto done;
1323 9b4458b4 2022-06-26 thomas }
1324 9b4458b4 2022-06-26 thomas
1325 9b4458b4 2022-06-26 thomas if (id2) {
1326 9b4458b4 2022-06-26 thomas err = got_object_id_str(&idstr, id2);
1327 9b4458b4 2022-06-26 thomas if (err)
1328 9b4458b4 2022-06-26 thomas goto done;
1329 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1330 9b4458b4 2022-06-26 thomas if (err)
1331 9b4458b4 2022-06-26 thomas goto done;
1332 9b4458b4 2022-06-26 thomas free(idstr);
1333 9b4458b4 2022-06-26 thomas idstr = NULL;
1334 9b4458b4 2022-06-26 thomas } else {
1335 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1336 82c78e96 2022-08-06 thomas outfile);
1337 9b4458b4 2022-06-26 thomas if (err)
1338 9b4458b4 2022-06-26 thomas goto done;
1339 9b4458b4 2022-06-26 thomas }
1340 9b4458b4 2022-06-26 thomas
1341 82c78e96 2022-08-06 thomas err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1342 82c78e96 2022-08-06 thomas paths, label1, label2, diff_context, ignore_whitespace,
1343 be97ab03 2023-01-19 thomas force_text_diff, dsa, repo, outfile, diff_algo);
1344 9b4458b4 2022-06-26 thomas done:
1345 9b4458b4 2022-06-26 thomas free(idstr);
1346 9b4458b4 2022-06-26 thomas return err;
1347 9b4458b4 2022-06-26 thomas }
1348 9b4458b4 2022-06-26 thomas
1349 9b4458b4 2022-06-26 thomas const struct got_error *
1350 82c78e96 2022-08-06 thomas got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1351 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
1352 19a6a6b5 2022-07-01 thomas struct got_object_id *id1, struct got_object_id *id2,
1353 25ec7006 2022-07-01 thomas struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1354 64453f7e 2020-11-21 stsp int diff_context, int ignore_whitespace, int force_text_diff,
1355 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg *dsa, struct got_repository *repo, FILE *outfile)
1356 11528a82 2018-05-19 stsp {
1357 11528a82 2018-05-19 stsp const struct got_error *err;
1358 11528a82 2018-05-19 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1359 9b4458b4 2022-06-26 thomas char *idstr = NULL;
1360 11528a82 2018-05-19 stsp
1361 15a94983 2018-12-23 stsp if (id2 == NULL)
1362 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
1363 b74c7625 2018-05-20 stsp
1364 15a94983 2018-12-23 stsp if (id1) {
1365 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit1, repo, id1);
1366 cd0acaa7 2018-05-20 stsp if (err)
1367 cd0acaa7 2018-05-20 stsp goto done;
1368 9b4458b4 2022-06-26 thomas err = got_object_id_str(&idstr, id1);
1369 9b4458b4 2022-06-26 thomas if (err)
1370 9b4458b4 2022-06-26 thomas goto done;
1371 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "commit", '-', idstr,
1372 82c78e96 2022-08-06 thomas outfile);
1373 9b4458b4 2022-06-26 thomas if (err)
1374 9b4458b4 2022-06-26 thomas goto done;
1375 9b4458b4 2022-06-26 thomas free(idstr);
1376 9b4458b4 2022-06-26 thomas idstr = NULL;
1377 9b4458b4 2022-06-26 thomas } else {
1378 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1379 82c78e96 2022-08-06 thomas outfile);
1380 9b4458b4 2022-06-26 thomas if (err)
1381 9b4458b4 2022-06-26 thomas goto done;
1382 cd0acaa7 2018-05-20 stsp }
1383 bacc9935 2018-05-20 stsp
1384 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, repo, id2);
1385 9b697879 2018-05-20 stsp if (err)
1386 9b697879 2018-05-20 stsp goto done;
1387 9b697879 2018-05-20 stsp
1388 9b4458b4 2022-06-26 thomas err = got_object_id_str(&idstr, id2);
1389 9b4458b4 2022-06-26 thomas if (err)
1390 9b4458b4 2022-06-26 thomas goto done;
1391 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1392 9b4458b4 2022-06-26 thomas if (err)
1393 9b4458b4 2022-06-26 thomas goto done;
1394 9b4458b4 2022-06-26 thomas
1395 82c78e96 2022-08-06 thomas err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1396 15a94983 2018-12-23 stsp commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1397 cc8021af 2021-10-12 thomas got_object_commit_get_tree_id(commit2), paths, "", "",
1398 be97ab03 2023-01-19 thomas diff_context, ignore_whitespace, force_text_diff, dsa, repo,
1399 be97ab03 2023-01-19 thomas outfile, diff_algo);
1400 11528a82 2018-05-19 stsp done:
1401 11528a82 2018-05-19 stsp if (commit1)
1402 11528a82 2018-05-19 stsp got_object_commit_close(commit1);
1403 11528a82 2018-05-19 stsp if (commit2)
1404 11528a82 2018-05-19 stsp got_object_commit_close(commit2);
1405 9b4458b4 2022-06-26 thomas free(idstr);
1406 11528a82 2018-05-19 stsp return err;
1407 11528a82 2018-05-19 stsp }
1408 dc424a06 2019-08-07 stsp
1409 dc424a06 2019-08-07 stsp const struct got_error *
1410 fe621944 2020-11-10 stsp got_diff_files(struct got_diffreg_result **resultp,
1411 dd2e2f52 2022-07-01 thomas FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1412 dd2e2f52 2022-07-01 thomas const char *label2, int diff_context, int ignore_whitespace,
1413 25ec7006 2022-07-01 thomas int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1414 dc424a06 2019-08-07 stsp {
1415 dc424a06 2019-08-07 stsp const struct got_error *err = NULL;
1416 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
1417 dc424a06 2019-08-07 stsp
1418 fe621944 2020-11-10 stsp if (resultp)
1419 fe621944 2020-11-10 stsp *resultp = NULL;
1420 dc424a06 2019-08-07 stsp
1421 dc424a06 2019-08-07 stsp if (outfile) {
1422 dc424a06 2019-08-07 stsp fprintf(outfile, "file - %s\n",
1423 dd2e2f52 2022-07-01 thomas f1_exists ? label1 : "/dev/null");
1424 dc424a06 2019-08-07 stsp fprintf(outfile, "file + %s\n",
1425 dd2e2f52 2022-07-01 thomas f2_exists ? label2 : "/dev/null");
1426 dc424a06 2019-08-07 stsp }
1427 fe621944 2020-11-10 stsp
1428 25ec7006 2022-07-01 thomas err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1429 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
1430 fe621944 2020-11-10 stsp if (err)
1431 fe621944 2020-11-10 stsp goto done;
1432 fe621944 2020-11-10 stsp
1433 fe621944 2020-11-10 stsp if (outfile) {
1434 fe621944 2020-11-10 stsp err = got_diffreg_output(NULL, NULL, diffreg_result,
1435 dd2e2f52 2022-07-01 thomas f1_exists, f2_exists, label1, label2,
1436 1cb46f00 2020-11-21 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1437 dc424a06 2019-08-07 stsp if (err)
1438 dc424a06 2019-08-07 stsp goto done;
1439 dc424a06 2019-08-07 stsp }
1440 fe621944 2020-11-10 stsp
1441 dc424a06 2019-08-07 stsp done:
1442 fe621944 2020-11-10 stsp if (resultp && err == NULL)
1443 fe621944 2020-11-10 stsp *resultp = diffreg_result;
1444 fe621944 2020-11-10 stsp else if (diffreg_result) {
1445 fe621944 2020-11-10 stsp const struct got_error *free_err;
1446 92a56907 2023-01-14 thomas
1447 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
1448 fe621944 2020-11-10 stsp if (free_err && err == NULL)
1449 fe621944 2020-11-10 stsp err = free_err;
1450 dc424a06 2019-08-07 stsp }
1451 fe621944 2020-11-10 stsp
1452 dc424a06 2019-08-07 stsp return err;
1453 dc424a06 2019-08-07 stsp }