Blame


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