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