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 10 /* 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);
191 if (err->code == GOT_ERR_NO_OBJ)
194 *changed = 1; /* The path was created in this commit. */
197 err = got_object_open_as_commit(&pcommit, repo, pid->id);
201 err = got_object_open_as_tree(&ptree, repo, pcommit->tree_id);
205 err = got_object_tree_path_changed(changed, tree, ptree, path,
210 got_object_tree_close(tree);
212 got_object_tree_close(ptree);
214 got_object_commit_close(pcommit);
219 add_node_to_iter_list(struct got_commit_graph *graph,
220 struct got_commit_graph_node *node,
221 struct got_commit_graph_node *child_node)
223 struct got_commit_graph_node *n, *next;
225 if (TAILQ_EMPTY(&graph->iter_list)) {
226 TAILQ_INSERT_HEAD(&graph->iter_list, node, entry);
227 graph->iter_node = node;
231 n = graph->iter_node;
232 /* Ensure that an iteration in progress will see this new commit. */
234 next = TAILQ_NEXT(n, entry);
235 if (next && node->commit_timestamp >= next->commit_timestamp) {
236 TAILQ_INSERT_BEFORE(next, node, entry);
241 TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
244 static const struct got_error *
245 add_vertex(struct got_object_id_queue *ids, struct got_object_id *id)
247 struct got_object_qid *qid;
249 qid = calloc(1, sizeof(*qid));
251 return got_error_from_errno();
253 qid->id = got_object_id_dup(id);
254 if (qid->id == NULL) {
255 const struct got_error *err = got_error_from_errno();
256 got_object_qid_free(qid);
260 SIMPLEQ_INSERT_TAIL(ids, qid, entry);
264 static const struct got_error *
265 close_branch(struct got_commit_graph *graph, struct got_object_id *commit_id)
267 const struct got_error *err;
269 err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
270 if (err && err->code != GOT_ERR_NO_OBJ)
275 static const struct got_error *
276 advance_branch(struct got_commit_graph *graph,
277 struct got_commit_graph_node *node,
278 struct got_object_id *commit_id, struct got_commit_object *commit,
279 struct got_repository *repo)
281 const struct got_error *err;
282 struct got_object_qid *qid;
284 err = close_branch(graph, commit_id);
288 if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
289 qid = SIMPLEQ_FIRST(&commit->parent_ids);
292 err = got_object_idset_add(graph->open_branches, qid->id, node);
297 * If we are graphing commits for a specific path, skip branches
298 * which do not contribute any content to this path.
300 if (is_merge_point(node) && !got_path_is_root_dir(graph->path)) {
301 struct got_object_id *id, *merged_id, *prev_id = NULL;
302 int branches_differ = 0;
304 err = got_object_id_by_path(&merged_id, repo, commit_id,
309 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
310 if (got_object_idset_get(graph->node_ids, qid->id))
311 continue; /* parent already traversed */
313 err = got_object_id_by_path(&id, repo, qid->id,
316 if (err->code == GOT_ERR_NO_OBJ) {
324 if (!branches_differ &&
325 got_object_id_cmp(merged_id, prev_id) != 0)
331 * If a branch has created the merged content we can
332 * skip any other branches.
334 if (got_object_id_cmp(merged_id, id) == 0) {
335 err = got_object_idset_add(graph->open_branches,
342 * If the path's content is the same on all branches,
343 * follow the first parent only.
345 if (!branches_differ) {
346 qid = SIMPLEQ_FIRST(&commit->parent_ids);
349 if (got_object_idset_get(graph->node_ids, qid->id))
350 return NULL; /* parent already traversed */
351 if (got_object_idset_get(graph->open_branches, qid->id))
353 return got_object_idset_add(graph->open_branches,
358 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
359 if (got_object_idset_get(graph->node_ids, qid->id))
360 continue; /* parent already traversed */
361 if (got_object_idset_get(graph->open_branches, qid->id))
363 err = got_object_idset_add(graph->open_branches, qid->id, node);
372 free_node(struct got_commit_graph_node *node)
374 while (!SIMPLEQ_EMPTY(&node->parent_ids)) {
375 struct got_object_qid *pid = SIMPLEQ_FIRST(&node->parent_ids);
376 SIMPLEQ_REMOVE_HEAD(&node->parent_ids, entry);
377 got_object_qid_free(pid);
382 static const struct got_error *
383 add_node(struct got_commit_graph_node **new_node,
384 struct got_commit_graph *graph, struct got_object_id *commit_id,
385 struct got_commit_object *commit, struct got_commit_graph_node *child_node,
386 struct got_repository *repo)
388 const struct got_error *err = NULL;
389 struct got_commit_graph_node *node;
390 struct got_object_qid *pid;
391 int changed = 0, branch_done = 0;
395 node = calloc(1, sizeof(*node));
397 return got_error_from_errno();
399 memcpy(&node->id, commit_id, sizeof(node->id));
400 SIMPLEQ_INIT(&node->parent_ids);
401 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
402 err = add_vertex(&node->parent_ids, pid->id);
410 err = got_object_idset_add(graph->node_ids, &node->id, node);
416 err = detect_changed_path(&changed, commit, commit_id, graph->path,
419 if (err->code == GOT_ERR_NO_OBJ) {
421 * History of the path stops here on the current
422 * branch. Keep going on other branches.
432 node->commit_timestamp = commit->committer_time;
433 if (node->commit_timestamp == -1) {
435 return got_error_from_errno();
439 add_node_to_iter_list(graph, node, child_node);
442 err = close_branch(graph, commit_id);
444 err = advance_branch(graph, node, commit_id, commit, repo);
453 const struct got_error *
454 got_commit_graph_open(struct got_commit_graph **graph,
455 struct got_object_id *commit_id, const char *path,
456 int first_parent_traversal, struct got_repository *repo)
458 const struct got_error *err = NULL;
459 struct got_commit_object *commit;
463 err = got_object_open_as_commit(&commit, repo, commit_id);
467 /* The path must exist in our initial commit. */
468 if (!got_path_is_root_dir(path)) {
469 struct got_object_id *obj_id;
470 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
476 *graph = alloc_graph(path);
477 if (*graph == NULL) {
478 got_object_commit_close(commit);
479 return got_error_from_errno();
482 if (first_parent_traversal)
483 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
485 err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL,
487 got_object_commit_close(commit);
489 got_commit_graph_close(*graph);
497 struct gather_branch_tips_arg {
498 struct got_commit_graph_branch_tip *tips;
503 gather_branch_tips(struct got_object_id *id, void *data, void *arg)
505 struct gather_branch_tips_arg *a = arg;
506 memcpy(&a->tips[a->ntips].id, id, sizeof(*id));
507 a->tips[a->ntips].node = data;
511 static const struct got_error *
512 fetch_commits_from_open_branches(int *ncommits,
513 struct got_object_id **changed_id, struct got_commit_graph *graph,
514 struct got_repository *repo)
516 const struct got_error *err;
517 struct gather_branch_tips_arg arg;
524 arg.ntips = got_object_idset_num_elements(graph->open_branches);
529 * Adding nodes to the graph might change the graph's open
530 * branches state. Create a local copy of the current state.
532 if (graph->ntips < arg.ntips) {
533 struct got_commit_graph_branch_tip *tips;
534 if (arg.ntips < GOT_COMMIT_GRAPH_MIN_TIPS)
535 arg.ntips = GOT_COMMIT_GRAPH_MIN_TIPS;
536 tips = reallocarray(graph->tips, arg.ntips, sizeof(*tips));
538 return got_error_from_errno();
540 graph->ntips = arg.ntips;
542 arg.ntips = 0; /* reset; gather_branch_tips() will increment */
543 arg.tips = graph->tips;
544 got_object_idset_for_each(graph->open_branches,
545 gather_branch_tips, &arg);
547 for (i = 0; i < arg.ntips; i++) {
548 struct got_object_id *commit_id;
549 struct got_commit_graph_node *child_node, *new_node;
550 struct got_commit_object *commit;
553 commit_id = &graph->tips[i].id;
554 child_node = graph->tips[i].node;
556 err = got_object_open_as_commit(&commit, repo, commit_id);
560 err = detect_changed_path(&changed, commit, commit_id,
563 got_object_commit_close(commit);
564 if (err->code != GOT_ERR_NO_OBJ)
566 err = close_branch(graph, commit_id);
571 if (changed && changed_id && *changed_id == NULL)
572 *changed_id = commit_id;
573 err = add_node(&new_node, graph, commit_id, commit, child_node,
575 got_object_commit_close(commit);
585 const struct got_error *
586 got_commit_graph_fetch_commits(struct got_commit_graph *graph, int limit,
587 struct got_repository *repo)
589 const struct got_error *err;
590 int nfetched = 0, ncommits;
591 struct got_object_id *changed_id = NULL;
593 while (nfetched < limit) {
594 err = fetch_commits_from_open_branches(&ncommits,
595 &changed_id, graph, repo);
601 nfetched += ncommits;
608 free_node_iter(struct got_object_id *id, void *data, void *arg)
610 struct got_commit_graph_node *node = data;
615 got_commit_graph_close(struct got_commit_graph *graph)
617 got_object_idset_free(graph->open_branches);
618 got_object_idset_for_each(graph->node_ids, free_node_iter, NULL);
619 got_object_idset_free(graph->node_ids);
625 const struct got_error *
626 got_commit_graph_iter_start(struct got_commit_graph *graph,
627 struct got_object_id *id, struct got_repository *repo)
629 const struct got_error *err = NULL;
630 struct got_commit_graph_node *start_node;
631 struct got_commit_object *commit;
634 start_node = got_object_idset_get(graph->node_ids, id);
635 if (start_node == NULL)
636 return got_error(GOT_ERR_NO_OBJ);
638 err = got_object_open_as_commit(&commit, repo, &start_node->id);
642 err = detect_changed_path(&changed, commit, &start_node->id,
645 got_object_commit_close(commit);
650 /* Locate first commit which changed graph->path. */
651 struct got_object_id *changed_id = NULL;
652 while (changed_id == NULL) {
654 err = fetch_commits_from_open_branches(&ncommits,
655 &changed_id, graph, repo);
657 got_object_commit_close(commit);
661 start_node = got_object_idset_get(graph->node_ids, changed_id);
663 got_object_commit_close(commit);
665 graph->iter_node = start_node;
669 const struct got_error *
670 got_commit_graph_iter_next(struct got_object_id **id,
671 struct got_commit_graph *graph)
675 if (graph->iter_node == NULL) {
676 /* We are done iterating, or iteration was not started. */
677 return got_error(GOT_ERR_ITER_COMPLETED);
680 if (graph->iter_node ==
681 TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
682 got_object_idset_num_elements(graph->open_branches) == 0) {
683 /* We are done iterating. */
684 *id = &graph->iter_node->id;
685 graph->iter_node = NULL;
689 if (TAILQ_NEXT(graph->iter_node, entry) == NULL)
690 return got_error(GOT_ERR_ITER_NEED_MORE);
692 *id = &graph->iter_node->id;
693 graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);