2 * Copyright (c) 2018, 2019, 2020 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>
28 #include "got_compat.h"
30 #include "got_error.h"
31 #include "got_object.h"
32 #include "got_cancel.h"
33 #include "got_commit_graph.h"
36 #include "got_lib_delta.h"
37 #include "got_lib_inflate.h"
38 #include "got_lib_object.h"
39 #include "got_lib_object_idset.h"
41 struct got_commit_graph_node {
42 struct got_object_id id;
44 /* Used only during iteration. */
46 TAILQ_ENTRY(got_commit_graph_node) entry;
49 TAILQ_HEAD(got_commit_graph_iter_list, got_commit_graph_node);
51 struct got_commit_graph_branch_tip {
52 struct got_object_id *commit_id;
53 struct got_commit_object *commit;
54 struct got_commit_graph_node *new_node;
57 struct got_commit_graph {
58 /* The set of all commits we have traversed. */
59 struct got_object_idset *node_ids;
62 #define GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL 0x01
65 * A set of object IDs of known parent commits which we have not yet
66 * traversed. Each commit ID in this set represents a branch in commit
67 * history: Either the first-parent branch of the head node, or another
68 * branch corresponding to a traversed merge commit for which we have
69 * not traversed a branch point commit yet.
71 * Whenever we add a commit with a matching ID to the graph, we remove
72 * its corresponding element from this set, and add new elements for
73 * each of that commit's parent commits which were not traversed yet.
75 * When API users ask us to fetch more commits, we fetch commits from
76 * all currently open branches. This allows API users to process
77 * commits in linear order even though the history contains branches.
79 struct got_object_idset *open_branches;
81 /* Array of branch tips for fetch_commits_from_open_branches(). */
82 struct got_commit_graph_branch_tip *tips;
85 /* Path of tree entry of interest to the API user. */
89 * Nodes which will be passed to the API user next, sorted by
92 struct got_commit_graph_iter_list iter_list;
95 * Temporary storage for the id returned by
96 * got_commit_graph_iter_next.
98 struct got_object_id id;
101 static const struct got_error *
102 detect_changed_path(int *changed, struct got_commit_object *commit,
103 struct got_object_id *commit_id, const char *path,
104 struct got_repository *repo)
106 const struct got_error *err = NULL;
107 struct got_commit_object *pcommit = NULL;
108 struct got_tree_object *tree = NULL, *ptree = NULL;
109 struct got_object_qid *pid;
111 if (got_path_is_root_dir(path)) {
118 pid = STAILQ_FIRST(&commit->parent_ids);
120 struct got_object_id *obj_id;
121 err = got_object_id_by_path(&obj_id, repo, commit, path);
123 if (err->code == GOT_ERR_NO_TREE_ENTRY)
126 *changed = 1; /* The path was created in this commit. */
131 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
135 err = got_object_open_as_commit(&pcommit, repo, &pid->id);
139 err = got_object_open_as_tree(&ptree, repo, pcommit->tree_id);
143 err = got_object_tree_path_changed(changed, tree, ptree, path, repo);
146 got_object_tree_close(tree);
148 got_object_tree_close(ptree);
150 got_object_commit_close(pcommit);
155 add_node_to_iter_list(struct got_commit_graph *graph,
156 struct got_commit_graph_node *node, time_t committer_time)
158 struct got_commit_graph_node *n, *next;
160 node->timestamp = committer_time;
162 n = TAILQ_FIRST(&graph->iter_list);
164 next = TAILQ_NEXT(n, entry);
165 if (next && node->timestamp >= next->timestamp) {
166 TAILQ_INSERT_BEFORE(next, node, entry);
171 TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
174 static const struct got_error *
175 add_node(struct got_commit_graph_node **new_node,
176 struct got_commit_graph *graph, struct got_object_id *commit_id,
177 struct got_repository *repo)
179 const struct got_error *err = NULL;
180 struct got_commit_graph_node *node;
184 node = calloc(1, sizeof(*node));
186 return got_error_from_errno("calloc");
188 memcpy(&node->id, commit_id, sizeof(node->id));
189 err = got_object_idset_add(graph->node_ids, &node->id, NULL);
198 * Ask got-read-pack to traverse first-parent history until a commit is
199 * encountered which modified graph->path, or until the pack file runs
200 * out of relevant commits. This is faster than sending an individual
201 * request for each commit stored in the pack file.
203 static const struct got_error *
204 packed_first_parent_traversal(int *ncommits_traversed,
205 struct got_commit_graph *graph, struct got_object_id *commit_id,
206 struct got_repository *repo)
208 const struct got_error *err = NULL;
209 struct got_object_id_queue traversed_commits;
210 struct got_object_qid *qid;
212 STAILQ_INIT(&traversed_commits);
213 *ncommits_traversed = 0;
215 err = got_traverse_packed_commits(&traversed_commits,
216 commit_id, graph->path, repo);
220 /* Add all traversed commits to the graph... */
221 STAILQ_FOREACH(qid, &traversed_commits, entry) {
222 if (got_object_idset_contains(graph->open_branches, &qid->id))
224 if (got_object_idset_contains(graph->node_ids, &qid->id))
227 (*ncommits_traversed)++;
229 /* ... except the last commit is the new branch tip. */
230 if (STAILQ_NEXT(qid, entry) == NULL) {
231 err = got_object_idset_add(graph->open_branches,
236 err = got_object_idset_add(graph->node_ids, &qid->id, NULL);
241 got_object_id_queue_free(&traversed_commits);
245 static const struct got_error *
246 close_branch(struct got_commit_graph *graph, struct got_object_id *commit_id)
248 const struct got_error *err;
250 err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
251 if (err && err->code != GOT_ERR_NO_OBJ)
256 static const struct got_error *
257 advance_branch(struct got_commit_graph *graph, struct got_object_id *commit_id,
258 struct got_commit_object *commit, struct got_repository *repo)
260 const struct got_error *err;
261 struct got_object_qid *qid;
263 err = close_branch(graph, commit_id);
267 if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
268 qid = STAILQ_FIRST(&commit->parent_ids);
270 got_object_idset_contains(graph->open_branches, &qid->id))
273 * The root directory always changes by definition, and when
274 * logging the root we want to traverse consecutive commits
275 * even if they point at the same tree.
276 * But if we are looking for a specific path then we can avoid
277 * fetching packed commits which did not modify the path and
278 * only fetch their IDs. This speeds up 'got blame'.
280 if (!got_path_is_root_dir(graph->path) &&
281 (commit->flags & GOT_COMMIT_FLAG_PACKED)) {
283 err = packed_first_parent_traversal(&ncommits,
284 graph, &qid->id, repo);
285 if (err || ncommits > 0)
288 return got_object_idset_add(graph->open_branches,
293 * If we are graphing commits for a specific path, skip branches
294 * which do not contribute any content to this path.
296 if (commit->nparents > 1 && !got_path_is_root_dir(graph->path)) {
297 struct got_object_id *merged_id, *prev_id = NULL;
298 int branches_differ = 0;
300 err = got_object_id_by_path(&merged_id, repo, commit,
305 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
306 struct got_object_id *id = NULL;
307 struct got_commit_object *pcommit = NULL;
309 if (got_object_idset_contains(graph->open_branches,
313 err = got_object_open_as_commit(&pcommit, repo,
320 err = got_object_id_by_path(&id, repo, pcommit,
322 got_object_commit_close(pcommit);
325 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
335 if (!branches_differ &&
336 got_object_id_cmp(id, prev_id) != 0)
343 * If a branch has created the merged content we can
344 * skip any other branches.
346 if (got_object_id_cmp(merged_id, id) == 0) {
347 err = got_object_idset_add(graph->open_branches,
361 * If the path's content is the same on all branches,
362 * follow the first parent only.
364 if (!branches_differ) {
365 qid = STAILQ_FIRST(&commit->parent_ids);
368 if (got_object_idset_contains(graph->open_branches,
371 if (got_object_idset_contains(graph->node_ids,
373 return NULL; /* parent already traversed */
374 return got_object_idset_add(graph->open_branches,
379 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
380 if (got_object_idset_contains(graph->open_branches, &qid->id))
382 if (got_object_idset_contains(graph->node_ids, &qid->id))
383 continue; /* parent already traversed */
384 err = got_object_idset_add(graph->open_branches, &qid->id,
393 const struct got_error *
394 got_commit_graph_open(struct got_commit_graph **graph,
395 const char *path, int first_parent_traversal)
397 const struct got_error *err = NULL;
399 *graph = calloc(1, sizeof(**graph));
401 return got_error_from_errno("calloc");
403 TAILQ_INIT(&(*graph)->iter_list);
405 (*graph)->path = strdup(path);
406 if ((*graph)->path == NULL) {
407 err = got_error_from_errno("strdup");
411 (*graph)->node_ids = got_object_idset_alloc();
412 if ((*graph)->node_ids == NULL) {
413 err = got_error_from_errno("got_object_idset_alloc");
417 (*graph)->open_branches = got_object_idset_alloc();
418 if ((*graph)->open_branches == NULL) {
419 err = got_error_from_errno("got_object_idset_alloc");
423 if (first_parent_traversal)
424 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
427 got_commit_graph_close(*graph);
433 struct add_branch_tip_arg {
434 struct got_commit_graph_branch_tip *tips;
436 struct got_repository *repo;
437 struct got_commit_graph *graph;
440 static const struct got_error *
441 add_branch_tip(struct got_object_id *commit_id, void *data, void *arg)
443 const struct got_error *err;
444 struct add_branch_tip_arg *a = arg;
445 struct got_commit_graph_node *new_node;
446 struct got_commit_object *commit;
448 err = got_object_open_as_commit(&commit, a->repo, commit_id);
452 err = add_node(&new_node, a->graph, commit_id, a->repo);
454 got_object_commit_close(commit);
458 a->tips[a->ntips].commit_id = &new_node->id;
459 a->tips[a->ntips].commit = commit;
460 a->tips[a->ntips].new_node = new_node;
466 static const struct got_error *
467 fetch_commits_from_open_branches(struct got_commit_graph *graph,
468 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
470 const struct got_error *err;
471 struct add_branch_tip_arg arg;
474 ntips = got_object_idset_num_elements(graph->open_branches);
478 /* (Re-)allocate branch tips array if necessary. */
479 if (graph->ntips < ntips) {
480 struct got_commit_graph_branch_tip *tips;
481 tips = recallocarray(graph->tips, graph->ntips, ntips,
484 return got_error_from_errno("recallocarray");
486 graph->ntips = ntips;
488 arg.tips = graph->tips;
489 arg.ntips = 0; /* add_branch_tip() will increment */
492 err = got_object_idset_for_each(graph->open_branches, add_branch_tip,
497 for (i = 0; i < arg.ntips; i++) {
498 struct got_object_id *commit_id;
499 struct got_commit_object *commit;
500 struct got_commit_graph_node *new_node;
504 err = (*cancel_cb)(cancel_arg);
509 commit_id = arg.tips[i].commit_id;
510 commit = arg.tips[i].commit;
511 new_node = arg.tips[i].new_node;
513 err = detect_changed_path(&changed, commit, commit_id,
516 if (err->code != GOT_ERR_NO_OBJ)
519 * History of the path stops here on the current
520 * branch. Keep going on other branches.
522 err = close_branch(graph, commit_id);
528 add_node_to_iter_list(graph, new_node,
529 got_object_commit_get_committer_time(commit));
530 arg.tips[i].new_node = NULL;
532 err = advance_branch(graph, commit_id, commit, repo);
537 for (i = 0; i < arg.ntips; i++) {
538 got_object_commit_close(arg.tips[i].commit);
539 free(arg.tips[i].new_node);
545 got_commit_graph_close(struct got_commit_graph *graph)
547 struct got_commit_graph_node *node;
549 while ((node = TAILQ_FIRST(&graph->iter_list))) {
550 TAILQ_REMOVE(&graph->iter_list, node, entry);
554 if (graph->open_branches)
555 got_object_idset_free(graph->open_branches);
557 got_object_idset_free(graph->node_ids);
563 const struct got_error *
564 got_commit_graph_iter_start(struct got_commit_graph *graph,
565 struct got_object_id *id, struct got_repository *repo,
566 got_cancel_cb cancel_cb, void *cancel_arg)
568 const struct got_error *err = NULL;
570 if (!TAILQ_EMPTY(&graph->iter_list))
571 return got_error(GOT_ERR_ITER_BUSY);
573 err = got_object_idset_add(graph->open_branches, id, NULL);
577 /* Locate first commit which changed graph->path. */
578 while (TAILQ_EMPTY(&graph->iter_list) &&
579 got_object_idset_num_elements(graph->open_branches) > 0) {
580 err = fetch_commits_from_open_branches(graph, repo,
581 cancel_cb, cancel_arg);
586 if (TAILQ_EMPTY(&graph->iter_list)) {
588 if (got_path_is_root_dir(graph->path))
589 return got_error_no_obj(id);
591 while (path[0] == '/')
593 return got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
599 const struct got_error *
600 got_commit_graph_iter_next(struct got_object_id **id,
601 struct got_commit_graph *graph, struct got_repository *repo,
602 got_cancel_cb cancel_cb, void *cancel_arg)
604 const struct got_error *err = NULL;
605 struct got_commit_graph_node *node;
609 node = TAILQ_FIRST(&graph->iter_list);
611 /* We are done iterating, or iteration was not started. */
612 return got_error(GOT_ERR_ITER_COMPLETED);
615 while (TAILQ_NEXT(node, entry) == NULL &&
616 got_object_idset_num_elements(graph->open_branches) > 0) {
617 err = fetch_commits_from_open_branches(graph, repo,
618 cancel_cb, cancel_arg);
623 memcpy(&graph->id, &node->id, sizeof(graph->id));
626 TAILQ_REMOVE(&graph->iter_list, node, entry);
631 const struct got_error *
632 got_commit_graph_find_youngest_common_ancestor(struct got_object_id **yca_id,
633 struct got_object_id *commit_id, struct got_object_id *commit_id2,
634 int first_parent_traversal,
635 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
637 const struct got_error *err = NULL;
638 struct got_commit_graph *graph = NULL, *graph2 = NULL;
639 int completed = 0, completed2 = 0;
640 struct got_object_idset *commit_ids;
644 commit_ids = got_object_idset_alloc();
645 if (commit_ids == NULL)
646 return got_error_from_errno("got_object_idset_alloc");
648 err = got_commit_graph_open(&graph, "/", first_parent_traversal);
652 err = got_commit_graph_open(&graph2, "/", first_parent_traversal);
656 err = got_commit_graph_iter_start(graph, commit_id, repo,
657 cancel_cb, cancel_arg);
661 err = got_commit_graph_iter_start(graph2, commit_id2, repo,
662 cancel_cb, cancel_arg);
667 struct got_object_id *id = NULL, *id2 = NULL;
670 err = (*cancel_cb)(cancel_arg);
676 err = got_commit_graph_iter_next(&id, graph, repo,
677 cancel_cb, cancel_arg);
679 if (err->code != GOT_ERR_ITER_COMPLETED)
687 err = got_commit_graph_iter_next(&id2, graph2, repo,
688 cancel_cb, cancel_arg);
690 if (err->code != GOT_ERR_ITER_COMPLETED)
698 if (got_object_idset_contains(commit_ids, id)) {
699 *yca_id = got_object_id_dup(id);
702 err = got_error_from_errno("got_object_id_dup");
706 err = got_object_idset_add(commit_ids, id, NULL);
711 if (got_object_idset_contains(commit_ids, id2)) {
712 *yca_id = got_object_id_dup(id2);
715 err = got_error_from_errno("got_object_id_dup");
719 err = got_object_idset_add(commit_ids, id2, NULL);
724 if (completed && completed2) {
725 err = got_error(GOT_ERR_ANCESTRY);
731 got_object_idset_free(commit_ids);
733 got_commit_graph_close(graph);
735 got_commit_graph_close(graph2);