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