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