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