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"
36 404c43c4 2018-06-21 stsp static const struct got_error *
37 404c43c4 2018-06-21 stsp diff_blobs(struct got_blob_object *blob1, struct got_blob_object *blob2,
38 404c43c4 2018-06-21 stsp const char *label1, const char *label2, FILE *outfile,
39 404c43c4 2018-06-21 stsp struct got_diff_changes *changes)
41 ed9e98a8 2017-11-29 stsp struct got_diff_state ds;
42 8ba9a219 2017-11-29 stsp struct got_diff_args args;
43 7d283eee 2017-11-29 stsp const struct got_error *err = NULL;
44 4e22badc 2017-11-30 stsp FILE *f1 = NULL, *f2 = NULL;
45 f78b0693 2017-11-29 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
46 f78b0693 2017-11-29 stsp char hex2[SHA1_DIGEST_STRING_LENGTH];
47 98abbc84 2017-11-30 stsp char *idstr1 = NULL, *idstr2 = NULL;
48 f934cf2c 2018-02-12 stsp size_t size1, size2;
49 4e22badc 2017-11-30 stsp int res, flags = 0;
51 4e22badc 2017-11-30 stsp if (blob1) {
52 a1fd68d8 2018-01-12 stsp f1 = got_opentemp();
53 4e22badc 2017-11-30 stsp if (f1 == NULL)
54 4e22badc 2017-11-30 stsp return got_error(GOT_ERR_FILE_OPEN);
56 4e22badc 2017-11-30 stsp flags |= D_EMPTY1;
58 4e22badc 2017-11-30 stsp if (blob2) {
59 a1fd68d8 2018-01-12 stsp f2 = got_opentemp();
60 4e22badc 2017-11-30 stsp if (f2 == NULL) {
62 4e22badc 2017-11-30 stsp return got_error(GOT_ERR_FILE_OPEN);
65 4e22badc 2017-11-30 stsp flags |= D_EMPTY2;
68 98abbc84 2017-11-30 stsp if (blob1) {
69 f934cf2c 2018-02-12 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
70 84451b3e 2018-07-10 stsp err = got_object_blob_dump_to_file(&size1, NULL, f1, blob1);
74 98abbc84 2017-11-30 stsp idstr1 = "/dev/null";
77 98abbc84 2017-11-30 stsp if (blob2) {
78 f934cf2c 2018-02-12 stsp idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
79 84451b3e 2018-07-10 stsp err = got_object_blob_dump_to_file(&size2, NULL, f2, blob2);
83 98abbc84 2017-11-30 stsp idstr2 = "/dev/null";
85 ed9e98a8 2017-11-29 stsp memset(&ds, 0, sizeof(ds));
86 f9d67749 2017-11-30 stsp /* XXX should stat buffers be passed in args instead of ds? */
87 f9d67749 2017-11-30 stsp ds.stb1.st_mode = S_IFREG;
89 f934cf2c 2018-02-12 stsp ds.stb1.st_size = size1;
90 f9d67749 2017-11-30 stsp ds.stb1.st_mtime = 0; /* XXX */
92 f9d67749 2017-11-30 stsp ds.stb2.st_mode = S_IFREG;
94 f934cf2c 2018-02-12 stsp ds.stb2.st_size = size2;
95 f9d67749 2017-11-30 stsp ds.stb2.st_mtime = 0; /* XXX */
97 f9d67749 2017-11-30 stsp memset(&args, 0, sizeof(args));
98 8ba9a219 2017-11-29 stsp args.diff_format = D_UNIFIED;
99 98abbc84 2017-11-30 stsp args.label[0] = label1 ? label1 : idstr1;
100 98abbc84 2017-11-30 stsp args.label[1] = label2 ? label2 : idstr2;
101 c2c21d46 2018-03-27 stsp args.diff_context = 3;
102 84eb021e 2018-03-27 stsp flags |= D_PROTOTYPE;
104 404c43c4 2018-06-21 stsp err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, changes);
107 98abbc84 2017-11-30 stsp fclose(f1);
109 98abbc84 2017-11-30 stsp fclose(f2);
110 7d283eee 2017-11-29 stsp return err;
113 404c43c4 2018-06-21 stsp const struct got_error *
114 404c43c4 2018-06-21 stsp got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
115 404c43c4 2018-06-21 stsp const char *label1, const char *label2, FILE *outfile)
117 404c43c4 2018-06-21 stsp return diff_blobs(blob1, blob2, label1, label2, outfile, NULL);
120 404c43c4 2018-06-21 stsp const struct got_error *
121 404c43c4 2018-06-21 stsp got_diff_blob_lines_changed(struct got_diff_changes **changes,
122 404c43c4 2018-06-21 stsp struct got_blob_object *blob1, struct got_blob_object *blob2)
124 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
126 404c43c4 2018-06-21 stsp *changes = calloc(1, sizeof(**changes));
127 404c43c4 2018-06-21 stsp if (*changes == NULL)
128 404c43c4 2018-06-21 stsp return got_error_from_errno();
129 404c43c4 2018-06-21 stsp SIMPLEQ_INIT(&(*changes)->entries);
131 404c43c4 2018-06-21 stsp err = diff_blobs(blob1, blob2, NULL, NULL, NULL, *changes);
133 404c43c4 2018-06-21 stsp got_diff_free_changes(*changes);
134 404c43c4 2018-06-21 stsp *changes = NULL;
136 404c43c4 2018-06-21 stsp return err;
140 404c43c4 2018-06-21 stsp got_diff_free_changes(struct got_diff_changes *changes)
142 404c43c4 2018-06-21 stsp struct got_diff_change *change;
143 404c43c4 2018-06-21 stsp while (!SIMPLEQ_EMPTY(&changes->entries)) {
144 404c43c4 2018-06-21 stsp change = SIMPLEQ_FIRST(&changes->entries);
145 404c43c4 2018-06-21 stsp SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
146 404c43c4 2018-06-21 stsp free(change);
148 404c43c4 2018-06-21 stsp free(changes);
151 a3e2cbea 2017-12-01 stsp struct got_tree_entry *
152 a3e2cbea 2017-12-01 stsp match_entry_by_name(struct got_tree_entry *te1, struct got_tree_object *tree2)
154 a3e2cbea 2017-12-01 stsp struct got_tree_entry *te2;
155 883f0469 2018-06-23 stsp const struct got_tree_entries *entries2;
157 883f0469 2018-06-23 stsp entries2 = got_object_tree_get_entries(tree2);
158 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te2, &entries2->head, entry) {
159 a3e2cbea 2017-12-01 stsp if (strcmp(te1->name, te2->name) == 0)
160 a3e2cbea 2017-12-01 stsp return te2;
162 474b4f94 2017-11-30 stsp return NULL;
165 474b4f94 2017-11-30 stsp static const struct got_error *
166 74671950 2018-02-11 stsp diff_added_blob(struct got_object_id *id, struct got_repository *repo,
167 74671950 2018-02-11 stsp FILE *outfile)
169 4e22badc 2017-11-30 stsp const struct got_error *err;
170 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
171 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
173 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
175 4e22badc 2017-11-30 stsp return err;
177 2acfca77 2018-04-01 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
180 2acfca77 2018-04-01 stsp err = got_diff_blob(NULL, blob, NULL, NULL, outfile);
182 2acfca77 2018-04-01 stsp got_object_close(obj);
184 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
185 2acfca77 2018-04-01 stsp return err;
188 474b4f94 2017-11-30 stsp static const struct got_error *
189 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
190 74671950 2018-02-11 stsp struct got_repository *repo, FILE *outfile)
192 6a213ccb 2017-11-30 stsp const struct got_error *err;
193 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
194 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
195 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
196 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
198 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
200 730a8aa0 2018-04-24 stsp return err;
201 eef6493a 2018-01-19 stsp if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB) {
202 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
206 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
209 eef6493a 2018-01-19 stsp if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB) {
210 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
214 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob1, repo, obj1, 8192);
218 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob2, repo, obj2, 8192);
222 74671950 2018-02-11 stsp err = got_diff_blob(blob1, blob2, NULL, NULL, outfile);
226 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
228 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
230 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
232 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
233 6a213ccb 2017-11-30 stsp return err;
236 474b4f94 2017-11-30 stsp static const struct got_error *
237 74671950 2018-02-11 stsp diff_deleted_blob(struct got_object_id *id, struct got_repository *repo,
238 74671950 2018-02-11 stsp FILE *outfile)
240 365fb436 2017-11-30 stsp const struct got_error *err;
241 2acfca77 2018-04-01 stsp struct got_blob_object *blob = NULL;
242 2acfca77 2018-04-01 stsp struct got_object *obj = NULL;
244 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
246 365fb436 2017-11-30 stsp return err;
248 2acfca77 2018-04-01 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
251 2acfca77 2018-04-01 stsp err = got_diff_blob(blob, NULL, NULL, NULL, outfile);
253 2acfca77 2018-04-01 stsp got_object_close(obj);
255 2acfca77 2018-04-01 stsp got_object_blob_close(blob);
256 2acfca77 2018-04-01 stsp return err;
259 474b4f94 2017-11-30 stsp static const struct got_error *
260 74671950 2018-02-11 stsp diff_added_tree(struct got_object_id *id, struct got_repository *repo,
261 74671950 2018-02-11 stsp FILE *outfile)
263 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
264 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
265 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
267 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
271 b107e67f 2018-01-19 stsp if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
272 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
276 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
280 74671950 2018-02-11 stsp err = got_diff_tree(NULL, tree, repo, outfile);
284 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
285 9c70d4c3 2017-11-30 stsp if (treeobj)
286 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
287 9c70d4c3 2017-11-30 stsp return err;
290 474b4f94 2017-11-30 stsp static const struct got_error *
291 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
292 74671950 2018-02-11 stsp struct got_repository *repo, FILE *outfile)
294 789689b5 2017-11-30 stsp const struct got_error *err = NULL;
295 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
296 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
297 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
298 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
300 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
304 eef6493a 2018-01-19 stsp if (got_object_get_type(treeobj1) != GOT_OBJ_TYPE_TREE) {
305 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
309 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
313 eef6493a 2018-01-19 stsp if (got_object_get_type(treeobj2) != GOT_OBJ_TYPE_TREE) {
314 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
318 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
322 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
326 74671950 2018-02-11 stsp err = got_diff_tree(tree1, tree2, repo, outfile);
330 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
332 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
333 789689b5 2017-11-30 stsp if (treeobj1)
334 789689b5 2017-11-30 stsp got_object_close(treeobj1);
335 789689b5 2017-11-30 stsp if (treeobj2)
336 789689b5 2017-11-30 stsp got_object_close(treeobj2);
337 789689b5 2017-11-30 stsp return err;
340 474b4f94 2017-11-30 stsp static const struct got_error *
341 74671950 2018-02-11 stsp diff_deleted_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
343 2c56f2ce 2017-11-30 stsp const struct got_error *err = NULL;
344 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
345 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
347 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
351 b107e67f 2018-01-19 stsp if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
352 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
356 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
360 74671950 2018-02-11 stsp err = got_diff_tree(tree, NULL, repo, outfile);
364 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
365 2c56f2ce 2017-11-30 stsp if (treeobj)
366 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
367 2c56f2ce 2017-11-30 stsp return err;
370 474b4f94 2017-11-30 stsp static const struct got_error *
371 74671950 2018-02-11 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
372 74671950 2018-02-11 stsp FILE *outfile)
374 013404a9 2017-11-30 stsp /* XXX TODO */
375 474b4f94 2017-11-30 stsp return NULL;
378 474b4f94 2017-11-30 stsp static const struct got_error *
379 6a213ccb 2017-11-30 stsp diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_object *tree2,
380 74671950 2018-02-11 stsp struct got_repository *repo, FILE *outfile)
382 07ccb00b 2018-03-27 stsp struct got_tree_entry *te2 = NULL;
385 07ccb00b 2018-03-27 stsp te2 = match_entry_by_name(te1, tree2);
386 474b4f94 2017-11-30 stsp if (te2 == NULL) {
387 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
388 59ece79d 2018-02-12 stsp return diff_deleted_tree(te1->id, repo, outfile);
389 59ece79d 2018-02-12 stsp return diff_deleted_blob(te1->id, repo, outfile);
392 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
393 59ece79d 2018-02-12 stsp if (got_object_id_cmp(te1->id, te2->id) != 0)
394 59ece79d 2018-02-12 stsp return diff_modified_tree(te1->id, te2->id, repo,
396 4209f790 2017-11-30 stsp } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
397 59ece79d 2018-02-12 stsp if (got_object_id_cmp(te1->id, te2->id) != 0)
398 59ece79d 2018-02-12 stsp return diff_modified_blob(te1->id, te2->id, repo,
402 59ece79d 2018-02-12 stsp return diff_kind_mismatch(te1->id, te2->id, outfile);
405 474b4f94 2017-11-30 stsp static const struct got_error *
406 4e22badc 2017-11-30 stsp diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_object *tree1,
407 74671950 2018-02-11 stsp struct got_repository *repo, FILE *outfile)
409 07ccb00b 2018-03-27 stsp if (tree1) {
410 07ccb00b 2018-03-27 stsp struct got_tree_entry *te1 = match_entry_by_name(te2, tree1);
411 07ccb00b 2018-03-27 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
412 07ccb00b 2018-03-27 stsp return NULL;
415 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
416 59ece79d 2018-02-12 stsp return diff_added_tree(te2->id, repo, outfile);
417 59ece79d 2018-02-12 stsp return diff_added_blob(te2->id, repo, outfile);
420 474b4f94 2017-11-30 stsp const struct got_error *
421 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
422 74671950 2018-02-11 stsp struct got_repository *repo, FILE *outfile)
424 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
425 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
426 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
428 883f0469 2018-06-23 stsp if (tree1) {
429 883f0469 2018-06-23 stsp const struct got_tree_entries *entries;
430 883f0469 2018-06-23 stsp entries = got_object_tree_get_entries(tree1);
431 883f0469 2018-06-23 stsp te1 = SIMPLEQ_FIRST(&entries->head);
433 883f0469 2018-06-23 stsp if (tree2) {
434 883f0469 2018-06-23 stsp const struct got_tree_entries *entries;
435 883f0469 2018-06-23 stsp entries = got_object_tree_get_entries(tree2);
436 883f0469 2018-06-23 stsp te2 = SIMPLEQ_FIRST(&entries->head);
441 74671950 2018-02-11 stsp err = diff_entry_old_new(te1, tree2, repo, outfile);
447 74671950 2018-02-11 stsp err = diff_entry_new_old(te2, tree1, repo, outfile);
453 474b4f94 2017-11-30 stsp te1 = SIMPLEQ_NEXT(te1, entry);
455 474b4f94 2017-11-30 stsp te2 = SIMPLEQ_NEXT(te2, entry);
456 474b4f94 2017-11-30 stsp } while (te1 || te2);
458 11528a82 2018-05-19 stsp return err;
461 11528a82 2018-05-19 stsp const struct got_error *
462 11528a82 2018-05-19 stsp got_diff_objects_as_blobs(struct got_object *obj1, struct got_object *obj2,
463 11528a82 2018-05-19 stsp struct got_repository *repo, FILE *outfile)
465 11528a82 2018-05-19 stsp const struct got_error *err;
466 11528a82 2018-05-19 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
468 b74c7625 2018-05-20 stsp if (obj1 == NULL && obj2 == NULL)
469 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
471 cd0acaa7 2018-05-20 stsp if (obj1) {
472 cd0acaa7 2018-05-20 stsp err = got_object_blob_open(&blob1, repo, obj1, 8192);
476 cd0acaa7 2018-05-20 stsp if (obj2) {
477 cd0acaa7 2018-05-20 stsp err = got_object_blob_open(&blob2, repo, obj2, 8192);
481 11528a82 2018-05-19 stsp err = got_diff_blob(blob1, blob2, NULL, NULL, outfile);
484 11528a82 2018-05-19 stsp got_object_blob_close(blob1);
486 11528a82 2018-05-19 stsp got_object_blob_close(blob2);
487 474b4f94 2017-11-30 stsp return err;
490 11528a82 2018-05-19 stsp const struct got_error *
491 11528a82 2018-05-19 stsp got_diff_objects_as_trees(struct got_object *obj1, struct got_object *obj2,
492 11528a82 2018-05-19 stsp struct got_repository *repo, FILE *outfile)
494 11528a82 2018-05-19 stsp const struct got_error *err;
495 11528a82 2018-05-19 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
497 b74c7625 2018-05-20 stsp if (obj1 == NULL && obj2 == NULL)
498 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
500 cd0acaa7 2018-05-20 stsp if (obj1) {
501 cd0acaa7 2018-05-20 stsp err = got_object_tree_open(&tree1, repo, obj1);
505 cd0acaa7 2018-05-20 stsp if (obj2) {
506 cd0acaa7 2018-05-20 stsp err = got_object_tree_open(&tree2, repo, obj2);
510 11528a82 2018-05-19 stsp err = got_diff_tree(tree1, tree2, repo, outfile);
513 11528a82 2018-05-19 stsp got_object_tree_close(tree1);
515 11528a82 2018-05-19 stsp got_object_tree_close(tree2);
516 11528a82 2018-05-19 stsp return err;
519 4bb494d5 2018-06-16 stsp static char *
520 4bb494d5 2018-06-16 stsp get_datestr(time_t *time, char *datebuf)
522 4bb494d5 2018-06-16 stsp char *p, *s = ctime_r(time, datebuf);
523 4bb494d5 2018-06-16 stsp p = strchr(s, '\n');
529 11528a82 2018-05-19 stsp const struct got_error *
530 11528a82 2018-05-19 stsp got_diff_objects_as_commits(struct got_object *obj1, struct got_object *obj2,
531 11528a82 2018-05-19 stsp struct got_repository *repo, FILE *outfile)
533 11528a82 2018-05-19 stsp const struct got_error *err;
534 11528a82 2018-05-19 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
535 11528a82 2018-05-19 stsp struct got_object *tree_obj1 = NULL, *tree_obj2 = NULL;
536 9b697879 2018-05-20 stsp char *id_str;
537 4bb494d5 2018-06-16 stsp char datebuf[26];
538 4bb494d5 2018-06-16 stsp time_t time;
540 9b697879 2018-05-20 stsp if (obj2 == NULL)
541 b74c7625 2018-05-20 stsp return got_error(GOT_ERR_NO_OBJ);
543 cd0acaa7 2018-05-20 stsp if (obj1) {
544 cd0acaa7 2018-05-20 stsp err = got_object_commit_open(&commit1, repo, obj1);
547 cd0acaa7 2018-05-20 stsp err = got_object_open(&tree_obj1, repo, commit1->tree_id);
552 9b697879 2018-05-20 stsp err = got_object_commit_open(&commit2, repo, obj2);
555 9b697879 2018-05-20 stsp err = got_object_open(&tree_obj2, repo, commit2->tree_id);
558 9b697879 2018-05-20 stsp err = got_object_get_id_str(&id_str, obj2);
561 9fc8d6a2 2018-05-20 stsp if (fprintf(outfile, "commit: %s\n", id_str) < 0) {
562 9fc8d6a2 2018-05-20 stsp err = got_error_from_errno();
563 9fc8d6a2 2018-05-20 stsp free(id_str);
566 9b697879 2018-05-20 stsp free(id_str);
567 4bb494d5 2018-06-16 stsp time = mktime(&commit2->tm_author);
568 4bb494d5 2018-06-16 stsp if (fprintf(outfile, "author: %s %s UTC\n", commit2->author,
569 4bb494d5 2018-06-16 stsp get_datestr(&time, datebuf)) < 0) {
570 9fc8d6a2 2018-05-20 stsp err = got_error_from_errno();
573 4bb494d5 2018-06-16 stsp time = mktime(&commit2->tm_committer);
574 9fc8d6a2 2018-05-20 stsp if (strcmp(commit2->author, commit2->committer) != 0 &&
575 4bb494d5 2018-06-16 stsp fprintf(outfile, "committer: %s %s UTC\n", commit2->committer,
576 4bb494d5 2018-06-16 stsp get_datestr(&time, datebuf)) < 0) {
577 9fc8d6a2 2018-05-20 stsp err = got_error_from_errno();
580 ede67fd9 2018-07-10 stsp if (fprintf(outfile, "%s\n", commit2->logmsg) < 0) {
581 9fc8d6a2 2018-05-20 stsp err = got_error_from_errno();
585 11528a82 2018-05-19 stsp err = got_diff_objects_as_trees(tree_obj1, tree_obj2, repo, outfile);
587 11528a82 2018-05-19 stsp if (tree_obj1)
588 11528a82 2018-05-19 stsp got_object_close(tree_obj1);
589 11528a82 2018-05-19 stsp if (tree_obj2)
590 11528a82 2018-05-19 stsp got_object_close(tree_obj2);
591 11528a82 2018-05-19 stsp if (commit1)
592 11528a82 2018-05-19 stsp got_object_commit_close(commit1);
593 11528a82 2018-05-19 stsp if (commit2)
594 11528a82 2018-05-19 stsp got_object_commit_close(commit2);
595 11528a82 2018-05-19 stsp return err;