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