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