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