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