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 adacb96f 2018-12-24 stsp static char *
40 adacb96f 2018-12-24 stsp get_datestr(time_t *time, char *datebuf)
42 adacb96f 2018-12-24 stsp char *p, *s = ctime_r(time, datebuf);
43 adacb96f 2018-12-24 stsp p = strchr(s, '\n');
49 adacb96f 2018-12-24 stsp static char *
50 adacb96f 2018-12-24 stsp get_label(const char *path, time_t time, const char *fallback)
52 adacb96f 2018-12-24 stsp char datebuf[26];
53 adacb96f 2018-12-24 stsp char *datestr = NULL;
54 adacb96f 2018-12-24 stsp char *label;
56 adacb96f 2018-12-24 stsp if (path == NULL)
57 adacb96f 2018-12-24 stsp return strdup(fallback);
60 adacb96f 2018-12-24 stsp datestr = get_datestr(&time, datebuf);
62 adacb96f 2018-12-24 stsp if (asprintf(&label, "%s%s%s%s", path, datestr ? "\t" : "",
63 adacb96f 2018-12-24 stsp datestr ? datestr : "", datestr ? " UTC" : "") == -1)
64 adacb96f 2018-12-24 stsp return NULL;
66 adacb96f 2018-12-24 stsp return label;
69 404c43c4 2018-06-21 stsp static const struct got_error *
70 404c43c4 2018-06-21 stsp diff_blobs(struct got_blob_object *blob1, struct got_blob_object *blob2,
71 adacb96f 2018-12-24 stsp const char *label1, const char *label2, time_t time1, time_t time2,
72 adacb96f 2018-12-24 stsp int diff_context, FILE *outfile, struct got_diff_changes *changes)
74 ed9e98a8 2017-11-29 stsp struct got_diff_state ds;
75 8ba9a219 2017-11-29 stsp struct got_diff_args args;
76 7d283eee 2017-11-29 stsp const struct got_error *err = NULL;
77 4e22badc 2017-11-30 stsp FILE *f1 = NULL, *f2 = NULL;
78 f78b0693 2017-11-29 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
79 f78b0693 2017-11-29 stsp char hex2[SHA1_DIGEST_STRING_LENGTH];
80 98abbc84 2017-11-30 stsp char *idstr1 = NULL, *idstr2 = NULL;
81 adacb96f 2018-12-24 stsp char *l1 = NULL, *l2 = NULL;
82 f934cf2c 2018-02-12 stsp size_t size1, size2;
83 4e22badc 2017-11-30 stsp int res, flags = 0;
85 adacb96f 2018-12-24 stsp memset(&args, 0, sizeof(args));
87 4e22badc 2017-11-30 stsp if (blob1) {
88 a1fd68d8 2018-01-12 stsp f1 = got_opentemp();
89 4e22badc 2017-11-30 stsp if (f1 == NULL)
90 4e22badc 2017-11-30 stsp return got_error(GOT_ERR_FILE_OPEN);
92 4e22badc 2017-11-30 stsp flags |= D_EMPTY1;
94 4e22badc 2017-11-30 stsp if (blob2) {
95 a1fd68d8 2018-01-12 stsp f2 = got_opentemp();
96 4e22badc 2017-11-30 stsp if (f2 == NULL) {
98 4e22badc 2017-11-30 stsp return got_error(GOT_ERR_FILE_OPEN);
101 4e22badc 2017-11-30 stsp flags |= D_EMPTY2;
104 98abbc84 2017-11-30 stsp if (blob1) {
105 f934cf2c 2018-02-12 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
106 84451b3e 2018-07-10 stsp err = got_object_blob_dump_to_file(&size1, NULL, f1, blob1);
110 98abbc84 2017-11-30 stsp idstr1 = "/dev/null";
113 98abbc84 2017-11-30 stsp if (blob2) {
114 f934cf2c 2018-02-12 stsp idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
115 84451b3e 2018-07-10 stsp err = got_object_blob_dump_to_file(&size2, NULL, f2, blob2);
119 98abbc84 2017-11-30 stsp idstr2 = "/dev/null";
121 ed9e98a8 2017-11-29 stsp memset(&ds, 0, sizeof(ds));
122 f9d67749 2017-11-30 stsp /* XXX should stat buffers be passed in args instead of ds? */
123 f9d67749 2017-11-30 stsp ds.stb1.st_mode = S_IFREG;
125 f934cf2c 2018-02-12 stsp ds.stb1.st_size = size1;
126 f9d67749 2017-11-30 stsp ds.stb1.st_mtime = 0; /* XXX */
128 f9d67749 2017-11-30 stsp ds.stb2.st_mode = S_IFREG;
130 f934cf2c 2018-02-12 stsp ds.stb2.st_size = size2;
131 f9d67749 2017-11-30 stsp ds.stb2.st_mtime = 0; /* XXX */
133 8ba9a219 2017-11-29 stsp args.diff_format = D_UNIFIED;
134 adacb96f 2018-12-24 stsp l1 = get_label(label1, time1, idstr1);
135 adacb96f 2018-12-24 stsp if (l1 == NULL) {
136 adacb96f 2018-12-24 stsp err = got_error_from_errno();
139 adacb96f 2018-12-24 stsp args.label[0] = l1;
140 adacb96f 2018-12-24 stsp l2 = get_label(label2, time2, idstr2);
141 adacb96f 2018-12-24 stsp if (l2 == NULL) {
142 adacb96f 2018-12-24 stsp err = got_error_from_errno();
145 adacb96f 2018-12-24 stsp args.label[1] = l2;
146 df2871d2 2018-10-18 stsp args.diff_context = diff_context;
147 84eb021e 2018-03-27 stsp flags |= D_PROTOTYPE;
149 18f42939 2018-12-24 stsp fprintf(outfile, "blob - %s\n", idstr1);
150 18f42939 2018-12-24 stsp fprintf(outfile, "blob + %s\n", idstr2);
151 404c43c4 2018-06-21 stsp err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, changes);
156 98abbc84 2017-11-30 stsp fclose(f1);
158 98abbc84 2017-11-30 stsp fclose(f2);
159 7d283eee 2017-11-29 stsp return err;
162 404c43c4 2018-06-21 stsp const struct got_error *
163 404c43c4 2018-06-21 stsp got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
164 adacb96f 2018-12-24 stsp const char *label1, const char *label2, time_t time1, time_t time2,
165 adacb96f 2018-12-24 stsp int diff_context, FILE *outfile)
167 adacb96f 2018-12-24 stsp return diff_blobs(blob1, blob2, label1, label2, time1, time2,
168 adacb96f 2018-12-24 stsp diff_context, outfile, NULL);
171 404c43c4 2018-06-21 stsp const struct got_error *
172 404c43c4 2018-06-21 stsp got_diff_blob_lines_changed(struct got_diff_changes **changes,
173 404c43c4 2018-06-21 stsp struct got_blob_object *blob1, struct got_blob_object *blob2)
175 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
177 404c43c4 2018-06-21 stsp *changes = calloc(1, sizeof(**changes));
178 404c43c4 2018-06-21 stsp if (*changes == NULL)
179 404c43c4 2018-06-21 stsp return got_error_from_errno();
180 404c43c4 2018-06-21 stsp SIMPLEQ_INIT(&(*changes)->entries);
182 adacb96f 2018-12-24 stsp err = diff_blobs(blob1, blob2, NULL, NULL, 0, 0, 3, NULL, *changes);
184 404c43c4 2018-06-21 stsp got_diff_free_changes(*changes);
185 404c43c4 2018-06-21 stsp *changes = NULL;
187 404c43c4 2018-06-21 stsp return err;
191 404c43c4 2018-06-21 stsp got_diff_free_changes(struct got_diff_changes *changes)
193 404c43c4 2018-06-21 stsp struct got_diff_change *change;
194 404c43c4 2018-06-21 stsp while (!SIMPLEQ_EMPTY(&changes->entries)) {
195 404c43c4 2018-06-21 stsp change = SIMPLEQ_FIRST(&changes->entries);
196 404c43c4 2018-06-21 stsp SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
197 404c43c4 2018-06-21 stsp free(change);
199 404c43c4 2018-06-21 stsp free(changes);
202 a3e2cbea 2017-12-01 stsp struct got_tree_entry *
203 a3e2cbea 2017-12-01 stsp match_entry_by_name(struct got_tree_entry *te1, struct got_tree_object *tree2)
205 a3e2cbea 2017-12-01 stsp struct got_tree_entry *te2;
206 883f0469 2018-06-23 stsp const struct got_tree_entries *entries2;
208 883f0469 2018-06-23 stsp entries2 = got_object_tree_get_entries(tree2);
209 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te2, &entries2->head, entry) {
210 e20293f2 2018-11-17 stsp if (strcmp(te1->name, te2->name) == 0)
211 a3e2cbea 2017-12-01 stsp return te2;
213 474b4f94 2017-11-30 stsp return NULL;
216 474b4f94 2017-11-30 stsp static const struct got_error *
217 adacb96f 2018-12-24 stsp diff_added_blob(struct got_object_id *id, const char *label, time_t time,
218 df2871d2 2018-10-18 stsp int diff_context, struct got_repository *repo, FILE *outfile)
220 4e22badc 2017-11-30 stsp const struct got_error *err;
221 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
222 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
224 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
226 4e22badc 2017-11-30 stsp return err;
228 2acfca77 2018-04-01 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
231 adacb96f 2018-12-24 stsp err = got_diff_blob(NULL, blob, NULL, label, 0, time, diff_context,
234 2acfca77 2018-04-01 stsp got_object_close(obj);
236 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
237 2acfca77 2018-04-01 stsp return err;
240 474b4f94 2017-11-30 stsp static const struct got_error *
241 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
242 adacb96f 2018-12-24 stsp const char *label1, const char *label2, time_t time1, time_t time2,
243 adacb96f 2018-12-24 stsp int diff_context, struct got_repository *repo, FILE *outfile)
245 6a213ccb 2017-11-30 stsp const struct got_error *err;
246 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
247 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
248 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
249 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
251 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
253 730a8aa0 2018-04-24 stsp return err;
254 15a94983 2018-12-23 stsp if (obj1->type != GOT_OBJ_TYPE_BLOB) {
255 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
259 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
262 15a94983 2018-12-23 stsp if (obj2->type != GOT_OBJ_TYPE_BLOB) {
263 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
267 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob1, repo, obj1, 8192);
271 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob2, repo, obj2, 8192);
275 adacb96f 2018-12-24 stsp err = got_diff_blob(blob1, blob2, label1, label2, time1, time2,
276 adacb96f 2018-12-24 stsp diff_context, outfile);
280 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
282 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
284 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
286 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
287 6a213ccb 2017-11-30 stsp return err;
290 474b4f94 2017-11-30 stsp static const struct got_error *
291 f6861a81 2018-09-13 stsp diff_deleted_blob(struct got_object_id *id, const char *label,
292 adacb96f 2018-12-24 stsp time_t time, int diff_context, struct got_repository *repo, FILE *outfile)
294 365fb436 2017-11-30 stsp const struct got_error *err;
295 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
296 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
298 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
300 365fb436 2017-11-30 stsp return err;
302 2acfca77 2018-04-01 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
305 adacb96f 2018-12-24 stsp err = got_diff_blob(blob, NULL, label, NULL, time, 0, diff_context,
308 2acfca77 2018-04-01 stsp got_object_close(obj);
310 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
311 2acfca77 2018-04-01 stsp return err;
314 474b4f94 2017-11-30 stsp static const struct got_error *
315 adacb96f 2018-12-24 stsp diff_added_tree(struct got_object_id *id, const char *label, time_t time,
316 df2871d2 2018-10-18 stsp int diff_context, struct got_repository *repo, FILE *outfile)
318 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
319 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
320 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
322 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
326 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
327 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
331 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
335 adacb96f 2018-12-24 stsp err = got_diff_tree(NULL, tree, NULL, label, 0, time,
336 adacb96f 2018-12-24 stsp diff_context, repo, outfile);
340 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
341 9c70d4c3 2017-11-30 stsp if (treeobj)
342 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
343 9c70d4c3 2017-11-30 stsp return err;
346 474b4f94 2017-11-30 stsp static const struct got_error *
347 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
348 adacb96f 2018-12-24 stsp const char *label1, const char *label2, time_t time1, time_t time2,
349 adacb96f 2018-12-24 stsp int diff_context, struct got_repository *repo, FILE *outfile)
351 f6861a81 2018-09-13 stsp const struct got_error *err;
352 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
353 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
354 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
355 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
357 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
361 15a94983 2018-12-23 stsp if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
362 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
366 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
370 15a94983 2018-12-23 stsp if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
371 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
375 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
379 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
383 adacb96f 2018-12-24 stsp err = got_diff_tree(tree1, tree2, label1, label2, time1, time2,
384 adacb96f 2018-12-24 stsp diff_context, repo, outfile);
388 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
390 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
391 789689b5 2017-11-30 stsp if (treeobj1)
392 789689b5 2017-11-30 stsp got_object_close(treeobj1);
393 789689b5 2017-11-30 stsp if (treeobj2)
394 789689b5 2017-11-30 stsp got_object_close(treeobj2);
395 789689b5 2017-11-30 stsp return err;
398 474b4f94 2017-11-30 stsp static const struct got_error *
399 adacb96f 2018-12-24 stsp diff_deleted_tree(struct got_object_id *id, const char *label, time_t time,
400 df2871d2 2018-10-18 stsp int diff_context, struct got_repository *repo, FILE *outfile)
402 f6861a81 2018-09-13 stsp const struct got_error *err;
403 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
404 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
406 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
410 15a94983 2018-12-23 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
411 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
415 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
419 adacb96f 2018-12-24 stsp err = got_diff_tree(tree, NULL, label, NULL, time, 0, diff_context,
420 adacb96f 2018-12-24 stsp repo, outfile);
423 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
424 2c56f2ce 2017-11-30 stsp if (treeobj)
425 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
426 2c56f2ce 2017-11-30 stsp return err;
429 474b4f94 2017-11-30 stsp static const struct got_error *
430 74671950 2018-02-11 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
431 f6861a81 2018-09-13 stsp const char *label1, const char *label2, FILE *outfile)
433 013404a9 2017-11-30 stsp /* XXX TODO */
434 474b4f94 2017-11-30 stsp return NULL;
437 474b4f94 2017-11-30 stsp static const struct got_error *
438 f6861a81 2018-09-13 stsp diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
439 adacb96f 2018-12-24 stsp const char *label1, const char *label2, time_t time1, time_t time2,
440 adacb96f 2018-12-24 stsp int diff_context, struct got_repository *repo, FILE *outfile)
442 f6861a81 2018-09-13 stsp const struct got_error *err = NULL;
443 19ae6da1 2018-11-05 stsp int id_match;
445 474b4f94 2017-11-30 stsp if (te2 == NULL) {
446 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
447 adacb96f 2018-12-24 stsp err = diff_deleted_tree(te1->id, label1, time1,
448 adacb96f 2018-12-24 stsp diff_context, repo, outfile);
450 adacb96f 2018-12-24 stsp err = diff_deleted_blob(te1->id, label1, time1,
451 adacb96f 2018-12-24 stsp diff_context, repo, outfile);
452 f6861a81 2018-09-13 stsp return err;
455 19ae6da1 2018-11-05 stsp id_match = (got_object_id_cmp(te1->id, te2->id) == 0);
456 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
457 19ae6da1 2018-11-05 stsp if (!id_match)
458 f6861a81 2018-09-13 stsp return diff_modified_tree(te1->id, te2->id,
459 adacb96f 2018-12-24 stsp label1, label2, time1, time2, diff_context,
460 adacb96f 2018-12-24 stsp repo, outfile);
461 4209f790 2017-11-30 stsp } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
462 19ae6da1 2018-11-05 stsp if (!id_match)
463 f6861a81 2018-09-13 stsp return diff_modified_blob(te1->id, te2->id,
464 adacb96f 2018-12-24 stsp label1, label2, time1, time2, diff_context,
465 adacb96f 2018-12-24 stsp repo, outfile);
468 19ae6da1 2018-11-05 stsp if (id_match)
469 f6861a81 2018-09-13 stsp return NULL;
471 f6861a81 2018-09-13 stsp return diff_kind_mismatch(te1->id, te2->id, label1, label2, outfile);
474 474b4f94 2017-11-30 stsp static const struct got_error *
475 f6861a81 2018-09-13 stsp diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_entry *te1,
476 adacb96f 2018-12-24 stsp const char *label2, time_t time2, int diff_context,
477 adacb96f 2018-12-24 stsp struct got_repository *repo, FILE *outfile)
479 f6861a81 2018-09-13 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
480 f6861a81 2018-09-13 stsp return NULL;
482 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
483 adacb96f 2018-12-24 stsp return diff_added_tree(te2->id, label2, time2, diff_context,
484 adacb96f 2018-12-24 stsp repo, outfile);
486 adacb96f 2018-12-24 stsp return diff_added_blob(te2->id, label2, time2, diff_context, repo,
490 474b4f94 2017-11-30 stsp const struct got_error *
491 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
492 adacb96f 2018-12-24 stsp const char *label1, const char *label2, time_t time1, time_t time2,
493 adacb96f 2018-12-24 stsp int diff_context, struct got_repository *repo, FILE *outfile)
495 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
496 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
497 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
498 f6861a81 2018-09-13 stsp char *l1 = NULL, *l2 = NULL;
500 883f0469 2018-06-23 stsp if (tree1) {
501 883f0469 2018-06-23 stsp const struct got_tree_entries *entries;
502 883f0469 2018-06-23 stsp entries = got_object_tree_get_entries(tree1);
503 883f0469 2018-06-23 stsp te1 = SIMPLEQ_FIRST(&entries->head);
504 60f50a58 2018-09-15 stsp if (te1 && asprintf(&l1, "%s%s%s", label1, label1[0] ? "/" : "",
505 f6861a81 2018-09-13 stsp te1->name) == -1)
506 f6861a81 2018-09-13 stsp return got_error_from_errno();
508 883f0469 2018-06-23 stsp if (tree2) {
509 883f0469 2018-06-23 stsp const struct got_tree_entries *entries;
510 883f0469 2018-06-23 stsp entries = got_object_tree_get_entries(tree2);
511 883f0469 2018-06-23 stsp te2 = SIMPLEQ_FIRST(&entries->head);
512 60f50a58 2018-09-15 stsp if (te2 && asprintf(&l2, "%s%s%s", label2, label2[0] ? "/" : "",
513 f6861a81 2018-09-13 stsp te2->name) == -1)
514 f6861a81 2018-09-13 stsp return got_error_from_errno();
519 f6861a81 2018-09-13 stsp struct got_tree_entry *te = NULL;
521 f6861a81 2018-09-13 stsp te = match_entry_by_name(te1, tree2);
525 f6861a81 2018-09-13 stsp if (te && asprintf(&l2, "%s%s%s", label2,
526 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te->name) == -1)
527 f6861a81 2018-09-13 stsp return got_error_from_errno();
529 adacb96f 2018-12-24 stsp err = diff_entry_old_new(te1, te, l1, l2, time1, time2,
530 adacb96f 2018-12-24 stsp diff_context, repo, outfile);
536 f6861a81 2018-09-13 stsp struct got_tree_entry *te = NULL;
538 f6861a81 2018-09-13 stsp te = match_entry_by_name(te2, tree1);
541 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
542 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te->name) == -1)
543 d6ce02f1 2018-11-17 stsp return got_error_from_errno();
545 d6ce02f1 2018-11-17 stsp if (asprintf(&l2, "%s%s%s", label2,
546 d6ce02f1 2018-11-17 stsp label2[0] ? "/" : "", te2->name) == -1)
547 d6ce02f1 2018-11-17 stsp return got_error_from_errno();
549 adacb96f 2018-12-24 stsp err = diff_entry_new_old(te2, te, l2, time2,
550 adacb96f 2018-12-24 stsp diff_context, repo, outfile);
558 474b4f94 2017-11-30 stsp te1 = SIMPLEQ_NEXT(te1, entry);
560 f6861a81 2018-09-13 stsp asprintf(&l1, "%s%s%s", label1,
561 f6861a81 2018-09-13 stsp label1[0] ? "/" : "", te1->name) == -1)
562 f6861a81 2018-09-13 stsp return got_error_from_errno();
567 474b4f94 2017-11-30 stsp te2 = SIMPLEQ_NEXT(te2, entry);
569 f6861a81 2018-09-13 stsp asprintf(&l2, "%s%s%s", label2,
570 f6861a81 2018-09-13 stsp label2[0] ? "/" : "", te2->name) == -1)
571 f6861a81 2018-09-13 stsp return got_error_from_errno();
573 474b4f94 2017-11-30 stsp } while (te1 || te2);
575 11528a82 2018-05-19 stsp return err;
578 11528a82 2018-05-19 stsp const struct got_error *
579 15a94983 2018-12-23 stsp got_diff_objects_as_blobs(struct got_object_id *id1, struct got_object_id *id2,
580 adacb96f 2018-12-24 stsp const char *label1, const char *label2, time_t time1, time_t time2,
581 adacb96f 2018-12-24 stsp int diff_context, struct got_repository *repo, FILE *outfile)
583 11528a82 2018-05-19 stsp const struct got_error *err;
584 11528a82 2018-05-19 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
586 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
587 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
590 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob1, repo, id1, 8192);
595 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob2, repo, id2, 8192);
599 adacb96f 2018-12-24 stsp err = got_diff_blob(blob1, blob2, label1, label2, time1, time2,
600 adacb96f 2018-12-24 stsp diff_context, outfile);
603 11528a82 2018-05-19 stsp got_object_blob_close(blob1);
605 11528a82 2018-05-19 stsp got_object_blob_close(blob2);
606 474b4f94 2017-11-30 stsp return err;
609 11528a82 2018-05-19 stsp const struct got_error *
610 15a94983 2018-12-23 stsp got_diff_objects_as_trees(struct got_object_id *id1, struct got_object_id *id2,
611 adacb96f 2018-12-24 stsp char *label1, char *label2, time_t time1, time_t time2,
612 adacb96f 2018-12-24 stsp int diff_context, struct got_repository *repo, FILE *outfile)
614 11528a82 2018-05-19 stsp const struct got_error *err;
615 11528a82 2018-05-19 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
617 15a94983 2018-12-23 stsp if (id1 == NULL && id2 == NULL)
618 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
621 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo, id1);
626 15a94983 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo, id2);
630 adacb96f 2018-12-24 stsp err = got_diff_tree(tree1, tree2, label1, label2, time1, time2,
631 adacb96f 2018-12-24 stsp diff_context, repo, outfile);
634 11528a82 2018-05-19 stsp got_object_tree_close(tree1);
636 11528a82 2018-05-19 stsp got_object_tree_close(tree2);
637 11528a82 2018-05-19 stsp return err;
640 11528a82 2018-05-19 stsp const struct got_error *
641 15a94983 2018-12-23 stsp got_diff_objects_as_commits(struct got_object_id *id1,
642 15a94983 2018-12-23 stsp struct got_object_id *id2, int diff_context,
643 15a94983 2018-12-23 stsp struct got_repository *repo, FILE *outfile)
645 11528a82 2018-05-19 stsp const struct got_error *err;
646 11528a82 2018-05-19 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
648 15a94983 2018-12-23 stsp if (id2 == NULL)
649 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
652 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit1, repo, id1);
657 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, repo, id2);
661 15a94983 2018-12-23 stsp err = got_diff_objects_as_trees(
662 15a94983 2018-12-23 stsp commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
663 adacb96f 2018-12-24 stsp got_object_commit_get_tree_id(commit2), "", "",
664 adacb96f 2018-12-24 stsp commit1 ? got_object_commit_get_committer_time(commit1) : 0,
665 adacb96f 2018-12-24 stsp got_object_commit_get_committer_time(commit2),
666 adacb96f 2018-12-24 stsp diff_context,
667 adacb96f 2018-12-24 stsp repo, outfile);
669 11528a82 2018-05-19 stsp if (commit1)
670 11528a82 2018-05-19 stsp got_object_commit_close(commit1);
671 11528a82 2018-05-19 stsp if (commit2)
672 11528a82 2018-05-19 stsp got_object_commit_close(commit2);
673 11528a82 2018-05-19 stsp return err;