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 511a516b 2018-05-19 stsp #include "got_opentemp.h"
32 324d37e7 2019-05-11 stsp #include "got_path.h"
33 0208f208 2020-05-05 stsp #include "got_cancel.h"
34 0208f208 2020-05-05 stsp #include "got_worktree.h"
35 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 fe621944 2020-11-10 stsp struct got_blob_object *blob2,
59 46f68b20 2019-10-19 stsp const char *label1, const char *label2, mode_t mode1, mode_t mode2,
60 fe621944 2020-11-10 stsp int diff_context, int ignore_whitespace, FILE *outfile)
61 7d283eee 2017-11-29 stsp {
62 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
63 4e22badc 2017-11-30 stsp FILE *f1 = NULL, *f2 = NULL;
64 f78b0693 2017-11-29 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
65 f78b0693 2017-11-29 stsp char hex2[SHA1_DIGEST_STRING_LENGTH];
66 98abbc84 2017-11-30 stsp char *idstr1 = NULL, *idstr2 = NULL;
67 be659d10 2020-11-18 stsp off_t size1, size2;
68 fe621944 2020-11-10 stsp struct got_diffreg_result *result;
69 fe621944 2020-11-10 stsp off_t outoff = 0;
70 fe621944 2020-11-10 stsp int n;
71 7d283eee 2017-11-29 stsp
72 fe621944 2020-11-10 stsp if (line_offsets && *line_offsets && *nlines > 0)
73 fe621944 2020-11-10 stsp outoff = (*line_offsets)[*nlines - 1];
74 fe621944 2020-11-10 stsp
75 fe621944 2020-11-10 stsp if (resultp)
76 fe621944 2020-11-10 stsp *resultp = NULL;
77 fe621944 2020-11-10 stsp
78 4e22badc 2017-11-30 stsp if (blob1) {
79 a1fd68d8 2018-01-12 stsp f1 = got_opentemp();
80 4e22badc 2017-11-30 stsp if (f1 == NULL)
81 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentemp");
82 fe621944 2020-11-10 stsp }
83 7d283eee 2017-11-29 stsp
84 4e22badc 2017-11-30 stsp if (blob2) {
85 a1fd68d8 2018-01-12 stsp f2 = got_opentemp();
86 4e22badc 2017-11-30 stsp if (f2 == NULL) {
87 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
88 4e22badc 2017-11-30 stsp fclose(f1);
89 fb43ecf1 2019-02-11 stsp return err;
90 4e22badc 2017-11-30 stsp }
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 fe621944 2020-11-10 stsp ignore_whitespace);
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 fe621944 2020-11-10 stsp err = got_diffreg_output(line_offsets, nlines, result, f1, f2,
170 fe621944 2020-11-10 stsp label1 ? label1 : idstr1,
171 fe621944 2020-11-10 stsp label2 ? label2 : idstr2,
172 fe621944 2020-11-10 stsp GOT_DIFF_OUTPUT_UNIDIFF, diff_context, outfile);
173 fe621944 2020-11-10 stsp if (err)
174 fe621944 2020-11-10 stsp goto done;
175 fe621944 2020-11-10 stsp }
176 fe621944 2020-11-10 stsp
177 fe621944 2020-11-10 stsp if (resultp && err == NULL)
178 fe621944 2020-11-10 stsp *resultp = result;
179 fe621944 2020-11-10 stsp else {
180 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
181 fe621944 2020-11-10 stsp if (free_err && err == NULL)
182 fe621944 2020-11-10 stsp err = free_err;
183 fe621944 2020-11-10 stsp }
184 7d283eee 2017-11-29 stsp done:
185 fb43ecf1 2019-02-11 stsp if (f1 && fclose(f1) != 0 && err == NULL)
186 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
187 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
188 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
189 7d283eee 2017-11-29 stsp return err;
190 aaa13589 2019-06-01 stsp }
191 aaa13589 2019-06-01 stsp
192 aaa13589 2019-06-01 stsp const struct got_error *
193 aaa13589 2019-06-01 stsp got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
194 aaa13589 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
195 aaa13589 2019-06-01 stsp struct got_object_id *id2, const char *label1, const char *label2,
196 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
197 aaa13589 2019-06-01 stsp {
198 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg *a = arg;
199 aaa13589 2019-06-01 stsp
200 fe621944 2020-11-10 stsp return diff_blobs(&a->line_offsets, &a->nlines, NULL,
201 fe621944 2020-11-10 stsp blob1, blob2, label1, label2, mode1, mode2, a->diff_context,
202 fe621944 2020-11-10 stsp a->ignore_whitespace, a->outfile);
203 7d283eee 2017-11-29 stsp }
204 474b4f94 2017-11-30 stsp
205 404c43c4 2018-06-21 stsp const struct got_error *
206 fe621944 2020-11-10 stsp got_diff_blob(off_t **line_offsets, size_t *nlines,
207 fe621944 2020-11-10 stsp struct got_blob_object *blob1, struct got_blob_object *blob2,
208 63035f9f 2019-10-06 stsp const char *label1, const char *label2, int diff_context,
209 63035f9f 2019-10-06 stsp int ignore_whitespace, FILE *outfile)
210 404c43c4 2018-06-21 stsp {
211 fe621944 2020-11-10 stsp return diff_blobs(line_offsets, nlines, NULL, blob1, blob2,
212 fe621944 2020-11-10 stsp label1, label2, 0, 0, diff_context, ignore_whitespace, 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 4ce46740 2019-08-08 stsp struct got_blob_object *blob1, const char *label1, FILE *f2, size_t size2,
218 63035f9f 2019-10-06 stsp const char *label2, int diff_context, int ignore_whitespace, FILE *outfile)
219 b72f483a 2019-02-05 stsp {
220 fe621944 2020-11-10 stsp const struct got_error *err = NULL, *free_err;
221 b72f483a 2019-02-05 stsp FILE *f1 = NULL;
222 b72f483a 2019-02-05 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
223 b72f483a 2019-02-05 stsp char *idstr1 = NULL;
224 be659d10 2020-11-18 stsp off_t size1;
225 fe621944 2020-11-10 stsp struct got_diffreg_result *result = NULL;
226 b72f483a 2019-02-05 stsp
227 fe621944 2020-11-10 stsp if (resultp)
228 fe621944 2020-11-10 stsp *resultp = NULL;
229 7f1f93af 2019-08-06 stsp
230 b72f483a 2019-02-05 stsp size1 = 0;
231 b72f483a 2019-02-05 stsp if (blob1) {
232 b72f483a 2019-02-05 stsp f1 = got_opentemp();
233 b72f483a 2019-02-05 stsp if (f1 == NULL)
234 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentemp");
235 b72f483a 2019-02-05 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
236 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
237 6c4c42e0 2019-06-24 stsp blob1);
238 b72f483a 2019-02-05 stsp if (err)
239 b72f483a 2019-02-05 stsp goto done;
240 b72f483a 2019-02-05 stsp } else {
241 b72f483a 2019-02-05 stsp idstr1 = "/dev/null";
242 b72f483a 2019-02-05 stsp }
243 b72f483a 2019-02-05 stsp
244 7f1f93af 2019-08-06 stsp if (outfile) {
245 4ce46740 2019-08-08 stsp fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
246 7f1f93af 2019-08-06 stsp fprintf(outfile, "file + %s\n",
247 7f1f93af 2019-08-06 stsp f2 == NULL ? "/dev/null" : label2);
248 7f1f93af 2019-08-06 stsp }
249 fe621944 2020-11-10 stsp
250 fe621944 2020-11-10 stsp err = got_diffreg(&result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
251 fe621944 2020-11-10 stsp ignore_whitespace);
252 fe621944 2020-11-10 stsp if (err)
253 fe621944 2020-11-10 stsp goto done;
254 fe621944 2020-11-10 stsp
255 fe621944 2020-11-10 stsp if (outfile) {
256 fe621944 2020-11-10 stsp err = got_diffreg_output(NULL, NULL, result, f1, f2,
257 fe621944 2020-11-10 stsp label2, label2, GOT_DIFF_OUTPUT_UNIDIFF, diff_context,
258 fe621944 2020-11-10 stsp outfile);
259 7f1f93af 2019-08-06 stsp if (err)
260 fe621944 2020-11-10 stsp goto done;
261 7f1f93af 2019-08-06 stsp }
262 fe621944 2020-11-10 stsp
263 fe621944 2020-11-10 stsp if (resultp && err == NULL)
264 fe621944 2020-11-10 stsp *resultp = result;
265 fe621944 2020-11-10 stsp else if (result) {
266 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(result);
267 fe621944 2020-11-10 stsp if (free_err && err == NULL)
268 fe621944 2020-11-10 stsp err = free_err;
269 fe621944 2020-11-10 stsp }
270 b72f483a 2019-02-05 stsp done:
271 fb43ecf1 2019-02-11 stsp if (f1 && fclose(f1) != 0 && err == NULL)
272 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
273 b72f483a 2019-02-05 stsp return err;
274 7f1f93af 2019-08-06 stsp }
275 7f1f93af 2019-08-06 stsp
276 7f1f93af 2019-08-06 stsp const struct got_error *
277 4ce46740 2019-08-08 stsp got_diff_blob_file(struct got_blob_object *blob1, const char *label1,
278 4ce46740 2019-08-08 stsp FILE *f2, size_t size2, const char *label2, int diff_context,
279 63035f9f 2019-10-06 stsp int ignore_whitespace, FILE *outfile)
280 7f1f93af 2019-08-06 stsp {
281 4ce46740 2019-08-08 stsp return diff_blob_file(NULL, blob1, label1, f2, size2, label2,
282 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, outfile);
283 474b4f94 2017-11-30 stsp }
284 474b4f94 2017-11-30 stsp
285 474b4f94 2017-11-30 stsp static const struct got_error *
286 46f68b20 2019-10-19 stsp diff_added_blob(struct got_object_id *id, const char *label, mode_t mode,
287 aaa13589 2019-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
288 474b4f94 2017-11-30 stsp {
289 4e22badc 2017-11-30 stsp const struct got_error *err;
290 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
291 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
292 4e22badc 2017-11-30 stsp
293 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
294 4e22badc 2017-11-30 stsp if (err)
295 4e22badc 2017-11-30 stsp return err;
296 4e22badc 2017-11-30 stsp
297 2acfca77 2018-04-01 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
298 2acfca77 2018-04-01 stsp if (err)
299 2acfca77 2018-04-01 stsp goto done;
300 46f68b20 2019-10-19 stsp err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, 0, mode, repo);
301 2acfca77 2018-04-01 stsp done:
302 2acfca77 2018-04-01 stsp got_object_close(obj);
303 2acfca77 2018-04-01 stsp if (blob)
304 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
305 2acfca77 2018-04-01 stsp return err;
306 474b4f94 2017-11-30 stsp }
307 474b4f94 2017-11-30 stsp
308 474b4f94 2017-11-30 stsp static const struct got_error *
309 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
310 46f68b20 2019-10-19 stsp const char *label1, const char *label2, mode_t mode1, mode_t mode2,
311 46f68b20 2019-10-19 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
312 474b4f94 2017-11-30 stsp {
313 6a213ccb 2017-11-30 stsp const struct got_error *err;
314 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
315 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
316 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
317 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
318 6a213ccb 2017-11-30 stsp
319 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
320 6a213ccb 2017-11-30 stsp if (err)
321 730a8aa0 2018-04-24 stsp return err;
322 15a94983 2018-12-23 stsp if (obj1->type != GOT_OBJ_TYPE_BLOB) {
323 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
324 6a213ccb 2017-11-30 stsp goto done;
325 6a213ccb 2017-11-30 stsp }
326 6a213ccb 2017-11-30 stsp
327 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
328 730a8aa0 2018-04-24 stsp if (err)
329 6a213ccb 2017-11-30 stsp goto done;
330 15a94983 2018-12-23 stsp if (obj2->type != GOT_OBJ_TYPE_BLOB) {
331 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
332 6a213ccb 2017-11-30 stsp goto done;
333 6a213ccb 2017-11-30 stsp }
334 6a213ccb 2017-11-30 stsp
335 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob1, repo, obj1, 8192);
336 2acfca77 2018-04-01 stsp if (err)
337 6a213ccb 2017-11-30 stsp goto done;
338 6a213ccb 2017-11-30 stsp
339 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob2, repo, obj2, 8192);
340 2acfca77 2018-04-01 stsp if (err)
341 6a213ccb 2017-11-30 stsp goto done;
342 6a213ccb 2017-11-30 stsp
343 46f68b20 2019-10-19 stsp err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, mode1, mode2,
344 46f68b20 2019-10-19 stsp repo);
345 6a213ccb 2017-11-30 stsp done:
346 a3e2cbea 2017-12-01 stsp if (obj1)
347 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
348 a3e2cbea 2017-12-01 stsp if (obj2)
349 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
350 a3e2cbea 2017-12-01 stsp if (blob1)
351 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
352 a3e2cbea 2017-12-01 stsp if (blob2)
353 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
354 6a213ccb 2017-11-30 stsp return err;
355 474b4f94 2017-11-30 stsp }
356 474b4f94 2017-11-30 stsp
357 474b4f94 2017-11-30 stsp static const struct got_error *
358 46f68b20 2019-10-19 stsp diff_deleted_blob(struct got_object_id *id, const char *label, mode_t mode,
359 aaa13589 2019-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
360 474b4f94 2017-11-30 stsp {
361 365fb436 2017-11-30 stsp const struct got_error *err;
362 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
363 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
364 365fb436 2017-11-30 stsp
365 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
366 365fb436 2017-11-30 stsp if (err)
367 365fb436 2017-11-30 stsp return err;
368 365fb436 2017-11-30 stsp
369 2acfca77 2018-04-01 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
370 2acfca77 2018-04-01 stsp if (err)
371 2acfca77 2018-04-01 stsp goto done;
372 46f68b20 2019-10-19 stsp err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, mode, 0, repo);
373 2acfca77 2018-04-01 stsp done:
374 2acfca77 2018-04-01 stsp got_object_close(obj);
375 2acfca77 2018-04-01 stsp if (blob)
376 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
377 2acfca77 2018-04-01 stsp return err;
378 474b4f94 2017-11-30 stsp }
379 474b4f94 2017-11-30 stsp
380 474b4f94 2017-11-30 stsp static const struct got_error *
381 54156555 2018-12-24 stsp diff_added_tree(struct got_object_id *id, const char *label,
382 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
383 31b4484f 2019-07-27 stsp int diff_content)
384 474b4f94 2017-11-30 stsp {
385 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
386 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
387 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
388 9c70d4c3 2017-11-30 stsp
389 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
390 9c70d4c3 2017-11-30 stsp if (err)
391 9c70d4c3 2017-11-30 stsp goto done;
392 9c70d4c3 2017-11-30 stsp
393 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
394 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
395 9c70d4c3 2017-11-30 stsp goto done;
396 9c70d4c3 2017-11-30 stsp }
397 9c70d4c3 2017-11-30 stsp
398 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
399 9c70d4c3 2017-11-30 stsp if (err)
400 9c70d4c3 2017-11-30 stsp goto done;
401 9c70d4c3 2017-11-30 stsp
402 31b4484f 2019-07-27 stsp err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
403 31b4484f 2019-07-27 stsp diff_content);
404 9c70d4c3 2017-11-30 stsp done:
405 9c70d4c3 2017-11-30 stsp if (tree)
406 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
407 9c70d4c3 2017-11-30 stsp if (treeobj)
408 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
409 9c70d4c3 2017-11-30 stsp return err;
410 474b4f94 2017-11-30 stsp }
411 474b4f94 2017-11-30 stsp
412 474b4f94 2017-11-30 stsp static const struct got_error *
413 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
414 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
415 31b4484f 2019-07-27 stsp got_diff_blob_cb cb, void *cb_arg, int diff_content)
416 474b4f94 2017-11-30 stsp {
417 f6861a81 2018-09-13 stsp const struct got_error *err;
418 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
419 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
420 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
421 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
422 789689b5 2017-11-30 stsp
423 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
424 789689b5 2017-11-30 stsp if (err)
425 789689b5 2017-11-30 stsp goto done;
426 789689b5 2017-11-30 stsp
427 15a94983 2018-12-23 stsp if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
428 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
429 789689b5 2017-11-30 stsp goto done;
430 789689b5 2017-11-30 stsp }
431 789689b5 2017-11-30 stsp
432 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
433 789689b5 2017-11-30 stsp if (err)
434 789689b5 2017-11-30 stsp goto done;
435 789689b5 2017-11-30 stsp
436 15a94983 2018-12-23 stsp if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
437 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
438 789689b5 2017-11-30 stsp goto done;
439 789689b5 2017-11-30 stsp }
440 789689b5 2017-11-30 stsp
441 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
442 789689b5 2017-11-30 stsp if (err)
443 789689b5 2017-11-30 stsp goto done;
444 789689b5 2017-11-30 stsp
445 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
446 789689b5 2017-11-30 stsp if (err)
447 789689b5 2017-11-30 stsp goto done;
448 789689b5 2017-11-30 stsp
449 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
450 31b4484f 2019-07-27 stsp diff_content);
451 789689b5 2017-11-30 stsp
452 789689b5 2017-11-30 stsp done:
453 789689b5 2017-11-30 stsp if (tree1)
454 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
455 789689b5 2017-11-30 stsp if (tree2)
456 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
457 789689b5 2017-11-30 stsp if (treeobj1)
458 789689b5 2017-11-30 stsp got_object_close(treeobj1);
459 789689b5 2017-11-30 stsp if (treeobj2)
460 789689b5 2017-11-30 stsp got_object_close(treeobj2);
461 789689b5 2017-11-30 stsp return err;
462 474b4f94 2017-11-30 stsp }
463 474b4f94 2017-11-30 stsp
464 474b4f94 2017-11-30 stsp static const struct got_error *
465 54156555 2018-12-24 stsp diff_deleted_tree(struct got_object_id *id, const char *label,
466 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
467 31b4484f 2019-07-27 stsp int diff_content)
468 474b4f94 2017-11-30 stsp {
469 f6861a81 2018-09-13 stsp const struct got_error *err;
470 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
471 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
472 2c56f2ce 2017-11-30 stsp
473 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
474 2c56f2ce 2017-11-30 stsp if (err)
475 2c56f2ce 2017-11-30 stsp goto done;
476 2c56f2ce 2017-11-30 stsp
477 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
478 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
479 2c56f2ce 2017-11-30 stsp goto done;
480 2c56f2ce 2017-11-30 stsp }
481 2c56f2ce 2017-11-30 stsp
482 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
483 2c56f2ce 2017-11-30 stsp if (err)
484 2c56f2ce 2017-11-30 stsp goto done;
485 2c56f2ce 2017-11-30 stsp
486 31b4484f 2019-07-27 stsp err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
487 31b4484f 2019-07-27 stsp diff_content);
488 2c56f2ce 2017-11-30 stsp done:
489 2c56f2ce 2017-11-30 stsp if (tree)
490 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
491 2c56f2ce 2017-11-30 stsp if (treeobj)
492 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
493 2c56f2ce 2017-11-30 stsp return err;
494 474b4f94 2017-11-30 stsp }
495 474b4f94 2017-11-30 stsp
496 474b4f94 2017-11-30 stsp static const struct got_error *
497 74671950 2018-02-11 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
498 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
499 aaa13589 2019-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
500 474b4f94 2017-11-30 stsp {
501 013404a9 2017-11-30 stsp /* XXX TODO */
502 474b4f94 2017-11-30 stsp return NULL;
503 474b4f94 2017-11-30 stsp }
504 474b4f94 2017-11-30 stsp
505 474b4f94 2017-11-30 stsp static const struct got_error *
506 56e0773d 2019-11-28 stsp diff_entry_old_new(struct got_tree_entry *te1,
507 56e0773d 2019-11-28 stsp struct got_tree_entry *te2, 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 56e0773d 2019-11-28 stsp err = diff_deleted_tree(&te1->id, label1, repo,
520 31b4484f 2019-07-27 stsp cb, cb_arg, diff_content);
521 31b4484f 2019-07-27 stsp else {
522 31b4484f 2019-07-27 stsp if (diff_content)
523 56e0773d 2019-11-28 stsp err = diff_deleted_blob(&te1->id, label1,
524 46f68b20 2019-10-19 stsp te1->mode, repo, cb, cb_arg);
525 31b4484f 2019-07-27 stsp else
526 56e0773d 2019-11-28 stsp err = cb(cb_arg, NULL, NULL, &te1->id, NULL,
527 46f68b20 2019-10-19 stsp label1, NULL, te1->mode, 0, repo);
528 31b4484f 2019-07-27 stsp }
529 f6861a81 2018-09-13 stsp return err;
530 63c5ca5d 2019-08-24 stsp } else if (got_object_tree_entry_is_submodule(te2))
531 63c5ca5d 2019-08-24 stsp return NULL;
532 474b4f94 2017-11-30 stsp
533 56e0773d 2019-11-28 stsp id_match = (got_object_id_cmp(&te1->id, &te2->id) == 0);
534 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
535 19ae6da1 2018-11-05 stsp if (!id_match)
536 56e0773d 2019-11-28 stsp return diff_modified_tree(&te1->id, &te2->id,
537 31b4484f 2019-07-27 stsp label1, label2, repo, cb, cb_arg, diff_content);
538 40dde666 2020-07-23 stsp } else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
539 40dde666 2020-07-23 stsp (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
540 46f68b20 2019-10-19 stsp if (!id_match ||
541 40dde666 2020-07-23 stsp ((te1->mode & (S_IFLNK | S_IXUSR))) !=
542 40dde666 2020-07-23 stsp (te2->mode & (S_IFLNK | S_IXUSR))) {
543 31b4484f 2019-07-27 stsp if (diff_content)
544 56e0773d 2019-11-28 stsp return diff_modified_blob(&te1->id, &te2->id,
545 46f68b20 2019-10-19 stsp label1, label2, te1->mode, te2->mode,
546 46f68b20 2019-10-19 stsp repo, cb, cb_arg);
547 31b4484f 2019-07-27 stsp else
548 56e0773d 2019-11-28 stsp return cb(cb_arg, NULL, NULL, &te1->id,
549 56e0773d 2019-11-28 stsp &te2->id, label1, label2, te1->mode,
550 46f68b20 2019-10-19 stsp te2->mode, repo);
551 31b4484f 2019-07-27 stsp }
552 413ea19d 2017-11-30 stsp }
553 474b4f94 2017-11-30 stsp
554 19ae6da1 2018-11-05 stsp if (id_match)
555 f6861a81 2018-09-13 stsp return NULL;
556 f6861a81 2018-09-13 stsp
557 56e0773d 2019-11-28 stsp return diff_kind_mismatch(&te1->id, &te2->id, label1, label2, repo,
558 aaa13589 2019-06-01 stsp cb, cb_arg);
559 474b4f94 2017-11-30 stsp }
560 474b4f94 2017-11-30 stsp
561 474b4f94 2017-11-30 stsp static const struct got_error *
562 56e0773d 2019-11-28 stsp diff_entry_new_old(struct got_tree_entry *te2,
563 56e0773d 2019-11-28 stsp struct got_tree_entry *te1, const char *label2,
564 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
565 31b4484f 2019-07-27 stsp int diff_content)
566 474b4f94 2017-11-30 stsp {
567 f6861a81 2018-09-13 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
568 63c5ca5d 2019-08-24 stsp return NULL;
569 63c5ca5d 2019-08-24 stsp
570 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te2))
571 f6861a81 2018-09-13 stsp return NULL;
572 474b4f94 2017-11-30 stsp
573 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
574 56e0773d 2019-11-28 stsp return diff_added_tree(&te2->id, label2, repo, cb, cb_arg,
575 31b4484f 2019-07-27 stsp diff_content);
576 f6861a81 2018-09-13 stsp
577 31b4484f 2019-07-27 stsp if (diff_content)
578 56e0773d 2019-11-28 stsp return diff_added_blob(&te2->id, label2, te2->mode, repo, cb,
579 46f68b20 2019-10-19 stsp cb_arg);
580 31b4484f 2019-07-27 stsp
581 56e0773d 2019-11-28 stsp return cb(cb_arg, NULL, NULL, NULL, &te2->id, NULL, label2, 0,
582 46f68b20 2019-10-19 stsp te2->mode, repo);
583 0208f208 2020-05-05 stsp }
584 0208f208 2020-05-05 stsp
585 0208f208 2020-05-05 stsp const struct got_error *
586 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
587 0208f208 2020-05-05 stsp struct got_blob_object *blob2, struct got_object_id *id1,
588 0208f208 2020-05-05 stsp struct got_object_id *id2, const char *label1, const char *label2,
589 0208f208 2020-05-05 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
590 0208f208 2020-05-05 stsp {
591 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
592 0208f208 2020-05-05 stsp struct got_pathlist_head *paths = arg;
593 0208f208 2020-05-05 stsp struct got_diff_changed_path *change = NULL;
594 0208f208 2020-05-05 stsp char *path = NULL;
595 0208f208 2020-05-05 stsp
596 0208f208 2020-05-05 stsp path = strdup(label2 ? label2 : label1);
597 0208f208 2020-05-05 stsp if (path == NULL)
598 0208f208 2020-05-05 stsp return got_error_from_errno("malloc");
599 0208f208 2020-05-05 stsp
600 0208f208 2020-05-05 stsp change = malloc(sizeof(*change));
601 0208f208 2020-05-05 stsp if (change == NULL) {
602 0208f208 2020-05-05 stsp err = got_error_from_errno("malloc");
603 0208f208 2020-05-05 stsp goto done;
604 0208f208 2020-05-05 stsp }
605 0208f208 2020-05-05 stsp
606 0208f208 2020-05-05 stsp change->status = GOT_STATUS_NO_CHANGE;
607 0208f208 2020-05-05 stsp if (id1 == NULL)
608 0208f208 2020-05-05 stsp change->status = GOT_STATUS_ADD;
609 0208f208 2020-05-05 stsp else if (id2 == NULL)
610 0208f208 2020-05-05 stsp change->status = GOT_STATUS_DELETE;
611 0208f208 2020-05-05 stsp else {
612 0208f208 2020-05-05 stsp if (got_object_id_cmp(id1, id2) != 0)
613 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODIFY;
614 0208f208 2020-05-05 stsp else if (mode1 != mode2)
615 0208f208 2020-05-05 stsp change->status = GOT_STATUS_MODE_CHANGE;
616 0208f208 2020-05-05 stsp }
617 0208f208 2020-05-05 stsp
618 0208f208 2020-05-05 stsp err = got_pathlist_insert(NULL, paths, path, change);
619 0208f208 2020-05-05 stsp done:
620 0208f208 2020-05-05 stsp if (err) {
621 0208f208 2020-05-05 stsp free(path);
622 0208f208 2020-05-05 stsp free(change);
623 0208f208 2020-05-05 stsp }
624 0208f208 2020-05-05 stsp return err;
625 474b4f94 2017-11-30 stsp }
626 474b4f94 2017-11-30 stsp
627 474b4f94 2017-11-30 stsp const struct got_error *
628 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
629 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
630 31b4484f 2019-07-27 stsp got_diff_blob_cb cb, void *cb_arg, int diff_content)
631 474b4f94 2017-11-30 stsp {
632 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
633 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
634 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
635 f6861a81 2018-09-13 stsp char *l1 = NULL, *l2 = NULL;
636 56e0773d 2019-11-28 stsp int tidx1 = 0, tidx2 = 0;
637 474b4f94 2017-11-30 stsp
638 883f0469 2018-06-23 stsp if (tree1) {
639 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, 0);
640 60f50a58 2018-09-15 stsp if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
641 f6861a81 2018-09-13 stsp te1->name) == -1)
642 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
643 883f0469 2018-06-23 stsp }
644 883f0469 2018-06-23 stsp if (tree2) {
645 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, 0);
646 60f50a58 2018-09-15 stsp if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
647 f6861a81 2018-09-13 stsp te2->name) == -1)
648 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
649 883f0469 2018-06-23 stsp }
650 474b4f94 2017-11-30 stsp
651 474b4f94 2017-11-30 stsp do {
652 474b4f94 2017-11-30 stsp if (te1) {
653 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
654 f6861a81 2018-09-13 stsp if (tree2)
655 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree2,
656 1de5e065 2019-06-01 stsp te1->name);
657 f6861a81 2018-09-13 stsp if (te) {
658 f6861a81 2018-09-13 stsp free(l2);
659 f6861a81 2018-09-13 stsp l2 = NULL;
660 f6861a81 2018-09-13 stsp if (te && asprintf(&l2, "%s%s%s", label2,
661 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te->name) == -1)
662 230a42bd 2019-05-11 jcs return
663 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
664 f6861a81 2018-09-13 stsp }
665 aaa13589 2019-06-01 stsp err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
666 31b4484f 2019-07-27 stsp cb_arg, diff_content);
667 474b4f94 2017-11-30 stsp if (err)
668 474b4f94 2017-11-30 stsp break;
669 474b4f94 2017-11-30 stsp }
670 474b4f94 2017-11-30 stsp
671 474b4f94 2017-11-30 stsp if (te2) {
672 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
673 f6861a81 2018-09-13 stsp if (tree1)
674 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree1,
675 1de5e065 2019-06-01 stsp te2->name);
676 d6ce02f1 2018-11-17 stsp free(l2);
677 d6ce02f1 2018-11-17 stsp if (te) {
678 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
679 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te->name) == -1)
680 230a42bd 2019-05-11 jcs return
681 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
682 d6ce02f1 2018-11-17 stsp } else {
683 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
684 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te2->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 }
688 31b4484f 2019-07-27 stsp err = diff_entry_new_old(te2, te, l2, repo,
689 31b4484f 2019-07-27 stsp cb, cb_arg, diff_content);
690 474b4f94 2017-11-30 stsp if (err)
691 474b4f94 2017-11-30 stsp break;
692 474b4f94 2017-11-30 stsp }
693 474b4f94 2017-11-30 stsp
694 f6861a81 2018-09-13 stsp free(l1);
695 f6861a81 2018-09-13 stsp l1 = NULL;
696 f6861a81 2018-09-13 stsp if (te1) {
697 56e0773d 2019-11-28 stsp tidx1++;
698 56e0773d 2019-11-28 stsp te1 = got_object_tree_get_entry(tree1, tidx1);
699 f6861a81 2018-09-13 stsp if (te1 &&
700 f6861a81 2018-09-13 stsp asprintf(&l1, "%s%s%s", label1,
701 f6861a81 2018-09-13 stsp label1[0] ? "/" : "", te1->name) == -1)
702 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
703 f6861a81 2018-09-13 stsp }
704 f6861a81 2018-09-13 stsp free(l2);
705 f6861a81 2018-09-13 stsp l2 = NULL;
706 f6861a81 2018-09-13 stsp if (te2) {
707 56e0773d 2019-11-28 stsp tidx2++;
708 56e0773d 2019-11-28 stsp te2 = got_object_tree_get_entry(tree2, tidx2);
709 f6861a81 2018-09-13 stsp if (te2 &&
710 f6861a81 2018-09-13 stsp asprintf(&l2, "%s%s%s", label2,
711 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te2->name) == -1)
712 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
713 f6861a81 2018-09-13 stsp }
714 474b4f94 2017-11-30 stsp } while (te1 || te2);
715 11528a82 2018-05-19 stsp
716 11528a82 2018-05-19 stsp return err;
717 11528a82 2018-05-19 stsp }
718 11528a82 2018-05-19 stsp
719 11528a82 2018-05-19 stsp const struct got_error *
720 fe621944 2020-11-10 stsp got_diff_objects_as_blobs(off_t **line_offsets, size_t *nlines,
721 fe621944 2020-11-10 stsp struct got_object_id *id1, struct got_object_id *id2,
722 54156555 2018-12-24 stsp const char *label1, const char *label2, int diff_context,
723 63035f9f 2019-10-06 stsp int ignore_whitespace, struct got_repository *repo, FILE *outfile)
724 11528a82 2018-05-19 stsp {
725 11528a82 2018-05-19 stsp const struct got_error *err;
726 11528a82 2018-05-19 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
727 b74c7625 2018-05-20 stsp
728 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
729 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
730 11528a82 2018-05-19 stsp
731 15a94983 2018-12-23 stsp if (id1) {
732 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob1, repo, id1, 8192);
733 cd0acaa7 2018-05-20 stsp if (err)
734 cd0acaa7 2018-05-20 stsp goto done;
735 cd0acaa7 2018-05-20 stsp }
736 15a94983 2018-12-23 stsp if (id2) {
737 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob2, repo, id2, 8192);
738 cd0acaa7 2018-05-20 stsp if (err)
739 cd0acaa7 2018-05-20 stsp goto done;
740 cd0acaa7 2018-05-20 stsp }
741 fe621944 2020-11-10 stsp err = got_diff_blob(line_offsets, nlines, blob1, blob2,
742 fe621944 2020-11-10 stsp label1, label2, diff_context, ignore_whitespace, outfile);
743 11528a82 2018-05-19 stsp done:
744 11528a82 2018-05-19 stsp if (blob1)
745 11528a82 2018-05-19 stsp got_object_blob_close(blob1);
746 11528a82 2018-05-19 stsp if (blob2)
747 11528a82 2018-05-19 stsp got_object_blob_close(blob2);
748 474b4f94 2017-11-30 stsp return err;
749 474b4f94 2017-11-30 stsp }
750 11528a82 2018-05-19 stsp
751 11528a82 2018-05-19 stsp const struct got_error *
752 fe621944 2020-11-10 stsp got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
753 fe621944 2020-11-10 stsp struct got_object_id *id1, struct got_object_id *id2,
754 63035f9f 2019-10-06 stsp char *label1, char *label2, int diff_context, int ignore_whitespace,
755 63035f9f 2019-10-06 stsp struct got_repository *repo, FILE *outfile)
756 11528a82 2018-05-19 stsp {
757 11528a82 2018-05-19 stsp const struct got_error *err;
758 11528a82 2018-05-19 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
759 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
760 fe621944 2020-11-10 stsp int want_lineoffsets = (line_offsets != NULL && *line_offsets != NULL);
761 11528a82 2018-05-19 stsp
762 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
763 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
764 b74c7625 2018-05-20 stsp
765 15a94983 2018-12-23 stsp if (id1) {
766 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo, id1);
767 cd0acaa7 2018-05-20 stsp if (err)
768 cd0acaa7 2018-05-20 stsp goto done;
769 cd0acaa7 2018-05-20 stsp }
770 15a94983 2018-12-23 stsp if (id2) {
771 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo, id2);
772 cd0acaa7 2018-05-20 stsp if (err)
773 cd0acaa7 2018-05-20 stsp goto done;
774 cd0acaa7 2018-05-20 stsp }
775 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
776 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
777 aaa13589 2019-06-01 stsp arg.outfile = outfile;
778 fe621944 2020-11-10 stsp if (want_lineoffsets) {
779 fe621944 2020-11-10 stsp arg.line_offsets = *line_offsets;
780 fe621944 2020-11-10 stsp arg.nlines = *nlines;
781 fe621944 2020-11-10 stsp } else {
782 fe621944 2020-11-10 stsp arg.line_offsets = NULL;
783 fe621944 2020-11-10 stsp arg.nlines = 0;
784 fe621944 2020-11-10 stsp }
785 aaa13589 2019-06-01 stsp err = got_diff_tree(tree1, tree2, label1, label2, repo,
786 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
787 fe621944 2020-11-10 stsp
788 fe621944 2020-11-10 stsp if (want_lineoffsets) {
789 fe621944 2020-11-10 stsp *line_offsets = arg.line_offsets; /* was likely re-allocated */
790 fe621944 2020-11-10 stsp *nlines = arg.nlines;
791 fe621944 2020-11-10 stsp }
792 11528a82 2018-05-19 stsp done:
793 11528a82 2018-05-19 stsp if (tree1)
794 11528a82 2018-05-19 stsp got_object_tree_close(tree1);
795 11528a82 2018-05-19 stsp if (tree2)
796 11528a82 2018-05-19 stsp got_object_tree_close(tree2);
797 11528a82 2018-05-19 stsp return err;
798 11528a82 2018-05-19 stsp }
799 11528a82 2018-05-19 stsp
800 11528a82 2018-05-19 stsp const struct got_error *
801 fe621944 2020-11-10 stsp got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
802 fe621944 2020-11-10 stsp struct got_object_id *id1, struct got_object_id *id2,
803 fe621944 2020-11-10 stsp int diff_context, int ignore_whitespace,
804 15a94983 2018-12-23 stsp struct got_repository *repo, FILE *outfile)
805 11528a82 2018-05-19 stsp {
806 11528a82 2018-05-19 stsp const struct got_error *err;
807 11528a82 2018-05-19 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
808 11528a82 2018-05-19 stsp
809 15a94983 2018-12-23 stsp if (id2 == NULL)
810 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
811 b74c7625 2018-05-20 stsp
812 15a94983 2018-12-23 stsp if (id1) {
813 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit1, repo, id1);
814 cd0acaa7 2018-05-20 stsp if (err)
815 cd0acaa7 2018-05-20 stsp goto done;
816 cd0acaa7 2018-05-20 stsp }
817 bacc9935 2018-05-20 stsp
818 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, repo, id2);
819 9b697879 2018-05-20 stsp if (err)
820 9b697879 2018-05-20 stsp goto done;
821 9b697879 2018-05-20 stsp
822 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(line_offsets, nlines,
823 15a94983 2018-12-23 stsp commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
824 63035f9f 2019-10-06 stsp got_object_commit_get_tree_id(commit2), "", "", diff_context,
825 63035f9f 2019-10-06 stsp ignore_whitespace, repo, outfile);
826 11528a82 2018-05-19 stsp done:
827 11528a82 2018-05-19 stsp if (commit1)
828 11528a82 2018-05-19 stsp got_object_commit_close(commit1);
829 11528a82 2018-05-19 stsp if (commit2)
830 11528a82 2018-05-19 stsp got_object_commit_close(commit2);
831 11528a82 2018-05-19 stsp return err;
832 11528a82 2018-05-19 stsp }
833 dc424a06 2019-08-07 stsp
834 dc424a06 2019-08-07 stsp const struct got_error *
835 fe621944 2020-11-10 stsp got_diff_files(struct got_diffreg_result **resultp,
836 fe621944 2020-11-10 stsp FILE *f1, const char *label1, FILE *f2, const char *label2,
837 fe621944 2020-11-10 stsp int diff_context, int ignore_whitespace, FILE *outfile)
838 dc424a06 2019-08-07 stsp {
839 dc424a06 2019-08-07 stsp const struct got_error *err = NULL;
840 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
841 dc424a06 2019-08-07 stsp
842 fe621944 2020-11-10 stsp if (resultp)
843 fe621944 2020-11-10 stsp *resultp = NULL;
844 dc424a06 2019-08-07 stsp
845 dc424a06 2019-08-07 stsp if (outfile) {
846 dc424a06 2019-08-07 stsp fprintf(outfile, "file - %s\n",
847 dc424a06 2019-08-07 stsp f1 == NULL ? "/dev/null" : label1);
848 dc424a06 2019-08-07 stsp fprintf(outfile, "file + %s\n",
849 dc424a06 2019-08-07 stsp f2 == NULL ? "/dev/null" : label2);
850 dc424a06 2019-08-07 stsp }
851 fe621944 2020-11-10 stsp
852 fe621944 2020-11-10 stsp err = got_diffreg(&diffreg_result, f1, f2, GOT_DIFF_ALGORITHM_PATIENCE,
853 fe621944 2020-11-10 stsp ignore_whitespace);
854 fe621944 2020-11-10 stsp if (err)
855 fe621944 2020-11-10 stsp goto done;
856 fe621944 2020-11-10 stsp
857 fe621944 2020-11-10 stsp if (outfile) {
858 fe621944 2020-11-10 stsp err = got_diffreg_output(NULL, NULL, diffreg_result,
859 fe621944 2020-11-10 stsp f1, f2, label1, label2, GOT_DIFF_OUTPUT_UNIDIFF,
860 fe621944 2020-11-10 stsp diff_context, outfile);
861 dc424a06 2019-08-07 stsp if (err)
862 dc424a06 2019-08-07 stsp goto done;
863 dc424a06 2019-08-07 stsp }
864 fe621944 2020-11-10 stsp
865 dc424a06 2019-08-07 stsp done:
866 fe621944 2020-11-10 stsp if (resultp && err == NULL)
867 fe621944 2020-11-10 stsp *resultp = diffreg_result;
868 fe621944 2020-11-10 stsp else if (diffreg_result) {
869 fe621944 2020-11-10 stsp const struct got_error *free_err;
870 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
871 fe621944 2020-11-10 stsp if (free_err && err == NULL)
872 fe621944 2020-11-10 stsp err = free_err;
873 dc424a06 2019-08-07 stsp }
874 fe621944 2020-11-10 stsp
875 dc424a06 2019-08-07 stsp return err;
876 dc424a06 2019-08-07 stsp }