Blame


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