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 8b925c6c 2022-07-16 thomas #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 588a8092 2023-02-23 thomas #include <sha2.h>
25 7d283eee 2017-11-29 stsp #include <zlib.h>
26 7d283eee 2017-11-29 stsp
27 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
28 dd038bc6 2021-09-21 thomas.ad
29 7d283eee 2017-11-29 stsp #include "got_object.h"
30 e09a504c 2019-06-28 stsp #include "got_repository.h"
31 7d283eee 2017-11-29 stsp #include "got_error.h"
32 789689b5 2017-11-30 stsp #include "got_diff.h"
33 324d37e7 2019-05-11 stsp #include "got_path.h"
34 0208f208 2020-05-05 stsp #include "got_cancel.h"
35 0208f208 2020-05-05 stsp #include "got_worktree.h"
36 1758cce7 2022-06-13 thomas #include "got_opentemp.h"
37 7d283eee 2017-11-29 stsp
38 718b3ab0 2018-03-17 stsp #include "got_lib_diff.h"
39 15a94983 2018-12-23 stsp #include "got_lib_delta.h"
40 15a94983 2018-12-23 stsp #include "got_lib_inflate.h"
41 15a94983 2018-12-23 stsp #include "got_lib_object.h"
42 772fcad5 2023-01-07 thomas
43 772fcad5 2023-01-07 thomas #ifndef MAX
44 772fcad5 2023-01-07 thomas #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
45 772fcad5 2023-01-07 thomas #endif
46 a7852263 2017-11-30 stsp
47 404c43c4 2018-06-21 stsp static const struct got_error *
48 82c78e96 2022-08-06 thomas add_line_metadata(struct got_diff_line **lines, size_t *nlines,
49 82c78e96 2022-08-06 thomas off_t off, uint8_t type)
50 fe621944 2020-11-10 stsp {
51 82c78e96 2022-08-06 thomas struct got_diff_line *p;
52 fe621944 2020-11-10 stsp
53 82c78e96 2022-08-06 thomas p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
54 fe621944 2020-11-10 stsp if (p == NULL)
55 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
56 82c78e96 2022-08-06 thomas *lines = p;
57 82c78e96 2022-08-06 thomas (*lines)[*nlines].offset = off;
58 82c78e96 2022-08-06 thomas (*lines)[*nlines].type = type;
59 fe621944 2020-11-10 stsp (*nlines)++;
60 82c78e96 2022-08-06 thomas
61 fe621944 2020-11-10 stsp return NULL;
62 fe621944 2020-11-10 stsp }
63 fe621944 2020-11-10 stsp
64 53d03f97 2023-01-10 thomas static void
65 53d03f97 2023-01-10 thomas diffstat_field_width(size_t *maxlen, int *add_cols, int *rm_cols, size_t len,
66 53d03f97 2023-01-10 thomas uint32_t add, uint32_t rm)
67 53d03f97 2023-01-10 thomas {
68 53d03f97 2023-01-10 thomas int d1 = 1, d2 = 1;
69 53d03f97 2023-01-10 thomas
70 53d03f97 2023-01-10 thomas if (maxlen)
71 53d03f97 2023-01-10 thomas *maxlen = MAX(*maxlen, len);
72 53d03f97 2023-01-10 thomas
73 53d03f97 2023-01-10 thomas while (add /= 10)
74 53d03f97 2023-01-10 thomas ++d1;
75 53d03f97 2023-01-10 thomas *add_cols = MAX(*add_cols, d1);
76 53d03f97 2023-01-10 thomas
77 53d03f97 2023-01-10 thomas while (rm /= 10)
78 53d03f97 2023-01-10 thomas ++d2;
79 53d03f97 2023-01-10 thomas *rm_cols = MAX(*rm_cols, d2);
80 53d03f97 2023-01-10 thomas }
81 53d03f97 2023-01-10 thomas
82 fe621944 2020-11-10 stsp static const struct got_error *
83 53d03f97 2023-01-10 thomas get_diffstat(struct got_diffstat_cb_arg *ds, const char *path,
84 53d03f97 2023-01-10 thomas struct diff_result *r, int force_text, int status)
85 53d03f97 2023-01-10 thomas {
86 53d03f97 2023-01-10 thomas const struct got_error *err;
87 53d03f97 2023-01-10 thomas struct got_pathlist_entry *pe;
88 53d03f97 2023-01-10 thomas struct got_diff_changed_path *change = NULL;
89 53d03f97 2023-01-10 thomas int flags = (r->left->atomizer_flags | r->right->atomizer_flags);
90 53d03f97 2023-01-10 thomas int isbin = (flags & DIFF_ATOMIZER_FOUND_BINARY_DATA);
91 53d03f97 2023-01-10 thomas int i;
92 53d03f97 2023-01-10 thomas
93 53d03f97 2023-01-10 thomas change = calloc(1, sizeof(*change));
94 53d03f97 2023-01-10 thomas if (change == NULL)
95 e80ef83e 2023-01-10 thomas return got_error_from_errno("calloc");
96 53d03f97 2023-01-10 thomas
97 53d03f97 2023-01-10 thomas if (!isbin || force_text) {
98 53d03f97 2023-01-10 thomas for (i = 0; i < r->chunks.len; ++i) {
99 53d03f97 2023-01-10 thomas struct diff_chunk *c;
100 53d03f97 2023-01-10 thomas int clc, crc;
101 53d03f97 2023-01-10 thomas
102 53d03f97 2023-01-10 thomas c = diff_chunk_get(r, i);
103 53d03f97 2023-01-10 thomas clc = diff_chunk_get_left_count(c);
104 53d03f97 2023-01-10 thomas crc = diff_chunk_get_right_count(c);
105 53d03f97 2023-01-10 thomas
106 53d03f97 2023-01-10 thomas if (crc && !clc)
107 53d03f97 2023-01-10 thomas change->add += crc;
108 53d03f97 2023-01-10 thomas if (clc && !crc)
109 53d03f97 2023-01-10 thomas change->rm += clc;
110 53d03f97 2023-01-10 thomas }
111 53d03f97 2023-01-10 thomas }
112 53d03f97 2023-01-10 thomas
113 53d03f97 2023-01-10 thomas change->status = status;
114 53d03f97 2023-01-10 thomas ds->ins += change->add;
115 53d03f97 2023-01-10 thomas ds->del += change->rm;
116 53d03f97 2023-01-10 thomas ++ds->nfiles;
117 53d03f97 2023-01-10 thomas
118 53d03f97 2023-01-10 thomas err = got_pathlist_append(ds->paths, path, change);
119 92a56907 2023-01-14 thomas if (err) {
120 92a56907 2023-01-14 thomas free(change);
121 53d03f97 2023-01-10 thomas return err;
122 92a56907 2023-01-14 thomas }
123 53d03f97 2023-01-10 thomas
124 53d03f97 2023-01-10 thomas pe = TAILQ_LAST(ds->paths, got_pathlist_head);
125 53d03f97 2023-01-10 thomas diffstat_field_width(&ds->max_path_len, &ds->add_cols, &ds->rm_cols,
126 53d03f97 2023-01-10 thomas pe->path_len, change->add, change->rm);
127 53d03f97 2023-01-10 thomas
128 53d03f97 2023-01-10 thomas return NULL;
129 53d03f97 2023-01-10 thomas }
130 53d03f97 2023-01-10 thomas
131 53d03f97 2023-01-10 thomas static const struct got_error *
132 82c78e96 2022-08-06 thomas diff_blobs(struct got_diff_line **lines, size_t *nlines,
133 fe621944 2020-11-10 stsp struct got_diffreg_result **resultp, struct got_blob_object *blob1,
134 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
135 46f68b20 2019-10-19 stsp const char *label1, const char *label2, mode_t mode1, mode_t mode2,
136 53d03f97 2023-01-10 thomas int diff_context, int ignore_whitespace, int force_text_diff,
137 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg *diffstat, FILE *outfile,
138 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo)
139 7d283eee 2017-11-29 stsp {
140 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
141 01986ce9 2023-02-07 thomas char hex1[GOT_OBJECT_ID_HEX_MAXLEN];
142 01986ce9 2023-02-07 thomas char hex2[GOT_OBJECT_ID_HEX_MAXLEN];
143 0c6f49ba 2022-07-01 thomas const char *idstr1 = NULL, *idstr2 = NULL;
144 92a56907 2023-01-14 thomas char *modestr1 = NULL, *modestr2 = NULL;
145 be659d10 2020-11-18 stsp off_t size1, size2;
146 a6d62598 2023-01-07 thomas struct got_diffreg_result *result = NULL;
147 fe621944 2020-11-10 stsp off_t outoff = 0;
148 fe621944 2020-11-10 stsp int n;
149 7d283eee 2017-11-29 stsp
150 3387ec82 2022-08-30 thomas if (lines && *lines && *nlines > 0)
151 82c78e96 2022-08-06 thomas outoff = (*lines)[*nlines - 1].offset;
152 82c78e96 2022-08-06 thomas else if (lines) {
153 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
154 9c659ea0 2020-11-22 stsp if (err)
155 9c659ea0 2020-11-22 stsp goto done;
156 9c659ea0 2020-11-22 stsp }
157 fe621944 2020-11-10 stsp
158 fe621944 2020-11-10 stsp if (resultp)
159 fe621944 2020-11-10 stsp *resultp = NULL;
160 fe621944 2020-11-10 stsp
161 a0f32f33 2022-06-13 thomas if (f1) {
162 1758cce7 2022-06-13 thomas err = got_opentemp_truncate(f1);
163 a0f32f33 2022-06-13 thomas if (err)
164 a0f32f33 2022-06-13 thomas goto done;
165 fe621944 2020-11-10 stsp }
166 a0f32f33 2022-06-13 thomas if (f2) {
167 1758cce7 2022-06-13 thomas err = got_opentemp_truncate(f2);
168 a0f32f33 2022-06-13 thomas if (err)
169 a0f32f33 2022-06-13 thomas goto done;
170 fe621944 2020-11-10 stsp }
171 7d283eee 2017-11-29 stsp
172 f934cf2c 2018-02-12 stsp size1 = 0;
173 98abbc84 2017-11-30 stsp if (blob1) {
174 f934cf2c 2018-02-12 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
175 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
176 6c4c42e0 2019-06-24 stsp blob1);
177 35e9ba5d 2018-06-21 stsp if (err)
178 35e9ba5d 2018-06-21 stsp goto done;
179 98abbc84 2017-11-30 stsp } else
180 98abbc84 2017-11-30 stsp idstr1 = "/dev/null";
181 7d283eee 2017-11-29 stsp
182 f934cf2c 2018-02-12 stsp size2 = 0;
183 98abbc84 2017-11-30 stsp if (blob2) {
184 f934cf2c 2018-02-12 stsp idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
185 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
186 6c4c42e0 2019-06-24 stsp blob2);
187 35e9ba5d 2018-06-21 stsp if (err)
188 35e9ba5d 2018-06-21 stsp goto done;
189 98abbc84 2017-11-30 stsp } else
190 98abbc84 2017-11-30 stsp idstr2 = "/dev/null";
191 7d283eee 2017-11-29 stsp
192 09de383e 2018-12-24 stsp if (outfile) {
193 40dde666 2020-07-23 stsp int modebits;
194 92a56907 2023-01-14 thomas
195 46f68b20 2019-10-19 stsp if (mode1 && mode1 != mode2) {
196 40dde666 2020-07-23 stsp if (S_ISLNK(mode1))
197 40dde666 2020-07-23 stsp modebits = S_IFLNK;
198 40dde666 2020-07-23 stsp else
199 40dde666 2020-07-23 stsp modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
200 46f68b20 2019-10-19 stsp if (asprintf(&modestr1, " (mode %o)",
201 40dde666 2020-07-23 stsp mode1 & modebits) == -1) {
202 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
203 46f68b20 2019-10-19 stsp goto done;
204 46f68b20 2019-10-19 stsp }
205 46f68b20 2019-10-19 stsp }
206 46f68b20 2019-10-19 stsp if (mode2 && mode1 != mode2) {
207 40dde666 2020-07-23 stsp if (S_ISLNK(mode2))
208 40dde666 2020-07-23 stsp modebits = S_IFLNK;
209 40dde666 2020-07-23 stsp else
210 40dde666 2020-07-23 stsp modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
211 46f68b20 2019-10-19 stsp if (asprintf(&modestr2, " (mode %o)",
212 40dde666 2020-07-23 stsp mode2 & modebits) == -1) {
213 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
214 46f68b20 2019-10-19 stsp goto done;
215 46f68b20 2019-10-19 stsp }
216 46f68b20 2019-10-19 stsp }
217 fe621944 2020-11-10 stsp n = fprintf(outfile, "blob - %s%s\n", idstr1,
218 46f68b20 2019-10-19 stsp modestr1 ? modestr1 : "");
219 fe621944 2020-11-10 stsp if (n < 0)
220 fe621944 2020-11-10 stsp goto done;
221 fe621944 2020-11-10 stsp outoff += n;
222 82c78e96 2022-08-06 thomas if (lines) {
223 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
224 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_BLOB_MIN);
225 fe621944 2020-11-10 stsp if (err)
226 fe621944 2020-11-10 stsp goto done;
227 fe621944 2020-11-10 stsp }
228 fe621944 2020-11-10 stsp
229 fe621944 2020-11-10 stsp n = fprintf(outfile, "blob + %s%s\n", idstr2,
230 46f68b20 2019-10-19 stsp modestr2 ? modestr2 : "");
231 fe621944 2020-11-10 stsp if (n < 0)
232 fe621944 2020-11-10 stsp goto done;
233 fe621944 2020-11-10 stsp outoff += n;
234 82c78e96 2022-08-06 thomas if (lines) {
235 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
236 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_BLOB_PLUS);
237 fe621944 2020-11-10 stsp if (err)
238 fe621944 2020-11-10 stsp goto done;
239 fe621944 2020-11-10 stsp }
240 09de383e 2018-12-24 stsp }
241 53d03f97 2023-01-10 thomas
242 25ec7006 2022-07-01 thomas err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
243 25ec7006 2022-07-01 thomas force_text_diff);
244 fe621944 2020-11-10 stsp if (err)
245 fe621944 2020-11-10 stsp goto done;
246 fe621944 2020-11-10 stsp
247 be97ab03 2023-01-19 thomas if (diffstat) {
248 53d03f97 2023-01-10 thomas char *path = NULL;
249 53d03f97 2023-01-10 thomas int status = GOT_STATUS_NO_CHANGE;
250 e80ef83e 2023-01-10 thomas
251 e80ef83e 2023-01-10 thomas if (blob1 == NULL)
252 e80ef83e 2023-01-10 thomas status = GOT_STATUS_ADD;
253 e80ef83e 2023-01-10 thomas else if (blob2 == NULL)
254 e80ef83e 2023-01-10 thomas status = GOT_STATUS_DELETE;
255 be97ab03 2023-01-19 thomas else {
256 be97ab03 2023-01-19 thomas if (strcmp(idstr1, idstr2) != 0)
257 be97ab03 2023-01-19 thomas status = GOT_STATUS_MODIFY;
258 be97ab03 2023-01-19 thomas else if (mode1 != mode2)
259 be97ab03 2023-01-19 thomas status = GOT_STATUS_MODE_CHANGE;
260 be97ab03 2023-01-19 thomas }
261 53d03f97 2023-01-10 thomas
262 53d03f97 2023-01-10 thomas if (label1 == NULL && label2 == NULL) {
263 53d03f97 2023-01-10 thomas /* diffstat of blobs, show hash instead of path */
264 53d03f97 2023-01-10 thomas if (asprintf(&path, "%.10s -> %.10s",
265 53d03f97 2023-01-10 thomas idstr1, idstr2) == -1) {
266 53d03f97 2023-01-10 thomas err = got_error_from_errno("asprintf");
267 53d03f97 2023-01-10 thomas goto done;
268 53d03f97 2023-01-10 thomas }
269 53d03f97 2023-01-10 thomas } else {
270 e80ef83e 2023-01-10 thomas if (label2 != NULL &&
271 e80ef83e 2023-01-10 thomas (status != GOT_STATUS_DELETE || label1 == NULL))
272 e80ef83e 2023-01-10 thomas path = strdup(label2);
273 e80ef83e 2023-01-10 thomas else
274 e80ef83e 2023-01-10 thomas path = strdup(label1);
275 53d03f97 2023-01-10 thomas if (path == NULL) {
276 e80ef83e 2023-01-10 thomas err = got_error_from_errno("strdup");
277 53d03f97 2023-01-10 thomas goto done;
278 53d03f97 2023-01-10 thomas }
279 53d03f97 2023-01-10 thomas }
280 53d03f97 2023-01-10 thomas
281 be97ab03 2023-01-19 thomas err = get_diffstat(diffstat, path, result->result,
282 be97ab03 2023-01-19 thomas force_text_diff, status);
283 53d03f97 2023-01-10 thomas if (err) {
284 53d03f97 2023-01-10 thomas free(path);
285 53d03f97 2023-01-10 thomas goto done;
286 53d03f97 2023-01-10 thomas }
287 53d03f97 2023-01-10 thomas }
288 53d03f97 2023-01-10 thomas
289 fe621944 2020-11-10 stsp if (outfile) {
290 82c78e96 2022-08-06 thomas err = got_diffreg_output(lines, nlines, result,
291 1cb46f00 2020-11-21 stsp blob1 != NULL, blob2 != NULL,
292 fe621944 2020-11-10 stsp label1 ? label1 : idstr1,
293 fe621944 2020-11-10 stsp label2 ? label2 : idstr2,
294 fe621944 2020-11-10 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
295 fe621944 2020-11-10 stsp if (err)
296 fe621944 2020-11-10 stsp goto done;
297 fe621944 2020-11-10 stsp }
298 fe621944 2020-11-10 stsp
299 a6d62598 2023-01-07 thomas done:
300 92a56907 2023-01-14 thomas free(modestr1);
301 92a56907 2023-01-14 thomas free(modestr2);
302 fe621944 2020-11-10 stsp if (resultp && err == NULL)
303 fe621944 2020-11-10 stsp *resultp = result;
304 a6d62598 2023-01-07 thomas else if (result) {
305 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
306 fe621944 2020-11-10 stsp if (free_err && err == NULL)
307 fe621944 2020-11-10 stsp err = free_err;
308 fe621944 2020-11-10 stsp }
309 a6d62598 2023-01-07 thomas
310 7d283eee 2017-11-29 stsp return err;
311 aaa13589 2019-06-01 stsp }
312 aaa13589 2019-06-01 stsp
313 aaa13589 2019-06-01 stsp const struct got_error *
314 aaa13589 2019-06-01 stsp got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
315 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
316 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
317 a0f32f33 2022-06-13 thomas const char *label1, const char *label2, mode_t mode1, mode_t mode2,
318 a0f32f33 2022-06-13 thomas struct got_repository *repo)
319 aaa13589 2019-06-01 stsp {
320 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg *a = arg;
321 aaa13589 2019-06-01 stsp
322 82c78e96 2022-08-06 thomas return diff_blobs(&a->lines, &a->nlines, NULL,
323 a0f32f33 2022-06-13 thomas blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
324 be97ab03 2023-01-19 thomas a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile,
325 be97ab03 2023-01-19 thomas a->diff_algo);
326 7d283eee 2017-11-29 stsp }
327 474b4f94 2017-11-30 stsp
328 404c43c4 2018-06-21 stsp const struct got_error *
329 82c78e96 2022-08-06 thomas got_diff_blob(struct got_diff_line **lines, size_t*nlines,
330 fe621944 2020-11-10 stsp struct got_blob_object *blob1, struct got_blob_object *blob2,
331 a0f32f33 2022-06-13 thomas FILE *f1, FILE *f2, const char *label1, const char *label2,
332 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo, int diff_context,
333 be97ab03 2023-01-19 thomas int ignore_whitespace, int force_text_diff,
334 53d03f97 2023-01-10 thomas struct got_diffstat_cb_arg *ds, FILE *outfile)
335 404c43c4 2018-06-21 stsp {
336 82c78e96 2022-08-06 thomas return diff_blobs(lines, nlines, NULL, blob1, blob2, f1, f2,
337 64453f7e 2020-11-21 stsp label1, label2, 0, 0, diff_context, ignore_whitespace,
338 be97ab03 2023-01-19 thomas force_text_diff, ds, outfile, diff_algo);
339 404c43c4 2018-06-21 stsp }
340 404c43c4 2018-06-21 stsp
341 7f1f93af 2019-08-06 stsp static const struct got_error *
342 fe621944 2020-11-10 stsp diff_blob_file(struct got_diffreg_result **resultp,
343 a0f32f33 2022-06-13 thomas struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
344 6d054bb9 2022-09-23 thomas FILE *f2, int f2_exists, struct stat *sb2, const char *label2,
345 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
346 be97ab03 2023-01-19 thomas int force_text_diff, struct got_diffstat_cb_arg *diffstat, FILE *outfile)
347 b72f483a 2019-02-05 stsp {
348 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
349 01986ce9 2023-02-07 thomas char hex1[GOT_OBJECT_ID_HEX_MAXLEN];
350 0c6f49ba 2022-07-01 thomas 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 a0f32f33 2022-06-13 thomas if (blob1)
357 b72f483a 2019-02-05 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
358 a0f32f33 2022-06-13 thomas 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 6d054bb9 2022-09-23 thomas char *mode = NULL;
363 6d054bb9 2022-09-23 thomas
364 6d054bb9 2022-09-23 thomas /* display file mode for new added files only */
365 6d054bb9 2022-09-23 thomas if (f2_exists && blob1 == NULL) {
366 6d054bb9 2022-09-23 thomas int mmask = (S_IRWXU | S_IRWXG | S_IRWXO);
367 6d054bb9 2022-09-23 thomas
368 6d054bb9 2022-09-23 thomas if (S_ISLNK(sb2->st_mode))
369 6d054bb9 2022-09-23 thomas mmask = S_IFLNK;
370 6d054bb9 2022-09-23 thomas if (asprintf(&mode, " (mode %o)",
371 6d054bb9 2022-09-23 thomas sb2->st_mode & mmask) == -1)
372 6d054bb9 2022-09-23 thomas return got_error_from_errno("asprintf");
373 6d054bb9 2022-09-23 thomas }
374 4ce46740 2019-08-08 stsp fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
375 6d054bb9 2022-09-23 thomas fprintf(outfile, "file + %s%s\n",
376 6d054bb9 2022-09-23 thomas f2_exists ? label2 : "/dev/null", mode ? mode : "");
377 6d054bb9 2022-09-23 thomas free(mode);
378 7f1f93af 2019-08-06 stsp }
379 fe621944 2020-11-10 stsp
380 25ec7006 2022-07-01 thomas err = got_diffreg(&result, f1, f2, diff_algo, ignore_whitespace,
381 25ec7006 2022-07-01 thomas 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 dd2e2f52 2022-07-01 thomas 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 53d03f97 2023-01-10 thomas goto done;
393 53d03f97 2023-01-10 thomas }
394 53d03f97 2023-01-10 thomas
395 be97ab03 2023-01-19 thomas if (diffstat) {
396 53d03f97 2023-01-10 thomas char *path = NULL;
397 53d03f97 2023-01-10 thomas int status = GOT_STATUS_NO_CHANGE;
398 53d03f97 2023-01-10 thomas
399 53d03f97 2023-01-10 thomas /*
400 53d03f97 2023-01-10 thomas * Ignore 'm'ode status change: if there's no accompanying
401 53d03f97 2023-01-10 thomas * content change, there'll be no diffstat, and if there
402 53d03f97 2023-01-10 thomas * are actual changes, 'M'odified takes precedence.
403 53d03f97 2023-01-10 thomas */
404 53d03f97 2023-01-10 thomas if (blob1 == NULL)
405 53d03f97 2023-01-10 thomas status = GOT_STATUS_ADD;
406 53d03f97 2023-01-10 thomas else if (!f2_exists)
407 53d03f97 2023-01-10 thomas status = GOT_STATUS_DELETE;
408 53d03f97 2023-01-10 thomas else
409 53d03f97 2023-01-10 thomas status = GOT_STATUS_MODIFY;
410 e80ef83e 2023-01-10 thomas
411 e80ef83e 2023-01-10 thomas if (label2 != NULL &&
412 e80ef83e 2023-01-10 thomas (status != GOT_STATUS_DELETE || label1 == NULL))
413 e80ef83e 2023-01-10 thomas path = strdup(label2);
414 e80ef83e 2023-01-10 thomas else
415 e80ef83e 2023-01-10 thomas path = strdup(label1);
416 e80ef83e 2023-01-10 thomas if (path == NULL) {
417 e80ef83e 2023-01-10 thomas err = got_error_from_errno("strdup");
418 e80ef83e 2023-01-10 thomas goto done;
419 e80ef83e 2023-01-10 thomas }
420 53d03f97 2023-01-10 thomas
421 be97ab03 2023-01-19 thomas err = get_diffstat(diffstat, path, result->result,
422 be97ab03 2023-01-19 thomas force_text_diff, status);
423 53d03f97 2023-01-10 thomas if (err) {
424 53d03f97 2023-01-10 thomas free(path);
425 fe621944 2020-11-10 stsp goto done;
426 53d03f97 2023-01-10 thomas }
427 7f1f93af 2019-08-06 stsp }
428 fe621944 2020-11-10 stsp
429 a6d62598 2023-01-07 thomas 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 a0f32f33 2022-06-13 thomas got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
442 6d054bb9 2022-09-23 thomas const char *label1, FILE *f2, int f2_exists, struct stat *sb2,
443 25ec7006 2022-07-01 thomas const char *label2, enum got_diff_algorithm diff_algo, int diff_context,
444 be97ab03 2023-01-19 thomas int ignore_whitespace, int force_text_diff,
445 53d03f97 2023-01-10 thomas struct got_diffstat_cb_arg *ds, FILE *outfile)
446 7f1f93af 2019-08-06 stsp {
447 dd2e2f52 2022-07-01 thomas return diff_blob_file(NULL, blob1, f1, size1, label1, f2, f2_exists,
448 6d054bb9 2022-09-23 thomas sb2, label2, diff_algo, diff_context, ignore_whitespace,
449 be97ab03 2023-01-19 thomas force_text_diff, 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 dd2e2f52 2022-07-01 thomas diff_added_blob(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
454 19a6a6b5 2022-07-01 thomas const char *label, mode_t mode, struct got_repository *repo,
455 a0f32f33 2022-06-13 thomas 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 dd2e2f52 2022-07-01 thomas 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 dd2e2f52 2022-07-01 thomas err = cb(cb_arg, NULL, blob, f1, f2, NULL, id,
469 a0f32f33 2022-06-13 thomas 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 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
480 19a6a6b5 2022-07-01 thomas const char *label1, const char *label2,
481 a0f32f33 2022-06-13 thomas mode_t mode1, mode_t mode2, struct got_repository *repo,
482 a0f32f33 2022-06-13 thomas 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 f4ae6ddb 2022-07-01 thomas
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 f4ae6ddb 2022-07-01 thomas 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 f4ae6ddb 2022-07-01 thomas 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 a0f32f33 2022-06-13 thomas err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
516 a0f32f33 2022-06-13 thomas 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 dd2e2f52 2022-07-01 thomas diff_deleted_blob(struct got_object_id *id, FILE *f1, int fd1,
531 dd2e2f52 2022-07-01 thomas FILE *f2, const char *label, mode_t mode, struct got_repository *repo,
532 19a6a6b5 2022-07-01 thomas 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 dd2e2f52 2022-07-01 thomas 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 dd2e2f52 2022-07-01 thomas err = cb(cb_arg, blob, NULL, f1, f2, id, NULL, label, NULL,
546 a0f32f33 2022-06-13 thomas 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 dd2e2f52 2022-07-01 thomas diff_added_tree(struct got_object_id *id, FILE *f1, FILE *f2, int fd2,
556 dd2e2f52 2022-07-01 thomas const char *label, struct got_repository *repo, got_diff_blob_cb cb,
557 dd2e2f52 2022-07-01 thomas 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 dd2e2f52 2022-07-01 thomas err = got_diff_tree(NULL, tree, f1, f2, -1, fd2, NULL, label,
577 a0f32f33 2022-06-13 thomas 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 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
589 19a6a6b5 2022-07-01 thomas const char *label1, const char *label2,
590 a0f32f33 2022-06-13 thomas struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
591 a0f32f33 2022-06-13 thomas 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 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
626 19a6a6b5 2022-07-01 thomas 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 dd2e2f52 2022-07-01 thomas diff_deleted_tree(struct got_object_id *id, FILE *f1, int fd1,
642 dd2e2f52 2022-07-01 thomas FILE *f2, const char *label, struct got_repository *repo,
643 19a6a6b5 2022-07-01 thomas 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 dd2e2f52 2022-07-01 thomas err = got_diff_tree(tree, NULL, f1, f2, fd1, -1, label, NULL,
663 a0f32f33 2022-06-13 thomas 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 a0f32f33 2022-06-13 thomas diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
683 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
684 19a6a6b5 2022-07-01 thomas 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 dd2e2f52 2022-07-01 thomas err = diff_deleted_tree(&te1->id, f1, fd1, f2,
697 dd2e2f52 2022-07-01 thomas label1, repo, cb, cb_arg, diff_content);
698 31b4484f 2019-07-27 stsp else {
699 31b4484f 2019-07-27 stsp if (diff_content)
700 19a6a6b5 2022-07-01 thomas err = diff_deleted_blob(&te1->id, f1, fd1,
701 dd2e2f52 2022-07-01 thomas f2, label1, te1->mode, repo, cb, cb_arg);
702 31b4484f 2019-07-27 stsp else
703 a0f32f33 2022-06-13 thomas err = cb(cb_arg, NULL, NULL, NULL, NULL,
704 a0f32f33 2022-06-13 thomas &te1->id, NULL, label1, NULL,
705 a0f32f33 2022-06-13 thomas 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 a0f32f33 2022-06-13 thomas return diff_modified_tree(&te1->id, &te2->id, f1, f2,
715 19a6a6b5 2022-07-01 thomas fd1, fd2, label1, label2, repo, cb, cb_arg,
716 19a6a6b5 2022-07-01 thomas 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 19a6a6b5 2022-07-01 thomas f1, f2, fd1, fd2, label1, label2,
725 19a6a6b5 2022-07-01 thomas te1->mode, te2->mode, repo, cb, cb_arg);
726 31b4484f 2019-07-27 stsp else
727 a0f32f33 2022-06-13 thomas return cb(cb_arg, NULL, NULL, NULL, NULL,
728 a0f32f33 2022-06-13 thomas &te1->id, &te2->id, label1, label2,
729 a0f32f33 2022-06-13 thomas 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 dd2e2f52 2022-07-01 thomas 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 dd2e2f52 2022-07-01 thomas return diff_added_tree(&te2->id, f1, f2, fd2, label2,
754 a0f32f33 2022-06-13 thomas repo, cb, cb_arg, diff_content);
755 f6861a81 2018-09-13 stsp
756 31b4484f 2019-07-27 stsp if (diff_content)
757 dd2e2f52 2022-07-01 thomas return diff_added_blob(&te2->id, f1, f2, fd2,
758 19a6a6b5 2022-07-01 thomas label2, te2->mode, repo, cb, cb_arg);
759 31b4484f 2019-07-27 stsp
760 a0f32f33 2022-06-13 thomas return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
761 a0f32f33 2022-06-13 thomas 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 772fcad5 2023-01-07 thomas got_diff_tree_compute_diffstat(void *arg, struct got_blob_object *blob1,
766 772fcad5 2023-01-07 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
767 772fcad5 2023-01-07 thomas struct got_object_id *id1, struct got_object_id *id2,
768 772fcad5 2023-01-07 thomas const char *label1, const char *label2,
769 772fcad5 2023-01-07 thomas mode_t mode1, mode_t mode2, struct got_repository *repo)
770 772fcad5 2023-01-07 thomas {
771 772fcad5 2023-01-07 thomas const struct got_error *err = NULL;
772 772fcad5 2023-01-07 thomas struct got_diffreg_result *result = NULL;
773 772fcad5 2023-01-07 thomas struct got_diffstat_cb_arg *a = arg;
774 772fcad5 2023-01-07 thomas char *path = NULL;
775 53d03f97 2023-01-10 thomas int status = GOT_STATUS_NO_CHANGE;
776 772fcad5 2023-01-07 thomas
777 772fcad5 2023-01-07 thomas path = strdup(label2 ? label2 : label1);
778 772fcad5 2023-01-07 thomas if (path == NULL)
779 e80ef83e 2023-01-10 thomas return got_error_from_errno("strdup");
780 772fcad5 2023-01-07 thomas
781 772fcad5 2023-01-07 thomas if (id1 == NULL)
782 53d03f97 2023-01-10 thomas status = GOT_STATUS_ADD;
783 772fcad5 2023-01-07 thomas else if (id2 == NULL)
784 53d03f97 2023-01-10 thomas status = GOT_STATUS_DELETE;
785 772fcad5 2023-01-07 thomas else {
786 772fcad5 2023-01-07 thomas if (got_object_id_cmp(id1, id2) != 0)
787 53d03f97 2023-01-10 thomas status = GOT_STATUS_MODIFY;
788 772fcad5 2023-01-07 thomas else if (mode1 != mode2)
789 53d03f97 2023-01-10 thomas status = GOT_STATUS_MODE_CHANGE;
790 772fcad5 2023-01-07 thomas }
791 772fcad5 2023-01-07 thomas
792 772fcad5 2023-01-07 thomas if (f1) {
793 772fcad5 2023-01-07 thomas err = got_opentemp_truncate(f1);
794 772fcad5 2023-01-07 thomas if (err)
795 772fcad5 2023-01-07 thomas goto done;
796 772fcad5 2023-01-07 thomas }
797 772fcad5 2023-01-07 thomas if (f2) {
798 772fcad5 2023-01-07 thomas err = got_opentemp_truncate(f2);
799 772fcad5 2023-01-07 thomas if (err)
800 772fcad5 2023-01-07 thomas goto done;
801 772fcad5 2023-01-07 thomas }
802 772fcad5 2023-01-07 thomas
803 772fcad5 2023-01-07 thomas if (blob1) {
804 772fcad5 2023-01-07 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1,
805 772fcad5 2023-01-07 thomas blob1);
806 772fcad5 2023-01-07 thomas if (err)
807 772fcad5 2023-01-07 thomas goto done;
808 772fcad5 2023-01-07 thomas }
809 772fcad5 2023-01-07 thomas if (blob2) {
810 772fcad5 2023-01-07 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2,
811 772fcad5 2023-01-07 thomas blob2);
812 772fcad5 2023-01-07 thomas if (err)
813 772fcad5 2023-01-07 thomas goto done;
814 772fcad5 2023-01-07 thomas }
815 772fcad5 2023-01-07 thomas
816 772fcad5 2023-01-07 thomas err = got_diffreg(&result, f1, f2, a->diff_algo, a->ignore_ws,
817 772fcad5 2023-01-07 thomas a->force_text);
818 772fcad5 2023-01-07 thomas if (err)
819 772fcad5 2023-01-07 thomas goto done;
820 772fcad5 2023-01-07 thomas
821 53d03f97 2023-01-10 thomas err = get_diffstat(a, path, result->result, a->force_text, status);
822 772fcad5 2023-01-07 thomas
823 772fcad5 2023-01-07 thomas done:
824 772fcad5 2023-01-07 thomas if (result) {
825 772fcad5 2023-01-07 thomas const struct got_error *free_err;
826 772fcad5 2023-01-07 thomas
827 772fcad5 2023-01-07 thomas free_err = got_diffreg_result_free(result);
828 772fcad5 2023-01-07 thomas if (free_err && err == NULL)
829 772fcad5 2023-01-07 thomas err = free_err;
830 772fcad5 2023-01-07 thomas }
831 53d03f97 2023-01-10 thomas if (err)
832 772fcad5 2023-01-07 thomas free(path);
833 772fcad5 2023-01-07 thomas return err;
834 772fcad5 2023-01-07 thomas }
835 772fcad5 2023-01-07 thomas
836 772fcad5 2023-01-07 thomas const struct got_error *
837 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
838 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
839 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
840 a0f32f33 2022-06-13 thomas 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 e80ef83e 2023-01-10 thomas 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 4c71f93b 2021-12-31 thomas 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 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
882 19a6a6b5 2022-07-01 thomas const char *label1, const char *label2,
883 a0f32f33 2022-06-13 thomas struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
884 a0f32f33 2022-06-13 thomas 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 92a56907 2023-01-14 thomas te2->name) == -1) {
902 92a56907 2023-01-14 thomas err = got_error_from_errno("asprintf");
903 92a56907 2023-01-14 thomas goto done;
904 92a56907 2023-01-14 thomas }
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 92a56907 2023-01-14 thomas
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 92a56907 2023-01-14 thomas label2[0] ? "/" : "", te->name) == -1) {
919 92a56907 2023-01-14 thomas err = got_error_from_errno("asprintf");
920 92a56907 2023-01-14 thomas goto done;
921 92a56907 2023-01-14 thomas }
922 f6861a81 2018-09-13 stsp }
923 92a56907 2023-01-14 thomas
924 19a6a6b5 2022-07-01 thomas err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
925 19a6a6b5 2022-07-01 thomas 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 92a56907 2023-01-14 thomas
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 92a56907 2023-01-14 thomas
937 d6ce02f1 2018-11-17 stsp free(l2);
938 92a56907 2023-01-14 thomas l2 = NULL;
939 d6ce02f1 2018-11-17 stsp if (te) {
940 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
941 92a56907 2023-01-14 thomas label2[0] ? "/" : "", te->name) == -1) {
942 92a56907 2023-01-14 thomas err = got_error_from_errno("asprintf");
943 92a56907 2023-01-14 thomas goto done;
944 92a56907 2023-01-14 thomas }
945 d6ce02f1 2018-11-17 stsp } else {
946 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
947 92a56907 2023-01-14 thomas label2[0] ? "/" : "", te2->name) == -1) {
948 92a56907 2023-01-14 thomas err = got_error_from_errno("asprintf");
949 92a56907 2023-01-14 thomas goto done;
950 92a56907 2023-01-14 thomas }
951 d6ce02f1 2018-11-17 stsp }
952 92a56907 2023-01-14 thomas
953 dd2e2f52 2022-07-01 thomas err = diff_entry_new_old(te2, te, f1, f2, fd2, l2,
954 dd2e2f52 2022-07-01 thomas 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 92a56907 2023-01-14 thomas label1[0] ? "/" : "", te1->name) == -1) {
967 92a56907 2023-01-14 thomas err = got_error_from_errno("asprintf");
968 92a56907 2023-01-14 thomas goto done;
969 92a56907 2023-01-14 thomas }
970 f6861a81 2018-09-13 stsp }
971 92a56907 2023-01-14 thomas
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 92a56907 2023-01-14 thomas label2[0] ? "/" : "", te2->name) == -1) {
980 92a56907 2023-01-14 thomas err = got_error_from_errno("asprintf");
981 92a56907 2023-01-14 thomas goto done;
982 92a56907 2023-01-14 thomas }
983 f6861a81 2018-09-13 stsp }
984 474b4f94 2017-11-30 stsp } while (te1 || te2);
985 11528a82 2018-05-19 stsp
986 92a56907 2023-01-14 thomas done:
987 92a56907 2023-01-14 thomas free(l1);
988 92a56907 2023-01-14 thomas 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 82c78e96 2022-08-06 thomas got_diff_objects_as_blobs(struct got_diff_line **lines, size_t *nlines,
994 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
995 19a6a6b5 2022-07-01 thomas struct got_object_id *id1, struct got_object_id *id2,
996 25ec7006 2022-07-01 thomas const char *label1, const char *label2,
997 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo, int diff_context,
998 be97ab03 2023-01-19 thomas int ignore_whitespace, int force_text_diff, struct got_diffstat_cb_arg *ds,
999 be97ab03 2023-01-19 thomas 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 f4ae6ddb 2022-07-01 thomas
1007 15a94983 2018-12-23 stsp if (id1) {
1008 f4ae6ddb 2022-07-01 thomas 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 f4ae6ddb 2022-07-01 thomas 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 82c78e96 2022-08-06 thomas err = got_diff_blob(lines, nlines, blob1, blob2, f1, f2, label1, label2,
1018 82c78e96 2022-08-06 thomas diff_algo, diff_context, ignore_whitespace, force_text_diff,
1019 be97ab03 2023-01-19 thomas ds, outfile);
1020 cc8021af 2021-10-12 thomas done:
1021 cc8021af 2021-10-12 thomas if (blob1)
1022 cc8021af 2021-10-12 thomas got_object_blob_close(blob1);
1023 cc8021af 2021-10-12 thomas if (blob2)
1024 cc8021af 2021-10-12 thomas got_object_blob_close(blob2);
1025 cc8021af 2021-10-12 thomas return err;
1026 cc8021af 2021-10-12 thomas }
1027 cc8021af 2021-10-12 thomas
1028 cc8021af 2021-10-12 thomas static const struct got_error *
1029 cc8021af 2021-10-12 thomas diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
1030 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
1031 a0f32f33 2022-06-13 thomas struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
1032 cc8021af 2021-10-12 thomas {
1033 cc8021af 2021-10-12 thomas const struct got_error *err = NULL;
1034 cc8021af 2021-10-12 thomas struct got_pathlist_entry *pe;
1035 cc8021af 2021-10-12 thomas struct got_object_id *id1 = NULL, *id2 = NULL;
1036 cc8021af 2021-10-12 thomas struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
1037 cc8021af 2021-10-12 thomas struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1038 cc8021af 2021-10-12 thomas
1039 cc8021af 2021-10-12 thomas TAILQ_FOREACH(pe, paths, entry) {
1040 cc8021af 2021-10-12 thomas int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
1041 cc8021af 2021-10-12 thomas mode_t mode1 = 0, mode2 = 0;
1042 cc8021af 2021-10-12 thomas
1043 cc8021af 2021-10-12 thomas free(id1);
1044 cc8021af 2021-10-12 thomas id1 = NULL;
1045 cc8021af 2021-10-12 thomas free(id2);
1046 cc8021af 2021-10-12 thomas id2 = NULL;
1047 cc8021af 2021-10-12 thomas if (subtree1) {
1048 cc8021af 2021-10-12 thomas got_object_tree_close(subtree1);
1049 cc8021af 2021-10-12 thomas subtree1 = NULL;
1050 cc8021af 2021-10-12 thomas }
1051 cc8021af 2021-10-12 thomas if (subtree2) {
1052 cc8021af 2021-10-12 thomas got_object_tree_close(subtree2);
1053 cc8021af 2021-10-12 thomas subtree2 = NULL;
1054 cc8021af 2021-10-12 thomas }
1055 cc8021af 2021-10-12 thomas if (blob1) {
1056 cc8021af 2021-10-12 thomas got_object_blob_close(blob1);
1057 cc8021af 2021-10-12 thomas blob1 = NULL;
1058 cc8021af 2021-10-12 thomas }
1059 cc8021af 2021-10-12 thomas if (blob2) {
1060 cc8021af 2021-10-12 thomas got_object_blob_close(blob2);
1061 cc8021af 2021-10-12 thomas blob2 = NULL;
1062 cc8021af 2021-10-12 thomas }
1063 cc8021af 2021-10-12 thomas
1064 cc8021af 2021-10-12 thomas err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
1065 cc8021af 2021-10-12 thomas pe->path);
1066 cc8021af 2021-10-12 thomas if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1067 cc8021af 2021-10-12 thomas goto done;
1068 cc8021af 2021-10-12 thomas err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
1069 cc8021af 2021-10-12 thomas pe->path);
1070 cc8021af 2021-10-12 thomas if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
1071 cc8021af 2021-10-12 thomas goto done;
1072 cc8021af 2021-10-12 thomas if (id1 == NULL && id2 == NULL) {
1073 cc8021af 2021-10-12 thomas err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
1074 cc8021af 2021-10-12 thomas goto done;
1075 cc8021af 2021-10-12 thomas }
1076 cc8021af 2021-10-12 thomas if (id1) {
1077 cc8021af 2021-10-12 thomas err = got_object_get_type(&type1, repo, id1);
1078 cc8021af 2021-10-12 thomas if (err)
1079 cc8021af 2021-10-12 thomas goto done;
1080 cc8021af 2021-10-12 thomas }
1081 cc8021af 2021-10-12 thomas if (id2) {
1082 cc8021af 2021-10-12 thomas err = got_object_get_type(&type2, repo, id2);
1083 cc8021af 2021-10-12 thomas if (err)
1084 cc8021af 2021-10-12 thomas goto done;
1085 cc8021af 2021-10-12 thomas }
1086 cc8021af 2021-10-12 thomas if (type1 == GOT_OBJ_TYPE_ANY &&
1087 cc8021af 2021-10-12 thomas type2 == GOT_OBJ_TYPE_ANY) {
1088 cc8021af 2021-10-12 thomas err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
1089 cc8021af 2021-10-12 thomas goto done;
1090 cc8021af 2021-10-12 thomas } else if (type1 != GOT_OBJ_TYPE_ANY &&
1091 cc8021af 2021-10-12 thomas type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
1092 cc8021af 2021-10-12 thomas err = got_error(GOT_ERR_OBJ_TYPE);
1093 cc8021af 2021-10-12 thomas goto done;
1094 cc8021af 2021-10-12 thomas }
1095 cc8021af 2021-10-12 thomas
1096 cc8021af 2021-10-12 thomas if (type1 == GOT_OBJ_TYPE_BLOB ||
1097 cc8021af 2021-10-12 thomas type2 == GOT_OBJ_TYPE_BLOB) {
1098 cc8021af 2021-10-12 thomas if (id1) {
1099 cc8021af 2021-10-12 thomas err = got_object_open_as_blob(&blob1, repo,
1100 f4ae6ddb 2022-07-01 thomas id1, 8192, fd1);
1101 cc8021af 2021-10-12 thomas if (err)
1102 cc8021af 2021-10-12 thomas goto done;
1103 cc8021af 2021-10-12 thomas }
1104 cc8021af 2021-10-12 thomas if (id2) {
1105 cc8021af 2021-10-12 thomas err = got_object_open_as_blob(&blob2, repo,
1106 f4ae6ddb 2022-07-01 thomas id2, 8192, fd2);
1107 cc8021af 2021-10-12 thomas if (err)
1108 cc8021af 2021-10-12 thomas goto done;
1109 cc8021af 2021-10-12 thomas }
1110 a0f32f33 2022-06-13 thomas err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
1111 cc8021af 2021-10-12 thomas id1 ? pe->path : "/dev/null",
1112 cc8021af 2021-10-12 thomas id2 ? pe->path : "/dev/null",
1113 cc8021af 2021-10-12 thomas mode1, mode2, repo);
1114 cc8021af 2021-10-12 thomas if (err)
1115 cc8021af 2021-10-12 thomas goto done;
1116 cc8021af 2021-10-12 thomas } else if (type1 == GOT_OBJ_TYPE_TREE ||
1117 cc8021af 2021-10-12 thomas type2 == GOT_OBJ_TYPE_TREE) {
1118 cc8021af 2021-10-12 thomas if (id1) {
1119 cc8021af 2021-10-12 thomas err = got_object_open_as_tree(&subtree1, repo,
1120 cc8021af 2021-10-12 thomas id1);
1121 cc8021af 2021-10-12 thomas if (err)
1122 cc8021af 2021-10-12 thomas goto done;
1123 cc8021af 2021-10-12 thomas }
1124 cc8021af 2021-10-12 thomas if (id2) {
1125 cc8021af 2021-10-12 thomas err = got_object_open_as_tree(&subtree2, repo,
1126 cc8021af 2021-10-12 thomas id2);
1127 cc8021af 2021-10-12 thomas if (err)
1128 cc8021af 2021-10-12 thomas goto done;
1129 cc8021af 2021-10-12 thomas }
1130 a0f32f33 2022-06-13 thomas err = got_diff_tree(subtree1, subtree2, f1, f2,
1131 19a6a6b5 2022-07-01 thomas fd1, fd2,
1132 cc8021af 2021-10-12 thomas id1 ? pe->path : "/dev/null",
1133 cc8021af 2021-10-12 thomas id2 ? pe->path : "/dev/null",
1134 cc8021af 2021-10-12 thomas repo, cb, cb_arg, 1);
1135 cc8021af 2021-10-12 thomas if (err)
1136 cc8021af 2021-10-12 thomas goto done;
1137 cc8021af 2021-10-12 thomas } else {
1138 cc8021af 2021-10-12 thomas err = got_error(GOT_ERR_OBJ_TYPE);
1139 cc8021af 2021-10-12 thomas goto done;
1140 cc8021af 2021-10-12 thomas }
1141 cc8021af 2021-10-12 thomas }
1142 11528a82 2018-05-19 stsp done:
1143 cc8021af 2021-10-12 thomas free(id1);
1144 cc8021af 2021-10-12 thomas free(id2);
1145 cc8021af 2021-10-12 thomas if (subtree1)
1146 cc8021af 2021-10-12 thomas got_object_tree_close(subtree1);
1147 cc8021af 2021-10-12 thomas if (subtree2)
1148 cc8021af 2021-10-12 thomas 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 9b4458b4 2022-06-26 thomas static const struct got_error *
1157 82c78e96 2022-08-06 thomas show_object_id(struct got_diff_line **lines, size_t *nlines,
1158 82c78e96 2022-08-06 thomas const char *obj_typestr, int ch, const char *id_str, FILE *outfile)
1159 9b4458b4 2022-06-26 thomas {
1160 9b4458b4 2022-06-26 thomas const struct got_error *err;
1161 9b4458b4 2022-06-26 thomas int n;
1162 9b4458b4 2022-06-26 thomas off_t outoff = 0;
1163 9b4458b4 2022-06-26 thomas
1164 9b4458b4 2022-06-26 thomas n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
1165 7e1f3a3d 2022-08-16 thomas if (n < 0)
1166 7e1f3a3d 2022-08-16 thomas return got_error_from_errno("fprintf");
1167 7e1f3a3d 2022-08-16 thomas
1168 82c78e96 2022-08-06 thomas if (lines != NULL && *lines != NULL) {
1169 9b4458b4 2022-06-26 thomas if (*nlines == 0) {
1170 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0,
1171 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
1172 9b4458b4 2022-06-26 thomas if (err)
1173 9b4458b4 2022-06-26 thomas return err;
1174 9b4458b4 2022-06-26 thomas } else
1175 82c78e96 2022-08-06 thomas outoff = (*lines)[*nlines - 1].offset;
1176 9b4458b4 2022-06-26 thomas
1177 9b4458b4 2022-06-26 thomas outoff += n;
1178 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
1179 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
1180 9b4458b4 2022-06-26 thomas if (err)
1181 9b4458b4 2022-06-26 thomas return err;
1182 9b4458b4 2022-06-26 thomas }
1183 9b4458b4 2022-06-26 thomas
1184 9b4458b4 2022-06-26 thomas return NULL;
1185 9b4458b4 2022-06-26 thomas }
1186 9b4458b4 2022-06-26 thomas
1187 9b4458b4 2022-06-26 thomas static const struct got_error *
1188 82c78e96 2022-08-06 thomas diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1189 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
1190 19a6a6b5 2022-07-01 thomas struct got_object_id *id1, struct got_object_id *id2,
1191 0c6f49ba 2022-07-01 thomas struct got_pathlist_head *paths, const char *label1, const char *label2,
1192 0c6f49ba 2022-07-01 thomas int diff_context, int ignore_whitespace, int force_text_diff,
1193 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg *dsa, struct got_repository *repo,
1194 be97ab03 2023-01-19 thomas FILE *outfile, enum got_diff_algorithm diff_algo)
1195 11528a82 2018-05-19 stsp {
1196 11528a82 2018-05-19 stsp const struct got_error *err;
1197 11528a82 2018-05-19 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1198 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
1199 82c78e96 2022-08-06 thomas int want_linemeta = (lines != NULL && *lines != NULL);
1200 11528a82 2018-05-19 stsp
1201 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
1202 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
1203 b74c7625 2018-05-20 stsp
1204 15a94983 2018-12-23 stsp if (id1) {
1205 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo, id1);
1206 cd0acaa7 2018-05-20 stsp if (err)
1207 cd0acaa7 2018-05-20 stsp goto done;
1208 cd0acaa7 2018-05-20 stsp }
1209 15a94983 2018-12-23 stsp if (id2) {
1210 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo, id2);
1211 cd0acaa7 2018-05-20 stsp if (err)
1212 cd0acaa7 2018-05-20 stsp goto done;
1213 cd0acaa7 2018-05-20 stsp }
1214 cc8021af 2021-10-12 thomas
1215 25ec7006 2022-07-01 thomas arg.diff_algo = diff_algo;
1216 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1217 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
1218 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
1219 53d03f97 2023-01-10 thomas arg.diffstat = dsa;
1220 aaa13589 2019-06-01 stsp arg.outfile = outfile;
1221 82c78e96 2022-08-06 thomas if (want_linemeta) {
1222 82c78e96 2022-08-06 thomas arg.lines = *lines;
1223 fe621944 2020-11-10 stsp arg.nlines = *nlines;
1224 fe621944 2020-11-10 stsp } else {
1225 82c78e96 2022-08-06 thomas arg.lines = NULL;
1226 fe621944 2020-11-10 stsp arg.nlines = 0;
1227 fe621944 2020-11-10 stsp }
1228 53d03f97 2023-01-10 thomas if (paths == NULL || TAILQ_EMPTY(paths))
1229 53d03f97 2023-01-10 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, label1,
1230 53d03f97 2023-01-10 thomas label2, repo, got_diff_blob_output_unidiff, &arg, 1);
1231 53d03f97 2023-01-10 thomas else
1232 19a6a6b5 2022-07-01 thomas err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
1233 cc8021af 2021-10-12 thomas got_diff_blob_output_unidiff, &arg);
1234 82c78e96 2022-08-06 thomas if (want_linemeta) {
1235 82c78e96 2022-08-06 thomas *lines = arg.lines; /* was likely re-allocated */
1236 fe621944 2020-11-10 stsp *nlines = arg.nlines;
1237 fe621944 2020-11-10 stsp }
1238 11528a82 2018-05-19 stsp done:
1239 11528a82 2018-05-19 stsp if (tree1)
1240 11528a82 2018-05-19 stsp got_object_tree_close(tree1);
1241 11528a82 2018-05-19 stsp if (tree2)
1242 11528a82 2018-05-19 stsp got_object_tree_close(tree2);
1243 11528a82 2018-05-19 stsp return err;
1244 11528a82 2018-05-19 stsp }
1245 11528a82 2018-05-19 stsp
1246 11528a82 2018-05-19 stsp const struct got_error *
1247 82c78e96 2022-08-06 thomas got_diff_objects_as_trees(struct got_diff_line **lines, size_t *nlines,
1248 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
1249 19a6a6b5 2022-07-01 thomas struct got_object_id *id1, struct got_object_id *id2,
1250 0c6f49ba 2022-07-01 thomas struct got_pathlist_head *paths, const char *label1, const char *label2,
1251 25ec7006 2022-07-01 thomas enum got_diff_algorithm diff_algo, int diff_context, int ignore_whitespace,
1252 be97ab03 2023-01-19 thomas int force_text_diff, struct got_diffstat_cb_arg *dsa,
1253 53d03f97 2023-01-10 thomas struct got_repository *repo, FILE *outfile)
1254 9b4458b4 2022-06-26 thomas {
1255 9b4458b4 2022-06-26 thomas const struct got_error *err;
1256 9b4458b4 2022-06-26 thomas char *idstr = NULL;
1257 9b4458b4 2022-06-26 thomas
1258 9b4458b4 2022-06-26 thomas if (id1 == NULL && id2 == NULL)
1259 9b4458b4 2022-06-26 thomas return got_error(GOT_ERR_NO_OBJ);
1260 9b4458b4 2022-06-26 thomas
1261 9b4458b4 2022-06-26 thomas if (id1) {
1262 9b4458b4 2022-06-26 thomas err = got_object_id_str(&idstr, id1);
1263 9b4458b4 2022-06-26 thomas if (err)
1264 9b4458b4 2022-06-26 thomas goto done;
1265 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "tree", '-', idstr, outfile);
1266 9b4458b4 2022-06-26 thomas if (err)
1267 9b4458b4 2022-06-26 thomas goto done;
1268 9b4458b4 2022-06-26 thomas free(idstr);
1269 9b4458b4 2022-06-26 thomas idstr = NULL;
1270 9b4458b4 2022-06-26 thomas } else {
1271 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "tree", '-', "/dev/null",
1272 82c78e96 2022-08-06 thomas outfile);
1273 9b4458b4 2022-06-26 thomas if (err)
1274 9b4458b4 2022-06-26 thomas goto done;
1275 9b4458b4 2022-06-26 thomas }
1276 9b4458b4 2022-06-26 thomas
1277 9b4458b4 2022-06-26 thomas if (id2) {
1278 9b4458b4 2022-06-26 thomas err = got_object_id_str(&idstr, id2);
1279 9b4458b4 2022-06-26 thomas if (err)
1280 9b4458b4 2022-06-26 thomas goto done;
1281 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "tree", '+', idstr, outfile);
1282 9b4458b4 2022-06-26 thomas if (err)
1283 9b4458b4 2022-06-26 thomas goto done;
1284 9b4458b4 2022-06-26 thomas free(idstr);
1285 9b4458b4 2022-06-26 thomas idstr = NULL;
1286 9b4458b4 2022-06-26 thomas } else {
1287 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "tree", '+', "/dev/null",
1288 82c78e96 2022-08-06 thomas outfile);
1289 9b4458b4 2022-06-26 thomas if (err)
1290 9b4458b4 2022-06-26 thomas goto done;
1291 9b4458b4 2022-06-26 thomas }
1292 9b4458b4 2022-06-26 thomas
1293 82c78e96 2022-08-06 thomas err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2, id1, id2,
1294 82c78e96 2022-08-06 thomas paths, label1, label2, diff_context, ignore_whitespace,
1295 be97ab03 2023-01-19 thomas force_text_diff, dsa, repo, outfile, diff_algo);
1296 9b4458b4 2022-06-26 thomas done:
1297 9b4458b4 2022-06-26 thomas free(idstr);
1298 9b4458b4 2022-06-26 thomas return err;
1299 9b4458b4 2022-06-26 thomas }
1300 9b4458b4 2022-06-26 thomas
1301 9b4458b4 2022-06-26 thomas const struct got_error *
1302 82c78e96 2022-08-06 thomas got_diff_objects_as_commits(struct got_diff_line **lines, size_t *nlines,
1303 19a6a6b5 2022-07-01 thomas FILE *f1, FILE *f2, int fd1, int fd2,
1304 19a6a6b5 2022-07-01 thomas struct got_object_id *id1, struct got_object_id *id2,
1305 25ec7006 2022-07-01 thomas struct got_pathlist_head *paths, enum got_diff_algorithm diff_algo,
1306 64453f7e 2020-11-21 stsp int diff_context, int ignore_whitespace, int force_text_diff,
1307 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg *dsa, struct got_repository *repo, FILE *outfile)
1308 11528a82 2018-05-19 stsp {
1309 11528a82 2018-05-19 stsp const struct got_error *err;
1310 11528a82 2018-05-19 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
1311 9b4458b4 2022-06-26 thomas char *idstr = NULL;
1312 11528a82 2018-05-19 stsp
1313 15a94983 2018-12-23 stsp if (id2 == NULL)
1314 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
1315 b74c7625 2018-05-20 stsp
1316 15a94983 2018-12-23 stsp if (id1) {
1317 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit1, repo, id1);
1318 cd0acaa7 2018-05-20 stsp if (err)
1319 cd0acaa7 2018-05-20 stsp goto done;
1320 9b4458b4 2022-06-26 thomas err = got_object_id_str(&idstr, id1);
1321 9b4458b4 2022-06-26 thomas if (err)
1322 9b4458b4 2022-06-26 thomas goto done;
1323 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "commit", '-', idstr,
1324 82c78e96 2022-08-06 thomas outfile);
1325 9b4458b4 2022-06-26 thomas if (err)
1326 9b4458b4 2022-06-26 thomas goto done;
1327 9b4458b4 2022-06-26 thomas free(idstr);
1328 9b4458b4 2022-06-26 thomas idstr = NULL;
1329 9b4458b4 2022-06-26 thomas } else {
1330 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "commit", '-', "/dev/null",
1331 82c78e96 2022-08-06 thomas outfile);
1332 9b4458b4 2022-06-26 thomas if (err)
1333 9b4458b4 2022-06-26 thomas goto done;
1334 cd0acaa7 2018-05-20 stsp }
1335 bacc9935 2018-05-20 stsp
1336 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, repo, id2);
1337 9b697879 2018-05-20 stsp if (err)
1338 9b697879 2018-05-20 stsp goto done;
1339 9b697879 2018-05-20 stsp
1340 9b4458b4 2022-06-26 thomas err = got_object_id_str(&idstr, id2);
1341 9b4458b4 2022-06-26 thomas if (err)
1342 9b4458b4 2022-06-26 thomas goto done;
1343 82c78e96 2022-08-06 thomas err = show_object_id(lines, nlines, "commit", '+', idstr, outfile);
1344 9b4458b4 2022-06-26 thomas if (err)
1345 9b4458b4 2022-06-26 thomas goto done;
1346 9b4458b4 2022-06-26 thomas
1347 82c78e96 2022-08-06 thomas err = diff_objects_as_trees(lines, nlines, f1, f2, fd1, fd2,
1348 15a94983 2018-12-23 stsp commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
1349 cc8021af 2021-10-12 thomas got_object_commit_get_tree_id(commit2), paths, "", "",
1350 be97ab03 2023-01-19 thomas diff_context, ignore_whitespace, force_text_diff, dsa, repo,
1351 be97ab03 2023-01-19 thomas outfile, diff_algo);
1352 11528a82 2018-05-19 stsp done:
1353 11528a82 2018-05-19 stsp if (commit1)
1354 11528a82 2018-05-19 stsp got_object_commit_close(commit1);
1355 11528a82 2018-05-19 stsp if (commit2)
1356 11528a82 2018-05-19 stsp got_object_commit_close(commit2);
1357 9b4458b4 2022-06-26 thomas free(idstr);
1358 11528a82 2018-05-19 stsp return err;
1359 11528a82 2018-05-19 stsp }
1360 dc424a06 2019-08-07 stsp
1361 dc424a06 2019-08-07 stsp const struct got_error *
1362 fe621944 2020-11-10 stsp got_diff_files(struct got_diffreg_result **resultp,
1363 dd2e2f52 2022-07-01 thomas FILE *f1, int f1_exists, const char *label1, FILE *f2, int f2_exists,
1364 dd2e2f52 2022-07-01 thomas const char *label2, int diff_context, int ignore_whitespace,
1365 25ec7006 2022-07-01 thomas int force_text_diff, FILE *outfile, enum got_diff_algorithm diff_algo)
1366 dc424a06 2019-08-07 stsp {
1367 dc424a06 2019-08-07 stsp const struct got_error *err = NULL;
1368 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
1369 dc424a06 2019-08-07 stsp
1370 fe621944 2020-11-10 stsp if (resultp)
1371 fe621944 2020-11-10 stsp *resultp = NULL;
1372 dc424a06 2019-08-07 stsp
1373 dc424a06 2019-08-07 stsp if (outfile) {
1374 dc424a06 2019-08-07 stsp fprintf(outfile, "file - %s\n",
1375 dd2e2f52 2022-07-01 thomas f1_exists ? label1 : "/dev/null");
1376 dc424a06 2019-08-07 stsp fprintf(outfile, "file + %s\n",
1377 dd2e2f52 2022-07-01 thomas f2_exists ? label2 : "/dev/null");
1378 dc424a06 2019-08-07 stsp }
1379 fe621944 2020-11-10 stsp
1380 25ec7006 2022-07-01 thomas err = got_diffreg(&diffreg_result, f1, f2, diff_algo,
1381 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
1382 fe621944 2020-11-10 stsp if (err)
1383 fe621944 2020-11-10 stsp goto done;
1384 fe621944 2020-11-10 stsp
1385 fe621944 2020-11-10 stsp if (outfile) {
1386 fe621944 2020-11-10 stsp err = got_diffreg_output(NULL, NULL, diffreg_result,
1387 dd2e2f52 2022-07-01 thomas f1_exists, f2_exists, label1, label2,
1388 1cb46f00 2020-11-21 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1389 dc424a06 2019-08-07 stsp if (err)
1390 dc424a06 2019-08-07 stsp goto done;
1391 dc424a06 2019-08-07 stsp }
1392 fe621944 2020-11-10 stsp
1393 dc424a06 2019-08-07 stsp done:
1394 fe621944 2020-11-10 stsp if (resultp && err == NULL)
1395 fe621944 2020-11-10 stsp *resultp = diffreg_result;
1396 fe621944 2020-11-10 stsp else if (diffreg_result) {
1397 fe621944 2020-11-10 stsp const struct got_error *free_err;
1398 92a56907 2023-01-14 thomas
1399 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
1400 fe621944 2020-11-10 stsp if (free_err && err == NULL)
1401 fe621944 2020-11-10 stsp err = free_err;
1402 dc424a06 2019-08-07 stsp }
1403 fe621944 2020-11-10 stsp
1404 dc424a06 2019-08-07 stsp return err;
1405 dc424a06 2019-08-07 stsp }