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>
20 #include <sys/stdint.h>
31 #include "got_error.h"
32 #include "got_object.h"
33 #include "got_cancel.h"
34 #include "got_commit_graph.h"
37 #include "got_lib_delta.h"
38 #include "got_lib_inflate.h"
39 #include "got_lib_object.h"
40 #include "got_lib_object_idset.h"
42 struct got_commit_graph_node {
43 struct got_object_id id;
45 /* Used only during iteration. */
47 TAILQ_ENTRY(got_commit_graph_node) entry;
50 TAILQ_HEAD(got_commit_graph_iter_list, got_commit_graph_node);
52 struct got_commit_graph_branch_tip {
53 struct got_object_id *commit_id;
54 struct got_commit_object *commit;
55 struct got_commit_graph_node *new_node;
58 struct got_commit_graph {
59 /* The set of all commits we have traversed. */
60 struct got_object_idset *node_ids;
63 #define GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL 0x01
66 * A set of object IDs of known parent commits which we have not yet
67 * traversed. Each commit ID in this set represents a branch in commit
68 * history: Either the first-parent branch of the head node, or another
69 * branch corresponding to a traversed merge commit for which we have
70 * not traversed a branch point commit yet.
72 * Whenever we add a commit with a matching ID to the graph, we remove
73 * its corresponding element from this set, and add new elements for
74 * each of that commit's parent commits which were not traversed yet.
76 * When API users ask us to fetch more commits, we fetch commits from
77 * all currently open branches. This allows API users to process
78 * commits in linear order even though the history contains branches.
80 struct got_object_idset *open_branches;
82 /* Array of branch tips for fetch_commits_from_open_branches(). */
83 struct got_commit_graph_branch_tip *tips;
86 /* Path of tree entry of interest to the API user. */
90 * Nodes which will be passed to the API user next, sorted by
93 struct got_commit_graph_iter_list iter_list;
96 static const struct got_error *
97 detect_changed_path(int *changed, struct got_commit_object *commit,
98 struct got_object_id *commit_id, const char *path,
99 struct got_repository *repo)
101 const struct got_error *err = NULL;
102 struct got_commit_object *pcommit = NULL;
103 struct got_tree_object *tree = NULL, *ptree = NULL;
104 struct got_object_qid *pid;
106 if (got_path_is_root_dir(path)) {
113 pid = STAILQ_FIRST(&commit->parent_ids);
115 struct got_object_id *obj_id;
116 err = got_object_id_by_path(&obj_id, repo, commit, path);
118 if (err->code == GOT_ERR_NO_TREE_ENTRY)
121 *changed = 1; /* The path was created in this commit. */
126 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
130 err = got_object_open_as_commit(&pcommit, repo, &pid->id);
134 err = got_object_open_as_tree(&ptree, repo, pcommit->tree_id);
138 err = got_object_tree_path_changed(changed, tree, ptree, path, repo);
141 got_object_tree_close(tree);
143 got_object_tree_close(ptree);
145 got_object_commit_close(pcommit);
150 add_node_to_iter_list(struct got_commit_graph *graph,
151 struct got_commit_graph_node *node, time_t committer_time)
153 struct got_commit_graph_node *n, *next;
155 node->timestamp = committer_time;
157 n = TAILQ_FIRST(&graph->iter_list);
159 next = TAILQ_NEXT(n, entry);
160 if (next && node->timestamp >= next->timestamp) {
161 TAILQ_INSERT_BEFORE(next, node, entry);
166 TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
169 static const struct got_error *
170 add_node(struct got_commit_graph_node **new_node,
171 struct got_commit_graph *graph, struct got_object_id *commit_id,
172 struct got_repository *repo)
174 const struct got_error *err = NULL;
175 struct got_commit_graph_node *node;
179 node = calloc(1, sizeof(*node));
181 return got_error_from_errno("calloc");
183 memcpy(&node->id, commit_id, sizeof(node->id));
184 err = got_object_idset_add(graph->node_ids, &node->id, NULL);
193 * Ask got-read-pack to traverse first-parent history until a commit is
194 * encountered which modified graph->path, or until the pack file runs
195 * out of relevant commits. This is faster than sending an individual
196 * request for each commit stored in the pack file.
198 static const struct got_error *
199 packed_first_parent_traversal(int *ncommits_traversed,
200 struct got_commit_graph *graph, struct got_object_id *commit_id,
201 struct got_repository *repo)
203 const struct got_error *err = NULL;
204 struct got_object_id_queue traversed_commits;
205 struct got_object_qid *qid;
207 STAILQ_INIT(&traversed_commits);
208 *ncommits_traversed = 0;
210 err = got_traverse_packed_commits(&traversed_commits,
211 commit_id, graph->path, repo);
215 /* Add all traversed commits to the graph... */
216 STAILQ_FOREACH(qid, &traversed_commits, entry) {
217 if (got_object_idset_contains(graph->open_branches, &qid->id))
219 if (got_object_idset_contains(graph->node_ids, &qid->id))
222 (*ncommits_traversed)++;
224 /* ... except the last commit is the new branch tip. */
225 if (STAILQ_NEXT(qid, entry) == NULL) {
226 err = got_object_idset_add(graph->open_branches,
231 err = got_object_idset_add(graph->node_ids, &qid->id, NULL);
236 got_object_id_queue_free(&traversed_commits);
240 static const struct got_error *
241 close_branch(struct got_commit_graph *graph, struct got_object_id *commit_id)
243 const struct got_error *err;
245 err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
246 if (err && err->code != GOT_ERR_NO_OBJ)
251 static const struct got_error *
252 advance_branch(struct got_commit_graph *graph, struct got_object_id *commit_id,
253 struct got_commit_object *commit, struct got_repository *repo)
255 const struct got_error *err;
256 struct got_object_qid *qid;
258 err = close_branch(graph, commit_id);
262 if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
263 qid = STAILQ_FIRST(&commit->parent_ids);
265 got_object_idset_contains(graph->open_branches, &qid->id))
268 * The root directory always changes by definition, and when
269 * logging the root we want to traverse consecutive commits
270 * even if they point at the same tree.
271 * But if we are looking for a specific path then we can avoid
272 * fetching packed commits which did not modify the path and
273 * only fetch their IDs. This speeds up 'got blame'.
275 if (!got_path_is_root_dir(graph->path) &&
276 (commit->flags & GOT_COMMIT_FLAG_PACKED)) {
278 err = packed_first_parent_traversal(&ncommits,
279 graph, &qid->id, repo);
280 if (err || ncommits > 0)
283 return got_object_idset_add(graph->open_branches,
288 * If we are graphing commits for a specific path, skip branches
289 * which do not contribute any content to this path.
291 if (commit->nparents > 1 && !got_path_is_root_dir(graph->path)) {
292 struct got_object_id *merged_id, *prev_id = NULL;
293 int branches_differ = 0;
295 err = got_object_id_by_path(&merged_id, repo, commit,
300 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
301 struct got_object_id *id = NULL;
302 struct got_commit_object *pcommit = NULL;
304 if (got_object_idset_contains(graph->open_branches,
308 err = got_object_open_as_commit(&pcommit, repo,
315 err = got_object_id_by_path(&id, repo, pcommit,
317 got_object_commit_close(pcommit);
320 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
330 if (!branches_differ &&
331 got_object_id_cmp(id, prev_id) != 0)
338 * If a branch has created the merged content we can
339 * skip any other branches.
341 if (got_object_id_cmp(merged_id, id) == 0) {
342 err = got_object_idset_add(graph->open_branches,
356 * If the path's content is the same on all branches,
357 * follow the first parent only.
359 if (!branches_differ) {
360 qid = STAILQ_FIRST(&commit->parent_ids);
363 if (got_object_idset_contains(graph->open_branches,
366 if (got_object_idset_contains(graph->node_ids,
368 return NULL; /* parent already traversed */
369 return got_object_idset_add(graph->open_branches,
374 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
375 if (got_object_idset_contains(graph->open_branches, &qid->id))
377 if (got_object_idset_contains(graph->node_ids, &qid->id))
378 continue; /* parent already traversed */
379 err = got_object_idset_add(graph->open_branches, &qid->id,
388 const struct got_error *
389 got_commit_graph_open(struct got_commit_graph **graph,
390 const char *path, int first_parent_traversal)
392 const struct got_error *err = NULL;
394 *graph = calloc(1, sizeof(**graph));
396 return got_error_from_errno("calloc");
398 TAILQ_INIT(&(*graph)->iter_list);
400 (*graph)->path = strdup(path);
401 if ((*graph)->path == NULL) {
402 err = got_error_from_errno("strdup");
406 (*graph)->node_ids = got_object_idset_alloc();
407 if ((*graph)->node_ids == NULL) {
408 err = got_error_from_errno("got_object_idset_alloc");
412 (*graph)->open_branches = got_object_idset_alloc();
413 if ((*graph)->open_branches == NULL) {
414 err = got_error_from_errno("got_object_idset_alloc");
418 if (first_parent_traversal)
419 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
422 got_commit_graph_close(*graph);
428 struct add_branch_tip_arg {
429 struct got_commit_graph_branch_tip *tips;
431 struct got_repository *repo;
432 struct got_commit_graph *graph;
435 static const struct got_error *
436 add_branch_tip(struct got_object_id *commit_id, void *data, void *arg)
438 const struct got_error *err;
439 struct add_branch_tip_arg *a = arg;
440 struct got_commit_graph_node *new_node;
441 struct got_commit_object *commit;
443 err = got_object_open_as_commit(&commit, a->repo, commit_id);
447 err = add_node(&new_node, a->graph, commit_id, a->repo);
449 got_object_commit_close(commit);
453 a->tips[a->ntips].commit_id = &new_node->id;
454 a->tips[a->ntips].commit = commit;
455 a->tips[a->ntips].new_node = new_node;
461 static const struct got_error *
462 fetch_commits_from_open_branches(struct got_commit_graph *graph,
463 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
465 const struct got_error *err;
466 struct add_branch_tip_arg arg;
469 ntips = got_object_idset_num_elements(graph->open_branches);
473 /* (Re-)allocate branch tips array if necessary. */
474 if (graph->ntips < ntips) {
475 struct got_commit_graph_branch_tip *tips;
476 tips = recallocarray(graph->tips, graph->ntips, ntips,
479 return got_error_from_errno("recallocarray");
481 graph->ntips = ntips;
483 arg.tips = graph->tips;
484 arg.ntips = 0; /* add_branch_tip() will increment */
487 err = got_object_idset_for_each(graph->open_branches, add_branch_tip,
492 for (i = 0; i < arg.ntips; i++) {
493 struct got_object_id *commit_id;
494 struct got_commit_object *commit;
495 struct got_commit_graph_node *new_node;
499 err = (*cancel_cb)(cancel_arg);
504 commit_id = arg.tips[i].commit_id;
505 commit = arg.tips[i].commit;
506 new_node = arg.tips[i].new_node;
508 err = detect_changed_path(&changed, commit, commit_id,
511 if (err->code != GOT_ERR_NO_OBJ)
514 * History of the path stops here on the current
515 * branch. Keep going on other branches.
517 err = close_branch(graph, commit_id);
523 add_node_to_iter_list(graph, new_node,
524 got_object_commit_get_committer_time(commit));
525 arg.tips[i].new_node = NULL;
527 err = advance_branch(graph, commit_id, commit, repo);
532 for (i = 0; i < arg.ntips; i++) {
533 got_object_commit_close(arg.tips[i].commit);
534 free(arg.tips[i].new_node);
540 got_commit_graph_close(struct got_commit_graph *graph)
542 struct got_commit_graph_node *node;
544 while ((node = TAILQ_FIRST(&graph->iter_list))) {
545 TAILQ_REMOVE(&graph->iter_list, node, entry);
549 if (graph->open_branches)
550 got_object_idset_free(graph->open_branches);
552 got_object_idset_free(graph->node_ids);
558 const struct got_error *
559 got_commit_graph_iter_start(struct got_commit_graph *graph,
560 struct got_object_id *id, struct got_repository *repo,
561 got_cancel_cb cancel_cb, void *cancel_arg)
563 const struct got_error *err = NULL;
565 if (!TAILQ_EMPTY(&graph->iter_list))
566 return got_error(GOT_ERR_ITER_BUSY);
568 err = got_object_idset_add(graph->open_branches, id, NULL);
572 /* Locate first commit which changed graph->path. */
573 while (TAILQ_EMPTY(&graph->iter_list) &&
574 got_object_idset_num_elements(graph->open_branches) > 0) {
575 err = fetch_commits_from_open_branches(graph, repo,
576 cancel_cb, cancel_arg);
581 if (TAILQ_EMPTY(&graph->iter_list)) {
583 if (got_path_is_root_dir(graph->path))
584 return got_error_no_obj(id);
586 while (path[0] == '/')
588 return got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
594 const struct got_error *
595 got_commit_graph_iter_next(struct got_object_id *id,
596 struct got_commit_graph *graph, struct got_repository *repo,
597 got_cancel_cb cancel_cb, void *cancel_arg)
599 const struct got_error *err = NULL;
600 struct got_commit_graph_node *node;
602 node = TAILQ_FIRST(&graph->iter_list);
604 /* We are done iterating, or iteration was not started. */
605 return got_error(GOT_ERR_ITER_COMPLETED);
608 while (TAILQ_NEXT(node, entry) == NULL &&
609 got_object_idset_num_elements(graph->open_branches) > 0) {
610 err = fetch_commits_from_open_branches(graph, repo,
611 cancel_cb, cancel_arg);
616 memcpy(id, &node->id, sizeof(*id));
618 TAILQ_REMOVE(&graph->iter_list, node, entry);
623 static const struct got_error *
624 find_yca_add_id(struct got_object_id **yca_id, struct got_commit_graph *graph,
625 struct got_object_idset *commit_ids, struct got_repository *repo,
626 got_cancel_cb cancel_cb, void *cancel_arg)
628 const struct got_error *err = NULL;
629 struct got_object_id id;
631 err = got_commit_graph_iter_next(&id, graph, repo, cancel_cb,
636 if (got_object_idset_contains(commit_ids, &id)) {
637 *yca_id = got_object_id_dup(&id);
639 err = got_error_from_errno("got_object_id_dup");
643 return got_object_idset_add(commit_ids, &id, NULL);
646 const struct got_error *
647 got_commit_graph_find_youngest_common_ancestor(struct got_object_id **yca_id,
648 struct got_object_id *commit_id, struct got_object_id *commit_id2,
649 int first_parent_traversal,
650 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
652 const struct got_error *err = NULL;
653 struct got_commit_graph *graph = NULL, *graph2 = NULL;
654 int completed = 0, completed2 = 0;
655 struct got_object_idset *commit_ids;
659 commit_ids = got_object_idset_alloc();
660 if (commit_ids == NULL)
661 return got_error_from_errno("got_object_idset_alloc");
663 err = got_commit_graph_open(&graph, "/", first_parent_traversal);
667 err = got_commit_graph_open(&graph2, "/", first_parent_traversal);
671 err = got_commit_graph_iter_start(graph, commit_id, repo,
672 cancel_cb, cancel_arg);
676 err = got_commit_graph_iter_start(graph2, commit_id2, repo,
677 cancel_cb, cancel_arg);
683 err = (*cancel_cb)(cancel_arg);
689 err = find_yca_add_id(yca_id, graph, commit_ids, repo,
690 cancel_cb, cancel_arg);
692 if (err->code != GOT_ERR_ITER_COMPLETED)
702 err = find_yca_add_id(yca_id, graph2, commit_ids, repo,
703 cancel_cb, cancel_arg);
705 if (err->code != GOT_ERR_ITER_COMPLETED)
714 if (completed && completed2) {
715 err = got_error(GOT_ERR_ANCESTRY);
720 got_object_idset_free(commit_ids);
722 got_commit_graph_close(graph);
724 got_commit_graph_close(graph2);