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