2 7d283eee 2017-11-29 stsp * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 7d283eee 2017-11-29 stsp #include <sys/queue.h>
18 1c7f0520 2017-11-29 stsp #include <sys/stat.h>
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>
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"
34 718b3ab0 2018-03-17 stsp #include "got_lib_diff.h"
35 15a94983 2018-12-23 stsp #include "got_lib_delta.h"
36 15a94983 2018-12-23 stsp #include "got_lib_inflate.h"
37 15a94983 2018-12-23 stsp #include "got_lib_object.h"
39 404c43c4 2018-06-21 stsp static const struct got_error *
40 404c43c4 2018-06-21 stsp diff_blobs(struct got_blob_object *blob1, struct got_blob_object *blob2,
41 54156555 2018-12-24 stsp const char *label1, const char *label2, int diff_context, FILE *outfile,
42 54156555 2018-12-24 stsp struct got_diff_changes *changes)
44 ed9e98a8 2017-11-29 stsp struct got_diff_state ds;
45 8ba9a219 2017-11-29 stsp struct got_diff_args args;
46 7d283eee 2017-11-29 stsp const struct got_error *err = NULL;
47 4e22badc 2017-11-30 stsp FILE *f1 = NULL, *f2 = NULL;
48 f78b0693 2017-11-29 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
49 f78b0693 2017-11-29 stsp char hex2[SHA1_DIGEST_STRING_LENGTH];
50 98abbc84 2017-11-30 stsp char *idstr1 = NULL, *idstr2 = NULL;
51 f934cf2c 2018-02-12 stsp size_t size1, size2;
52 4e22badc 2017-11-30 stsp int res, flags = 0;
54 4e22badc 2017-11-30 stsp if (blob1) {
55 a1fd68d8 2018-01-12 stsp f1 = got_opentemp();
56 4e22badc 2017-11-30 stsp if (f1 == NULL)
57 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentemp");
59 4e22badc 2017-11-30 stsp flags |= D_EMPTY1;
61 4e22badc 2017-11-30 stsp if (blob2) {
62 a1fd68d8 2018-01-12 stsp f2 = got_opentemp();
63 4e22badc 2017-11-30 stsp if (f2 == NULL) {
64 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
69 4e22badc 2017-11-30 stsp flags |= D_EMPTY2;
72 98abbc84 2017-11-30 stsp if (blob1) {
73 f934cf2c 2018-02-12 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
74 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
79 98abbc84 2017-11-30 stsp idstr1 = "/dev/null";
82 98abbc84 2017-11-30 stsp if (blob2) {
83 f934cf2c 2018-02-12 stsp idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
84 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size2, NULL, NULL, f2,
89 98abbc84 2017-11-30 stsp idstr2 = "/dev/null";
91 ed9e98a8 2017-11-29 stsp memset(&ds, 0, sizeof(ds));
92 f9d67749 2017-11-30 stsp /* XXX should stat buffers be passed in args instead of ds? */
93 f9d67749 2017-11-30 stsp ds.stb1.st_mode = S_IFREG;
95 f934cf2c 2018-02-12 stsp ds.stb1.st_size = size1;
96 f9d67749 2017-11-30 stsp ds.stb1.st_mtime = 0; /* XXX */
98 f9d67749 2017-11-30 stsp ds.stb2.st_mode = S_IFREG;
100 f934cf2c 2018-02-12 stsp ds.stb2.st_size = size2;
101 f9d67749 2017-11-30 stsp ds.stb2.st_mtime = 0; /* XXX */
103 54156555 2018-12-24 stsp memset(&args, 0, sizeof(args));
104 8ba9a219 2017-11-29 stsp args.diff_format = D_UNIFIED;
105 54156555 2018-12-24 stsp args.label[0] = label1 ? label1 : idstr1;
106 54156555 2018-12-24 stsp args.label[1] = label2 ? label2 : idstr2;
107 df2871d2 2018-10-18 stsp args.diff_context = diff_context;
108 84eb021e 2018-03-27 stsp flags |= D_PROTOTYPE;
110 09de383e 2018-12-24 stsp if (outfile) {
111 09de383e 2018-12-24 stsp fprintf(outfile, "blob - %s\n", idstr1);
112 09de383e 2018-12-24 stsp fprintf(outfile, "blob + %s\n", idstr2);
114 404c43c4 2018-06-21 stsp err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, changes);
115 dc424a06 2019-08-07 stsp got_diff_state_free(&ds);
117 fb43ecf1 2019-02-11 stsp if (f1 && fclose(f1) != 0 && err == NULL)
118 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
119 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
120 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
121 7d283eee 2017-11-29 stsp return err;
124 aaa13589 2019-06-01 stsp const struct got_error *
125 aaa13589 2019-06-01 stsp got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
126 aaa13589 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
127 aaa13589 2019-06-01 stsp struct got_object_id *id2, const char *label1, const char *label2,
128 aaa13589 2019-06-01 stsp struct got_repository *repo)
130 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg *a = arg;
132 aaa13589 2019-06-01 stsp return diff_blobs(blob1, blob2, label1, label2, a->diff_context,
133 aaa13589 2019-06-01 stsp a->outfile, NULL);
136 404c43c4 2018-06-21 stsp const struct got_error *
137 404c43c4 2018-06-21 stsp got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
138 54156555 2018-12-24 stsp const char *label1, const char *label2, int diff_context, FILE *outfile)
140 54156555 2018-12-24 stsp return diff_blobs(blob1, blob2, label1, label2, diff_context, outfile,
144 7f1f93af 2019-08-06 stsp static const struct got_error *
145 7f1f93af 2019-08-06 stsp alloc_changes(struct got_diff_changes **changes)
147 7f1f93af 2019-08-06 stsp *changes = calloc(1, sizeof(**changes));
148 7f1f93af 2019-08-06 stsp if (*changes == NULL)
149 7f1f93af 2019-08-06 stsp return got_error_from_errno("calloc");
150 7f1f93af 2019-08-06 stsp SIMPLEQ_INIT(&(*changes)->entries);
151 7f1f93af 2019-08-06 stsp return NULL;
154 7f1f93af 2019-08-06 stsp static const struct got_error *
155 7f1f93af 2019-08-06 stsp diff_blob_file(struct got_diff_changes **changes,
156 4ce46740 2019-08-08 stsp struct got_blob_object *blob1, const char *label1, FILE *f2, size_t size2,
157 b72f483a 2019-02-05 stsp const char *label2, int diff_context, FILE *outfile)
159 b72f483a 2019-02-05 stsp struct got_diff_state ds;
160 b72f483a 2019-02-05 stsp struct got_diff_args args;
161 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
162 b72f483a 2019-02-05 stsp FILE *f1 = NULL;
163 b72f483a 2019-02-05 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
164 b72f483a 2019-02-05 stsp char *idstr1 = NULL;
165 b72f483a 2019-02-05 stsp size_t size1;
166 b72f483a 2019-02-05 stsp int res, flags = 0;
168 7f1f93af 2019-08-06 stsp if (changes)
169 7f1f93af 2019-08-06 stsp *changes = NULL;
172 b72f483a 2019-02-05 stsp if (blob1) {
173 b72f483a 2019-02-05 stsp f1 = got_opentemp();
174 b72f483a 2019-02-05 stsp if (f1 == NULL)
175 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentemp");
176 b72f483a 2019-02-05 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
177 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
182 b72f483a 2019-02-05 stsp flags |= D_EMPTY1;
183 b72f483a 2019-02-05 stsp idstr1 = "/dev/null";
186 b72f483a 2019-02-05 stsp if (f2 == NULL)
187 b72f483a 2019-02-05 stsp flags |= D_EMPTY2;
189 b72f483a 2019-02-05 stsp memset(&ds, 0, sizeof(ds));
190 b72f483a 2019-02-05 stsp /* XXX should stat buffers be passed in args instead of ds? */
191 b72f483a 2019-02-05 stsp ds.stb1.st_mode = S_IFREG;
193 b72f483a 2019-02-05 stsp ds.stb1.st_size = size1;
194 b72f483a 2019-02-05 stsp ds.stb1.st_mtime = 0; /* XXX */
196 b72f483a 2019-02-05 stsp ds.stb2.st_mode = S_IFREG;
197 b72f483a 2019-02-05 stsp ds.stb2.st_size = size2;
198 b72f483a 2019-02-05 stsp ds.stb2.st_mtime = 0; /* XXX */
200 b72f483a 2019-02-05 stsp memset(&args, 0, sizeof(args));
201 b72f483a 2019-02-05 stsp args.diff_format = D_UNIFIED;
202 b72f483a 2019-02-05 stsp args.label[0] = label2;
203 b72f483a 2019-02-05 stsp args.label[1] = label2;
204 b72f483a 2019-02-05 stsp args.diff_context = diff_context;
205 b72f483a 2019-02-05 stsp flags |= D_PROTOTYPE;
207 7f1f93af 2019-08-06 stsp if (outfile) {
208 4ce46740 2019-08-08 stsp fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
209 7f1f93af 2019-08-06 stsp fprintf(outfile, "file + %s\n",
210 7f1f93af 2019-08-06 stsp f2 == NULL ? "/dev/null" : label2);
212 7f1f93af 2019-08-06 stsp if (changes) {
213 7f1f93af 2019-08-06 stsp err = alloc_changes(changes);
215 7f1f93af 2019-08-06 stsp return err;
217 7f1f93af 2019-08-06 stsp err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile,
218 7f1f93af 2019-08-06 stsp changes ? *changes : NULL);
219 dc424a06 2019-08-07 stsp got_diff_state_free(&ds);
221 fb43ecf1 2019-02-11 stsp if (f1 && fclose(f1) != 0 && err == NULL)
222 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
223 b72f483a 2019-02-05 stsp return err;
226 7f1f93af 2019-08-06 stsp const struct got_error *
227 4ce46740 2019-08-08 stsp got_diff_blob_file(struct got_blob_object *blob1, const char *label1,
228 4ce46740 2019-08-08 stsp FILE *f2, size_t size2, const char *label2, int diff_context,
229 4ce46740 2019-08-08 stsp FILE *outfile)
231 4ce46740 2019-08-08 stsp return diff_blob_file(NULL, blob1, label1, f2, size2, label2,
232 4ce46740 2019-08-08 stsp diff_context, outfile);
235 4c9641fd 2019-08-21 stsp const struct got_error *
236 4c9641fd 2019-08-21 stsp got_diff_blob_file_lines_changed(struct got_diff_changes **changes,
237 4c9641fd 2019-08-21 stsp struct got_blob_object *blob1, FILE *f2, size_t size2)
239 4c9641fd 2019-08-21 stsp return diff_blob_file(changes, blob1, NULL, f2, size2, NULL,
243 7f1f93af 2019-08-06 stsp const struct got_error *
244 404c43c4 2018-06-21 stsp got_diff_blob_lines_changed(struct got_diff_changes **changes,
245 404c43c4 2018-06-21 stsp struct got_blob_object *blob1, struct got_blob_object *blob2)
247 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
249 7f1f93af 2019-08-06 stsp err = alloc_changes(changes);
251 7f1f93af 2019-08-06 stsp return err;
253 54156555 2018-12-24 stsp err = diff_blobs(blob1, blob2, NULL, NULL, 3, NULL, *changes);
255 404c43c4 2018-06-21 stsp got_diff_free_changes(*changes);
256 404c43c4 2018-06-21 stsp *changes = NULL;
258 404c43c4 2018-06-21 stsp return err;
262 404c43c4 2018-06-21 stsp got_diff_free_changes(struct got_diff_changes *changes)
264 404c43c4 2018-06-21 stsp struct got_diff_change *change;
265 404c43c4 2018-06-21 stsp while (!SIMPLEQ_EMPTY(&changes->entries)) {
266 404c43c4 2018-06-21 stsp change = SIMPLEQ_FIRST(&changes->entries);
267 404c43c4 2018-06-21 stsp SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
268 404c43c4 2018-06-21 stsp free(change);
270 404c43c4 2018-06-21 stsp free(changes);
273 474b4f94 2017-11-30 stsp static const struct got_error *
274 54156555 2018-12-24 stsp diff_added_blob(struct got_object_id *id, const char *label,
275 aaa13589 2019-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
277 4e22badc 2017-11-30 stsp const struct got_error *err;
278 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
279 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
281 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
283 4e22badc 2017-11-30 stsp return err;
285 2acfca77 2018-04-01 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
288 aaa13589 2019-06-01 stsp err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, repo);
290 2acfca77 2018-04-01 stsp got_object_close(obj);
292 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
293 2acfca77 2018-04-01 stsp return err;
296 474b4f94 2017-11-30 stsp static const struct got_error *
297 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
298 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
299 aaa13589 2019-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
301 6a213ccb 2017-11-30 stsp const struct got_error *err;
302 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
303 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
304 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
305 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
307 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
309 730a8aa0 2018-04-24 stsp return err;
310 15a94983 2018-12-23 stsp if (obj1->type != GOT_OBJ_TYPE_BLOB) {
311 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
315 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
318 15a94983 2018-12-23 stsp if (obj2->type != GOT_OBJ_TYPE_BLOB) {
319 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
323 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob1, repo, obj1, 8192);
327 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob2, repo, obj2, 8192);
331 aaa13589 2019-06-01 stsp err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, repo);
334 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
336 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
338 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
340 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
341 6a213ccb 2017-11-30 stsp return err;
344 474b4f94 2017-11-30 stsp static const struct got_error *
345 f6861a81 2018-09-13 stsp diff_deleted_blob(struct got_object_id *id, const char *label,
346 aaa13589 2019-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
348 365fb436 2017-11-30 stsp const struct got_error *err;
349 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
350 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
352 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
354 365fb436 2017-11-30 stsp return err;
356 2acfca77 2018-04-01 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
359 aaa13589 2019-06-01 stsp err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, repo);
361 2acfca77 2018-04-01 stsp got_object_close(obj);
363 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
364 2acfca77 2018-04-01 stsp return err;
367 474b4f94 2017-11-30 stsp static const struct got_error *
368 54156555 2018-12-24 stsp diff_added_tree(struct got_object_id *id, const char *label,
369 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
370 31b4484f 2019-07-27 stsp int diff_content)
372 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
373 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
374 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
376 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
380 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
381 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
385 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
389 31b4484f 2019-07-27 stsp err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
390 31b4484f 2019-07-27 stsp diff_content);
393 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
394 9c70d4c3 2017-11-30 stsp if (treeobj)
395 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
396 9c70d4c3 2017-11-30 stsp return err;
399 474b4f94 2017-11-30 stsp static const struct got_error *
400 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
401 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
402 31b4484f 2019-07-27 stsp got_diff_blob_cb cb, void *cb_arg, int diff_content)
404 f6861a81 2018-09-13 stsp const struct got_error *err;
405 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
406 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
407 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
408 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
410 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
414 15a94983 2018-12-23 stsp if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
415 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
419 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
423 15a94983 2018-12-23 stsp if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
424 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
428 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
432 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
436 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
437 31b4484f 2019-07-27 stsp diff_content);
441 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
443 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
444 789689b5 2017-11-30 stsp if (treeobj1)
445 789689b5 2017-11-30 stsp got_object_close(treeobj1);
446 789689b5 2017-11-30 stsp if (treeobj2)
447 789689b5 2017-11-30 stsp got_object_close(treeobj2);
448 789689b5 2017-11-30 stsp return err;
451 474b4f94 2017-11-30 stsp static const struct got_error *
452 54156555 2018-12-24 stsp diff_deleted_tree(struct got_object_id *id, const char *label,
453 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
454 31b4484f 2019-07-27 stsp int diff_content)
456 f6861a81 2018-09-13 stsp const struct got_error *err;
457 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
458 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
460 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
464 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
465 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
469 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
473 31b4484f 2019-07-27 stsp err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
474 31b4484f 2019-07-27 stsp diff_content);
477 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
478 2c56f2ce 2017-11-30 stsp if (treeobj)
479 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
480 2c56f2ce 2017-11-30 stsp return err;
483 474b4f94 2017-11-30 stsp static const struct got_error *
484 74671950 2018-02-11 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
485 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
486 aaa13589 2019-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
488 013404a9 2017-11-30 stsp /* XXX TODO */
489 474b4f94 2017-11-30 stsp return NULL;
492 474b4f94 2017-11-30 stsp static const struct got_error *
493 1de5e065 2019-06-01 stsp diff_entry_old_new(const struct got_tree_entry *te1,
494 1de5e065 2019-06-01 stsp const struct got_tree_entry *te2, const char *label1, const char *label2,
495 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
496 31b4484f 2019-07-27 stsp int diff_content)
498 f6861a81 2018-09-13 stsp const struct got_error *err = NULL;
499 19ae6da1 2018-11-05 stsp int id_match;
501 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te1))
502 63c5ca5d 2019-08-24 stsp return NULL;
504 474b4f94 2017-11-30 stsp if (te2 == NULL) {
505 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
506 aaa13589 2019-06-01 stsp err = diff_deleted_tree(te1->id, label1, repo,
507 31b4484f 2019-07-27 stsp cb, cb_arg, diff_content);
509 31b4484f 2019-07-27 stsp if (diff_content)
510 31b4484f 2019-07-27 stsp err = diff_deleted_blob(te1->id, label1, repo,
511 31b4484f 2019-07-27 stsp cb, cb_arg);
513 31b4484f 2019-07-27 stsp err = cb(cb_arg, NULL, NULL, te1->id, NULL,
514 31b4484f 2019-07-27 stsp label1, NULL, repo);
516 f6861a81 2018-09-13 stsp return err;
517 63c5ca5d 2019-08-24 stsp } else if (got_object_tree_entry_is_submodule(te2))
518 63c5ca5d 2019-08-24 stsp return NULL;
520 19ae6da1 2018-11-05 stsp id_match = (got_object_id_cmp(te1->id, te2->id) == 0);
521 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
522 19ae6da1 2018-11-05 stsp if (!id_match)
523 f6861a81 2018-09-13 stsp return diff_modified_tree(te1->id, te2->id,
524 31b4484f 2019-07-27 stsp label1, label2, repo, cb, cb_arg, diff_content);
525 4209f790 2017-11-30 stsp } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
526 31b4484f 2019-07-27 stsp if (!id_match) {
527 31b4484f 2019-07-27 stsp if (diff_content)
528 31b4484f 2019-07-27 stsp return diff_modified_blob(te1->id, te2->id,
529 31b4484f 2019-07-27 stsp label1, label2, repo, cb, cb_arg);
531 31b4484f 2019-07-27 stsp return cb(cb_arg, NULL, NULL, te1->id,
532 31b4484f 2019-07-27 stsp te2->id, label1, label2, repo);
536 19ae6da1 2018-11-05 stsp if (id_match)
537 f6861a81 2018-09-13 stsp return NULL;
539 aaa13589 2019-06-01 stsp return diff_kind_mismatch(te1->id, te2->id, label1, label2, repo,
540 aaa13589 2019-06-01 stsp cb, cb_arg);
543 474b4f94 2017-11-30 stsp static const struct got_error *
544 1de5e065 2019-06-01 stsp diff_entry_new_old(const struct got_tree_entry *te2,
545 aaa13589 2019-06-01 stsp const struct got_tree_entry *te1, const char *label2,
546 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
547 31b4484f 2019-07-27 stsp int diff_content)
549 f6861a81 2018-09-13 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
550 63c5ca5d 2019-08-24 stsp return NULL;
552 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te2))
553 f6861a81 2018-09-13 stsp return NULL;
555 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
556 31b4484f 2019-07-27 stsp return diff_added_tree(te2->id, label2, repo, cb, cb_arg,
557 31b4484f 2019-07-27 stsp diff_content);
559 31b4484f 2019-07-27 stsp if (diff_content)
560 31b4484f 2019-07-27 stsp return diff_added_blob(te2->id, label2, repo, cb, cb_arg);
562 31b4484f 2019-07-27 stsp return cb(cb_arg, NULL, NULL, NULL, te2->id, NULL, label2, repo);
565 474b4f94 2017-11-30 stsp const struct got_error *
566 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
567 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
568 31b4484f 2019-07-27 stsp got_diff_blob_cb cb, void *cb_arg, int diff_content)
570 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
571 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
572 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
573 f6861a81 2018-09-13 stsp char *l1 = NULL, *l2 = NULL;
575 883f0469 2018-06-23 stsp if (tree1) {
576 883f0469 2018-06-23 stsp const struct got_tree_entries *entries;
577 883f0469 2018-06-23 stsp entries = got_object_tree_get_entries(tree1);
578 883f0469 2018-06-23 stsp te1 = SIMPLEQ_FIRST(&entries->head);
579 60f50a58 2018-09-15 stsp if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
580 f6861a81 2018-09-13 stsp te1->name) == -1)
581 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
583 883f0469 2018-06-23 stsp if (tree2) {
584 883f0469 2018-06-23 stsp const struct got_tree_entries *entries;
585 883f0469 2018-06-23 stsp entries = got_object_tree_get_entries(tree2);
586 883f0469 2018-06-23 stsp te2 = SIMPLEQ_FIRST(&entries->head);
587 60f50a58 2018-09-15 stsp if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
588 f6861a81 2018-09-13 stsp te2->name) == -1)
589 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
594 1de5e065 2019-06-01 stsp const struct got_tree_entry *te = NULL;
596 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree2,
597 1de5e065 2019-06-01 stsp te1->name);
601 f6861a81 2018-09-13 stsp if (te && asprintf(&l2, "%s%s%s", label2,
602 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te->name) == -1)
604 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
606 aaa13589 2019-06-01 stsp err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
607 31b4484f 2019-07-27 stsp cb_arg, diff_content);
613 1de5e065 2019-06-01 stsp const struct got_tree_entry *te = NULL;
615 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree1,
616 1de5e065 2019-06-01 stsp te2->name);
619 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
620 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te->name) == -1)
622 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
624 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
625 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te2->name) == -1)
627 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
629 31b4484f 2019-07-27 stsp err = diff_entry_new_old(te2, te, l2, repo,
630 31b4484f 2019-07-27 stsp cb, cb_arg, diff_content);
638 474b4f94 2017-11-30 stsp te1 = SIMPLEQ_NEXT(te1, entry);
640 f6861a81 2018-09-13 stsp asprintf(&l1, "%s%s%s", label1,
641 f6861a81 2018-09-13 stsp label1[0] ? "/" : "", te1->name) == -1)
642 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
647 474b4f94 2017-11-30 stsp te2 = SIMPLEQ_NEXT(te2, entry);
649 f6861a81 2018-09-13 stsp asprintf(&l2, "%s%s%s", label2,
650 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te2->name) == -1)
651 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
653 474b4f94 2017-11-30 stsp } while (te1 || te2);
655 11528a82 2018-05-19 stsp return err;
658 11528a82 2018-05-19 stsp const struct got_error *
659 15a94983 2018-12-23 stsp got_diff_objects_as_blobs(struct got_object_id *id1, struct got_object_id *id2,
660 54156555 2018-12-24 stsp const char *label1, const char *label2, int diff_context,
661 54156555 2018-12-24 stsp struct got_repository *repo, FILE *outfile)
663 11528a82 2018-05-19 stsp const struct got_error *err;
664 11528a82 2018-05-19 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
666 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
667 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
670 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob1, repo, id1, 8192);
675 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob2, repo, id2, 8192);
679 54156555 2018-12-24 stsp err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
683 11528a82 2018-05-19 stsp got_object_blob_close(blob1);
685 11528a82 2018-05-19 stsp got_object_blob_close(blob2);
686 474b4f94 2017-11-30 stsp return err;
689 11528a82 2018-05-19 stsp const struct got_error *
690 15a94983 2018-12-23 stsp got_diff_objects_as_trees(struct got_object_id *id1, struct got_object_id *id2,
691 54156555 2018-12-24 stsp char *label1, char *label2, int diff_context, struct got_repository *repo,
692 54156555 2018-12-24 stsp FILE *outfile)
694 11528a82 2018-05-19 stsp const struct got_error *err;
695 11528a82 2018-05-19 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
696 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
698 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
699 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
702 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo, id1);
707 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo, id2);
711 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
712 aaa13589 2019-06-01 stsp arg.outfile = outfile;
713 aaa13589 2019-06-01 stsp err = got_diff_tree(tree1, tree2, label1, label2, repo,
714 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
717 11528a82 2018-05-19 stsp got_object_tree_close(tree1);
719 11528a82 2018-05-19 stsp got_object_tree_close(tree2);
720 11528a82 2018-05-19 stsp return err;
723 11528a82 2018-05-19 stsp const struct got_error *
724 15a94983 2018-12-23 stsp got_diff_objects_as_commits(struct got_object_id *id1,
725 15a94983 2018-12-23 stsp struct got_object_id *id2, int diff_context,
726 15a94983 2018-12-23 stsp struct got_repository *repo, FILE *outfile)
728 11528a82 2018-05-19 stsp const struct got_error *err;
729 11528a82 2018-05-19 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
731 15a94983 2018-12-23 stsp if (id2 == NULL)
732 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
735 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit1, repo, id1);
740 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, repo, id2);
744 15a94983 2018-12-23 stsp err = got_diff_objects_as_trees(
745 15a94983 2018-12-23 stsp commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
746 54156555 2018-12-24 stsp got_object_commit_get_tree_id(commit2), "", "", diff_context, repo,
749 11528a82 2018-05-19 stsp if (commit1)
750 11528a82 2018-05-19 stsp got_object_commit_close(commit1);
751 11528a82 2018-05-19 stsp if (commit2)
752 11528a82 2018-05-19 stsp got_object_commit_close(commit2);
753 11528a82 2018-05-19 stsp return err;
756 dc424a06 2019-08-07 stsp const struct got_error *
757 dc424a06 2019-08-07 stsp got_diff_files(struct got_diff_changes **changes,
758 dc424a06 2019-08-07 stsp struct got_diff_state **ds,
759 dc424a06 2019-08-07 stsp struct got_diff_args **args,
760 dc424a06 2019-08-07 stsp int *flags,
761 dc424a06 2019-08-07 stsp FILE *f1, size_t size1, const char *label1,
762 dc424a06 2019-08-07 stsp FILE *f2, size_t size2, const char *label2,
763 dc424a06 2019-08-07 stsp int diff_context, FILE *outfile)
765 dc424a06 2019-08-07 stsp const struct got_error *err = NULL;
768 dc424a06 2019-08-07 stsp *flags = 0;
769 dc424a06 2019-08-07 stsp *ds = calloc(1, sizeof(**ds));
770 dc424a06 2019-08-07 stsp if (*ds == NULL)
771 dc424a06 2019-08-07 stsp return got_error_from_errno("calloc");
772 dc424a06 2019-08-07 stsp *args = calloc(1, sizeof(**args));
773 dc424a06 2019-08-07 stsp if (*args == NULL) {
774 dc424a06 2019-08-07 stsp err = got_error_from_errno("calloc");
778 dc424a06 2019-08-07 stsp if (changes)
779 dc424a06 2019-08-07 stsp *changes = NULL;
781 dc424a06 2019-08-07 stsp if (f1 == NULL)
782 dc424a06 2019-08-07 stsp *flags |= D_EMPTY1;
784 dc424a06 2019-08-07 stsp if (f2 == NULL)
785 dc424a06 2019-08-07 stsp *flags |= D_EMPTY2;
787 dc424a06 2019-08-07 stsp /* XXX should stat buffers be passed in args instead of ds? */
788 dc424a06 2019-08-07 stsp (*ds)->stb1.st_mode = S_IFREG;
789 dc424a06 2019-08-07 stsp (*ds)->stb1.st_size = size1;
790 dc424a06 2019-08-07 stsp (*ds)->stb1.st_mtime = 0; /* XXX */
792 dc424a06 2019-08-07 stsp (*ds)->stb2.st_mode = S_IFREG;
793 dc424a06 2019-08-07 stsp (*ds)->stb2.st_size = size2;
794 dc424a06 2019-08-07 stsp (*ds)->stb2.st_mtime = 0; /* XXX */
796 dc424a06 2019-08-07 stsp (*args)->diff_format = D_UNIFIED;
797 dc424a06 2019-08-07 stsp (*args)->label[0] = label1;
798 dc424a06 2019-08-07 stsp (*args)->label[1] = label2;
799 dc424a06 2019-08-07 stsp (*args)->diff_context = diff_context;
800 dc424a06 2019-08-07 stsp *flags |= D_PROTOTYPE;
802 dc424a06 2019-08-07 stsp if (outfile) {
803 dc424a06 2019-08-07 stsp fprintf(outfile, "file - %s\n",
804 dc424a06 2019-08-07 stsp f1 == NULL ? "/dev/null" : label1);
805 dc424a06 2019-08-07 stsp fprintf(outfile, "file + %s\n",
806 dc424a06 2019-08-07 stsp f2 == NULL ? "/dev/null" : label2);
808 dc424a06 2019-08-07 stsp if (changes) {
809 dc424a06 2019-08-07 stsp err = alloc_changes(changes);
813 dc424a06 2019-08-07 stsp err = got_diffreg(&res, f1, f2, *flags, *args, *ds, outfile,
814 dc424a06 2019-08-07 stsp changes ? *changes : NULL);
818 dc424a06 2019-08-07 stsp got_diff_state_free(*ds);
820 dc424a06 2019-08-07 stsp *ds = NULL;
822 dc424a06 2019-08-07 stsp if (*args) {
823 dc424a06 2019-08-07 stsp free(*args);
824 dc424a06 2019-08-07 stsp *args = NULL;
826 dc424a06 2019-08-07 stsp if (changes) {
827 dc424a06 2019-08-07 stsp if (*changes)
828 dc424a06 2019-08-07 stsp got_diff_free_changes(*changes);
829 dc424a06 2019-08-07 stsp *changes = NULL;
832 dc424a06 2019-08-07 stsp return err;