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