Blame


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