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_merge_point(struct got_commit_graph_node *node)
149 return node->nparents > 1;
153 is_branch_point(struct got_commit_graph_node *node)
155 return node->nchildren > 1;
159 is_root_node(struct got_commit_graph_node *node)
161 return node->nparents == 0;
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_TAIL(&graph->iter_list, node, entry);
229 /* Ensure that an iteration in progress will see this new commit. */
230 if (graph->iter_node) {
231 n = graph->iter_node;
233 next = TAILQ_NEXT(n, entry);
235 node->commit_timestamp >= next->commit_timestamp) {
236 TAILQ_INSERT_BEFORE(next, node, entry);
241 TAILQ_INSERT_AFTER(&graph->iter_list, graph->iter_node,
247 * If a child node is known, begin looping over the list there
248 * instead of beginning from the list head.
250 n = child_node ? child_node : TAILQ_FIRST(&graph->iter_list);
252 /* Insert into list based on committer timestamp. */
254 if (node->commit_timestamp == n->commit_timestamp) {
255 TAILQ_INSERT_AFTER(&graph->iter_list, n, node, entry);
257 } else if (node->commit_timestamp < n->commit_timestamp) {
258 next = TAILQ_NEXT(n, entry);
260 TAILQ_INSERT_AFTER(&graph->iter_list, n,
264 if (node->commit_timestamp >= next->commit_timestamp) {
265 TAILQ_INSERT_BEFORE(next, node, entry);
269 TAILQ_INSERT_BEFORE(n, node, entry);
272 n = TAILQ_NEXT(n, entry);
276 static const struct got_error *
277 add_vertex(struct got_object_id_queue *ids, struct got_object_id *id)
279 struct got_object_qid *qid;
281 qid = calloc(1, sizeof(*qid));
283 return got_error_from_errno();
285 qid->id = got_object_id_dup(id);
286 if (qid->id == NULL) {
287 const struct got_error *err = got_error_from_errno();
288 got_object_qid_free(qid);
292 SIMPLEQ_INSERT_TAIL(ids, qid, entry);
296 static const struct got_error *
297 close_branch(struct got_commit_graph *graph, struct got_object_id *commit_id)
299 const struct got_error *err;
301 err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
302 if (err && err->code != GOT_ERR_NO_OBJ)
307 static const struct got_error *
308 advance_branch(struct got_commit_graph *graph,
309 struct got_commit_graph_node *node,
310 struct got_object_id *commit_id, struct got_commit_object *commit)
312 const struct got_error *err;
313 struct got_object_qid *qid;
315 err = close_branch(graph, commit_id);
319 if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
320 qid = SIMPLEQ_FIRST(&commit->parent_ids);
323 err = got_object_idset_add(NULL, graph->open_branches, qid->id,
325 if (err && err->code != GOT_ERR_OBJ_EXISTS)
330 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
331 if (got_object_idset_get(graph->node_ids, qid->id))
332 continue; /* parent already traversed */
333 err = got_object_idset_add(NULL, graph->open_branches,
335 if (err && err->code != GOT_ERR_OBJ_EXISTS)
343 free_node(struct got_commit_graph_node *node)
345 while (!SIMPLEQ_EMPTY(&node->parent_ids)) {
346 struct got_object_qid *pid = SIMPLEQ_FIRST(&node->parent_ids);
347 SIMPLEQ_REMOVE_HEAD(&node->parent_ids, entry);
348 got_object_qid_free(pid);
353 static const struct got_error *
354 add_node(struct got_commit_graph_node **new_node,
355 struct got_commit_graph *graph, struct got_object_id *commit_id,
356 struct got_commit_object *commit, struct got_commit_graph_node *child_node,
357 struct got_repository *repo)
359 const struct got_error *err = NULL;
360 struct got_commit_graph_node *node;
361 struct got_object_qid *pid;
362 int changed = 0, branch_done = 0;
366 node = calloc(1, sizeof(*node));
368 return got_error_from_errno();
370 memcpy(&node->id, commit_id, sizeof(node->id));
371 SIMPLEQ_INIT(&node->parent_ids);
372 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
373 err = add_vertex(&node->parent_ids, pid->id);
380 node->commit_timestamp = mktime(&commit->tm_committer);
381 if (node->commit_timestamp == -1) {
383 return got_error_from_errno();
386 err = got_object_idset_add(NULL, graph->node_ids, &node->id, node);
388 if (err->code == GOT_ERR_OBJ_EXISTS)
394 err = detect_changed_path(&changed, commit, commit_id, graph->path,
397 if (err->code == GOT_ERR_NO_OBJ) {
399 * History of the path stops here on the current
400 * branch. Keep going on other branches.
411 add_node_to_iter_list(graph, node, child_node);
414 err = close_branch(graph, commit_id);
416 err = advance_branch(graph, node, commit_id, commit);
425 const struct got_error *
426 got_commit_graph_open(struct got_commit_graph **graph,
427 struct got_object_id *commit_id, const char *path,
428 int first_parent_traversal, struct got_repository *repo)
430 const struct got_error *err = NULL;
431 struct got_commit_object *commit;
435 err = got_object_open_as_commit(&commit, repo, commit_id);
439 /* The path must exist in our initial commit. */
440 if (!got_path_is_root_dir(path)) {
441 struct got_object_id *obj_id;
442 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
448 *graph = alloc_graph(path);
449 if (*graph == NULL) {
450 got_object_commit_close(commit);
451 return got_error_from_errno();
454 if (first_parent_traversal)
455 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
457 err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL,
459 got_object_commit_close(commit);
461 got_commit_graph_close(*graph);
469 struct gather_branch_tips_arg {
470 struct got_commit_graph_branch_tip *tips;
475 gather_branch_tips(struct got_object_id *id, void *data, void *arg)
477 struct gather_branch_tips_arg *a = arg;
478 memcpy(&a->tips[a->ntips].id, id, sizeof(*id));
479 a->tips[a->ntips].node = data;
483 static const struct got_error *
484 fetch_commits_from_open_branches(int *ncommits, int *wanted_id_added,
485 struct got_object_id **changed_id, struct got_commit_graph *graph,
486 struct got_repository *repo, struct got_object_id *wanted_id)
488 const struct got_error *err;
489 struct gather_branch_tips_arg arg;
494 *wanted_id_added = 0;
498 arg.ntips = got_object_idset_num_elements(graph->open_branches);
503 * Adding nodes to the graph might change the graph's open
504 * branches state. Create a local copy of the current state.
506 if (graph->ntips < arg.ntips) {
507 struct got_commit_graph_branch_tip *tips;
508 if (arg.ntips < GOT_COMMIT_GRAPH_MIN_TIPS)
509 arg.ntips = GOT_COMMIT_GRAPH_MIN_TIPS;
510 tips = reallocarray(graph->tips, arg.ntips, sizeof(*tips));
512 return got_error_from_errno();
514 graph->ntips = arg.ntips;
516 arg.ntips = 0; /* reset; gather_branch_tips() will increment */
517 arg.tips = graph->tips;
518 got_object_idset_for_each(graph->open_branches,
519 gather_branch_tips, &arg);
521 for (i = 0; i < arg.ntips; i++) {
522 struct got_object_id *commit_id;
523 struct got_commit_graph_node *child_node, *new_node;
524 struct got_commit_object *commit;
527 commit_id = &graph->tips[i].id;
528 child_node = graph->tips[i].node;
530 err = got_object_open_as_commit(&commit, repo, commit_id);
534 err = detect_changed_path(&changed, commit, commit_id,
537 got_object_commit_close(commit);
538 if (err->code != GOT_ERR_NO_OBJ)
540 err = close_branch(graph, commit_id);
545 if (changed && changed_id && *changed_id == NULL)
546 *changed_id = commit_id;
547 err = add_node(&new_node, graph, commit_id, commit, child_node,
549 got_object_commit_close(commit);
554 if (wanted_id && got_object_id_cmp(commit_id, wanted_id) == 0)
555 *wanted_id_added = 1;
561 const struct got_error *
562 got_commit_graph_fetch_commits(struct got_commit_graph *graph, int limit,
563 struct got_repository *repo)
565 const struct got_error *err;
566 int nfetched = 0, ncommits;
567 struct got_object_id *changed_id = NULL;
569 while (nfetched < limit) {
570 err = fetch_commits_from_open_branches(&ncommits, NULL,
571 &changed_id, graph, repo, NULL);
577 nfetched += ncommits;
583 const struct got_error *
584 got_commit_graph_fetch_commits_up_to(int *nfetched,
585 struct got_commit_graph *graph, struct got_object_id *wanted_id,
586 struct got_repository *repo)
588 const struct got_error *err;
589 int ncommits, wanted_id_added = 0;
593 if (got_object_idset_get(graph->node_ids, wanted_id) != NULL)
596 while (!wanted_id_added) {
597 err = fetch_commits_from_open_branches(&ncommits,
598 &wanted_id_added, NULL, graph, repo, wanted_id);
603 *nfetched += ncommits;
610 free_node_iter(struct got_object_id *id, void *data, void *arg)
612 struct got_commit_graph_node *node = data;
617 got_commit_graph_close(struct got_commit_graph *graph)
619 got_object_idset_free(graph->open_branches);
620 got_object_idset_for_each(graph->node_ids, free_node_iter, NULL);
621 got_object_idset_free(graph->node_ids);
627 const struct got_error *
628 got_commit_graph_iter_start(struct got_commit_graph *graph,
629 struct got_object_id *id, struct got_repository *repo)
631 const struct got_error *err = NULL;
632 struct got_commit_graph_node *start_node;
633 struct got_commit_object *commit;
636 start_node = got_object_idset_get(graph->node_ids, id);
637 if (start_node == NULL)
638 return got_error(GOT_ERR_NO_OBJ);
641 err = got_object_open_as_commit(&commit, repo, &start_node->id);
645 err = detect_changed_path(&changed, commit, &start_node->id,
648 got_object_commit_close(commit);
653 /* Locate first commit which changed graph->path. */
654 struct got_object_id *changed_id = NULL;
655 while (changed_id == NULL) {
657 err = fetch_commits_from_open_branches(&ncommits, NULL,
658 &changed_id, graph, repo, NULL);
660 got_object_commit_close(commit);
664 start_node = got_object_idset_get(graph->node_ids, changed_id);
666 got_object_commit_close(commit);
668 graph->iter_node = start_node;
672 const struct got_error *
673 got_commit_graph_iter_next(struct got_object_id **id,
674 struct got_commit_graph *graph)
678 if (graph->iter_node == NULL) {
679 /* We are done interating, or iteration was not started. */
680 return got_error(GOT_ERR_ITER_COMPLETED);
683 if (graph->iter_node ==
684 TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
685 got_object_idset_num_elements(graph->open_branches) == 0) {
686 *id = &graph->iter_node->id;
687 /* We are done interating. */
688 graph->iter_node = NULL;
692 if (TAILQ_NEXT(graph->iter_node, entry) == NULL)
693 return got_error(GOT_ERR_ITER_NEED_MORE);
695 *id = &graph->iter_node->id;
696 graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);