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_repository.h"
28 7d283eee 2017-11-29 stsp #include "got_object.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"
33 718b3ab0 2018-03-17 stsp #include "got_lib_diff.h"
34 718b3ab0 2018-03-17 stsp #include "got_lib_path.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 df2871d2 2018-10-18 stsp const char *label1, const char *label2, int diff_context, FILE *outfile,
42 404c43c4 2018-06-21 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 4e22badc 2017-11-30 stsp return got_error(GOT_ERR_FILE_OPEN);
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) {
65 4e22badc 2017-11-30 stsp return got_error(GOT_ERR_FILE_OPEN);
68 4e22badc 2017-11-30 stsp flags |= D_EMPTY2;
71 98abbc84 2017-11-30 stsp if (blob1) {
72 f934cf2c 2018-02-12 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
73 84451b3e 2018-07-10 stsp err = got_object_blob_dump_to_file(&size1, NULL, f1, blob1);
77 98abbc84 2017-11-30 stsp idstr1 = "/dev/null";
80 98abbc84 2017-11-30 stsp if (blob2) {
81 f934cf2c 2018-02-12 stsp idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
82 84451b3e 2018-07-10 stsp err = got_object_blob_dump_to_file(&size2, NULL, f2, blob2);
86 98abbc84 2017-11-30 stsp idstr2 = "/dev/null";
88 ed9e98a8 2017-11-29 stsp memset(&ds, 0, sizeof(ds));
89 f9d67749 2017-11-30 stsp /* XXX should stat buffers be passed in args instead of ds? */
90 f9d67749 2017-11-30 stsp ds.stb1.st_mode = S_IFREG;
92 f934cf2c 2018-02-12 stsp ds.stb1.st_size = size1;
93 f9d67749 2017-11-30 stsp ds.stb1.st_mtime = 0; /* XXX */
95 f9d67749 2017-11-30 stsp ds.stb2.st_mode = S_IFREG;
97 f934cf2c 2018-02-12 stsp ds.stb2.st_size = size2;
98 f9d67749 2017-11-30 stsp ds.stb2.st_mtime = 0; /* XXX */
100 f9d67749 2017-11-30 stsp memset(&args, 0, sizeof(args));
101 8ba9a219 2017-11-29 stsp args.diff_format = D_UNIFIED;
102 98abbc84 2017-11-30 stsp args.label[0] = label1 ? label1 : idstr1;
103 98abbc84 2017-11-30 stsp args.label[1] = label2 ? label2 : idstr2;
104 df2871d2 2018-10-18 stsp args.diff_context = diff_context;
105 84eb021e 2018-03-27 stsp flags |= D_PROTOTYPE;
107 405a764e 2018-09-13 stsp if (label1 && strcmp(label1, idstr1) != 0)
108 405a764e 2018-09-13 stsp fprintf(outfile, "blob - %s\n", idstr1);
109 405a764e 2018-09-13 stsp if (label2 && strcmp(label2, idstr2) != 0)
110 405a764e 2018-09-13 stsp fprintf(outfile, "blob + %s\n", idstr2);
112 404c43c4 2018-06-21 stsp err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, changes);
115 98abbc84 2017-11-30 stsp fclose(f1);
117 98abbc84 2017-11-30 stsp fclose(f2);
118 7d283eee 2017-11-29 stsp return err;
121 404c43c4 2018-06-21 stsp const struct got_error *
122 404c43c4 2018-06-21 stsp got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
123 df2871d2 2018-10-18 stsp const char *label1, const char *label2, int diff_context, FILE *outfile)
125 df2871d2 2018-10-18 stsp return diff_blobs(blob1, blob2, label1, label2, diff_context, outfile,
129 404c43c4 2018-06-21 stsp const struct got_error *
130 404c43c4 2018-06-21 stsp got_diff_blob_lines_changed(struct got_diff_changes **changes,
131 404c43c4 2018-06-21 stsp struct got_blob_object *blob1, struct got_blob_object *blob2)
133 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
135 404c43c4 2018-06-21 stsp *changes = calloc(1, sizeof(**changes));
136 404c43c4 2018-06-21 stsp if (*changes == NULL)
137 404c43c4 2018-06-21 stsp return got_error_from_errno();
138 404c43c4 2018-06-21 stsp SIMPLEQ_INIT(&(*changes)->entries);
140 df2871d2 2018-10-18 stsp err = diff_blobs(blob1, blob2, NULL, NULL, 3, NULL, *changes);
142 404c43c4 2018-06-21 stsp got_diff_free_changes(*changes);
143 404c43c4 2018-06-21 stsp *changes = NULL;
145 404c43c4 2018-06-21 stsp return err;
149 404c43c4 2018-06-21 stsp got_diff_free_changes(struct got_diff_changes *changes)
151 404c43c4 2018-06-21 stsp struct got_diff_change *change;
152 404c43c4 2018-06-21 stsp while (!SIMPLEQ_EMPTY(&changes->entries)) {
153 404c43c4 2018-06-21 stsp change = SIMPLEQ_FIRST(&changes->entries);
154 404c43c4 2018-06-21 stsp SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
155 404c43c4 2018-06-21 stsp free(change);
157 404c43c4 2018-06-21 stsp free(changes);
160 a3e2cbea 2017-12-01 stsp struct got_tree_entry *
161 a3e2cbea 2017-12-01 stsp match_entry_by_name(struct got_tree_entry *te1, struct got_tree_object *tree2)
163 a3e2cbea 2017-12-01 stsp struct got_tree_entry *te2;
164 883f0469 2018-06-23 stsp const struct got_tree_entries *entries2;
166 883f0469 2018-06-23 stsp entries2 = got_object_tree_get_entries(tree2);
167 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te2, &entries2->head, entry) {
168 e20293f2 2018-11-17 stsp if (strcmp(te1->name, te2->name) == 0)
169 a3e2cbea 2017-12-01 stsp return te2;
171 474b4f94 2017-11-30 stsp return NULL;
174 474b4f94 2017-11-30 stsp static const struct got_error *
175 f6861a81 2018-09-13 stsp diff_added_blob(struct got_object_id *id, const char *label,
176 df2871d2 2018-10-18 stsp int diff_context, struct got_repository *repo, FILE *outfile)
178 4e22badc 2017-11-30 stsp const struct got_error *err;
179 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
180 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
182 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
184 4e22badc 2017-11-30 stsp return err;
186 2acfca77 2018-04-01 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
189 df2871d2 2018-10-18 stsp err = got_diff_blob(NULL, blob, NULL, label, diff_context, outfile);
191 2acfca77 2018-04-01 stsp got_object_close(obj);
193 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
194 2acfca77 2018-04-01 stsp return err;
197 474b4f94 2017-11-30 stsp static const struct got_error *
198 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
199 df2871d2 2018-10-18 stsp const char *label1, const char *label2, int diff_context,
200 df2871d2 2018-10-18 stsp struct got_repository *repo, FILE *outfile)
202 6a213ccb 2017-11-30 stsp const struct got_error *err;
203 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
204 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
205 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
206 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
208 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
210 730a8aa0 2018-04-24 stsp return err;
211 15a94983 2018-12-23 stsp if (obj1->type != GOT_OBJ_TYPE_BLOB) {
212 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
216 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
219 15a94983 2018-12-23 stsp if (obj2->type != GOT_OBJ_TYPE_BLOB) {
220 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
224 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob1, repo, obj1, 8192);
228 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob2, repo, obj2, 8192);
232 df2871d2 2018-10-18 stsp err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
237 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
239 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
241 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
243 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
244 6a213ccb 2017-11-30 stsp return err;
247 474b4f94 2017-11-30 stsp static const struct got_error *
248 f6861a81 2018-09-13 stsp diff_deleted_blob(struct got_object_id *id, const char *label,
249 df2871d2 2018-10-18 stsp int diff_context, struct got_repository *repo, FILE *outfile)
251 365fb436 2017-11-30 stsp const struct got_error *err;
252 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
253 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
255 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
257 365fb436 2017-11-30 stsp return err;
259 2acfca77 2018-04-01 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
262 df2871d2 2018-10-18 stsp err = got_diff_blob(blob, NULL, label, NULL, diff_context, outfile);
264 2acfca77 2018-04-01 stsp got_object_close(obj);
266 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
267 2acfca77 2018-04-01 stsp return err;
270 474b4f94 2017-11-30 stsp static const struct got_error *
271 f6861a81 2018-09-13 stsp diff_added_tree(struct got_object_id *id, const char *label,
272 df2871d2 2018-10-18 stsp int diff_context, struct got_repository *repo, FILE *outfile)
274 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
275 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
276 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
278 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
282 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
283 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
287 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
291 df2871d2 2018-10-18 stsp err = got_diff_tree(NULL, tree, NULL, label, diff_context, repo,
296 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
297 9c70d4c3 2017-11-30 stsp if (treeobj)
298 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
299 9c70d4c3 2017-11-30 stsp return err;
302 474b4f94 2017-11-30 stsp static const struct got_error *
303 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
304 df2871d2 2018-10-18 stsp const char *label1, const char *label2, int diff_context,
305 df2871d2 2018-10-18 stsp struct got_repository *repo, FILE *outfile)
307 f6861a81 2018-09-13 stsp const struct got_error *err;
308 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
309 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
310 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
311 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
313 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
317 15a94983 2018-12-23 stsp if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
318 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
322 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
326 15a94983 2018-12-23 stsp if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
327 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
331 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
335 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
339 df2871d2 2018-10-18 stsp err = got_diff_tree(tree1, tree2, label1, label2, diff_context, repo,
344 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
346 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
347 789689b5 2017-11-30 stsp if (treeobj1)
348 789689b5 2017-11-30 stsp got_object_close(treeobj1);
349 789689b5 2017-11-30 stsp if (treeobj2)
350 789689b5 2017-11-30 stsp got_object_close(treeobj2);
351 789689b5 2017-11-30 stsp return err;
354 474b4f94 2017-11-30 stsp static const struct got_error *
355 f6861a81 2018-09-13 stsp diff_deleted_tree(struct got_object_id *id, const char *label,
356 df2871d2 2018-10-18 stsp int diff_context, struct got_repository *repo, FILE *outfile)
358 f6861a81 2018-09-13 stsp const struct got_error *err;
359 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
360 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
362 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
366 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
367 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
371 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
375 df2871d2 2018-10-18 stsp err = got_diff_tree(tree, NULL, label, NULL, diff_context, repo,
379 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
380 2c56f2ce 2017-11-30 stsp if (treeobj)
381 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
382 2c56f2ce 2017-11-30 stsp return err;
385 474b4f94 2017-11-30 stsp static const struct got_error *
386 74671950 2018-02-11 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
387 f6861a81 2018-09-13 stsp const char *label1, const char *label2, FILE *outfile)
389 013404a9 2017-11-30 stsp /* XXX TODO */
390 474b4f94 2017-11-30 stsp return NULL;
393 474b4f94 2017-11-30 stsp static const struct got_error *
394 f6861a81 2018-09-13 stsp diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
395 df2871d2 2018-10-18 stsp const char *label1, const char *label2, int diff_context,
396 df2871d2 2018-10-18 stsp struct got_repository *repo, FILE *outfile)
398 f6861a81 2018-09-13 stsp const struct got_error *err = NULL;
399 19ae6da1 2018-11-05 stsp int id_match;
401 474b4f94 2017-11-30 stsp if (te2 == NULL) {
402 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
403 df2871d2 2018-10-18 stsp err = diff_deleted_tree(te1->id, label1, diff_context,
404 df2871d2 2018-10-18 stsp repo, outfile);
406 df2871d2 2018-10-18 stsp err = diff_deleted_blob(te1->id, label1, diff_context,
407 df2871d2 2018-10-18 stsp repo, outfile);
408 f6861a81 2018-09-13 stsp return err;
411 19ae6da1 2018-11-05 stsp id_match = (got_object_id_cmp(te1->id, te2->id) == 0);
412 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
413 19ae6da1 2018-11-05 stsp if (!id_match)
414 f6861a81 2018-09-13 stsp return diff_modified_tree(te1->id, te2->id,
415 df2871d2 2018-10-18 stsp label1, label2, diff_context, repo, outfile);
416 4209f790 2017-11-30 stsp } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
417 19ae6da1 2018-11-05 stsp if (!id_match)
418 f6861a81 2018-09-13 stsp return diff_modified_blob(te1->id, te2->id,
419 df2871d2 2018-10-18 stsp label1, label2, diff_context, repo, outfile);
422 19ae6da1 2018-11-05 stsp if (id_match)
423 f6861a81 2018-09-13 stsp return NULL;
425 f6861a81 2018-09-13 stsp return diff_kind_mismatch(te1->id, te2->id, label1, label2, outfile);
428 474b4f94 2017-11-30 stsp static const struct got_error *
429 f6861a81 2018-09-13 stsp diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_entry *te1,
430 df2871d2 2018-10-18 stsp const char *label2, int diff_context, struct got_repository *repo,
431 df2871d2 2018-10-18 stsp FILE *outfile)
433 f6861a81 2018-09-13 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
434 f6861a81 2018-09-13 stsp return NULL;
436 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
437 df2871d2 2018-10-18 stsp return diff_added_tree(te2->id, label2, diff_context, repo,
440 df2871d2 2018-10-18 stsp return diff_added_blob(te2->id, label2, diff_context, repo, outfile);
443 474b4f94 2017-11-30 stsp const struct got_error *
444 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
445 df2871d2 2018-10-18 stsp const char *label1, const char *label2, int diff_context,
446 df2871d2 2018-10-18 stsp struct got_repository *repo, FILE *outfile)
448 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
449 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
450 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
451 f6861a81 2018-09-13 stsp char *l1 = NULL, *l2 = NULL;
453 883f0469 2018-06-23 stsp if (tree1) {
454 883f0469 2018-06-23 stsp const struct got_tree_entries *entries;
455 883f0469 2018-06-23 stsp entries = got_object_tree_get_entries(tree1);
456 883f0469 2018-06-23 stsp te1 = SIMPLEQ_FIRST(&entries->head);
457 60f50a58 2018-09-15 stsp if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
458 f6861a81 2018-09-13 stsp te1->name) == -1)
459 f6861a81 2018-09-13 stsp return got_error_from_errno();
461 883f0469 2018-06-23 stsp if (tree2) {
462 883f0469 2018-06-23 stsp const struct got_tree_entries *entries;
463 883f0469 2018-06-23 stsp entries = got_object_tree_get_entries(tree2);
464 883f0469 2018-06-23 stsp te2 = SIMPLEQ_FIRST(&entries->head);
465 60f50a58 2018-09-15 stsp if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
466 f6861a81 2018-09-13 stsp te2->name) == -1)
467 f6861a81 2018-09-13 stsp return got_error_from_errno();
472 f6861a81 2018-09-13 stsp struct got_tree_entry *te = NULL;
474 f6861a81 2018-09-13 stsp te = match_entry_by_name(te1, tree2);
478 f6861a81 2018-09-13 stsp if (te && asprintf(&l2, "%s%s%s", label2,
479 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te->name) == -1)
480 f6861a81 2018-09-13 stsp return got_error_from_errno();
482 df2871d2 2018-10-18 stsp err = diff_entry_old_new(te1, te, l1, l2, diff_context,
483 df2871d2 2018-10-18 stsp repo, outfile);
489 f6861a81 2018-09-13 stsp struct got_tree_entry *te = NULL;
491 f6861a81 2018-09-13 stsp te = match_entry_by_name(te2, tree1);
494 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
495 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te->name) == -1)
496 d6ce02f1 2018-11-17 stsp return got_error_from_errno();
498 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
499 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te2->name) == -1)
500 d6ce02f1 2018-11-17 stsp return got_error_from_errno();
502 df2871d2 2018-10-18 stsp err = diff_entry_new_old(te2, te, l2, diff_context,
503 df2871d2 2018-10-18 stsp repo, outfile);
511 474b4f94 2017-11-30 stsp te1 = SIMPLEQ_NEXT(te1, entry);
513 f6861a81 2018-09-13 stsp asprintf(&l1, "%s%s%s", label1,
514 f6861a81 2018-09-13 stsp label1[0] ? "/" : "", te1->name) == -1)
515 f6861a81 2018-09-13 stsp return got_error_from_errno();
520 474b4f94 2017-11-30 stsp te2 = SIMPLEQ_NEXT(te2, entry);
522 f6861a81 2018-09-13 stsp asprintf(&l2, "%s%s%s", label2,
523 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te2->name) == -1)
524 f6861a81 2018-09-13 stsp return got_error_from_errno();
526 474b4f94 2017-11-30 stsp } while (te1 || te2);
528 11528a82 2018-05-19 stsp return err;
531 11528a82 2018-05-19 stsp const struct got_error *
532 15a94983 2018-12-23 stsp got_diff_objects_as_blobs(struct got_object_id *id1, struct got_object_id *id2,
533 df2871d2 2018-10-18 stsp const char *label1, const char *label2, int diff_context,
534 df2871d2 2018-10-18 stsp struct got_repository *repo, FILE *outfile)
536 11528a82 2018-05-19 stsp const struct got_error *err;
537 11528a82 2018-05-19 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
539 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
540 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
543 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob1, repo, id1, 8192);
548 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob2, repo, id2, 8192);
552 df2871d2 2018-10-18 stsp err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
556 11528a82 2018-05-19 stsp got_object_blob_close(blob1);
558 11528a82 2018-05-19 stsp got_object_blob_close(blob2);
559 474b4f94 2017-11-30 stsp return err;
562 11528a82 2018-05-19 stsp const struct got_error *
563 15a94983 2018-12-23 stsp got_diff_objects_as_trees(struct got_object_id *id1, struct got_object_id *id2,
564 df2871d2 2018-10-18 stsp char *label1, char *label2, int diff_context, struct got_repository *repo,
565 df2871d2 2018-10-18 stsp FILE *outfile)
567 11528a82 2018-05-19 stsp const struct got_error *err;
568 11528a82 2018-05-19 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
570 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
571 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
574 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo, id1);
579 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo, id2);
583 df2871d2 2018-10-18 stsp err = got_diff_tree(tree1, tree2, label1, label2, diff_context,
584 df2871d2 2018-10-18 stsp repo, outfile);
587 11528a82 2018-05-19 stsp got_object_tree_close(tree1);
589 11528a82 2018-05-19 stsp got_object_tree_close(tree2);
590 11528a82 2018-05-19 stsp return err;
593 11528a82 2018-05-19 stsp const struct got_error *
594 15a94983 2018-12-23 stsp got_diff_objects_as_commits(struct got_object_id *id1,
595 15a94983 2018-12-23 stsp struct got_object_id *id2, int diff_context,
596 15a94983 2018-12-23 stsp struct got_repository *repo, FILE *outfile)
598 11528a82 2018-05-19 stsp const struct got_error *err;
599 11528a82 2018-05-19 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
601 15a94983 2018-12-23 stsp if (id2 == NULL)
602 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
605 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit1, repo, id1);
610 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, repo, id2);
614 15a94983 2018-12-23 stsp err = got_diff_objects_as_trees(
615 15a94983 2018-12-23 stsp commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
616 15a94983 2018-12-23 stsp got_object_commit_get_tree_id(commit2), "", "", diff_context, repo,
619 11528a82 2018-05-19 stsp if (commit1)
620 11528a82 2018-05-19 stsp got_object_commit_close(commit1);
621 11528a82 2018-05-19 stsp if (commit2)
622 11528a82 2018-05-19 stsp got_object_commit_close(commit2);
623 11528a82 2018-05-19 stsp return err;