Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/queue.h>
20 #include <sys/stdint.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sha1.h>
26 #include <zlib.h>
27 #include <ctype.h>
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_cancel.h"
32 #include "got_commit_graph.h"
33 #include "got_path.h"
35 #include "got_lib_delta.h"
36 #include "got_lib_inflate.h"
37 #include "got_lib_object.h"
38 #include "got_lib_object_idset.h"
40 struct got_commit_graph_node {
41 struct got_object_id id;
42 time_t timestamp;
44 /* Used during graph iteration. */
45 TAILQ_ENTRY(got_commit_graph_node) entry;
46 };
48 TAILQ_HEAD(got_commit_graph_iter_list, got_commit_graph_node);
50 struct got_commit_graph_branch_tip {
51 struct got_object_id *commit_id;
52 struct got_commit_object *commit;
53 struct got_commit_graph_node *new_node;
54 int changed;
55 int branch_done;
56 };
58 struct got_commit_graph {
59 /* The set of all commits we have traversed. */
60 struct got_object_idset *node_ids;
62 /* The commit at which traversal began (youngest commit in node_ids). */
63 struct got_commit_graph_node *head_node;
65 int flags;
66 #define GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL 0x01
68 /*
69 * A set of object IDs of known parent commits which we have not yet
70 * traversed. Each commit ID in this set represents a branch in commit
71 * history: Either the first-parent branch of the head node, or another
72 * branch corresponding to a traversed merge commit for which we have
73 * not traversed a branch point commit yet.
74 *
75 * Whenever we add a commit with a matching ID to the graph, we remove
76 * its corresponding element from this set, and add new elements for
77 * each of that commit's parent commits which were not traversed yet.
78 *
79 * When API users ask us to fetch more commits, we fetch commits from
80 * all currently open branches. This allows API users to process
81 * commits in linear order even though the history contains branches.
82 */
83 struct got_object_idset *open_branches;
85 /* Array of branch tips for fetch_commits_from_open_branches(). */
86 struct got_commit_graph_branch_tip *tips;
87 int ntips;
89 /* Path of tree entry of interest to the API user. */
90 char *path;
92 /* The next commit to return when the API user asks for one. */
93 struct got_commit_graph_node *iter_node;
95 /* The graph iteration list contains all nodes in sorted order. */
96 struct got_commit_graph_iter_list iter_list;
97 };
99 static struct got_commit_graph *
100 alloc_graph(const char *path)
102 struct got_commit_graph *graph;
104 graph = calloc(1, sizeof(*graph));
105 if (graph == NULL)
106 return NULL;
108 graph->path = strdup(path);
109 if (graph->path == NULL) {
110 free(graph);
111 return NULL;
114 graph->node_ids = got_object_idset_alloc();
115 if (graph->node_ids == NULL) {
116 free(graph->path);
117 free(graph);
118 return NULL;
121 graph->open_branches = got_object_idset_alloc();
122 if (graph->open_branches == NULL) {
123 got_object_idset_free(graph->node_ids);
124 free(graph->path);
125 free(graph);
126 return NULL;
129 TAILQ_INIT(&graph->iter_list);
130 return graph;
133 static const struct got_error *
134 detect_changed_path(int *changed, struct got_commit_object *commit,
135 struct got_object_id *commit_id, const char *path,
136 struct got_repository *repo)
138 const struct got_error *err = NULL;
139 struct got_commit_object *pcommit = NULL;
140 struct got_tree_object *tree = NULL, *ptree = NULL;
141 struct got_object_qid *pid;
143 if (got_path_is_root_dir(path)) {
144 *changed = 1;
145 return NULL;
148 *changed = 0;
150 pid = SIMPLEQ_FIRST(&commit->parent_ids);
151 if (pid == NULL) {
152 struct got_object_id *obj_id;
153 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
154 if (err) {
155 if (err->code == GOT_ERR_NO_TREE_ENTRY)
156 err = NULL;
157 } else
158 *changed = 1; /* The path was created in this commit. */
159 free(obj_id);
160 return err;
163 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
164 if (err)
165 return err;
167 err = got_object_open_as_commit(&pcommit, repo, pid->id);
168 if (err)
169 goto done;
171 err = got_object_open_as_tree(&ptree, repo, pcommit->tree_id);
172 if (err)
173 goto done;
175 err = got_object_tree_path_changed(changed, tree, ptree, path, repo);
176 done:
177 if (tree)
178 got_object_tree_close(tree);
179 if (ptree)
180 got_object_tree_close(ptree);
181 if (pcommit)
182 got_object_commit_close(pcommit);
183 return err;
186 static void
187 add_node_to_iter_list(struct got_commit_graph *graph,
188 struct got_commit_graph_node *node,
189 struct got_commit_graph_node *child_node)
191 struct got_commit_graph_node *n, *next;
193 if (TAILQ_EMPTY(&graph->iter_list)) {
194 TAILQ_INSERT_HEAD(&graph->iter_list, node, entry);
195 graph->iter_node = node;
196 return;
199 n = graph->iter_node;
200 /* Ensure that an iteration in progress will see this new commit. */
201 while (n) {
202 next = TAILQ_NEXT(n, entry);
203 if (next && node->timestamp >= next->timestamp) {
204 TAILQ_INSERT_BEFORE(next, node, entry);
205 return;
207 n = next;
209 TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
212 static const struct got_error *
213 close_branch(struct got_commit_graph *graph, struct got_object_id *commit_id)
215 const struct got_error *err;
217 err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
218 if (err && err->code != GOT_ERR_NO_OBJ)
219 return err;
220 return NULL;
223 static const struct got_error *
224 advance_branch(struct got_commit_graph *graph,
225 struct got_commit_graph_node *node,
226 struct got_object_id *commit_id, struct got_commit_object *commit,
227 struct got_repository *repo)
229 const struct got_error *err;
230 struct got_object_qid *qid;
232 err = close_branch(graph, commit_id);
233 if (err)
234 return err;
236 if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
237 qid = SIMPLEQ_FIRST(&commit->parent_ids);
238 if (qid == NULL ||
239 got_object_idset_get(graph->open_branches, qid->id))
240 return NULL;
241 return got_object_idset_add(graph->open_branches,
242 qid->id, node);
245 /*
246 * If we are graphing commits for a specific path, skip branches
247 * which do not contribute any content to this path.
248 */
249 if (commit->nparents > 1 && !got_path_is_root_dir(graph->path)) {
250 struct got_object_id *merged_id, *prev_id = NULL;
251 int branches_differ = 0;
253 err = got_object_id_by_path(&merged_id, repo, commit_id,
254 graph->path);
255 if (err)
256 return err;
258 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
259 struct got_object_id *id;
261 if (got_object_idset_get(graph->open_branches, qid->id))
262 continue;
264 err = got_object_id_by_path(&id, repo, qid->id,
265 graph->path);
266 if (err) {
267 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
268 branches_differ = 1;
269 continue;
271 free(merged_id);
272 free(prev_id);
273 return err;
276 if (prev_id) {
277 if (!branches_differ &&
278 got_object_id_cmp(id, prev_id) != 0)
279 branches_differ = 1;
280 free(prev_id);
282 prev_id = id;
284 /*
285 * If a branch has created the merged content we can
286 * skip any other branches.
287 */
288 if (got_object_id_cmp(merged_id, id) == 0) {
289 err = got_object_idset_add(graph->open_branches,
290 qid->id, node);
291 free(merged_id);
292 free(id);
293 return err;
297 free(prev_id);
298 prev_id = NULL;
299 free(merged_id);
300 merged_id = NULL;
302 /*
303 * If the path's content is the same on all branches,
304 * follow the first parent only.
305 */
306 if (!branches_differ) {
307 qid = SIMPLEQ_FIRST(&commit->parent_ids);
308 if (qid == NULL)
309 return NULL;
310 if (got_object_idset_get(graph->open_branches, qid->id))
311 return NULL;
312 if (got_object_idset_get(graph->node_ids, qid->id))
313 return NULL; /* parent already traversed */
314 return got_object_idset_add(graph->open_branches,
315 qid->id, node);
319 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
320 if (got_object_idset_get(graph->open_branches, qid->id))
321 continue;
322 if (got_object_idset_get(graph->node_ids, qid->id))
323 continue; /* parent already traversed */
324 err = got_object_idset_add(graph->open_branches, qid->id, node);
325 if (err)
326 return err;
329 return NULL;
332 static const struct got_error *
333 add_node(struct got_commit_graph_node **new_node, int *changed,
334 int *branch_done, struct got_commit_graph *graph,
335 struct got_object_id *commit_id, struct got_commit_object *commit,
336 struct got_commit_graph_node *child_node, struct got_repository *repo)
338 const struct got_error *err = NULL;
339 struct got_commit_graph_node *node;
341 *new_node = NULL;
342 *changed = 0;
343 *branch_done = 0;
345 node = calloc(1, sizeof(*node));
346 if (node == NULL)
347 return got_error_from_errno("calloc");
349 memcpy(&node->id, commit_id, sizeof(node->id));
350 node->timestamp = commit->committer_time;
352 err = got_object_idset_add(graph->node_ids, &node->id, node);
353 if (err) {
354 free(node);
355 return err;
358 err = detect_changed_path(changed, commit, commit_id, graph->path,
359 repo);
360 if (err) {
361 if (err->code == GOT_ERR_NO_OBJ) {
362 /*
363 * History of the path stops here on the current
364 * branch. Keep going on other branches.
365 */
366 err = NULL;
367 *branch_done = 1;
368 } else {
369 got_object_idset_remove(NULL, graph->node_ids,
370 &node->id);
371 free(node);
372 return err;
376 if (*changed)
377 add_node_to_iter_list(graph, node, child_node);
378 *new_node = node;
379 return NULL;
382 const struct got_error *
383 got_commit_graph_open(struct got_commit_graph **graph,
384 struct got_object_id *commit_id, const char *path,
385 int first_parent_traversal, struct got_repository *repo)
387 const struct got_error *err = NULL;
388 struct got_commit_object *commit;
389 int changed, branch_done;
391 *graph = NULL;
393 err = got_object_open_as_commit(&commit, repo, commit_id);
394 if (err)
395 return err;
397 /* The path must exist in our initial commit. */
398 if (!got_path_is_root_dir(path)) {
399 struct got_object_id *obj_id;
400 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
401 if (err)
402 return err;
403 free(obj_id);
406 *graph = alloc_graph(path);
407 if (*graph == NULL) {
408 err = got_error_from_errno("alloc_graph");
409 got_object_commit_close(commit);
410 return err;
413 if (first_parent_traversal)
414 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
416 err = add_node(&(*graph)->head_node, &changed, &branch_done, *graph,
417 commit_id, commit, NULL, repo);
418 if (err == NULL) {
419 err = advance_branch(*graph, (*graph)->head_node, commit_id,
420 commit, repo);
422 got_object_commit_close(commit);
423 if (err) {
424 got_commit_graph_close(*graph);
425 *graph = NULL;
426 return err;
429 return NULL;
432 struct add_branch_tip_arg {
433 struct got_commit_graph_branch_tip *tips;
434 int ntips;
435 struct got_repository *repo;
436 struct got_commit_graph *graph;
437 };
439 static const struct got_error *
440 add_branch_tip(struct got_object_id *commit_id, void *data, void *arg)
442 const struct got_error *err;
443 struct got_commit_graph_node *child_node = data;
444 struct add_branch_tip_arg *a = arg;
445 struct got_commit_graph_node *new_node;
446 struct got_commit_object *commit;
447 int changed, branch_done;
449 err = got_object_open_as_commit(&commit, a->repo, commit_id);
450 if (err)
451 return err;
453 err = add_node(&new_node, &changed, &branch_done, a->graph,
454 commit_id, commit, child_node, a->repo);
455 if (err)
456 return err;
458 a->tips[a->ntips].commit_id = new_node ? &new_node->id : NULL;
459 a->tips[a->ntips].commit = commit;
460 a->tips[a->ntips].new_node = new_node;
461 a->tips[a->ntips].changed = changed;
462 a->tips[a->ntips].branch_done = branch_done;
463 a->ntips++;
465 return NULL;
468 static const struct got_error *
469 fetch_commits_from_open_branches(int *nfetched,
470 struct got_object_id **changed_id, struct got_commit_graph *graph,
471 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
473 const struct got_error *err;
474 struct add_branch_tip_arg arg;
475 int i, ntips;
477 *nfetched = 0;
478 if (changed_id)
479 *changed_id = NULL;
481 ntips = got_object_idset_num_elements(graph->open_branches);
482 if (ntips == 0)
483 return NULL;
485 /* (Re-)allocate branch tips array if necessary. */
486 if (graph->ntips < ntips) {
487 struct got_commit_graph_branch_tip *tips;
488 tips = recallocarray(graph->tips, graph->ntips, ntips,
489 sizeof(*tips));
490 if (tips == NULL)
491 return got_error_from_errno("recallocarray");
492 graph->tips = tips;
493 graph->ntips = ntips;
495 arg.tips = graph->tips;
496 arg.ntips = 0; /* add_branch_tip() will increment */
497 arg.repo = repo;
498 arg.graph = graph;
499 err = got_object_idset_for_each(graph->open_branches, add_branch_tip,
500 &arg);
501 if (err)
502 goto done;
504 for (i = 0; i < arg.ntips; i++) {
505 struct got_object_id *commit_id;
506 struct got_commit_object *commit;
507 struct got_commit_graph_node *new_node;
508 int branch_done, changed;
510 if (cancel_cb) {
511 err = (*cancel_cb)(cancel_arg);
512 if (err)
513 break;
516 commit_id = arg.tips[i].commit_id;
517 commit = arg.tips[i].commit;
518 new_node = arg.tips[i].new_node;
519 branch_done = arg.tips[i].branch_done;
520 changed = arg.tips[i].changed;
522 if (branch_done)
523 err = close_branch(graph, commit_id);
524 else
525 err = advance_branch(graph, new_node, commit_id,
526 commit, repo);
527 if (err)
528 break;
529 if (changed && changed_id && *changed_id == NULL)
530 *changed_id = commit_id;
532 done:
533 for (i = 0; i < arg.ntips; i++)
534 got_object_commit_close(arg.tips[i].commit);
535 (*nfetched) = arg.ntips;
536 return err;
539 const struct got_error *
540 got_commit_graph_fetch_commits(struct got_commit_graph *graph, int limit,
541 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
543 const struct got_error *err;
544 int nfetched = 0, ncommits;
545 struct got_object_id *changed_id = NULL;
547 while (nfetched < limit) {
548 if (cancel_cb) {
549 err = (*cancel_cb)(cancel_arg);
550 if (err)
551 return err;
553 err = fetch_commits_from_open_branches(&ncommits,
554 &changed_id, graph, repo, cancel_cb, cancel_arg);
555 if (err)
556 return err;
557 if (ncommits == 0)
558 break;
559 if (changed_id)
560 nfetched += ncommits;
563 return NULL;
566 static const struct got_error *
567 free_node_iter(struct got_object_id *id, void *data, void *arg)
569 struct got_commit_graph_node *node = data;
570 free(node);
571 return NULL;
574 void
575 got_commit_graph_close(struct got_commit_graph *graph)
577 got_object_idset_free(graph->open_branches);
578 got_object_idset_for_each(graph->node_ids, free_node_iter, NULL);
579 got_object_idset_free(graph->node_ids);
580 free(graph->tips);
581 free(graph->path);
582 free(graph);
585 const struct got_error *
586 got_commit_graph_iter_start(struct got_commit_graph *graph,
587 struct got_object_id *id, struct got_repository *repo,
588 got_cancel_cb cancel_cb, void *cancel_arg)
590 const struct got_error *err = NULL;
591 struct got_commit_graph_node *start_node;
592 struct got_commit_object *commit;
593 int changed;
595 start_node = got_object_idset_get(graph->node_ids, id);
596 while (start_node == NULL) {
597 int ncommits;
598 err = fetch_commits_from_open_branches(&ncommits, NULL, graph,
599 repo, cancel_cb, cancel_arg);
600 if (err)
601 return err;
602 if (ncommits == 0)
603 return got_error_no_obj(id);
604 start_node = got_object_idset_get(graph->node_ids, id);
607 err = got_object_open_as_commit(&commit, repo, &start_node->id);
608 if (err)
609 return err;
611 err = detect_changed_path(&changed, commit, &start_node->id,
612 graph->path, repo);
613 if (err) {
614 got_object_commit_close(commit);
615 return err;
618 if (!changed) {
619 /* Locate first commit which changed graph->path. */
620 struct got_object_id *changed_id = NULL;
621 while (changed_id == NULL) {
622 int ncommits;
623 err = fetch_commits_from_open_branches(&ncommits,
624 &changed_id, graph, repo, cancel_cb, cancel_arg);
625 if (err) {
626 got_object_commit_close(commit);
627 return err;
630 start_node = got_object_idset_get(graph->node_ids, changed_id);
632 got_object_commit_close(commit);
634 graph->iter_node = start_node;
635 return NULL;
638 const struct got_error *
639 got_commit_graph_iter_next(struct got_object_id **id,
640 struct got_commit_graph *graph)
642 *id = NULL;
644 if (graph->iter_node == NULL) {
645 /* We are done iterating, or iteration was not started. */
646 return got_error(GOT_ERR_ITER_COMPLETED);
649 if (graph->iter_node ==
650 TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
651 got_object_idset_num_elements(graph->open_branches) == 0) {
652 /* We are done iterating. */
653 *id = &graph->iter_node->id;
654 graph->iter_node = NULL;
655 return NULL;
658 if (TAILQ_NEXT(graph->iter_node, entry) == NULL)
659 return got_error(GOT_ERR_ITER_NEED_MORE);
661 *id = &graph->iter_node->id;
662 graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);
663 return NULL;
666 const struct got_error *
667 got_commit_graph_find_youngest_common_ancestor(struct got_object_id **yca_id,
668 struct got_object_id *commit_id, struct got_object_id *commit_id2,
669 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
671 const struct got_error *err = NULL;
672 struct got_commit_graph *graph = NULL, *graph2 = NULL;
673 int completed = 0, completed2 = 0;
674 struct got_object_idset *commit_ids;
676 *yca_id = NULL;
678 commit_ids = got_object_idset_alloc();
679 if (commit_ids == NULL)
680 return got_error_from_errno("got_object_idset_alloc");
682 err = got_commit_graph_open(&graph, commit_id, "/", 1, repo);
683 if (err)
684 goto done;
686 err = got_commit_graph_open(&graph2, commit_id2, "/", 1, repo);
687 if (err)
688 goto done;
690 err = got_commit_graph_iter_start(graph, commit_id, repo,
691 cancel_cb, cancel_arg);
692 if (err)
693 goto done;
695 err = got_commit_graph_iter_start(graph2, commit_id2, repo,
696 cancel_cb, cancel_arg);
697 if (err)
698 goto done;
700 for (;;) {
701 struct got_object_id *id, *id2;
703 if (cancel_cb) {
704 err = (*cancel_cb)(cancel_arg);
705 if (err)
706 break;
709 if (!completed) {
710 err = got_commit_graph_iter_next(&id, graph);
711 if (err) {
712 if (err->code == GOT_ERR_ITER_COMPLETED)
713 completed = 1;
714 else if (err->code != GOT_ERR_ITER_NEED_MORE)
715 break;
716 err = got_commit_graph_fetch_commits(graph, 1,
717 repo, cancel_cb, cancel_arg);
718 if (err)
719 break;
723 if (!completed2) {
724 err = got_commit_graph_iter_next(&id2, graph2);
725 if (err) {
726 if (err->code == GOT_ERR_ITER_COMPLETED)
727 completed2 = 1;
728 else if (err->code != GOT_ERR_ITER_NEED_MORE)
729 break;
730 err = got_commit_graph_fetch_commits(graph2, 1,
731 repo, cancel_cb, cancel_arg);
732 if (err)
733 break;
737 if (id) {
738 if (got_object_idset_get(commit_ids, id)) {
739 *yca_id = got_object_id_dup(id);
740 if (*yca_id)
741 break;
742 err = got_error_from_errno("got_object_id_dup");
743 break;
746 err = got_object_idset_add(commit_ids, id, id);
747 if (err)
748 break;
750 if (id2) {
751 if (got_object_idset_get(commit_ids, id2)) {
752 *yca_id = got_object_id_dup(id2);
753 if (*yca_id)
754 break;
755 err = got_error_from_errno("got_object_id_dup");
756 break;
759 err = got_object_idset_add(commit_ids, id2, id2);
760 if (err)
761 break;
764 if (completed && completed2) {
765 err = got_error(GOT_ERR_ANCESTRY);
766 break;
770 done:
771 got_object_idset_free(commit_ids);
772 if (graph)
773 got_commit_graph_close(graph);
774 if (graph2)
775 got_commit_graph_close(graph2);
776 return err;