Blame


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