2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/stdint.h>
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_commit_graph.h"
33 #include "got_lib_delta.h"
34 #include "got_lib_inflate.h"
35 #include "got_lib_object.h"
36 #include "got_lib_object_idset.h"
37 #include "got_lib_path.h"
39 struct got_commit_graph_node {
40 struct got_object_id id;
43 * Each graph node corresponds to a commit object.
44 * Graph vertices are modelled with an adjacency list.
45 * Adjacencies of a graph node are parent (older) commits.
48 struct got_object_id_queue parent_ids;
50 time_t commit_timestamp;
52 /* Used during graph iteration. */
53 TAILQ_ENTRY(got_commit_graph_node) entry;
56 TAILQ_HEAD(got_commit_graph_iter_list, got_commit_graph_node);
58 struct got_commit_graph_branch_tip {
59 struct got_object_id id;
60 struct got_commit_graph_node *node;
63 struct got_commit_graph {
64 /* The set of all commits we have traversed. */
65 struct got_object_idset *node_ids;
67 /* The commit at which traversal began (youngest commit in node_ids). */
68 struct got_commit_graph_node *head_node;
71 #define GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL 0x01
74 * A set of object IDs of known parent commits which we have not yet
75 * traversed. Each commit ID in this set represents a branch in commit
76 * history: Either the first-parent branch of the head node, or another
77 * branch corresponding to a traversed merge commit for which we have
78 * not traversed a branch point commit yet.
80 * Whenever we add a commit with a matching ID to the graph, we remove
81 * its corresponding element from this set, and add new elements for
82 * each of that commit's parent commits which were not traversed yet.
84 * When API users ask us to fetch more commits, we fetch commits from
85 * all currently open branches. This allows API users to process
86 * commits in linear order even though the history contains branches.
88 struct got_object_idset *open_branches;
90 /* Copy of known branch tips for fetch_commits_from_open_branches(). */
91 struct got_commit_graph_branch_tip *tips;
93 #define GOT_COMMIT_GRAPH_MIN_TIPS 100 /* minimum amount of tips to allocate */
95 /* Path of tree entry of interest to the API user. */
98 /* The next commit to return when the API user asks for one. */
99 struct got_commit_graph_node *iter_node;
101 /* The graph iteration list contains all nodes in sorted order. */
102 struct got_commit_graph_iter_list iter_list;
105 static struct got_commit_graph *
106 alloc_graph(const char *path)
108 struct got_commit_graph *graph;
110 graph = calloc(1, sizeof(*graph));
114 graph->path = strdup(path);
115 if (graph->path == NULL) {
120 graph->node_ids = got_object_idset_alloc();
121 if (graph->node_ids == NULL) {
127 graph->open_branches = got_object_idset_alloc();
128 if (graph->open_branches == NULL) {
129 got_object_idset_free(graph->node_ids);
135 TAILQ_INIT(&graph->iter_list);
141 is_head_node(struct got_commit_graph_node *node)
143 return node->nchildren == 0;
147 is_branch_point(struct got_commit_graph_node *node)
149 return node->nchildren > 1;
153 is_root_node(struct got_commit_graph_node *node)
155 return node->nparents == 0;
160 is_merge_point(struct got_commit_graph_node *node)
162 return node->nparents > 1;
165 static const struct got_error *
166 detect_changed_path(int *changed, struct got_commit_object *commit,
167 struct got_object_id *commit_id, const char *path,
168 struct got_repository *repo)
170 const struct got_error *err = NULL;
171 struct got_commit_object *pcommit = NULL;
172 struct got_tree_object *tree = NULL, *ptree = NULL;
173 struct got_object_qid *pid;
175 if (got_path_is_root_dir(path)) {
182 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
186 pid = SIMPLEQ_FIRST(&commit->parent_ids);
188 struct got_object_id *obj_id;
189 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
198 err = got_object_open_as_commit(&pcommit, repo, pid->id);
202 err = got_object_open_as_tree(&ptree, repo, pcommit->tree_id);
206 err = got_object_tree_path_changed(changed, tree, ptree, path, repo);
209 got_object_tree_close(tree);
211 got_object_tree_close(ptree);
213 got_object_commit_close(pcommit);
218 add_node_to_iter_list(struct got_commit_graph *graph,
219 struct got_commit_graph_node *node,
220 struct got_commit_graph_node *child_node)
222 struct got_commit_graph_node *n, *next;
224 if (TAILQ_EMPTY(&graph->iter_list)) {
225 TAILQ_INSERT_HEAD(&graph->iter_list, node, entry);
226 graph->iter_node = node;
230 n = graph->iter_node;
231 /* Ensure that an iteration in progress will see this new commit. */
233 next = TAILQ_NEXT(n, entry);
234 if (next && node->commit_timestamp >= next->commit_timestamp) {
235 TAILQ_INSERT_BEFORE(next, node, entry);
240 TAILQ_INSERT_AFTER(&graph->iter_list, graph->iter_node, node, entry);
243 static const struct got_error *
244 add_vertex(struct got_object_id_queue *ids, struct got_object_id *id)
246 struct got_object_qid *qid;
248 qid = calloc(1, sizeof(*qid));
250 return got_error_from_errno();
252 qid->id = got_object_id_dup(id);
253 if (qid->id == NULL) {
254 const struct got_error *err = got_error_from_errno();
255 got_object_qid_free(qid);
259 SIMPLEQ_INSERT_TAIL(ids, qid, entry);
263 static const struct got_error *
264 close_branch(struct got_commit_graph *graph, struct got_object_id *commit_id)
266 const struct got_error *err;
268 err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
269 if (err && err->code != GOT_ERR_NO_OBJ)
274 static const struct got_error *
275 advance_branch(struct got_commit_graph *graph,
276 struct got_commit_graph_node *node,
277 struct got_object_id *commit_id, struct got_commit_object *commit,
278 struct got_repository *repo)
280 const struct got_error *err;
281 struct got_object_qid *qid;
283 err = close_branch(graph, commit_id);
287 if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
288 qid = SIMPLEQ_FIRST(&commit->parent_ids);
291 err = got_object_idset_add(NULL, graph->open_branches, qid->id,
293 if (err && err->code != GOT_ERR_OBJ_EXISTS)
299 * If we are graphing commits for a specific path, skip branches
300 * which do not contribute any content to this path.
302 if (is_merge_point(node) && !got_path_is_root_dir(graph->path)) {
303 struct got_object_id *id, *merged_id, *prev_id = NULL;
304 int branches_differ = 0;
309 err = got_object_id_by_path(&merged_id, repo, commit_id,
314 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
315 if (got_object_idset_get(graph->node_ids, qid->id))
316 continue; /* parent already traversed */
318 err = got_object_id_by_path(&id, repo, qid->id,
321 if (err->code == GOT_ERR_NO_OBJ) {
329 if (!branches_differ &&
330 got_object_id_cmp(merged_id, prev_id) != 0)
336 * If a branch has created the merged content we can
337 * skip any other branches.
339 if (got_object_id_cmp(merged_id, id) == 0) {
340 err = got_object_idset_add(NULL,
341 graph->open_branches, qid->id, node);
342 if (err && err->code != GOT_ERR_OBJ_EXISTS)
349 * If the path's content is the same on all branches,
350 * follow the first parent only.
352 if (!branches_differ) {
353 qid = SIMPLEQ_FIRST(&commit->parent_ids);
356 if (got_object_idset_get(graph->node_ids, qid->id))
357 return NULL; /* parent already traversed */
358 err = got_object_idset_add(NULL,
359 graph->open_branches, qid->id, node);
360 if (err && err->code != GOT_ERR_OBJ_EXISTS)
366 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
367 if (got_object_idset_get(graph->node_ids, qid->id))
368 continue; /* parent already traversed */
369 err = got_object_idset_add(NULL, graph->open_branches,
371 if (err && err->code != GOT_ERR_OBJ_EXISTS)
379 free_node(struct got_commit_graph_node *node)
381 while (!SIMPLEQ_EMPTY(&node->parent_ids)) {
382 struct got_object_qid *pid = SIMPLEQ_FIRST(&node->parent_ids);
383 SIMPLEQ_REMOVE_HEAD(&node->parent_ids, entry);
384 got_object_qid_free(pid);
389 static const struct got_error *
390 add_node(struct got_commit_graph_node **new_node,
391 struct got_commit_graph *graph, struct got_object_id *commit_id,
392 struct got_commit_object *commit, struct got_commit_graph_node *child_node,
393 struct got_repository *repo)
395 const struct got_error *err = NULL;
396 struct got_commit_graph_node *node;
397 struct got_object_qid *pid;
398 int changed = 0, branch_done = 0;
402 node = calloc(1, sizeof(*node));
404 return got_error_from_errno();
406 memcpy(&node->id, commit_id, sizeof(node->id));
407 SIMPLEQ_INIT(&node->parent_ids);
408 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
409 err = add_vertex(&node->parent_ids, pid->id);
417 err = got_object_idset_add(NULL, graph->node_ids, &node->id, node);
419 if (err->code == GOT_ERR_OBJ_EXISTS)
425 err = detect_changed_path(&changed, commit, commit_id, graph->path,
428 if (err->code == GOT_ERR_NO_OBJ) {
430 * History of the path stops here on the current
431 * branch. Keep going on other branches.
441 node->commit_timestamp = mktime(&commit->tm_committer);
442 if (node->commit_timestamp == -1) {
444 return got_error_from_errno();
447 if (changed && !is_merge_point(node))
448 add_node_to_iter_list(graph, node, child_node);
451 err = close_branch(graph, commit_id);
453 err = advance_branch(graph, node, commit_id, commit, repo);
462 const struct got_error *
463 got_commit_graph_open(struct got_commit_graph **graph,
464 struct got_object_id *commit_id, const char *path,
465 int first_parent_traversal, struct got_repository *repo)
467 const struct got_error *err = NULL;
468 struct got_commit_object *commit;
472 err = got_object_open_as_commit(&commit, repo, commit_id);
476 /* The path must exist in our initial commit. */
477 if (!got_path_is_root_dir(path)) {
478 struct got_object_id *obj_id;
479 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
485 *graph = alloc_graph(path);
486 if (*graph == NULL) {
487 got_object_commit_close(commit);
488 return got_error_from_errno();
491 if (first_parent_traversal)
492 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
494 err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL,
496 got_object_commit_close(commit);
498 got_commit_graph_close(*graph);
506 struct gather_branch_tips_arg {
507 struct got_commit_graph_branch_tip *tips;
512 gather_branch_tips(struct got_object_id *id, void *data, void *arg)
514 struct gather_branch_tips_arg *a = arg;
515 memcpy(&a->tips[a->ntips].id, id, sizeof(*id));
516 a->tips[a->ntips].node = data;
520 static const struct got_error *
521 fetch_commits_from_open_branches(int *ncommits, int *wanted_id_added,
522 struct got_object_id **changed_id, struct got_commit_graph *graph,
523 struct got_repository *repo, struct got_object_id *wanted_id)
525 const struct got_error *err;
526 struct gather_branch_tips_arg arg;
531 *wanted_id_added = 0;
535 arg.ntips = got_object_idset_num_elements(graph->open_branches);
540 * Adding nodes to the graph might change the graph's open
541 * branches state. Create a local copy of the current state.
543 if (graph->ntips < arg.ntips) {
544 struct got_commit_graph_branch_tip *tips;
545 if (arg.ntips < GOT_COMMIT_GRAPH_MIN_TIPS)
546 arg.ntips = GOT_COMMIT_GRAPH_MIN_TIPS;
547 tips = reallocarray(graph->tips, arg.ntips, sizeof(*tips));
549 return got_error_from_errno();
551 graph->ntips = arg.ntips;
553 arg.ntips = 0; /* reset; gather_branch_tips() will increment */
554 arg.tips = graph->tips;
555 got_object_idset_for_each(graph->open_branches,
556 gather_branch_tips, &arg);
558 for (i = 0; i < arg.ntips; i++) {
559 struct got_object_id *commit_id;
560 struct got_commit_graph_node *child_node, *new_node;
561 struct got_commit_object *commit;
564 commit_id = &graph->tips[i].id;
565 child_node = graph->tips[i].node;
567 err = got_object_open_as_commit(&commit, repo, commit_id);
571 err = detect_changed_path(&changed, commit, commit_id,
574 got_object_commit_close(commit);
575 if (err->code != GOT_ERR_NO_OBJ)
577 err = close_branch(graph, commit_id);
582 if (changed && changed_id && *changed_id == NULL)
583 *changed_id = commit_id;
584 err = add_node(&new_node, graph, commit_id, commit, child_node,
586 got_object_commit_close(commit);
591 if (wanted_id && got_object_id_cmp(commit_id, wanted_id) == 0)
592 *wanted_id_added = 1;
598 const struct got_error *
599 got_commit_graph_fetch_commits(struct got_commit_graph *graph, int limit,
600 struct got_repository *repo)
602 const struct got_error *err;
603 int nfetched = 0, ncommits;
604 struct got_object_id *changed_id = NULL;
606 while (nfetched < limit) {
607 err = fetch_commits_from_open_branches(&ncommits, NULL,
608 &changed_id, graph, repo, NULL);
614 nfetched += ncommits;
620 const struct got_error *
621 got_commit_graph_fetch_commits_up_to(int *nfetched,
622 struct got_commit_graph *graph, struct got_object_id *wanted_id,
623 struct got_repository *repo)
625 const struct got_error *err;
626 int ncommits, wanted_id_added = 0;
630 if (got_object_idset_get(graph->node_ids, wanted_id) != NULL)
633 while (!wanted_id_added) {
634 err = fetch_commits_from_open_branches(&ncommits,
635 &wanted_id_added, NULL, graph, repo, wanted_id);
640 *nfetched += ncommits;
647 free_node_iter(struct got_object_id *id, void *data, void *arg)
649 struct got_commit_graph_node *node = data;
654 got_commit_graph_close(struct got_commit_graph *graph)
656 got_object_idset_free(graph->open_branches);
657 got_object_idset_for_each(graph->node_ids, free_node_iter, NULL);
658 got_object_idset_free(graph->node_ids);
664 const struct got_error *
665 got_commit_graph_iter_start(struct got_commit_graph *graph,
666 struct got_object_id *id, struct got_repository *repo)
668 const struct got_error *err = NULL;
669 struct got_commit_graph_node *start_node;
670 struct got_commit_object *commit;
673 start_node = got_object_idset_get(graph->node_ids, id);
674 if (start_node == NULL)
675 return got_error(GOT_ERR_NO_OBJ);
678 err = got_object_open_as_commit(&commit, repo, &start_node->id);
682 err = detect_changed_path(&changed, commit, &start_node->id,
685 got_object_commit_close(commit);
690 /* Locate first commit which changed graph->path. */
691 struct got_object_id *changed_id = NULL;
692 while (changed_id == NULL) {
694 err = fetch_commits_from_open_branches(&ncommits, NULL,
695 &changed_id, graph, repo, NULL);
697 got_object_commit_close(commit);
701 start_node = got_object_idset_get(graph->node_ids, changed_id);
703 got_object_commit_close(commit);
705 graph->iter_node = start_node;
709 const struct got_error *
710 got_commit_graph_iter_next(struct got_object_id **id,
711 struct got_commit_graph *graph)
715 if (graph->iter_node == NULL) {
716 /* We are done iterating, or iteration was not started. */
717 return got_error(GOT_ERR_ITER_COMPLETED);
720 if (graph->iter_node ==
721 TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
722 got_object_idset_num_elements(graph->open_branches) == 0) {
723 /* We are done iterating. */
724 *id = &graph->iter_node->id;
725 graph->iter_node = NULL;
729 if (TAILQ_NEXT(graph->iter_node, entry) == NULL)
730 return got_error(GOT_ERR_ITER_NEED_MORE);
732 *id = &graph->iter_node->id;
733 graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);