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 1c7f0520 2017-11-29 stsp #include <sys/stat.h>
18 7d283eee 2017-11-29 stsp
19 7d283eee 2017-11-29 stsp #include <stdio.h>
20 7d283eee 2017-11-29 stsp #include <stdlib.h>
21 7d283eee 2017-11-29 stsp #include <string.h>
22 f9d67749 2017-11-30 stsp #include <limits.h>
23 7d283eee 2017-11-29 stsp #include <zlib.h>
24 7d283eee 2017-11-29 stsp
25 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
26 dd038bc6 2021-09-21 thomas.ad
27 7d283eee 2017-11-29 stsp #include "got_object.h"
28 e09a504c 2019-06-28 stsp #include "got_repository.h"
29 7d283eee 2017-11-29 stsp #include "got_error.h"
30 789689b5 2017-11-30 stsp #include "got_diff.h"
31 324d37e7 2019-05-11 stsp #include "got_path.h"
32 0208f208 2020-05-05 stsp #include "got_cancel.h"
33 0208f208 2020-05-05 stsp #include "got_worktree.h"
34 7d283eee 2017-11-29 stsp
35 718b3ab0 2018-03-17 stsp #include "got_lib_diff.h"
36 15a94983 2018-12-23 stsp #include "got_lib_delta.h"
37 15a94983 2018-12-23 stsp #include "got_lib_inflate.h"
38 15a94983 2018-12-23 stsp #include "got_lib_object.h"
39 a7852263 2017-11-30 stsp
40 404c43c4 2018-06-21 stsp static const struct got_error *
41 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
42 fe621944 2020-11-10 stsp {
43 fe621944 2020-11-10 stsp off_t *p;
44 fe621944 2020-11-10 stsp
45 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
46 fe621944 2020-11-10 stsp if (p == NULL)
47 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
48 fe621944 2020-11-10 stsp *line_offsets = p;
49 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
50 fe621944 2020-11-10 stsp (*nlines)++;
51 fe621944 2020-11-10 stsp return NULL;
52 fe621944 2020-11-10 stsp }
53 fe621944 2020-11-10 stsp
54 fe621944 2020-11-10 stsp static const struct got_error *
55 a0f32f33 2022-06-13 thomas reset_file(FILE *f)
56 a0f32f33 2022-06-13 thomas {
57 a0f32f33 2022-06-13 thomas if (fpurge(f) == EOF)
58 a0f32f33 2022-06-13 thomas return got_error_from_errno("fpurge");
59 a0f32f33 2022-06-13 thomas if (ftruncate(fileno(f), 0L) == -1)
60 a0f32f33 2022-06-13 thomas return got_error_from_errno("ftruncate");
61 a0f32f33 2022-06-13 thomas if (fseeko(f, 0L, SEEK_SET) == -1)
62 a0f32f33 2022-06-13 thomas return got_error_from_errno("fseeko");
63 a0f32f33 2022-06-13 thomas return NULL;
64 a0f32f33 2022-06-13 thomas }
65 a0f32f33 2022-06-13 thomas
66 a0f32f33 2022-06-13 thomas static const struct got_error *
67 fe621944 2020-11-10 stsp diff_blobs(off_t **line_offsets, size_t *nlines,
68 fe621944 2020-11-10 stsp struct got_diffreg_result **resultp, struct got_blob_object *blob1,
69 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
70 46f68b20 2019-10-19 stsp const char *label1, const char *label2, mode_t mode1, mode_t mode2,
71 64453f7e 2020-11-21 stsp int diff_context, int ignore_whitespace, int force_text_diff, FILE *outfile)
72 7d283eee 2017-11-29 stsp {
73 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
74 f78b0693 2017-11-29 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
75 f78b0693 2017-11-29 stsp char hex2[SHA1_DIGEST_STRING_LENGTH];
76 98abbc84 2017-11-30 stsp char *idstr1 = NULL, *idstr2 = NULL;
77 be659d10 2020-11-18 stsp off_t size1, size2;
78 fe621944 2020-11-10 stsp struct got_diffreg_result *result;
79 fe621944 2020-11-10 stsp off_t outoff = 0;
80 fe621944 2020-11-10 stsp int n;
81 7d283eee 2017-11-29 stsp
82 fe621944 2020-11-10 stsp if (line_offsets && *line_offsets && *nlines > 0)
83 fe621944 2020-11-10 stsp outoff = (*line_offsets)[*nlines - 1];
84 9c659ea0 2020-11-22 stsp else if (line_offsets) {
85 9c659ea0 2020-11-22 stsp err = add_line_offset(line_offsets, nlines, 0);
86 9c659ea0 2020-11-22 stsp if (err)
87 9c659ea0 2020-11-22 stsp goto done;
88 9c659ea0 2020-11-22 stsp }
89 fe621944 2020-11-10 stsp
90 fe621944 2020-11-10 stsp if (resultp)
91 fe621944 2020-11-10 stsp *resultp = NULL;
92 fe621944 2020-11-10 stsp
93 a0f32f33 2022-06-13 thomas if (f1) {
94 a0f32f33 2022-06-13 thomas err = reset_file(f1);
95 a0f32f33 2022-06-13 thomas if (err)
96 a0f32f33 2022-06-13 thomas goto done;
97 fe621944 2020-11-10 stsp }
98 a0f32f33 2022-06-13 thomas if (f2) {
99 a0f32f33 2022-06-13 thomas err = reset_file(f2);
100 a0f32f33 2022-06-13 thomas if (err)
101 a0f32f33 2022-06-13 thomas goto done;
102 fe621944 2020-11-10 stsp }
103 7d283eee 2017-11-29 stsp
104 f934cf2c 2018-02-12 stsp size1 = 0;
105 98abbc84 2017-11-30 stsp if (blob1) {
106 f934cf2c 2018-02-12 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
107 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
108 6c4c42e0 2019-06-24 stsp blob1);
109 35e9ba5d 2018-06-21 stsp if (err)
110 35e9ba5d 2018-06-21 stsp goto done;
111 98abbc84 2017-11-30 stsp } else
112 98abbc84 2017-11-30 stsp idstr1 = "/dev/null";
113 7d283eee 2017-11-29 stsp
114 f934cf2c 2018-02-12 stsp size2 = 0;
115 98abbc84 2017-11-30 stsp if (blob2) {
116 f934cf2c 2018-02-12 stsp idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
117 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
118 6c4c42e0 2019-06-24 stsp blob2);
119 35e9ba5d 2018-06-21 stsp if (err)
120 35e9ba5d 2018-06-21 stsp goto done;
121 98abbc84 2017-11-30 stsp } else
122 98abbc84 2017-11-30 stsp idstr2 = "/dev/null";
123 7d283eee 2017-11-29 stsp
124 09de383e 2018-12-24 stsp if (outfile) {
125 46f68b20 2019-10-19 stsp char *modestr1 = NULL, *modestr2 = NULL;
126 40dde666 2020-07-23 stsp int modebits;
127 46f68b20 2019-10-19 stsp if (mode1 && mode1 != mode2) {
128 40dde666 2020-07-23 stsp if (S_ISLNK(mode1))
129 40dde666 2020-07-23 stsp modebits = S_IFLNK;
130 40dde666 2020-07-23 stsp else
131 40dde666 2020-07-23 stsp modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
132 46f68b20 2019-10-19 stsp if (asprintf(&modestr1, " (mode %o)",
133 40dde666 2020-07-23 stsp mode1 & modebits) == -1) {
134 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
135 46f68b20 2019-10-19 stsp goto done;
136 46f68b20 2019-10-19 stsp }
137 46f68b20 2019-10-19 stsp }
138 46f68b20 2019-10-19 stsp if (mode2 && mode1 != mode2) {
139 40dde666 2020-07-23 stsp if (S_ISLNK(mode2))
140 40dde666 2020-07-23 stsp modebits = S_IFLNK;
141 40dde666 2020-07-23 stsp else
142 40dde666 2020-07-23 stsp modebits = (S_IRWXU | S_IRWXG | S_IRWXO);
143 46f68b20 2019-10-19 stsp if (asprintf(&modestr2, " (mode %o)",
144 40dde666 2020-07-23 stsp mode2 & modebits) == -1) {
145 46f68b20 2019-10-19 stsp err = got_error_from_errno("asprintf");
146 46f68b20 2019-10-19 stsp goto done;
147 46f68b20 2019-10-19 stsp }
148 46f68b20 2019-10-19 stsp }
149 fe621944 2020-11-10 stsp n = fprintf(outfile, "blob - %s%s\n", idstr1,
150 46f68b20 2019-10-19 stsp modestr1 ? modestr1 : "");
151 fe621944 2020-11-10 stsp if (n < 0)
152 fe621944 2020-11-10 stsp goto done;
153 fe621944 2020-11-10 stsp outoff += n;
154 fe621944 2020-11-10 stsp if (line_offsets) {
155 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
156 fe621944 2020-11-10 stsp if (err)
157 fe621944 2020-11-10 stsp goto done;
158 fe621944 2020-11-10 stsp }
159 fe621944 2020-11-10 stsp
160 fe621944 2020-11-10 stsp n = fprintf(outfile, "blob + %s%s\n", idstr2,
161 46f68b20 2019-10-19 stsp modestr2 ? modestr2 : "");
162 fe621944 2020-11-10 stsp if (n < 0)
163 fe621944 2020-11-10 stsp goto done;
164 fe621944 2020-11-10 stsp outoff += n;
165 fe621944 2020-11-10 stsp if (line_offsets) {
166 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
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
171 46f68b20 2019-10-19 stsp free(modestr1);
172 46f68b20 2019-10-19 stsp free(modestr2);
173 09de383e 2018-12-24 stsp }
174 fe621944 2020-11-10 stsp err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
175 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
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 if (outfile) {
180 1cb46f00 2020-11-21 stsp err = got_diffreg_output(line_offsets, nlines, result,
181 1cb46f00 2020-11-21 stsp blob1 != NULL, blob2 != NULL,
182 fe621944 2020-11-10 stsp label1 ? label1 : idstr1,
183 fe621944 2020-11-10 stsp label2 ? label2 : idstr2,
184 fe621944 2020-11-10 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
185 fe621944 2020-11-10 stsp if (err)
186 fe621944 2020-11-10 stsp goto done;
187 fe621944 2020-11-10 stsp }
188 fe621944 2020-11-10 stsp
189 fe621944 2020-11-10 stsp if (resultp && err == NULL)
190 fe621944 2020-11-10 stsp *resultp = result;
191 fe621944 2020-11-10 stsp else {
192 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
193 fe621944 2020-11-10 stsp if (free_err && err == NULL)
194 fe621944 2020-11-10 stsp err = free_err;
195 fe621944 2020-11-10 stsp }
196 7d283eee 2017-11-29 stsp done:
197 7d283eee 2017-11-29 stsp return err;
198 aaa13589 2019-06-01 stsp }
199 aaa13589 2019-06-01 stsp
200 aaa13589 2019-06-01 stsp const struct got_error *
201 aaa13589 2019-06-01 stsp got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
202 a0f32f33 2022-06-13 thomas struct got_blob_object *blob2, FILE *f1, FILE *f2,
203 a0f32f33 2022-06-13 thomas struct got_object_id *id1, struct got_object_id *id2,
204 a0f32f33 2022-06-13 thomas const char *label1, const char *label2, mode_t mode1, mode_t mode2,
205 a0f32f33 2022-06-13 thomas struct got_repository *repo)
206 aaa13589 2019-06-01 stsp {
207 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg *a = arg;
208 aaa13589 2019-06-01 stsp
209 fe621944 2020-11-10 stsp return diff_blobs(&a->line_offsets, &a->nlines, NULL,
210 a0f32f33 2022-06-13 thomas blob1, blob2, f1, f2, label1, label2, mode1, mode2, a->diff_context,
211 64453f7e 2020-11-21 stsp a->ignore_whitespace, a->force_text_diff, a->outfile);
212 7d283eee 2017-11-29 stsp }
213 474b4f94 2017-11-30 stsp
214 404c43c4 2018-06-21 stsp const struct got_error *
215 fe621944 2020-11-10 stsp got_diff_blob(off_t **line_offsets, size_t *nlines,
216 fe621944 2020-11-10 stsp struct got_blob_object *blob1, struct got_blob_object *blob2,
217 a0f32f33 2022-06-13 thomas FILE *f1, FILE *f2, const char *label1, const char *label2,
218 a0f32f33 2022-06-13 thomas int diff_context, int ignore_whitespace, int force_text_diff,
219 a0f32f33 2022-06-13 thomas FILE *outfile)
220 404c43c4 2018-06-21 stsp {
221 a0f32f33 2022-06-13 thomas return diff_blobs(line_offsets, nlines, NULL, blob1, blob2, f1, f2,
222 64453f7e 2020-11-21 stsp label1, label2, 0, 0, diff_context, ignore_whitespace,
223 64453f7e 2020-11-21 stsp force_text_diff, outfile);
224 404c43c4 2018-06-21 stsp }
225 404c43c4 2018-06-21 stsp
226 7f1f93af 2019-08-06 stsp static const struct got_error *
227 fe621944 2020-11-10 stsp diff_blob_file(struct got_diffreg_result **resultp,
228 a0f32f33 2022-06-13 thomas struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
229 a0f32f33 2022-06-13 thomas FILE *f2, size_t size2, const char *label2, int diff_context,
230 a0f32f33 2022-06-13 thomas int ignore_whitespace, int force_text_diff, FILE *outfile)
231 b72f483a 2019-02-05 stsp {
232 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
233 b72f483a 2019-02-05 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
234 b72f483a 2019-02-05 stsp char *idstr1 = NULL;
235 fe621944 2020-11-10 stsp struct got_diffreg_result *result = NULL;
236 b72f483a 2019-02-05 stsp
237 fe621944 2020-11-10 stsp if (resultp)
238 fe621944 2020-11-10 stsp *resultp = NULL;
239 7f1f93af 2019-08-06 stsp
240 a0f32f33 2022-06-13 thomas if (blob1)
241 b72f483a 2019-02-05 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
242 a0f32f33 2022-06-13 thomas else
243 b72f483a 2019-02-05 stsp idstr1 = "/dev/null";
244 b72f483a 2019-02-05 stsp
245 7f1f93af 2019-08-06 stsp if (outfile) {
246 4ce46740 2019-08-08 stsp fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
247 7f1f93af 2019-08-06 stsp fprintf(outfile, "file + %s\n",
248 7f1f93af 2019-08-06 stsp f2 == NULL ? "/dev/null" : label2);
249 7f1f93af 2019-08-06 stsp }
250 fe621944 2020-11-10 stsp
251 fe621944 2020-11-10 stsp err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
252 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
253 fe621944 2020-11-10 stsp if (err)
254 fe621944 2020-11-10 stsp goto done;
255 fe621944 2020-11-10 stsp
256 fe621944 2020-11-10 stsp if (outfile) {
257 1cb46f00 2020-11-21 stsp err = got_diffreg_output(NULL, NULL, result,
258 a0f32f33 2022-06-13 thomas f1 != NULL, f2 != NULL,
259 1cb46f00 2020-11-21 stsp label2, /* show local file's path, not a blob ID */
260 1cb46f00 2020-11-21 stsp label2, GOT_DIFF_OUTPUT_UNIDIFF,
261 1cb46f00 2020-11-21 stsp diff_context, outfile);
262 7f1f93af 2019-08-06 stsp if (err)
263 fe621944 2020-11-10 stsp goto done;
264 7f1f93af 2019-08-06 stsp }
265 fe621944 2020-11-10 stsp
266 fe621944 2020-11-10 stsp if (resultp && err == NULL)
267 fe621944 2020-11-10 stsp *resultp = result;
268 fe621944 2020-11-10 stsp else if (result) {
269 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
270 fe621944 2020-11-10 stsp if (free_err && err == NULL)
271 fe621944 2020-11-10 stsp err = free_err;
272 fe621944 2020-11-10 stsp }
273 b72f483a 2019-02-05 stsp done:
274 b72f483a 2019-02-05 stsp return err;
275 7f1f93af 2019-08-06 stsp }
276 7f1f93af 2019-08-06 stsp
277 7f1f93af 2019-08-06 stsp const struct got_error *
278 a0f32f33 2022-06-13 thomas got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
279 a0f32f33 2022-06-13 thomas const char *label1, FILE *f2, size_t size2, const char *label2,
280 a0f32f33 2022-06-13 thomas int diff_context, int ignore_whitespace, int force_text_diff, FILE *outfile)
281 7f1f93af 2019-08-06 stsp {
282 a0f32f33 2022-06-13 thomas return diff_blob_file(NULL, blob1, f1, size1, label1, f2, size2, label2,
283 64453f7e 2020-11-21 stsp diff_context, ignore_whitespace, force_text_diff, outfile);
284 474b4f94 2017-11-30 stsp }
285 474b4f94 2017-11-30 stsp
286 474b4f94 2017-11-30 stsp static const struct got_error *
287 a0f32f33 2022-06-13 thomas diff_added_blob(struct got_object_id *id, FILE *f, const char *label,
288 a0f32f33 2022-06-13 thomas mode_t mode, struct got_repository *repo,
289 a0f32f33 2022-06-13 thomas got_diff_blob_cb cb, void *cb_arg)
290 474b4f94 2017-11-30 stsp {
291 4e22badc 2017-11-30 stsp const struct got_error *err;
292 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
293 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
294 4e22badc 2017-11-30 stsp
295 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
296 4e22badc 2017-11-30 stsp if (err)
297 4e22badc 2017-11-30 stsp return err;
298 4e22badc 2017-11-30 stsp
299 2acfca77 2018-04-01 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
300 2acfca77 2018-04-01 stsp if (err)
301 2acfca77 2018-04-01 stsp goto done;
302 a0f32f33 2022-06-13 thomas err = cb(cb_arg, NULL, blob, NULL, f, NULL, id,
303 a0f32f33 2022-06-13 thomas NULL, label, 0, mode, repo);
304 2acfca77 2018-04-01 stsp done:
305 2acfca77 2018-04-01 stsp got_object_close(obj);
306 2acfca77 2018-04-01 stsp if (blob)
307 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
308 2acfca77 2018-04-01 stsp return err;
309 474b4f94 2017-11-30 stsp }
310 474b4f94 2017-11-30 stsp
311 474b4f94 2017-11-30 stsp static const struct got_error *
312 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
313 a0f32f33 2022-06-13 thomas FILE *f1, FILE *f2, const char *label1, const char *label2,
314 a0f32f33 2022-06-13 thomas mode_t mode1, mode_t mode2, struct got_repository *repo,
315 a0f32f33 2022-06-13 thomas got_diff_blob_cb cb, void *cb_arg)
316 474b4f94 2017-11-30 stsp {
317 6a213ccb 2017-11-30 stsp const struct got_error *err;
318 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
319 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
320 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
321 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
322 6a213ccb 2017-11-30 stsp
323 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
324 6a213ccb 2017-11-30 stsp if (err)
325 730a8aa0 2018-04-24 stsp return err;
326 15a94983 2018-12-23 stsp if (obj1->type != GOT_OBJ_TYPE_BLOB) {
327 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
328 6a213ccb 2017-11-30 stsp goto done;
329 6a213ccb 2017-11-30 stsp }
330 6a213ccb 2017-11-30 stsp
331 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
332 730a8aa0 2018-04-24 stsp if (err)
333 6a213ccb 2017-11-30 stsp goto done;
334 15a94983 2018-12-23 stsp if (obj2->type != GOT_OBJ_TYPE_BLOB) {
335 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
336 6a213ccb 2017-11-30 stsp goto done;
337 6a213ccb 2017-11-30 stsp }
338 6a213ccb 2017-11-30 stsp
339 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob1, repo, obj1, 8192);
340 2acfca77 2018-04-01 stsp if (err)
341 6a213ccb 2017-11-30 stsp goto done;
342 6a213ccb 2017-11-30 stsp
343 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob2, repo, obj2, 8192);
344 2acfca77 2018-04-01 stsp if (err)
345 6a213ccb 2017-11-30 stsp goto done;
346 6a213ccb 2017-11-30 stsp
347 a0f32f33 2022-06-13 thomas err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
348 a0f32f33 2022-06-13 thomas mode1, mode2, repo);
349 6a213ccb 2017-11-30 stsp done:
350 a3e2cbea 2017-12-01 stsp if (obj1)
351 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
352 a3e2cbea 2017-12-01 stsp if (obj2)
353 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
354 a3e2cbea 2017-12-01 stsp if (blob1)
355 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
356 a3e2cbea 2017-12-01 stsp if (blob2)
357 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
358 6a213ccb 2017-11-30 stsp return err;
359 474b4f94 2017-11-30 stsp }
360 474b4f94 2017-11-30 stsp
361 474b4f94 2017-11-30 stsp static const struct got_error *
362 a0f32f33 2022-06-13 thomas diff_deleted_blob(struct got_object_id *id, FILE *f, const char *label,
363 a0f32f33 2022-06-13 thomas mode_t mode, struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
364 474b4f94 2017-11-30 stsp {
365 365fb436 2017-11-30 stsp const struct got_error *err;
366 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
367 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
368 365fb436 2017-11-30 stsp
369 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
370 365fb436 2017-11-30 stsp if (err)
371 365fb436 2017-11-30 stsp return err;
372 365fb436 2017-11-30 stsp
373 2acfca77 2018-04-01 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
374 2acfca77 2018-04-01 stsp if (err)
375 2acfca77 2018-04-01 stsp goto done;
376 a0f32f33 2022-06-13 thomas err = cb(cb_arg, blob, NULL, f, NULL, id, NULL, label, NULL,
377 a0f32f33 2022-06-13 thomas mode, 0, repo);
378 2acfca77 2018-04-01 stsp done:
379 2acfca77 2018-04-01 stsp got_object_close(obj);
380 2acfca77 2018-04-01 stsp if (blob)
381 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
382 2acfca77 2018-04-01 stsp return err;
383 474b4f94 2017-11-30 stsp }
384 474b4f94 2017-11-30 stsp
385 474b4f94 2017-11-30 stsp static const struct got_error *
386 a0f32f33 2022-06-13 thomas diff_added_tree(struct got_object_id *id, FILE *f, const char *label,
387 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
388 31b4484f 2019-07-27 stsp int diff_content)
389 474b4f94 2017-11-30 stsp {
390 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
391 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
392 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
393 9c70d4c3 2017-11-30 stsp
394 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
395 9c70d4c3 2017-11-30 stsp if (err)
396 9c70d4c3 2017-11-30 stsp goto done;
397 9c70d4c3 2017-11-30 stsp
398 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
399 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
400 9c70d4c3 2017-11-30 stsp goto done;
401 9c70d4c3 2017-11-30 stsp }
402 9c70d4c3 2017-11-30 stsp
403 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
404 9c70d4c3 2017-11-30 stsp if (err)
405 9c70d4c3 2017-11-30 stsp goto done;
406 9c70d4c3 2017-11-30 stsp
407 a0f32f33 2022-06-13 thomas err = got_diff_tree(NULL, tree, NULL, f, NULL, label,
408 a0f32f33 2022-06-13 thomas repo, cb, cb_arg, diff_content);
409 9c70d4c3 2017-11-30 stsp done:
410 9c70d4c3 2017-11-30 stsp if (tree)
411 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
412 9c70d4c3 2017-11-30 stsp if (treeobj)
413 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
414 9c70d4c3 2017-11-30 stsp return err;
415 474b4f94 2017-11-30 stsp }
416 474b4f94 2017-11-30 stsp
417 474b4f94 2017-11-30 stsp static const struct got_error *
418 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
419 a0f32f33 2022-06-13 thomas FILE *f1, FILE *f2, const char *label1, const char *label2,
420 a0f32f33 2022-06-13 thomas struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
421 a0f32f33 2022-06-13 thomas int diff_content)
422 474b4f94 2017-11-30 stsp {
423 f6861a81 2018-09-13 stsp const struct got_error *err;
424 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
425 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
426 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
427 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
428 789689b5 2017-11-30 stsp
429 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
430 789689b5 2017-11-30 stsp if (err)
431 789689b5 2017-11-30 stsp goto done;
432 789689b5 2017-11-30 stsp
433 15a94983 2018-12-23 stsp if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
434 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
435 789689b5 2017-11-30 stsp goto done;
436 789689b5 2017-11-30 stsp }
437 789689b5 2017-11-30 stsp
438 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
439 789689b5 2017-11-30 stsp if (err)
440 789689b5 2017-11-30 stsp goto done;
441 789689b5 2017-11-30 stsp
442 15a94983 2018-12-23 stsp if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
443 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
444 789689b5 2017-11-30 stsp goto done;
445 789689b5 2017-11-30 stsp }
446 789689b5 2017-11-30 stsp
447 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
448 789689b5 2017-11-30 stsp if (err)
449 789689b5 2017-11-30 stsp goto done;
450 789689b5 2017-11-30 stsp
451 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
452 789689b5 2017-11-30 stsp if (err)
453 789689b5 2017-11-30 stsp goto done;
454 789689b5 2017-11-30 stsp
455 a0f32f33 2022-06-13 thomas err = got_diff_tree(tree1, tree2, f1, f2, label1, label2,
456 a0f32f33 2022-06-13 thomas repo, cb, cb_arg, diff_content);
457 789689b5 2017-11-30 stsp
458 789689b5 2017-11-30 stsp done:
459 789689b5 2017-11-30 stsp if (tree1)
460 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
461 789689b5 2017-11-30 stsp if (tree2)
462 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
463 789689b5 2017-11-30 stsp if (treeobj1)
464 789689b5 2017-11-30 stsp got_object_close(treeobj1);
465 789689b5 2017-11-30 stsp if (treeobj2)
466 789689b5 2017-11-30 stsp got_object_close(treeobj2);
467 789689b5 2017-11-30 stsp return err;
468 474b4f94 2017-11-30 stsp }
469 474b4f94 2017-11-30 stsp
470 474b4f94 2017-11-30 stsp static const struct got_error *
471 a0f32f33 2022-06-13 thomas diff_deleted_tree(struct got_object_id *id, FILE *f, const char *label,
472 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
473 31b4484f 2019-07-27 stsp int diff_content)
474 474b4f94 2017-11-30 stsp {
475 f6861a81 2018-09-13 stsp const struct got_error *err;
476 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
477 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
478 2c56f2ce 2017-11-30 stsp
479 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
480 2c56f2ce 2017-11-30 stsp if (err)
481 2c56f2ce 2017-11-30 stsp goto done;
482 2c56f2ce 2017-11-30 stsp
483 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
484 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
485 2c56f2ce 2017-11-30 stsp goto done;
486 2c56f2ce 2017-11-30 stsp }
487 2c56f2ce 2017-11-30 stsp
488 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
489 2c56f2ce 2017-11-30 stsp if (err)
490 2c56f2ce 2017-11-30 stsp goto done;
491 2c56f2ce 2017-11-30 stsp
492 a0f32f33 2022-06-13 thomas err = got_diff_tree(tree, NULL, f, NULL, label, NULL,
493 a0f32f33 2022-06-13 thomas repo, cb, cb_arg, diff_content);
494 2c56f2ce 2017-11-30 stsp done:
495 2c56f2ce 2017-11-30 stsp if (tree)
496 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
497 2c56f2ce 2017-11-30 stsp if (treeobj)
498 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
499 2c56f2ce 2017-11-30 stsp return err;
500 474b4f94 2017-11-30 stsp }
501 474b4f94 2017-11-30 stsp
502 474b4f94 2017-11-30 stsp static const struct got_error *
503 74671950 2018-02-11 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
504 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
505 aaa13589 2019-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
506 474b4f94 2017-11-30 stsp {
507 013404a9 2017-11-30 stsp /* XXX TODO */
508 474b4f94 2017-11-30 stsp return NULL;
509 474b4f94 2017-11-30 stsp }
510 474b4f94 2017-11-30 stsp
511 474b4f94 2017-11-30 stsp static const struct got_error *
512 a0f32f33 2022-06-13 thomas diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
513 a0f32f33 2022-06-13 thomas FILE *f1, FILE *f2, const char *label1, const char *label2,
514 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
515 31b4484f 2019-07-27 stsp int diff_content)
516 474b4f94 2017-11-30 stsp {
517 f6861a81 2018-09-13 stsp const struct got_error *err = NULL;
518 19ae6da1 2018-11-05 stsp int id_match;
519 63c5ca5d 2019-08-24 stsp
520 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te1))
521 63c5ca5d 2019-08-24 stsp return NULL;
522 474b4f94 2017-11-30 stsp
523 474b4f94 2017-11-30 stsp if (te2 == NULL) {
524 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
525 a0f32f33 2022-06-13 thomas err = diff_deleted_tree(&te1->id, f1, label1, repo,
526 31b4484f 2019-07-27 stsp cb, cb_arg, diff_content);
527 31b4484f 2019-07-27 stsp else {
528 31b4484f 2019-07-27 stsp if (diff_content)
529 a0f32f33 2022-06-13 thomas err = diff_deleted_blob(&te1->id, f1, label1,
530 46f68b20 2019-10-19 stsp te1->mode, repo, cb, cb_arg);
531 31b4484f 2019-07-27 stsp else
532 a0f32f33 2022-06-13 thomas err = cb(cb_arg, NULL, NULL, NULL, NULL,
533 a0f32f33 2022-06-13 thomas &te1->id, NULL, label1, NULL,
534 a0f32f33 2022-06-13 thomas te1->mode, 0, repo);
535 31b4484f 2019-07-27 stsp }
536 f6861a81 2018-09-13 stsp return err;
537 63c5ca5d 2019-08-24 stsp } else if (got_object_tree_entry_is_submodule(te2))
538 63c5ca5d 2019-08-24 stsp return NULL;
539 474b4f94 2017-11-30 stsp
540 56e0773d 2019-11-28 stsp id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
541 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
542 19ae6da1 2018-11-05 stsp if (!id_match)
543 a0f32f33 2022-06-13 thomas return diff_modified_tree(&te1->id, &te2->id, f1, f2,
544 31b4484f 2019-07-27 stsp label1, label2, repo, cb, cb_arg, 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 a0f32f33 2022-06-13 thomas f1, f2, label1, label2, te1->mode,
553 a0f32f33 2022-06-13 thomas 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 a0f32f33 2022-06-13 thomas struct got_tree_entry *te1, FILE *f2, 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 a0f32f33 2022-06-13 thomas return diff_added_tree(&te2->id, f2, 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 a0f32f33 2022-06-13 thomas return diff_added_blob(&te2->id, f2, label2, te2->mode,
586 a0f32f33 2022-06-13 thomas 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 a0f32f33 2022-06-13 thomas FILE *f1, FILE *f2, const char *label1, const char *label2,
638 a0f32f33 2022-06-13 thomas struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
639 a0f32f33 2022-06-13 thomas int diff_content)
640 474b4f94 2017-11-30 stsp {
641 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
642 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
643 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
644 f6861a81 2018-09-13 stsp char *l1 = NULL, *l2 = NULL;
645 56e0773d 2019-11-28 stsp int tidx1 = 0, tidx2 = 0;
646 474b4f94 2017-11-30 stsp
647 883f0469 2018-06-23 stsp if (tree1) {
648 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, 0);
649 60f50a58 2018-09-15 stsp if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
650 f6861a81 2018-09-13 stsp te1->name) == -1)
651 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
652 883f0469 2018-06-23 stsp }
653 883f0469 2018-06-23 stsp if (tree2) {
654 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, 0);
655 60f50a58 2018-09-15 stsp if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
656 f6861a81 2018-09-13 stsp te2->name) == -1)
657 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
658 883f0469 2018-06-23 stsp }
659 474b4f94 2017-11-30 stsp
660 474b4f94 2017-11-30 stsp do {
661 474b4f94 2017-11-30 stsp if (te1) {
662 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
663 f6861a81 2018-09-13 stsp if (tree2)
664 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree2,
665 1de5e065 2019-06-01 stsp te1->name);
666 f6861a81 2018-09-13 stsp if (te) {
667 f6861a81 2018-09-13 stsp free(l2);
668 f6861a81 2018-09-13 stsp l2 = NULL;
669 f6861a81 2018-09-13 stsp if (te && asprintf(&l2, "%s%s%s", label2,
670 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te->name) == -1)
671 230a42bd 2019-05-11 jcs return
672 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
673 f6861a81 2018-09-13 stsp }
674 a0f32f33 2022-06-13 thomas err = diff_entry_old_new(te1, te, f1, f2, l1, l2,
675 a0f32f33 2022-06-13 thomas repo, cb, cb_arg, diff_content);
676 474b4f94 2017-11-30 stsp if (err)
677 474b4f94 2017-11-30 stsp break;
678 474b4f94 2017-11-30 stsp }
679 474b4f94 2017-11-30 stsp
680 474b4f94 2017-11-30 stsp if (te2) {
681 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
682 f6861a81 2018-09-13 stsp if (tree1)
683 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree1,
684 1de5e065 2019-06-01 stsp te2->name);
685 d6ce02f1 2018-11-17 stsp free(l2);
686 d6ce02f1 2018-11-17 stsp if (te) {
687 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
688 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te->name) == -1)
689 230a42bd 2019-05-11 jcs return
690 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
691 d6ce02f1 2018-11-17 stsp } else {
692 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
693 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te2->name) == -1)
694 230a42bd 2019-05-11 jcs return
695 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
696 d6ce02f1 2018-11-17 stsp }
697 a0f32f33 2022-06-13 thomas err = diff_entry_new_old(te2, te, f2, l2, repo,
698 31b4484f 2019-07-27 stsp cb, cb_arg, diff_content);
699 474b4f94 2017-11-30 stsp if (err)
700 474b4f94 2017-11-30 stsp break;
701 474b4f94 2017-11-30 stsp }
702 474b4f94 2017-11-30 stsp
703 f6861a81 2018-09-13 stsp free(l1);
704 f6861a81 2018-09-13 stsp l1 = NULL;
705 f6861a81 2018-09-13 stsp if (te1) {
706 56e0773d 2019-11-28 stsp tidx1++;
707 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, tidx1);
708 f6861a81 2018-09-13 stsp if (te1 &&
709 f6861a81 2018-09-13 stsp asprintf(&l1, "%s%s%s", label1,
710 f6861a81 2018-09-13 stsp label1[0] ? "/" : "", te1->name) == -1)
711 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
712 f6861a81 2018-09-13 stsp }
713 f6861a81 2018-09-13 stsp free(l2);
714 f6861a81 2018-09-13 stsp l2 = NULL;
715 f6861a81 2018-09-13 stsp if (te2) {
716 56e0773d 2019-11-28 stsp tidx2++;
717 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, tidx2);
718 f6861a81 2018-09-13 stsp if (te2 &&
719 f6861a81 2018-09-13 stsp asprintf(&l2, "%s%s%s", label2,
720 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te2->name) == -1)
721 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
722 f6861a81 2018-09-13 stsp }
723 474b4f94 2017-11-30 stsp } while (te1 || te2);
724 11528a82 2018-05-19 stsp
725 11528a82 2018-05-19 stsp return err;
726 11528a82 2018-05-19 stsp }
727 11528a82 2018-05-19 stsp
728 11528a82 2018-05-19 stsp const struct got_error *
729 fe621944 2020-11-10 stsp got_diff_objects_as_blobs(off_t **line_offsets, size_t *nlines,
730 a0f32f33 2022-06-13 thomas FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
731 54156555 2018-12-24 stsp const char *label1, const char *label2, int diff_context,
732 64453f7e 2020-11-21 stsp int ignore_whitespace, int force_text_diff,
733 64453f7e 2020-11-21 stsp struct got_repository *repo, FILE *outfile)
734 11528a82 2018-05-19 stsp {
735 11528a82 2018-05-19 stsp const struct got_error *err;
736 11528a82 2018-05-19 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
737 b74c7625 2018-05-20 stsp
738 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
739 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
740 11528a82 2018-05-19 stsp
741 15a94983 2018-12-23 stsp if (id1) {
742 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob1, repo, id1, 8192);
743 cd0acaa7 2018-05-20 stsp if (err)
744 cd0acaa7 2018-05-20 stsp goto done;
745 cd0acaa7 2018-05-20 stsp }
746 15a94983 2018-12-23 stsp if (id2) {
747 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob2, repo, id2, 8192);
748 cd0acaa7 2018-05-20 stsp if (err)
749 cd0acaa7 2018-05-20 stsp goto done;
750 cd0acaa7 2018-05-20 stsp }
751 a0f32f33 2022-06-13 thomas err = got_diff_blob(line_offsets, nlines, blob1, blob2, f1, f2,
752 64453f7e 2020-11-21 stsp label1, label2, diff_context, ignore_whitespace, force_text_diff,
753 64453f7e 2020-11-21 stsp outfile);
754 cc8021af 2021-10-12 thomas done:
755 cc8021af 2021-10-12 thomas if (blob1)
756 cc8021af 2021-10-12 thomas got_object_blob_close(blob1);
757 cc8021af 2021-10-12 thomas if (blob2)
758 cc8021af 2021-10-12 thomas got_object_blob_close(blob2);
759 cc8021af 2021-10-12 thomas return err;
760 cc8021af 2021-10-12 thomas }
761 cc8021af 2021-10-12 thomas
762 cc8021af 2021-10-12 thomas static const struct got_error *
763 cc8021af 2021-10-12 thomas diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
764 a0f32f33 2022-06-13 thomas FILE *f1, FILE *f2, struct got_pathlist_head *paths,
765 a0f32f33 2022-06-13 thomas struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
766 cc8021af 2021-10-12 thomas {
767 cc8021af 2021-10-12 thomas const struct got_error *err = NULL;
768 cc8021af 2021-10-12 thomas struct got_pathlist_entry *pe;
769 cc8021af 2021-10-12 thomas struct got_object_id *id1 = NULL, *id2 = NULL;
770 cc8021af 2021-10-12 thomas struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
771 cc8021af 2021-10-12 thomas struct got_blob_object *blob1 = NULL, *blob2 = NULL;
772 cc8021af 2021-10-12 thomas
773 cc8021af 2021-10-12 thomas TAILQ_FOREACH(pe, paths, entry) {
774 cc8021af 2021-10-12 thomas int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
775 cc8021af 2021-10-12 thomas mode_t mode1 = 0, mode2 = 0;
776 cc8021af 2021-10-12 thomas
777 cc8021af 2021-10-12 thomas free(id1);
778 cc8021af 2021-10-12 thomas id1 = NULL;
779 cc8021af 2021-10-12 thomas free(id2);
780 cc8021af 2021-10-12 thomas id2 = NULL;
781 cc8021af 2021-10-12 thomas if (subtree1) {
782 cc8021af 2021-10-12 thomas got_object_tree_close(subtree1);
783 cc8021af 2021-10-12 thomas subtree1 = NULL;
784 cc8021af 2021-10-12 thomas }
785 cc8021af 2021-10-12 thomas if (subtree2) {
786 cc8021af 2021-10-12 thomas got_object_tree_close(subtree2);
787 cc8021af 2021-10-12 thomas subtree2 = NULL;
788 cc8021af 2021-10-12 thomas }
789 cc8021af 2021-10-12 thomas if (blob1) {
790 cc8021af 2021-10-12 thomas got_object_blob_close(blob1);
791 cc8021af 2021-10-12 thomas blob1 = NULL;
792 cc8021af 2021-10-12 thomas }
793 cc8021af 2021-10-12 thomas if (blob2) {
794 cc8021af 2021-10-12 thomas got_object_blob_close(blob2);
795 cc8021af 2021-10-12 thomas blob2 = NULL;
796 cc8021af 2021-10-12 thomas }
797 cc8021af 2021-10-12 thomas
798 cc8021af 2021-10-12 thomas err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
799 cc8021af 2021-10-12 thomas pe->path);
800 cc8021af 2021-10-12 thomas if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
801 cc8021af 2021-10-12 thomas goto done;
802 cc8021af 2021-10-12 thomas err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
803 cc8021af 2021-10-12 thomas pe->path);
804 cc8021af 2021-10-12 thomas if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
805 cc8021af 2021-10-12 thomas goto done;
806 cc8021af 2021-10-12 thomas if (id1 == NULL && id2 == NULL) {
807 cc8021af 2021-10-12 thomas err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
808 cc8021af 2021-10-12 thomas goto done;
809 cc8021af 2021-10-12 thomas }
810 cc8021af 2021-10-12 thomas if (id1) {
811 cc8021af 2021-10-12 thomas err = got_object_get_type(&type1, repo, id1);
812 cc8021af 2021-10-12 thomas if (err)
813 cc8021af 2021-10-12 thomas goto done;
814 cc8021af 2021-10-12 thomas }
815 cc8021af 2021-10-12 thomas if (id2) {
816 cc8021af 2021-10-12 thomas err = got_object_get_type(&type2, repo, id2);
817 cc8021af 2021-10-12 thomas if (err)
818 cc8021af 2021-10-12 thomas goto done;
819 cc8021af 2021-10-12 thomas }
820 cc8021af 2021-10-12 thomas if (type1 == GOT_OBJ_TYPE_ANY &&
821 cc8021af 2021-10-12 thomas type2 == GOT_OBJ_TYPE_ANY) {
822 cc8021af 2021-10-12 thomas err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
823 cc8021af 2021-10-12 thomas goto done;
824 cc8021af 2021-10-12 thomas } else if (type1 != GOT_OBJ_TYPE_ANY &&
825 cc8021af 2021-10-12 thomas type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
826 cc8021af 2021-10-12 thomas err = got_error(GOT_ERR_OBJ_TYPE);
827 cc8021af 2021-10-12 thomas goto done;
828 cc8021af 2021-10-12 thomas }
829 cc8021af 2021-10-12 thomas
830 cc8021af 2021-10-12 thomas if (type1 == GOT_OBJ_TYPE_BLOB ||
831 cc8021af 2021-10-12 thomas type2 == GOT_OBJ_TYPE_BLOB) {
832 cc8021af 2021-10-12 thomas if (id1) {
833 cc8021af 2021-10-12 thomas err = got_object_open_as_blob(&blob1, repo,
834 cc8021af 2021-10-12 thomas id1, 8192);
835 cc8021af 2021-10-12 thomas if (err)
836 cc8021af 2021-10-12 thomas goto done;
837 cc8021af 2021-10-12 thomas }
838 cc8021af 2021-10-12 thomas if (id2) {
839 cc8021af 2021-10-12 thomas err = got_object_open_as_blob(&blob2, repo,
840 cc8021af 2021-10-12 thomas id2, 8192);
841 cc8021af 2021-10-12 thomas if (err)
842 cc8021af 2021-10-12 thomas goto done;
843 cc8021af 2021-10-12 thomas }
844 a0f32f33 2022-06-13 thomas err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
845 cc8021af 2021-10-12 thomas id1 ? pe->path : "/dev/null",
846 cc8021af 2021-10-12 thomas id2 ? pe->path : "/dev/null",
847 cc8021af 2021-10-12 thomas mode1, mode2, repo);
848 cc8021af 2021-10-12 thomas if (err)
849 cc8021af 2021-10-12 thomas goto done;
850 cc8021af 2021-10-12 thomas } else if (type1 == GOT_OBJ_TYPE_TREE ||
851 cc8021af 2021-10-12 thomas type2 == GOT_OBJ_TYPE_TREE) {
852 cc8021af 2021-10-12 thomas if (id1) {
853 cc8021af 2021-10-12 thomas err = got_object_open_as_tree(&subtree1, repo,
854 cc8021af 2021-10-12 thomas id1);
855 cc8021af 2021-10-12 thomas if (err)
856 cc8021af 2021-10-12 thomas goto done;
857 cc8021af 2021-10-12 thomas }
858 cc8021af 2021-10-12 thomas if (id2) {
859 cc8021af 2021-10-12 thomas err = got_object_open_as_tree(&subtree2, repo,
860 cc8021af 2021-10-12 thomas id2);
861 cc8021af 2021-10-12 thomas if (err)
862 cc8021af 2021-10-12 thomas goto done;
863 cc8021af 2021-10-12 thomas }
864 a0f32f33 2022-06-13 thomas err = got_diff_tree(subtree1, subtree2, f1, f2,
865 cc8021af 2021-10-12 thomas id1 ? pe->path : "/dev/null",
866 cc8021af 2021-10-12 thomas id2 ? pe->path : "/dev/null",
867 cc8021af 2021-10-12 thomas repo, cb, cb_arg, 1);
868 cc8021af 2021-10-12 thomas if (err)
869 cc8021af 2021-10-12 thomas goto done;
870 cc8021af 2021-10-12 thomas } else {
871 cc8021af 2021-10-12 thomas err = got_error(GOT_ERR_OBJ_TYPE);
872 cc8021af 2021-10-12 thomas goto done;
873 cc8021af 2021-10-12 thomas }
874 cc8021af 2021-10-12 thomas }
875 11528a82 2018-05-19 stsp done:
876 cc8021af 2021-10-12 thomas free(id1);
877 cc8021af 2021-10-12 thomas free(id2);
878 cc8021af 2021-10-12 thomas if (subtree1)
879 cc8021af 2021-10-12 thomas got_object_tree_close(subtree1);
880 cc8021af 2021-10-12 thomas if (subtree2)
881 cc8021af 2021-10-12 thomas got_object_tree_close(subtree2);
882 11528a82 2018-05-19 stsp if (blob1)
883 11528a82 2018-05-19 stsp got_object_blob_close(blob1);
884 11528a82 2018-05-19 stsp if (blob2)
885 11528a82 2018-05-19 stsp got_object_blob_close(blob2);
886 474b4f94 2017-11-30 stsp return err;
887 474b4f94 2017-11-30 stsp }
888 11528a82 2018-05-19 stsp
889 11528a82 2018-05-19 stsp const struct got_error *
890 fe621944 2020-11-10 stsp got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
891 a0f32f33 2022-06-13 thomas FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
892 cc8021af 2021-10-12 thomas struct got_pathlist_head *paths,
893 63035f9f 2019-10-06 stsp char *label1, char *label2, int diff_context, int ignore_whitespace,
894 64453f7e 2020-11-21 stsp int force_text_diff, struct got_repository *repo, FILE *outfile)
895 11528a82 2018-05-19 stsp {
896 11528a82 2018-05-19 stsp const struct got_error *err;
897 11528a82 2018-05-19 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
898 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
899 fe621944 2020-11-10 stsp int want_lineoffsets = (line_offsets != NULL && *line_offsets != NULL);
900 11528a82 2018-05-19 stsp
901 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
902 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
903 b74c7625 2018-05-20 stsp
904 15a94983 2018-12-23 stsp if (id1) {
905 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo, id1);
906 cd0acaa7 2018-05-20 stsp if (err)
907 cd0acaa7 2018-05-20 stsp goto done;
908 cd0acaa7 2018-05-20 stsp }
909 15a94983 2018-12-23 stsp if (id2) {
910 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo, id2);
911 cd0acaa7 2018-05-20 stsp if (err)
912 cd0acaa7 2018-05-20 stsp goto done;
913 cd0acaa7 2018-05-20 stsp }
914 cc8021af 2021-10-12 thomas
915 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
916 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
917 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
918 aaa13589 2019-06-01 stsp arg.outfile = outfile;
919 fe621944 2020-11-10 stsp if (want_lineoffsets) {
920 fe621944 2020-11-10 stsp arg.line_offsets = *line_offsets;
921 fe621944 2020-11-10 stsp arg.nlines = *nlines;
922 fe621944 2020-11-10 stsp } else {
923 fe621944 2020-11-10 stsp arg.line_offsets = NULL;
924 fe621944 2020-11-10 stsp arg.nlines = 0;
925 fe621944 2020-11-10 stsp }
926 cc8021af 2021-10-12 thomas if (paths == NULL || TAILQ_EMPTY(paths)) {
927 a0f32f33 2022-06-13 thomas err = got_diff_tree(tree1, tree2, f1, f2, label1, label2,
928 a0f32f33 2022-06-13 thomas repo, got_diff_blob_output_unidiff, &arg, 1);
929 cc8021af 2021-10-12 thomas } else {
930 a0f32f33 2022-06-13 thomas err = diff_paths(tree1, tree2, f1, f2, paths, repo,
931 cc8021af 2021-10-12 thomas got_diff_blob_output_unidiff, &arg);
932 cc8021af 2021-10-12 thomas }
933 fe621944 2020-11-10 stsp if (want_lineoffsets) {
934 fe621944 2020-11-10 stsp *line_offsets = arg.line_offsets; /* was likely re-allocated */
935 fe621944 2020-11-10 stsp *nlines = arg.nlines;
936 fe621944 2020-11-10 stsp }
937 11528a82 2018-05-19 stsp done:
938 11528a82 2018-05-19 stsp if (tree1)
939 11528a82 2018-05-19 stsp got_object_tree_close(tree1);
940 11528a82 2018-05-19 stsp if (tree2)
941 11528a82 2018-05-19 stsp got_object_tree_close(tree2);
942 11528a82 2018-05-19 stsp return err;
943 11528a82 2018-05-19 stsp }
944 11528a82 2018-05-19 stsp
945 11528a82 2018-05-19 stsp const struct got_error *
946 fe621944 2020-11-10 stsp got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
947 a0f32f33 2022-06-13 thomas FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
948 cc8021af 2021-10-12 thomas struct got_pathlist_head *paths,
949 64453f7e 2020-11-21 stsp int diff_context, int ignore_whitespace, int force_text_diff,
950 15a94983 2018-12-23 stsp struct got_repository *repo, FILE *outfile)
951 11528a82 2018-05-19 stsp {
952 11528a82 2018-05-19 stsp const struct got_error *err;
953 11528a82 2018-05-19 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
954 11528a82 2018-05-19 stsp
955 15a94983 2018-12-23 stsp if (id2 == NULL)
956 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
957 b74c7625 2018-05-20 stsp
958 15a94983 2018-12-23 stsp if (id1) {
959 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit1, repo, id1);
960 cd0acaa7 2018-05-20 stsp if (err)
961 cd0acaa7 2018-05-20 stsp goto done;
962 cd0acaa7 2018-05-20 stsp }
963 bacc9935 2018-05-20 stsp
964 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, repo, id2);
965 9b697879 2018-05-20 stsp if (err)
966 9b697879 2018-05-20 stsp goto done;
967 9b697879 2018-05-20 stsp
968 a0f32f33 2022-06-13 thomas err = got_diff_objects_as_trees(line_offsets, nlines, f1, f2,
969 15a94983 2018-12-23 stsp commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
970 cc8021af 2021-10-12 thomas got_object_commit_get_tree_id(commit2), paths, "", "",
971 cc8021af 2021-10-12 thomas diff_context, ignore_whitespace, force_text_diff, repo, outfile);
972 11528a82 2018-05-19 stsp done:
973 11528a82 2018-05-19 stsp if (commit1)
974 11528a82 2018-05-19 stsp got_object_commit_close(commit1);
975 11528a82 2018-05-19 stsp if (commit2)
976 11528a82 2018-05-19 stsp got_object_commit_close(commit2);
977 11528a82 2018-05-19 stsp return err;
978 11528a82 2018-05-19 stsp }
979 dc424a06 2019-08-07 stsp
980 dc424a06 2019-08-07 stsp const struct got_error *
981 fe621944 2020-11-10 stsp got_diff_files(struct got_diffreg_result **resultp,
982 fe621944 2020-11-10 stsp FILE *f1, const char *label1, FILE *f2, const char *label2,
983 64453f7e 2020-11-21 stsp int diff_context, int ignore_whitespace, int force_text_diff,
984 64453f7e 2020-11-21 stsp FILE *outfile)
985 dc424a06 2019-08-07 stsp {
986 dc424a06 2019-08-07 stsp const struct got_error *err = NULL;
987 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
988 dc424a06 2019-08-07 stsp
989 fe621944 2020-11-10 stsp if (resultp)
990 fe621944 2020-11-10 stsp *resultp = NULL;
991 dc424a06 2019-08-07 stsp
992 dc424a06 2019-08-07 stsp if (outfile) {
993 dc424a06 2019-08-07 stsp fprintf(outfile, "file - %s\n",
994 dc424a06 2019-08-07 stsp f1 == NULL ? "/dev/null" : label1);
995 dc424a06 2019-08-07 stsp fprintf(outfile, "file + %s\n",
996 dc424a06 2019-08-07 stsp f2 == NULL ? "/dev/null" : label2);
997 dc424a06 2019-08-07 stsp }
998 fe621944 2020-11-10 stsp
999 fe621944 2020-11-10 stsp err = got_diffreg(&diffreg_result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
1000 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
1001 fe621944 2020-11-10 stsp if (err)
1002 fe621944 2020-11-10 stsp goto done;
1003 fe621944 2020-11-10 stsp
1004 fe621944 2020-11-10 stsp if (outfile) {
1005 fe621944 2020-11-10 stsp err = got_diffreg_output(NULL, NULL, diffreg_result,
1006 1cb46f00 2020-11-21 stsp f1 != NULL, f2 != NULL, label1, label2,
1007 1cb46f00 2020-11-21 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
1008 dc424a06 2019-08-07 stsp if (err)
1009 dc424a06 2019-08-07 stsp goto done;
1010 dc424a06 2019-08-07 stsp }
1011 fe621944 2020-11-10 stsp
1012 dc424a06 2019-08-07 stsp done:
1013 fe621944 2020-11-10 stsp if (resultp && err == NULL)
1014 fe621944 2020-11-10 stsp *resultp = diffreg_result;
1015 fe621944 2020-11-10 stsp else if (diffreg_result) {
1016 fe621944 2020-11-10 stsp const struct got_error *free_err;
1017 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
1018 fe621944 2020-11-10 stsp if (free_err && err == NULL)
1019 fe621944 2020-11-10 stsp err = free_err;
1020 dc424a06 2019-08-07 stsp }
1021 fe621944 2020-11-10 stsp
1022 dc424a06 2019-08-07 stsp return err;
1023 dc424a06 2019-08-07 stsp }