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>
30 #include "got_compat.h"
32 #include "got_error.h"
33 #include "got_object.h"
34 #include "got_cancel.h"
35 #include "got_commit_graph.h"
38 #include "got_lib_delta.h"
39 #include "got_lib_inflate.h"
40 #include "got_lib_object.h"
41 #include "got_lib_object_idset.h"
43 struct got_commit_graph_node {
44 struct got_object_id id;
46 /* Used only during iteration. */
48 TAILQ_ENTRY(got_commit_graph_node) entry;
51 TAILQ_HEAD(got_commit_graph_iter_list, got_commit_graph_node);
53 struct got_commit_graph_branch_tip {
54 struct got_object_id *commit_id;
55 struct got_commit_object *commit;
56 struct got_commit_graph_node *new_node;
59 struct got_commit_graph {
60 /* The set of all commits we have traversed. */
61 struct got_object_idset *node_ids;
64 #define GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL 0x01
67 * A set of object IDs of known parent commits which we have not yet
68 * traversed. Each commit ID in this set represents a branch in commit
69 * history: Either the first-parent branch of the head node, or another
70 * branch corresponding to a traversed merge commit for which we have
71 * not traversed a branch point commit yet.
73 * Whenever we add a commit with a matching ID to the graph, we remove
74 * its corresponding element from this set, and add new elements for
75 * each of that commit's parent commits which were not traversed yet.
77 * When API users ask us to fetch more commits, we fetch commits from
78 * all currently open branches. This allows API users to process
79 * commits in linear order even though the history contains branches.
81 struct got_object_idset *open_branches;
83 /* Array of branch tips for fetch_commits_from_open_branches(). */
84 struct got_commit_graph_branch_tip *tips;
87 /* Path of tree entry of interest to the API user. */
91 * Nodes which will be passed to the API user next, sorted by
94 struct got_commit_graph_iter_list iter_list;
97 static const struct got_error *
98 detect_changed_path(int *changed, struct got_commit_object *commit,
99 struct got_object_id *commit_id, const char *path,
100 struct got_repository *repo)
102 const struct got_error *err = NULL;
103 struct got_commit_object *pcommit = NULL;
104 struct got_tree_object *tree = NULL, *ptree = NULL;
105 struct got_object_qid *pid;
107 if (got_path_is_root_dir(path)) {
114 pid = STAILQ_FIRST(&commit->parent_ids);
116 struct got_object_id *obj_id;
117 err = got_object_id_by_path(&obj_id, repo, commit, path);
119 if (err->code == GOT_ERR_NO_TREE_ENTRY)
122 *changed = 1; /* The path was created in this commit. */
127 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
131 err = got_object_open_as_commit(&pcommit, repo, &pid->id);
135 err = got_object_open_as_tree(&ptree, repo, pcommit->tree_id);
139 err = got_object_tree_path_changed(changed, tree, ptree, path, repo);
142 got_object_tree_close(tree);
144 got_object_tree_close(ptree);
146 got_object_commit_close(pcommit);
151 add_node_to_iter_list(struct got_commit_graph *graph,
152 struct got_commit_graph_node *node, time_t committer_time)
154 struct got_commit_graph_node *n, *next;
156 node->timestamp = committer_time;
158 n = TAILQ_FIRST(&graph->iter_list);
160 next = TAILQ_NEXT(n, entry);
161 if (next && node->timestamp >= next->timestamp) {
162 TAILQ_INSERT_BEFORE(next, node, entry);
167 TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
170 static const struct got_error *
171 add_node(struct got_commit_graph_node **new_node,
172 struct got_commit_graph *graph, struct got_object_id *commit_id,
173 struct got_repository *repo)
175 const struct got_error *err = NULL;
176 struct got_commit_graph_node *node;
180 node = calloc(1, sizeof(*node));
182 return got_error_from_errno("calloc");
184 memcpy(&node->id, commit_id, sizeof(node->id));
185 err = got_object_idset_add(graph->node_ids, &node->id, NULL);
194 * Ask got-read-pack to traverse first-parent history until a commit is
195 * encountered which modified graph->path, or until the pack file runs
196 * out of relevant commits. This is faster than sending an individual
197 * request for each commit stored in the pack file.
199 static const struct got_error *
200 packed_first_parent_traversal(int *ncommits_traversed,
201 struct got_commit_graph *graph, struct got_object_id *commit_id,
202 struct got_repository *repo)
204 const struct got_error *err = NULL;
205 struct got_object_id_queue traversed_commits;
206 struct got_object_qid *qid;
208 STAILQ_INIT(&traversed_commits);
209 *ncommits_traversed = 0;
211 err = got_traverse_packed_commits(&traversed_commits,
212 commit_id, graph->path, repo);
216 /* Add all traversed commits to the graph... */
217 STAILQ_FOREACH(qid, &traversed_commits, entry) {
218 if (got_object_idset_contains(graph->open_branches, &qid->id))
220 if (got_object_idset_contains(graph->node_ids, &qid->id))
223 (*ncommits_traversed)++;
225 /* ... except the last commit is the new branch tip. */
226 if (STAILQ_NEXT(qid, entry) == NULL) {
227 err = got_object_idset_add(graph->open_branches,
232 err = got_object_idset_add(graph->node_ids, &qid->id, NULL);
237 got_object_id_queue_free(&traversed_commits);
241 static const struct got_error *
242 close_branch(struct got_commit_graph *graph, struct got_object_id *commit_id)
244 const struct got_error *err;
246 err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
247 if (err && err->code != GOT_ERR_NO_OBJ)
252 static const struct got_error *
253 advance_branch(struct got_commit_graph *graph, struct got_object_id *commit_id,
254 struct got_commit_object *commit, struct got_repository *repo)
256 const struct got_error *err;
257 struct got_object_qid *qid;
259 err = close_branch(graph, commit_id);
263 if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
264 qid = STAILQ_FIRST(&commit->parent_ids);
266 got_object_idset_contains(graph->open_branches, &qid->id))
269 * The root directory always changes by definition, and when
270 * logging the root we want to traverse consecutive commits
271 * even if they point at the same tree.
272 * But if we are looking for a specific path then we can avoid
273 * fetching packed commits which did not modify the path and
274 * only fetch their IDs. This speeds up 'got blame'.
276 if (!got_path_is_root_dir(graph->path) &&
277 (commit->flags & GOT_COMMIT_FLAG_PACKED)) {
279 err = packed_first_parent_traversal(&ncommits,
280 graph, &qid->id, repo);
281 if (err || ncommits > 0)
284 return got_object_idset_add(graph->open_branches,
289 * If we are graphing commits for a specific path, skip branches
290 * which do not contribute any content to this path.
292 if (commit->nparents > 1 && !got_path_is_root_dir(graph->path)) {
293 struct got_object_id *merged_id, *prev_id = NULL;
294 int branches_differ = 0;
296 err = got_object_id_by_path(&merged_id, repo, commit,
301 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
302 struct got_object_id *id = NULL;
303 struct got_commit_object *pcommit = NULL;
305 if (got_object_idset_contains(graph->open_branches,
309 err = got_object_open_as_commit(&pcommit, repo,
316 err = got_object_id_by_path(&id, repo, pcommit,
318 got_object_commit_close(pcommit);
321 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
331 if (!branches_differ &&
332 got_object_id_cmp(id, prev_id) != 0)
339 * If a branch has created the merged content we can
340 * skip any other branches.
342 if (got_object_id_cmp(merged_id, id) == 0) {
343 err = got_object_idset_add(graph->open_branches,
357 * If the path's content is the same on all branches,
358 * follow the first parent only.
360 if (!branches_differ) {
361 qid = STAILQ_FIRST(&commit->parent_ids);
364 if (got_object_idset_contains(graph->open_branches,
367 if (got_object_idset_contains(graph->node_ids,
369 return NULL; /* parent already traversed */
370 return got_object_idset_add(graph->open_branches,
375 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
376 if (got_object_idset_contains(graph->open_branches, &qid->id))
378 if (got_object_idset_contains(graph->node_ids, &qid->id))
379 continue; /* parent already traversed */
380 err = got_object_idset_add(graph->open_branches, &qid->id,
389 const struct got_error *
390 got_commit_graph_open(struct got_commit_graph **graph,
391 const char *path, int first_parent_traversal)
393 const struct got_error *err = NULL;
395 *graph = calloc(1, sizeof(**graph));
397 return got_error_from_errno("calloc");
399 TAILQ_INIT(&(*graph)->iter_list);
401 (*graph)->path = strdup(path);
402 if ((*graph)->path == NULL) {
403 err = got_error_from_errno("strdup");
407 (*graph)->node_ids = got_object_idset_alloc();
408 if ((*graph)->node_ids == NULL) {
409 err = got_error_from_errno("got_object_idset_alloc");
413 (*graph)->open_branches = got_object_idset_alloc();
414 if ((*graph)->open_branches == NULL) {
415 err = got_error_from_errno("got_object_idset_alloc");
419 if (first_parent_traversal)
420 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
423 got_commit_graph_close(*graph);
429 struct add_branch_tip_arg {
430 struct got_commit_graph_branch_tip *tips;
432 struct got_repository *repo;
433 struct got_commit_graph *graph;
436 static const struct got_error *
437 add_branch_tip(struct got_object_id *commit_id, void *data, void *arg)
439 const struct got_error *err;
440 struct add_branch_tip_arg *a = arg;
441 struct got_commit_graph_node *new_node;
442 struct got_commit_object *commit;
444 err = got_object_open_as_commit(&commit, a->repo, commit_id);
448 err = add_node(&new_node, a->graph, commit_id, a->repo);
450 got_object_commit_close(commit);
454 a->tips[a->ntips].commit_id = &new_node->id;
455 a->tips[a->ntips].commit = commit;
456 a->tips[a->ntips].new_node = new_node;
462 static const struct got_error *
463 fetch_commits_from_open_branches(struct got_commit_graph *graph,
464 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
466 const struct got_error *err;
467 struct add_branch_tip_arg arg;
470 ntips = got_object_idset_num_elements(graph->open_branches);
474 /* (Re-)allocate branch tips array if necessary. */
475 if (graph->ntips < ntips) {
476 struct got_commit_graph_branch_tip *tips;
477 tips = recallocarray(graph->tips, graph->ntips, ntips,
480 return got_error_from_errno("recallocarray");
482 graph->ntips = ntips;
484 arg.tips = graph->tips;
485 arg.ntips = 0; /* add_branch_tip() will increment */
488 err = got_object_idset_for_each(graph->open_branches, add_branch_tip,
493 for (i = 0; i < arg.ntips; i++) {
494 struct got_object_id *commit_id;
495 struct got_commit_object *commit;
496 struct got_commit_graph_node *new_node;
500 err = (*cancel_cb)(cancel_arg);
505 commit_id = arg.tips[i].commit_id;
506 commit = arg.tips[i].commit;
507 new_node = arg.tips[i].new_node;
509 err = detect_changed_path(&changed, commit, commit_id,
512 if (err->code != GOT_ERR_NO_OBJ)
515 * History of the path stops here on the current
516 * branch. Keep going on other branches.
518 err = close_branch(graph, commit_id);
524 add_node_to_iter_list(graph, new_node,
525 got_object_commit_get_committer_time(commit));
526 arg.tips[i].new_node = NULL;
528 err = advance_branch(graph, commit_id, commit, repo);
533 for (i = 0; i < arg.ntips; i++) {
534 got_object_commit_close(arg.tips[i].commit);
535 free(arg.tips[i].new_node);
541 got_commit_graph_close(struct got_commit_graph *graph)
543 struct got_commit_graph_node *node;
545 while ((node = TAILQ_FIRST(&graph->iter_list))) {
546 TAILQ_REMOVE(&graph->iter_list, node, entry);
550 if (graph->open_branches)
551 got_object_idset_free(graph->open_branches);
553 got_object_idset_free(graph->node_ids);
559 const struct got_error *
560 got_commit_graph_iter_start(struct got_commit_graph *graph,
561 struct got_object_id *id, struct got_repository *repo,
562 got_cancel_cb cancel_cb, void *cancel_arg)
564 const struct got_error *err = NULL;
566 if (!TAILQ_EMPTY(&graph->iter_list))
567 return got_error(GOT_ERR_ITER_BUSY);
569 err = got_object_idset_add(graph->open_branches, id, NULL);
573 /* Locate first commit which changed graph->path. */
574 while (TAILQ_EMPTY(&graph->iter_list) &&
575 got_object_idset_num_elements(graph->open_branches) > 0) {
576 err = fetch_commits_from_open_branches(graph, repo,
577 cancel_cb, cancel_arg);
582 if (TAILQ_EMPTY(&graph->iter_list)) {
584 if (got_path_is_root_dir(graph->path))
585 return got_error_no_obj(id);
587 while (path[0] == '/')
589 return got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
595 const struct got_error *
596 got_commit_graph_iter_next(struct got_object_id *id,
597 struct got_commit_graph *graph, struct got_repository *repo,
598 got_cancel_cb cancel_cb, void *cancel_arg)
600 const struct got_error *err = NULL;
601 struct got_commit_graph_node *node;
603 node = TAILQ_FIRST(&graph->iter_list);
605 /* We are done iterating, or iteration was not started. */
606 return got_error(GOT_ERR_ITER_COMPLETED);
609 while (TAILQ_NEXT(node, entry) == NULL &&
610 got_object_idset_num_elements(graph->open_branches) > 0) {
611 err = fetch_commits_from_open_branches(graph, repo,
612 cancel_cb, cancel_arg);
617 memcpy(id, &node->id, sizeof(*id));
619 TAILQ_REMOVE(&graph->iter_list, node, entry);
624 static const struct got_error *
625 find_yca_add_id(struct got_object_id **yca_id, struct got_commit_graph *graph,
626 struct got_object_idset *commit_ids, struct got_repository *repo,
627 got_cancel_cb cancel_cb, void *cancel_arg)
629 const struct got_error *err = NULL;
630 struct got_object_id id;
632 err = got_commit_graph_iter_next(&id, graph, repo, cancel_cb,
637 if (got_object_idset_contains(commit_ids, &id)) {
638 *yca_id = got_object_id_dup(&id);
640 err = got_error_from_errno("got_object_id_dup");
644 return got_object_idset_add(commit_ids, &id, NULL);
647 const struct got_error *
648 got_commit_graph_find_youngest_common_ancestor(struct got_object_id **yca_id,
649 struct got_object_id *commit_id, struct got_object_id *commit_id2,
650 int first_parent_traversal,
651 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
653 const struct got_error *err = NULL;
654 struct got_commit_graph *graph = NULL, *graph2 = NULL;
655 int completed = 0, completed2 = 0;
656 struct got_object_idset *commit_ids;
660 commit_ids = got_object_idset_alloc();
661 if (commit_ids == NULL)
662 return got_error_from_errno("got_object_idset_alloc");
664 err = got_commit_graph_open(&graph, "/", first_parent_traversal);
668 err = got_commit_graph_open(&graph2, "/", first_parent_traversal);
672 err = got_commit_graph_iter_start(graph, commit_id, repo,
673 cancel_cb, cancel_arg);
677 err = got_commit_graph_iter_start(graph2, commit_id2, repo,
678 cancel_cb, cancel_arg);
684 err = (*cancel_cb)(cancel_arg);
690 err = find_yca_add_id(yca_id, graph, commit_ids, repo,
691 cancel_cb, cancel_arg);
693 if (err->code != GOT_ERR_ITER_COMPLETED)
703 err = find_yca_add_id(yca_id, graph2, commit_ids, repo,
704 cancel_cb, cancel_arg);
706 if (err->code != GOT_ERR_ITER_COMPLETED)
715 if (completed && completed2) {
716 err = got_error(GOT_ERR_ANCESTRY);
721 got_object_idset_free(commit_ids);
723 got_commit_graph_close(graph);
725 got_commit_graph_close(graph2);