Blame


1 7d283eee 2017-11-29 stsp /*
2 7d283eee 2017-11-29 stsp * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
3 7d283eee 2017-11-29 stsp *
4 7d283eee 2017-11-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 7d283eee 2017-11-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 7d283eee 2017-11-29 stsp * copyright notice and this permission notice appear in all copies.
7 7d283eee 2017-11-29 stsp *
8 7d283eee 2017-11-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 7d283eee 2017-11-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 7d283eee 2017-11-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 7d283eee 2017-11-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 7d283eee 2017-11-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 7d283eee 2017-11-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 7d283eee 2017-11-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 7d283eee 2017-11-29 stsp */
16 7d283eee 2017-11-29 stsp
17 7d283eee 2017-11-29 stsp #include <sys/queue.h>
18 1c7f0520 2017-11-29 stsp #include <sys/stat.h>
19 7d283eee 2017-11-29 stsp
20 7d283eee 2017-11-29 stsp #include <stdio.h>
21 7d283eee 2017-11-29 stsp #include <stdlib.h>
22 7d283eee 2017-11-29 stsp #include <string.h>
23 f9d67749 2017-11-30 stsp #include <limits.h>
24 7d283eee 2017-11-29 stsp #include <sha1.h>
25 7d283eee 2017-11-29 stsp #include <zlib.h>
26 7d283eee 2017-11-29 stsp
27 7d283eee 2017-11-29 stsp #include "got_object.h"
28 e09a504c 2019-06-28 stsp #include "got_repository.h"
29 7d283eee 2017-11-29 stsp #include "got_error.h"
30 789689b5 2017-11-30 stsp #include "got_diff.h"
31 324d37e7 2019-05-11 stsp #include "got_path.h"
32 0208f208 2020-05-05 stsp #include "got_cancel.h"
33 0208f208 2020-05-05 stsp #include "got_worktree.h"
34 a558dd1b 2022-06-08 stsp #include "got_opentemp.h"
35 7d283eee 2017-11-29 stsp
36 718b3ab0 2018-03-17 stsp #include "got_lib_diff.h"
37 15a94983 2018-12-23 stsp #include "got_lib_delta.h"
38 15a94983 2018-12-23 stsp #include "got_lib_inflate.h"
39 15a94983 2018-12-23 stsp #include "got_lib_object.h"
40 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 b72706c3 2022-06-01 stsp 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 58e31a80 2022-06-27 op 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 b72706c3 2022-06-01 stsp if (f1) {
83 a558dd1b 2022-06-08 stsp err = got_opentemp_truncate(f1);
84 b72706c3 2022-06-01 stsp if (err)
85 b72706c3 2022-06-01 stsp goto done;
86 fe621944 2020-11-10 stsp }
87 b72706c3 2022-06-01 stsp if (f2) {
88 a558dd1b 2022-06-08 stsp err = got_opentemp_truncate(f2);
89 b72706c3 2022-06-01 stsp if (err)
90 b72706c3 2022-06-01 stsp 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 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
192 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
193 b72706c3 2022-06-01 stsp const char *label1, const char *label2, mode_t mode1, mode_t mode2,
194 b72706c3 2022-06-01 stsp 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 b72706c3 2022-06-01 stsp 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 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, const char *label1, const char *label2,
207 b72706c3 2022-06-01 stsp int diff_context, int ignore_whitespace, int force_text_diff,
208 b72706c3 2022-06-01 stsp FILE *outfile)
209 404c43c4 2018-06-21 stsp {
210 b72706c3 2022-06-01 stsp 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 b72706c3 2022-06-01 stsp struct got_blob_object *blob1, FILE *f1, off_t size1, const char *label1,
218 b72706c3 2022-06-01 stsp FILE *f2, size_t size2, const char *label2, int diff_context,
219 b72706c3 2022-06-01 stsp 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 58e31a80 2022-06-27 op 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 b72706c3 2022-06-01 stsp if (blob1)
230 b72f483a 2019-02-05 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
231 b72706c3 2022-06-01 stsp 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 b72706c3 2022-06-01 stsp 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 b72706c3 2022-06-01 stsp got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
268 b72706c3 2022-06-01 stsp const char *label1, FILE *f2, size_t size2, const char *label2,
269 b72706c3 2022-06-01 stsp int diff_context, int ignore_whitespace, int force_text_diff, FILE *outfile)
270 7f1f93af 2019-08-06 stsp {
271 b72706c3 2022-06-01 stsp 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 b72706c3 2022-06-01 stsp diff_added_blob(struct got_object_id *id, FILE *f, const char *label,
277 b72706c3 2022-06-01 stsp mode_t mode, struct got_repository *repo,
278 b72706c3 2022-06-01 stsp 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 eb81bc23 2022-06-28 tracey 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 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
290 eb81bc23 2022-06-28 tracey if (fd == -1) {
291 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
292 eb81bc23 2022-06-28 tracey goto done;
293 eb81bc23 2022-06-28 tracey }
294 eb81bc23 2022-06-28 tracey
295 eb81bc23 2022-06-28 tracey 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 b72706c3 2022-06-01 stsp err = cb(cb_arg, NULL, blob, NULL, f, NULL, id,
299 b72706c3 2022-06-01 stsp NULL, label, 0, mode, repo);
300 2acfca77 2018-04-01 stsp done:
301 2acfca77 2018-04-01 stsp got_object_close(obj);
302 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
303 eb81bc23 2022-06-28 tracey 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 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, const char *label1, const char *label2,
312 b72706c3 2022-06-01 stsp mode_t mode1, mode_t mode2, struct got_repository *repo,
313 b72706c3 2022-06-01 stsp 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 eb81bc23 2022-06-28 tracey 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 eb81bc23 2022-06-28 tracey
326 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
327 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
328 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
329 eb81bc23 2022-06-28 tracey goto done;
330 eb81bc23 2022-06-28 tracey }
331 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
332 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
333 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
334 eb81bc23 2022-06-28 tracey goto done;
335 eb81bc23 2022-06-28 tracey }
336 eb81bc23 2022-06-28 tracey
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 eb81bc23 2022-06-28 tracey 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 eb81bc23 2022-06-28 tracey 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 b72706c3 2022-06-01 stsp err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2, label1, label2,
359 b72706c3 2022-06-01 stsp 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 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
366 eb81bc23 2022-06-28 tracey 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 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
370 eb81bc23 2022-06-28 tracey 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 b72706c3 2022-06-01 stsp diff_deleted_blob(struct got_object_id *id, FILE *f, const char *label,
378 b72706c3 2022-06-01 stsp 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 eb81bc23 2022-06-28 tracey int fd = -1;
384 365fb436 2017-11-30 stsp
385 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
386 eb81bc23 2022-06-28 tracey if (fd == -1)
387 eb81bc23 2022-06-28 tracey return got_error_from_errno("got_opentempfd");
388 eb81bc23 2022-06-28 tracey
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 eb81bc23 2022-06-28 tracey 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 b72706c3 2022-06-01 stsp err = cb(cb_arg, blob, NULL, f, NULL, id, NULL, label, NULL,
397 b72706c3 2022-06-01 stsp mode, 0, repo);
398 2acfca77 2018-04-01 stsp done:
399 2acfca77 2018-04-01 stsp got_object_close(obj);
400 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
401 eb81bc23 2022-06-28 tracey 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 b72706c3 2022-06-01 stsp 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 b72706c3 2022-06-01 stsp err = got_diff_tree(NULL, tree, NULL, f, NULL, label,
430 b72706c3 2022-06-01 stsp 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 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, const char *label1, const char *label2,
442 b72706c3 2022-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
443 b72706c3 2022-06-01 stsp 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 b72706c3 2022-06-01 stsp err = got_diff_tree(tree1, tree2, f1, f2, label1, label2,
478 b72706c3 2022-06-01 stsp 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 b72706c3 2022-06-01 stsp 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 b72706c3 2022-06-01 stsp err = got_diff_tree(tree, NULL, f, NULL, label, NULL,
515 b72706c3 2022-06-01 stsp 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 b72706c3 2022-06-01 stsp diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
535 b72706c3 2022-06-01 stsp 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 b72706c3 2022-06-01 stsp 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 b72706c3 2022-06-01 stsp 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 b72706c3 2022-06-01 stsp err = cb(cb_arg, NULL, NULL, NULL, NULL,
555 b72706c3 2022-06-01 stsp &te1->id, NULL, label1, NULL,
556 b72706c3 2022-06-01 stsp 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 b72706c3 2022-06-01 stsp 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 b72706c3 2022-06-01 stsp f1, f2, label1, label2, te1->mode,
575 b72706c3 2022-06-01 stsp te2->mode, repo, cb, cb_arg);
576 31b4484f 2019-07-27 stsp else
577 b72706c3 2022-06-01 stsp return cb(cb_arg, NULL, NULL, NULL, NULL,
578 b72706c3 2022-06-01 stsp &te1->id, &te2->id, label1, label2,
579 b72706c3 2022-06-01 stsp te1->mode, te2->mode, repo);
580 31b4484f 2019-07-27 stsp }
581 413ea19d 2017-11-30 stsp }
582 474b4f94 2017-11-30 stsp
583 19ae6da1 2018-11-05 stsp if (id_match)
584 f6861a81 2018-09-13 stsp return NULL;
585 f6861a81 2018-09-13 stsp
586 56e0773d 2019-11-28 stsp return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
587 aaa13589 2019-06-01 stsp cb, cb_arg);
588 474b4f94 2017-11-30 stsp }
589 474b4f94 2017-11-30 stsp
590 474b4f94 2017-11-30 stsp static const struct got_error *
591 56e0773d 2019-11-28 stsp diff_entry_new_old(struct got_tree_entry *te2,
592 b72706c3 2022-06-01 stsp 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 b72706c3 2022-06-01 stsp return diff_added_tree(&te2->id, f2, label2,
604 b72706c3 2022-06-01 stsp repo, cb, cb_arg, diff_content);
605 f6861a81 2018-09-13 stsp
606 31b4484f 2019-07-27 stsp if (diff_content)
607 b72706c3 2022-06-01 stsp return diff_added_blob(&te2->id, f2, label2, te2->mode,
608 b72706c3 2022-06-01 stsp repo, cb, cb_arg);
609 31b4484f 2019-07-27 stsp
610 b72706c3 2022-06-01 stsp return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
611 b72706c3 2022-06-01 stsp NULL, label2, 0, te2->mode, repo);
612 0208f208 2020-05-05 stsp }
613 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 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
617 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
618 b72706c3 2022-06-01 stsp 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 46ea77db 2021-12-20 stsp 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 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, const char *label1, const char *label2,
660 b72706c3 2022-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
661 b72706c3 2022-06-01 stsp 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 b72706c3 2022-06-01 stsp err = diff_entry_old_new(te1, te, f1, f2, l1, l2,
697 b72706c3 2022-06-01 stsp 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 b72706c3 2022-06-01 stsp 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 b72706c3 2022-06-01 stsp 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 eb81bc23 2022-06-28 tracey 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 eb81bc23 2022-06-28 tracey
764 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
765 eb81bc23 2022-06-28 tracey if (fd1 == -1)
766 eb81bc23 2022-06-28 tracey return got_error_from_errno("got_opentempfd");
767 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
768 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
769 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
770 eb81bc23 2022-06-28 tracey goto done;
771 eb81bc23 2022-06-28 tracey }
772 11528a82 2018-05-19 stsp
773 15a94983 2018-12-23 stsp if (id1) {
774 eb81bc23 2022-06-28 tracey 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 eb81bc23 2022-06-28 tracey 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 b72706c3 2022-06-01 stsp 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 67b631c9 2021-10-10 stsp done:
787 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
788 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
789 67b631c9 2021-10-10 stsp if (blob1)
790 67b631c9 2021-10-10 stsp got_object_blob_close(blob1);
791 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
792 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
793 67b631c9 2021-10-10 stsp if (blob2)
794 67b631c9 2021-10-10 stsp got_object_blob_close(blob2);
795 67b631c9 2021-10-10 stsp return err;
796 67b631c9 2021-10-10 stsp }
797 67b631c9 2021-10-10 stsp
798 67b631c9 2021-10-10 stsp static const struct got_error *
799 67b631c9 2021-10-10 stsp diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
800 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, struct got_pathlist_head *paths,
801 b72706c3 2022-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
802 67b631c9 2021-10-10 stsp {
803 67b631c9 2021-10-10 stsp const struct got_error *err = NULL;
804 67b631c9 2021-10-10 stsp struct got_pathlist_entry *pe;
805 67b631c9 2021-10-10 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
806 67b631c9 2021-10-10 stsp struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
807 67b631c9 2021-10-10 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
808 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
809 67b631c9 2021-10-10 stsp
810 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
811 eb81bc23 2022-06-28 tracey if (fd1 == -1)
812 eb81bc23 2022-06-28 tracey return got_error_from_errno("got_opentempfd");
813 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
814 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
815 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
816 eb81bc23 2022-06-28 tracey goto done;
817 eb81bc23 2022-06-28 tracey }
818 67b631c9 2021-10-10 stsp TAILQ_FOREACH(pe, paths, entry) {
819 67b631c9 2021-10-10 stsp int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
820 67b631c9 2021-10-10 stsp mode_t mode1 = 0, mode2 = 0;
821 67b631c9 2021-10-10 stsp
822 67b631c9 2021-10-10 stsp free(id1);
823 67b631c9 2021-10-10 stsp id1 = NULL;
824 67b631c9 2021-10-10 stsp free(id2);
825 67b631c9 2021-10-10 stsp id2 = NULL;
826 67b631c9 2021-10-10 stsp if (subtree1) {
827 67b631c9 2021-10-10 stsp got_object_tree_close(subtree1);
828 67b631c9 2021-10-10 stsp subtree1 = NULL;
829 67b631c9 2021-10-10 stsp }
830 67b631c9 2021-10-10 stsp if (subtree2) {
831 67b631c9 2021-10-10 stsp got_object_tree_close(subtree2);
832 67b631c9 2021-10-10 stsp subtree2 = NULL;
833 67b631c9 2021-10-10 stsp }
834 67b631c9 2021-10-10 stsp if (blob1) {
835 67b631c9 2021-10-10 stsp got_object_blob_close(blob1);
836 67b631c9 2021-10-10 stsp blob1 = NULL;
837 67b631c9 2021-10-10 stsp }
838 67b631c9 2021-10-10 stsp if (blob2) {
839 67b631c9 2021-10-10 stsp got_object_blob_close(blob2);
840 67b631c9 2021-10-10 stsp blob2 = NULL;
841 67b631c9 2021-10-10 stsp }
842 67b631c9 2021-10-10 stsp
843 67b631c9 2021-10-10 stsp err = got_object_tree_find_path(&id1, &mode1, repo, tree1,
844 67b631c9 2021-10-10 stsp pe->path);
845 67b631c9 2021-10-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
846 67b631c9 2021-10-10 stsp goto done;
847 67b631c9 2021-10-10 stsp err = got_object_tree_find_path(&id2, &mode2, repo, tree2,
848 67b631c9 2021-10-10 stsp pe->path);
849 67b631c9 2021-10-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
850 67b631c9 2021-10-10 stsp goto done;
851 67b631c9 2021-10-10 stsp if (id1 == NULL && id2 == NULL) {
852 67b631c9 2021-10-10 stsp err = got_error_path(pe->path, GOT_ERR_NO_TREE_ENTRY);
853 67b631c9 2021-10-10 stsp goto done;
854 67b631c9 2021-10-10 stsp }
855 67b631c9 2021-10-10 stsp if (id1) {
856 67b631c9 2021-10-10 stsp err = got_object_get_type(&type1, repo, id1);
857 67b631c9 2021-10-10 stsp if (err)
858 67b631c9 2021-10-10 stsp goto done;
859 67b631c9 2021-10-10 stsp }
860 67b631c9 2021-10-10 stsp if (id2) {
861 67b631c9 2021-10-10 stsp err = got_object_get_type(&type2, repo, id2);
862 67b631c9 2021-10-10 stsp if (err)
863 67b631c9 2021-10-10 stsp goto done;
864 67b631c9 2021-10-10 stsp }
865 67b631c9 2021-10-10 stsp if (type1 == GOT_OBJ_TYPE_ANY &&
866 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_ANY) {
867 67b631c9 2021-10-10 stsp err = got_error_path(pe->path, GOT_ERR_NO_OBJ);
868 67b631c9 2021-10-10 stsp goto done;
869 67b631c9 2021-10-10 stsp } else if (type1 != GOT_OBJ_TYPE_ANY &&
870 67b631c9 2021-10-10 stsp type2 != GOT_OBJ_TYPE_ANY && type1 != type2) {
871 67b631c9 2021-10-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
872 67b631c9 2021-10-10 stsp goto done;
873 67b631c9 2021-10-10 stsp }
874 67b631c9 2021-10-10 stsp
875 67b631c9 2021-10-10 stsp if (type1 == GOT_OBJ_TYPE_BLOB ||
876 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_BLOB) {
877 67b631c9 2021-10-10 stsp if (id1) {
878 67b631c9 2021-10-10 stsp err = got_object_open_as_blob(&blob1, repo,
879 eb81bc23 2022-06-28 tracey id1, 8192, fd1);
880 67b631c9 2021-10-10 stsp if (err)
881 67b631c9 2021-10-10 stsp goto done;
882 67b631c9 2021-10-10 stsp }
883 67b631c9 2021-10-10 stsp if (id2) {
884 67b631c9 2021-10-10 stsp err = got_object_open_as_blob(&blob2, repo,
885 eb81bc23 2022-06-28 tracey id2, 8192, fd2);
886 67b631c9 2021-10-10 stsp if (err)
887 67b631c9 2021-10-10 stsp goto done;
888 67b631c9 2021-10-10 stsp }
889 b72706c3 2022-06-01 stsp err = cb(cb_arg, blob1, blob2, f1, f2, id1, id2,
890 67b631c9 2021-10-10 stsp id1 ? pe->path : "/dev/null",
891 67b631c9 2021-10-10 stsp id2 ? pe->path : "/dev/null",
892 67b631c9 2021-10-10 stsp mode1, mode2, repo);
893 67b631c9 2021-10-10 stsp if (err)
894 67b631c9 2021-10-10 stsp goto done;
895 67b631c9 2021-10-10 stsp } else if (type1 == GOT_OBJ_TYPE_TREE ||
896 67b631c9 2021-10-10 stsp type2 == GOT_OBJ_TYPE_TREE) {
897 67b631c9 2021-10-10 stsp if (id1) {
898 67b631c9 2021-10-10 stsp err = got_object_open_as_tree(&subtree1, repo,
899 67b631c9 2021-10-10 stsp id1);
900 67b631c9 2021-10-10 stsp if (err)
901 67b631c9 2021-10-10 stsp goto done;
902 67b631c9 2021-10-10 stsp }
903 67b631c9 2021-10-10 stsp if (id2) {
904 67b631c9 2021-10-10 stsp err = got_object_open_as_tree(&subtree2, repo,
905 67b631c9 2021-10-10 stsp id2);
906 67b631c9 2021-10-10 stsp if (err)
907 67b631c9 2021-10-10 stsp goto done;
908 67b631c9 2021-10-10 stsp }
909 b72706c3 2022-06-01 stsp err = got_diff_tree(subtree1, subtree2, f1, f2,
910 67b631c9 2021-10-10 stsp id1 ? pe->path : "/dev/null",
911 67b631c9 2021-10-10 stsp id2 ? pe->path : "/dev/null",
912 67b631c9 2021-10-10 stsp repo, cb, cb_arg, 1);
913 67b631c9 2021-10-10 stsp if (err)
914 67b631c9 2021-10-10 stsp goto done;
915 67b631c9 2021-10-10 stsp } else {
916 67b631c9 2021-10-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
917 67b631c9 2021-10-10 stsp goto done;
918 67b631c9 2021-10-10 stsp }
919 eb81bc23 2022-06-28 tracey if (ftruncate(fd1, 0L) == -1)
920 eb81bc23 2022-06-28 tracey return got_error_from_errno("ftruncate");
921 eb81bc23 2022-06-28 tracey if (ftruncate(fd2, 0L) == -1)
922 eb81bc23 2022-06-28 tracey return got_error_from_errno("ftruncate");
923 67b631c9 2021-10-10 stsp }
924 11528a82 2018-05-19 stsp done:
925 67b631c9 2021-10-10 stsp free(id1);
926 67b631c9 2021-10-10 stsp free(id2);
927 67b631c9 2021-10-10 stsp if (subtree1)
928 67b631c9 2021-10-10 stsp got_object_tree_close(subtree1);
929 67b631c9 2021-10-10 stsp if (subtree2)
930 67b631c9 2021-10-10 stsp got_object_tree_close(subtree2);
931 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
932 eb81bc23 2022-06-28 tracey 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 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
936 eb81bc23 2022-06-28 tracey 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 8469d821 2022-06-25 stsp static const struct got_error *
943 8469d821 2022-06-25 stsp show_object_id(off_t **line_offsets, size_t *nlines, const char *obj_typestr,
944 8469d821 2022-06-25 stsp int ch, const char *id_str, FILE *outfile)
945 8469d821 2022-06-25 stsp {
946 8469d821 2022-06-25 stsp const struct got_error *err;
947 8469d821 2022-06-25 stsp int n;
948 8469d821 2022-06-25 stsp off_t outoff = 0;
949 8469d821 2022-06-25 stsp
950 8469d821 2022-06-25 stsp n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
951 8469d821 2022-06-25 stsp if (line_offsets != NULL && *line_offsets != NULL) {
952 8469d821 2022-06-25 stsp if (*nlines == 0) {
953 8469d821 2022-06-25 stsp err = add_line_offset(line_offsets, nlines, 0);
954 8469d821 2022-06-25 stsp if (err)
955 8469d821 2022-06-25 stsp return err;
956 8469d821 2022-06-25 stsp } else
957 8469d821 2022-06-25 stsp outoff = (*line_offsets)[*nlines - 1];
958 8469d821 2022-06-25 stsp
959 8469d821 2022-06-25 stsp outoff += n;
960 8469d821 2022-06-25 stsp err = add_line_offset(line_offsets, nlines, outoff);
961 8469d821 2022-06-25 stsp if (err)
962 8469d821 2022-06-25 stsp return err;
963 8469d821 2022-06-25 stsp }
964 8469d821 2022-06-25 stsp
965 8469d821 2022-06-25 stsp return NULL;
966 8469d821 2022-06-25 stsp }
967 8469d821 2022-06-25 stsp
968 8469d821 2022-06-25 stsp static const struct got_error *
969 8469d821 2022-06-25 stsp diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
970 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
971 58e31a80 2022-06-27 op struct got_pathlist_head *paths, const char *label1, const char *label2,
972 58e31a80 2022-06-27 op int diff_context, int ignore_whitespace, int force_text_diff,
973 58e31a80 2022-06-27 op 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 67b631c9 2021-10-10 stsp
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 67b631c9 2021-10-10 stsp if (paths == NULL || TAILQ_EMPTY(paths)) {
1006 b72706c3 2022-06-01 stsp err = got_diff_tree(tree1, tree2, f1, f2, label1, label2,
1007 b72706c3 2022-06-01 stsp repo, got_diff_blob_output_unidiff, &arg, 1);
1008 67b631c9 2021-10-10 stsp } else {
1009 b72706c3 2022-06-01 stsp err = diff_paths(tree1, tree2, f1, f2, paths, repo,
1010 67b631c9 2021-10-10 stsp got_diff_blob_output_unidiff, &arg);
1011 67b631c9 2021-10-10 stsp }
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 8469d821 2022-06-25 stsp got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
1026 8469d821 2022-06-25 stsp FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
1027 58e31a80 2022-06-27 op struct got_pathlist_head *paths, const char *label1, const char *label2,
1028 58e31a80 2022-06-27 op int diff_context, int ignore_whitespace, int force_text_diff,
1029 58e31a80 2022-06-27 op struct got_repository *repo, FILE *outfile)
1030 8469d821 2022-06-25 stsp {
1031 8469d821 2022-06-25 stsp const struct got_error *err;
1032 8469d821 2022-06-25 stsp char *idstr = NULL;
1033 8469d821 2022-06-25 stsp
1034 8469d821 2022-06-25 stsp if (id1 == NULL && id2 == NULL)
1035 8469d821 2022-06-25 stsp return got_error(GOT_ERR_NO_OBJ);
1036 8469d821 2022-06-25 stsp
1037 8469d821 2022-06-25 stsp if (id1) {
1038 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id1);
1039 8469d821 2022-06-25 stsp if (err)
1040 8469d821 2022-06-25 stsp goto done;
1041 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "tree", '-',
1042 8469d821 2022-06-25 stsp idstr, outfile);
1043 8469d821 2022-06-25 stsp if (err)
1044 8469d821 2022-06-25 stsp goto done;
1045 8469d821 2022-06-25 stsp free(idstr);
1046 8469d821 2022-06-25 stsp idstr = NULL;
1047 8469d821 2022-06-25 stsp } else {
1048 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "tree", '-',
1049 8469d821 2022-06-25 stsp "/dev/null", outfile);
1050 8469d821 2022-06-25 stsp if (err)
1051 8469d821 2022-06-25 stsp goto done;
1052 8469d821 2022-06-25 stsp }
1053 8469d821 2022-06-25 stsp
1054 8469d821 2022-06-25 stsp if (id2) {
1055 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id2);
1056 8469d821 2022-06-25 stsp if (err)
1057 8469d821 2022-06-25 stsp goto done;
1058 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "tree", '+',
1059 8469d821 2022-06-25 stsp idstr, outfile);
1060 8469d821 2022-06-25 stsp if (err)
1061 8469d821 2022-06-25 stsp goto done;
1062 8469d821 2022-06-25 stsp free(idstr);
1063 8469d821 2022-06-25 stsp idstr = NULL;
1064 8469d821 2022-06-25 stsp } else {
1065 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "tree", '+',
1066 8469d821 2022-06-25 stsp "/dev/null", outfile);
1067 8469d821 2022-06-25 stsp if (err)
1068 8469d821 2022-06-25 stsp goto done;
1069 8469d821 2022-06-25 stsp }
1070 8469d821 2022-06-25 stsp
1071 8469d821 2022-06-25 stsp err = diff_objects_as_trees(line_offsets, nlines, f1, f2, id1, id2,
1072 8469d821 2022-06-25 stsp paths, label1, label2, diff_context, ignore_whitespace,
1073 8469d821 2022-06-25 stsp force_text_diff, repo, outfile);
1074 8469d821 2022-06-25 stsp done:
1075 8469d821 2022-06-25 stsp free(idstr);
1076 8469d821 2022-06-25 stsp return err;
1077 8469d821 2022-06-25 stsp }
1078 8469d821 2022-06-25 stsp
1079 8469d821 2022-06-25 stsp const struct got_error *
1080 fe621944 2020-11-10 stsp got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
1081 b72706c3 2022-06-01 stsp FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
1082 67b631c9 2021-10-10 stsp 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 8469d821 2022-06-25 stsp 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 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id1);
1098 8469d821 2022-06-25 stsp if (err)
1099 8469d821 2022-06-25 stsp goto done;
1100 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "commit", '-',
1101 8469d821 2022-06-25 stsp idstr, outfile);
1102 8469d821 2022-06-25 stsp if (err)
1103 8469d821 2022-06-25 stsp goto done;
1104 8469d821 2022-06-25 stsp free(idstr);
1105 8469d821 2022-06-25 stsp idstr = NULL;
1106 8469d821 2022-06-25 stsp } else {
1107 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "commit", '-',
1108 8469d821 2022-06-25 stsp "/dev/null", outfile);
1109 8469d821 2022-06-25 stsp if (err)
1110 8469d821 2022-06-25 stsp 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 8469d821 2022-06-25 stsp err = got_object_id_str(&idstr, id2);
1118 8469d821 2022-06-25 stsp if (err)
1119 8469d821 2022-06-25 stsp goto done;
1120 8469d821 2022-06-25 stsp err = show_object_id(line_offsets, nlines, "commit", '+',
1121 8469d821 2022-06-25 stsp idstr, outfile);
1122 8469d821 2022-06-25 stsp if (err)
1123 8469d821 2022-06-25 stsp goto done;
1124 8469d821 2022-06-25 stsp
1125 8469d821 2022-06-25 stsp 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 67b631c9 2021-10-10 stsp got_object_commit_get_tree_id(commit2), paths, "", "",
1128 67b631c9 2021-10-10 stsp 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 8469d821 2022-06-25 stsp 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 }