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