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);
116 fb43ecf1 2019-02-11 stsp if (f1 && fclose(f1) != 0 && err == NULL)
117 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
118 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
119 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
120 7d283eee 2017-11-29 stsp return err;
123 aaa13589 2019-06-01 stsp const struct got_error *
124 aaa13589 2019-06-01 stsp got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
125 aaa13589 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
126 aaa13589 2019-06-01 stsp struct got_object_id *id2, const char *label1, const char *label2,
127 aaa13589 2019-06-01 stsp struct got_repository *repo)
129 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg *a = arg;
131 aaa13589 2019-06-01 stsp return diff_blobs(blob1, blob2, label1, label2, a->diff_context,
132 aaa13589 2019-06-01 stsp a->outfile, NULL);
135 404c43c4 2018-06-21 stsp const struct got_error *
136 404c43c4 2018-06-21 stsp got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
137 54156555 2018-12-24 stsp const char *label1, const char *label2, int diff_context, FILE *outfile)
139 54156555 2018-12-24 stsp return diff_blobs(blob1, blob2, label1, label2, diff_context, outfile,
143 404c43c4 2018-06-21 stsp const struct got_error *
144 b72f483a 2019-02-05 stsp got_diff_blob_file(struct got_blob_object *blob1, FILE *f2, size_t size2,
145 b72f483a 2019-02-05 stsp const char *label2, int diff_context, FILE *outfile)
147 b72f483a 2019-02-05 stsp struct got_diff_state ds;
148 b72f483a 2019-02-05 stsp struct got_diff_args args;
149 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
150 b72f483a 2019-02-05 stsp FILE *f1 = NULL;
151 b72f483a 2019-02-05 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
152 b72f483a 2019-02-05 stsp char *idstr1 = NULL;
153 b72f483a 2019-02-05 stsp size_t size1;
154 b72f483a 2019-02-05 stsp int res, flags = 0;
157 b72f483a 2019-02-05 stsp if (blob1) {
158 b72f483a 2019-02-05 stsp f1 = got_opentemp();
159 b72f483a 2019-02-05 stsp if (f1 == NULL)
160 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentemp");
161 b72f483a 2019-02-05 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
162 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
167 b72f483a 2019-02-05 stsp flags |= D_EMPTY1;
168 b72f483a 2019-02-05 stsp idstr1 = "/dev/null";
171 b72f483a 2019-02-05 stsp if (f2 == NULL)
172 b72f483a 2019-02-05 stsp flags |= D_EMPTY2;
174 b72f483a 2019-02-05 stsp memset(&ds, 0, sizeof(ds));
175 b72f483a 2019-02-05 stsp /* XXX should stat buffers be passed in args instead of ds? */
176 b72f483a 2019-02-05 stsp ds.stb1.st_mode = S_IFREG;
178 b72f483a 2019-02-05 stsp ds.stb1.st_size = size1;
179 b72f483a 2019-02-05 stsp ds.stb1.st_mtime = 0; /* XXX */
181 b72f483a 2019-02-05 stsp ds.stb2.st_mode = S_IFREG;
182 b72f483a 2019-02-05 stsp ds.stb2.st_size = size2;
183 b72f483a 2019-02-05 stsp ds.stb2.st_mtime = 0; /* XXX */
185 b72f483a 2019-02-05 stsp memset(&args, 0, sizeof(args));
186 b72f483a 2019-02-05 stsp args.diff_format = D_UNIFIED;
187 b72f483a 2019-02-05 stsp args.label[0] = label2;
188 b72f483a 2019-02-05 stsp args.label[1] = label2;
189 b72f483a 2019-02-05 stsp args.diff_context = diff_context;
190 b72f483a 2019-02-05 stsp flags |= D_PROTOTYPE;
192 b72f483a 2019-02-05 stsp fprintf(outfile, "blob - %s\n", idstr1);
193 049da17d 2019-03-26 stsp fprintf(outfile, "file + %s\n", f2 == NULL ? "/dev/null" : label2);
194 b72f483a 2019-02-05 stsp err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, NULL);
196 fb43ecf1 2019-02-11 stsp if (f1 && fclose(f1) != 0 && err == NULL)
197 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
198 b72f483a 2019-02-05 stsp return err;
201 b72f483a 2019-02-05 stsp const struct got_error *
202 404c43c4 2018-06-21 stsp got_diff_blob_lines_changed(struct got_diff_changes **changes,
203 404c43c4 2018-06-21 stsp struct got_blob_object *blob1, struct got_blob_object *blob2)
205 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
207 404c43c4 2018-06-21 stsp *changes = calloc(1, sizeof(**changes));
208 404c43c4 2018-06-21 stsp if (*changes == NULL)
209 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
210 404c43c4 2018-06-21 stsp SIMPLEQ_INIT(&(*changes)->entries);
212 54156555 2018-12-24 stsp err = diff_blobs(blob1, blob2, NULL, NULL, 3, NULL, *changes);
214 404c43c4 2018-06-21 stsp got_diff_free_changes(*changes);
215 404c43c4 2018-06-21 stsp *changes = NULL;
217 404c43c4 2018-06-21 stsp return err;
221 404c43c4 2018-06-21 stsp got_diff_free_changes(struct got_diff_changes *changes)
223 404c43c4 2018-06-21 stsp struct got_diff_change *change;
224 404c43c4 2018-06-21 stsp while (!SIMPLEQ_EMPTY(&changes->entries)) {
225 404c43c4 2018-06-21 stsp change = SIMPLEQ_FIRST(&changes->entries);
226 404c43c4 2018-06-21 stsp SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
227 404c43c4 2018-06-21 stsp free(change);
229 404c43c4 2018-06-21 stsp free(changes);
232 474b4f94 2017-11-30 stsp static const struct got_error *
233 54156555 2018-12-24 stsp diff_added_blob(struct got_object_id *id, const char *label,
234 aaa13589 2019-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
236 4e22badc 2017-11-30 stsp const struct got_error *err;
237 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
238 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
240 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
242 4e22badc 2017-11-30 stsp return err;
244 2acfca77 2018-04-01 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
247 aaa13589 2019-06-01 stsp err = cb(cb_arg, NULL, blob, NULL, id, NULL, label, repo);
249 2acfca77 2018-04-01 stsp got_object_close(obj);
251 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
252 2acfca77 2018-04-01 stsp return err;
255 474b4f94 2017-11-30 stsp static const struct got_error *
256 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
257 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
258 aaa13589 2019-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
260 6a213ccb 2017-11-30 stsp const struct got_error *err;
261 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
262 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
263 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
264 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
266 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
268 730a8aa0 2018-04-24 stsp return err;
269 15a94983 2018-12-23 stsp if (obj1->type != GOT_OBJ_TYPE_BLOB) {
270 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
274 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
277 15a94983 2018-12-23 stsp if (obj2->type != GOT_OBJ_TYPE_BLOB) {
278 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
282 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob1, repo, obj1, 8192);
286 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob2, repo, obj2, 8192);
290 aaa13589 2019-06-01 stsp err = cb(cb_arg, blob1, blob2, id1, id2, label1, label2, repo);
293 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
295 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
297 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
299 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
300 6a213ccb 2017-11-30 stsp return err;
303 474b4f94 2017-11-30 stsp static const struct got_error *
304 f6861a81 2018-09-13 stsp diff_deleted_blob(struct got_object_id *id, const char *label,
305 aaa13589 2019-06-01 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
307 365fb436 2017-11-30 stsp const struct got_error *err;
308 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
309 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
311 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
313 365fb436 2017-11-30 stsp return err;
315 2acfca77 2018-04-01 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
318 aaa13589 2019-06-01 stsp err = cb(cb_arg, blob, NULL, id, NULL, label, NULL, repo);
320 2acfca77 2018-04-01 stsp got_object_close(obj);
322 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
323 2acfca77 2018-04-01 stsp return err;
326 474b4f94 2017-11-30 stsp static const struct got_error *
327 54156555 2018-12-24 stsp diff_added_tree(struct got_object_id *id, const char *label,
328 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
329 31b4484f 2019-07-27 stsp int diff_content)
331 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
332 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
333 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
335 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
339 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
340 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
344 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
348 31b4484f 2019-07-27 stsp err = got_diff_tree(NULL, tree, NULL, label, repo, cb, cb_arg,
349 31b4484f 2019-07-27 stsp diff_content);
352 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
353 9c70d4c3 2017-11-30 stsp if (treeobj)
354 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
355 9c70d4c3 2017-11-30 stsp return err;
358 474b4f94 2017-11-30 stsp static const struct got_error *
359 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
360 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
361 31b4484f 2019-07-27 stsp got_diff_blob_cb cb, void *cb_arg, int diff_content)
363 f6861a81 2018-09-13 stsp const struct got_error *err;
364 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
365 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
366 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
367 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
369 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
373 15a94983 2018-12-23 stsp if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
374 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
378 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
382 15a94983 2018-12-23 stsp if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
383 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
387 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
391 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
395 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, label1, label2, repo, cb, cb_arg,
396 31b4484f 2019-07-27 stsp diff_content);
400 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
402 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
403 789689b5 2017-11-30 stsp if (treeobj1)
404 789689b5 2017-11-30 stsp got_object_close(treeobj1);
405 789689b5 2017-11-30 stsp if (treeobj2)
406 789689b5 2017-11-30 stsp got_object_close(treeobj2);
407 789689b5 2017-11-30 stsp return err;
410 474b4f94 2017-11-30 stsp static const struct got_error *
411 54156555 2018-12-24 stsp diff_deleted_tree(struct got_object_id *id, const char *label,
412 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
413 31b4484f 2019-07-27 stsp int diff_content)
415 f6861a81 2018-09-13 stsp const struct got_error *err;
416 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
417 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
419 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
423 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
424 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
428 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
432 31b4484f 2019-07-27 stsp err = got_diff_tree(tree, NULL, label, NULL, repo, cb, cb_arg,
433 31b4484f 2019-07-27 stsp diff_content);
436 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
437 2c56f2ce 2017-11-30 stsp if (treeobj)
438 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
439 2c56f2ce 2017-11-30 stsp return err;
442 474b4f94 2017-11-30 stsp static const struct got_error *
443 74671950 2018-02-11 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
444 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
445 aaa13589 2019-06-01 stsp got_diff_blob_cb cb, void *cb_arg)
447 013404a9 2017-11-30 stsp /* XXX TODO */
448 474b4f94 2017-11-30 stsp return NULL;
451 474b4f94 2017-11-30 stsp static const struct got_error *
452 1de5e065 2019-06-01 stsp diff_entry_old_new(const struct got_tree_entry *te1,
453 1de5e065 2019-06-01 stsp const struct got_tree_entry *te2, const char *label1, const char *label2,
454 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
455 31b4484f 2019-07-27 stsp int diff_content)
457 f6861a81 2018-09-13 stsp const struct got_error *err = NULL;
458 19ae6da1 2018-11-05 stsp int id_match;
460 474b4f94 2017-11-30 stsp if (te2 == NULL) {
461 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
462 aaa13589 2019-06-01 stsp err = diff_deleted_tree(te1->id, label1, repo,
463 31b4484f 2019-07-27 stsp cb, cb_arg, diff_content);
465 31b4484f 2019-07-27 stsp if (diff_content)
466 31b4484f 2019-07-27 stsp err = diff_deleted_blob(te1->id, label1, repo,
467 31b4484f 2019-07-27 stsp cb, cb_arg);
469 31b4484f 2019-07-27 stsp err = cb(cb_arg, NULL, NULL, te1->id, NULL,
470 31b4484f 2019-07-27 stsp label1, NULL, repo);
472 f6861a81 2018-09-13 stsp return err;
475 19ae6da1 2018-11-05 stsp id_match = (got_object_id_cmp(te1->id, te2->id) == 0);
476 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
477 19ae6da1 2018-11-05 stsp if (!id_match)
478 f6861a81 2018-09-13 stsp return diff_modified_tree(te1->id, te2->id,
479 31b4484f 2019-07-27 stsp label1, label2, repo, cb, cb_arg, diff_content);
480 4209f790 2017-11-30 stsp } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
481 31b4484f 2019-07-27 stsp if (!id_match) {
482 31b4484f 2019-07-27 stsp if (diff_content)
483 31b4484f 2019-07-27 stsp return diff_modified_blob(te1->id, te2->id,
484 31b4484f 2019-07-27 stsp label1, label2, repo, cb, cb_arg);
486 31b4484f 2019-07-27 stsp return cb(cb_arg, NULL, NULL, te1->id,
487 31b4484f 2019-07-27 stsp te2->id, label1, label2, repo);
491 19ae6da1 2018-11-05 stsp if (id_match)
492 f6861a81 2018-09-13 stsp return NULL;
494 aaa13589 2019-06-01 stsp return diff_kind_mismatch(te1->id, te2->id, label1, label2, repo,
495 aaa13589 2019-06-01 stsp cb, cb_arg);
498 474b4f94 2017-11-30 stsp static const struct got_error *
499 1de5e065 2019-06-01 stsp diff_entry_new_old(const struct got_tree_entry *te2,
500 aaa13589 2019-06-01 stsp const struct got_tree_entry *te1, const char *label2,
501 31b4484f 2019-07-27 stsp struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
502 31b4484f 2019-07-27 stsp int diff_content)
504 f6861a81 2018-09-13 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
505 f6861a81 2018-09-13 stsp return NULL;
507 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
508 31b4484f 2019-07-27 stsp return diff_added_tree(te2->id, label2, repo, cb, cb_arg,
509 31b4484f 2019-07-27 stsp diff_content);
511 31b4484f 2019-07-27 stsp if (diff_content)
512 31b4484f 2019-07-27 stsp return diff_added_blob(te2->id, label2, repo, cb, cb_arg);
514 31b4484f 2019-07-27 stsp return cb(cb_arg, NULL, NULL, NULL, te2->id, NULL, label2, repo);
517 474b4f94 2017-11-30 stsp const struct got_error *
518 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
519 aaa13589 2019-06-01 stsp const char *label1, const char *label2, struct got_repository *repo,
520 31b4484f 2019-07-27 stsp got_diff_blob_cb cb, void *cb_arg, int diff_content)
522 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
523 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
524 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
525 f6861a81 2018-09-13 stsp char *l1 = NULL, *l2 = NULL;
527 883f0469 2018-06-23 stsp if (tree1) {
528 883f0469 2018-06-23 stsp const struct got_tree_entries *entries;
529 883f0469 2018-06-23 stsp entries = got_object_tree_get_entries(tree1);
530 883f0469 2018-06-23 stsp te1 = SIMPLEQ_FIRST(&entries->head);
531 60f50a58 2018-09-15 stsp if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
532 f6861a81 2018-09-13 stsp te1->name) == -1)
533 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
535 883f0469 2018-06-23 stsp if (tree2) {
536 883f0469 2018-06-23 stsp const struct got_tree_entries *entries;
537 883f0469 2018-06-23 stsp entries = got_object_tree_get_entries(tree2);
538 883f0469 2018-06-23 stsp te2 = SIMPLEQ_FIRST(&entries->head);
539 60f50a58 2018-09-15 stsp if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
540 f6861a81 2018-09-13 stsp te2->name) == -1)
541 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
546 1de5e065 2019-06-01 stsp const struct got_tree_entry *te = NULL;
548 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree2,
549 1de5e065 2019-06-01 stsp te1->name);
553 f6861a81 2018-09-13 stsp if (te && asprintf(&l2, "%s%s%s", label2,
554 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te->name) == -1)
556 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
558 aaa13589 2019-06-01 stsp err = diff_entry_old_new(te1, te, l1, l2, repo, cb,
559 31b4484f 2019-07-27 stsp cb_arg, diff_content);
565 1de5e065 2019-06-01 stsp const struct got_tree_entry *te = NULL;
567 1de5e065 2019-06-01 stsp te = got_object_tree_find_entry(tree1,
568 1de5e065 2019-06-01 stsp te2->name);
571 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
572 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te->name) == -1)
574 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
576 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
577 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te2->name) == -1)
579 638f9024 2019-05-13 stsp got_error_from_errno("asprintf");
581 31b4484f 2019-07-27 stsp err = diff_entry_new_old(te2, te, l2, repo,
582 31b4484f 2019-07-27 stsp cb, cb_arg, diff_content);
590 474b4f94 2017-11-30 stsp te1 = SIMPLEQ_NEXT(te1, entry);
592 f6861a81 2018-09-13 stsp asprintf(&l1, "%s%s%s", label1,
593 f6861a81 2018-09-13 stsp label1[0] ? "/" : "", te1->name) == -1)
594 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
599 474b4f94 2017-11-30 stsp te2 = SIMPLEQ_NEXT(te2, entry);
601 f6861a81 2018-09-13 stsp asprintf(&l2, "%s%s%s", label2,
602 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te2->name) == -1)
603 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
605 474b4f94 2017-11-30 stsp } while (te1 || te2);
607 11528a82 2018-05-19 stsp return err;
610 11528a82 2018-05-19 stsp const struct got_error *
611 15a94983 2018-12-23 stsp got_diff_objects_as_blobs(struct got_object_id *id1, struct got_object_id *id2,
612 54156555 2018-12-24 stsp const char *label1, const char *label2, int diff_context,
613 54156555 2018-12-24 stsp struct got_repository *repo, FILE *outfile)
615 11528a82 2018-05-19 stsp const struct got_error *err;
616 11528a82 2018-05-19 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
618 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
619 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
622 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob1, repo, id1, 8192);
627 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob2, repo, id2, 8192);
631 54156555 2018-12-24 stsp err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
635 11528a82 2018-05-19 stsp got_object_blob_close(blob1);
637 11528a82 2018-05-19 stsp got_object_blob_close(blob2);
638 474b4f94 2017-11-30 stsp return err;
641 11528a82 2018-05-19 stsp const struct got_error *
642 15a94983 2018-12-23 stsp got_diff_objects_as_trees(struct got_object_id *id1, struct got_object_id *id2,
643 54156555 2018-12-24 stsp char *label1, char *label2, int diff_context, struct got_repository *repo,
644 54156555 2018-12-24 stsp FILE *outfile)
646 11528a82 2018-05-19 stsp const struct got_error *err;
647 11528a82 2018-05-19 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
648 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
650 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
651 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
654 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo, id1);
659 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo, id2);
663 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
664 aaa13589 2019-06-01 stsp arg.outfile = outfile;
665 aaa13589 2019-06-01 stsp err = got_diff_tree(tree1, tree2, label1, label2, repo,
666 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
669 11528a82 2018-05-19 stsp got_object_tree_close(tree1);
671 11528a82 2018-05-19 stsp got_object_tree_close(tree2);
672 11528a82 2018-05-19 stsp return err;
675 11528a82 2018-05-19 stsp const struct got_error *
676 15a94983 2018-12-23 stsp got_diff_objects_as_commits(struct got_object_id *id1,
677 15a94983 2018-12-23 stsp struct got_object_id *id2, int diff_context,
678 15a94983 2018-12-23 stsp struct got_repository *repo, FILE *outfile)
680 11528a82 2018-05-19 stsp const struct got_error *err;
681 11528a82 2018-05-19 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
683 15a94983 2018-12-23 stsp if (id2 == NULL)
684 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
687 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit1, repo, id1);
692 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, repo, id2);
696 15a94983 2018-12-23 stsp err = got_diff_objects_as_trees(
697 15a94983 2018-12-23 stsp commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
698 54156555 2018-12-24 stsp got_object_commit_get_tree_id(commit2), "", "", diff_context, repo,
701 11528a82 2018-05-19 stsp if (commit1)
702 11528a82 2018-05-19 stsp got_object_commit_close(commit1);
703 11528a82 2018-05-19 stsp if (commit2)
704 11528a82 2018-05-19 stsp got_object_commit_close(commit2);
705 11528a82 2018-05-19 stsp return err;