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