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