Blame


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