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