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>
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;
45 /* Used during graph 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. */
88 /* The next commit to return when the API user asks for one. */
89 struct got_commit_graph_node *iter_node;
91 /* The graph iteration list contains all nodes in sorted order. */
92 struct got_commit_graph_iter_list iter_list;
95 static const struct got_error *
96 detect_changed_path(int *changed, struct got_commit_object *commit,
97 struct got_object_id *commit_id, const char *path,
98 struct got_repository *repo)
100 const struct got_error *err = NULL;
101 struct got_commit_object *pcommit = NULL;
102 struct got_tree_object *tree = NULL, *ptree = NULL;
103 struct got_object_qid *pid;
105 if (got_path_is_root_dir(path)) {
112 pid = SIMPLEQ_FIRST(&commit->parent_ids);
114 struct got_object_id *obj_id;
115 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
117 if (err->code == GOT_ERR_NO_TREE_ENTRY)
120 *changed = 1; /* The path was created in this commit. */
125 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
129 err = got_object_open_as_commit(&pcommit, repo, pid->id);
133 err = got_object_open_as_tree(&ptree, repo, pcommit->tree_id);
137 err = got_object_tree_path_changed(changed, tree, ptree, path, repo);
140 got_object_tree_close(tree);
142 got_object_tree_close(ptree);
144 got_object_commit_close(pcommit);
149 add_node_to_iter_list(struct got_commit_graph *graph,
150 struct got_commit_graph_node *node)
152 struct got_commit_graph_node *n, *next;
154 if (TAILQ_EMPTY(&graph->iter_list)) {
155 TAILQ_INSERT_HEAD(&graph->iter_list, node, entry);
156 graph->iter_node = node;
160 n = graph->iter_node;
161 /* Ensure that an iteration in progress will see this new commit. */
163 next = TAILQ_NEXT(n, entry);
164 if (next && node->timestamp >= next->timestamp) {
165 TAILQ_INSERT_BEFORE(next, node, entry);
170 TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
173 static const struct got_error *
174 close_branch(struct got_commit_graph *graph, struct got_object_id *commit_id)
176 const struct got_error *err;
178 err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
179 if (err && err->code != GOT_ERR_NO_OBJ)
184 static const struct got_error *
185 advance_branch(struct got_commit_graph *graph,
186 struct got_commit_graph_node *node,
187 struct got_object_id *commit_id, struct got_commit_object *commit,
188 struct got_repository *repo)
190 const struct got_error *err;
191 struct got_object_qid *qid;
193 err = close_branch(graph, commit_id);
197 if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
198 qid = SIMPLEQ_FIRST(&commit->parent_ids);
200 got_object_idset_get(graph->open_branches, qid->id))
202 return got_object_idset_add(graph->open_branches,
207 * If we are graphing commits for a specific path, skip branches
208 * which do not contribute any content to this path.
210 if (commit->nparents > 1 && !got_path_is_root_dir(graph->path)) {
211 struct got_object_id *merged_id, *prev_id = NULL;
212 int branches_differ = 0;
214 err = got_object_id_by_path(&merged_id, repo, commit_id,
219 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
220 struct got_object_id *id;
222 if (got_object_idset_get(graph->open_branches, qid->id))
225 err = got_object_id_by_path(&id, repo, qid->id,
228 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
238 if (!branches_differ &&
239 got_object_id_cmp(id, prev_id) != 0)
246 * If a branch has created the merged content we can
247 * skip any other branches.
249 if (got_object_id_cmp(merged_id, id) == 0) {
250 err = got_object_idset_add(graph->open_branches,
264 * If the path's content is the same on all branches,
265 * follow the first parent only.
267 if (!branches_differ) {
268 qid = SIMPLEQ_FIRST(&commit->parent_ids);
271 if (got_object_idset_get(graph->open_branches, qid->id))
273 if (got_object_idset_get(graph->node_ids, qid->id))
274 return NULL; /* parent already traversed */
275 return got_object_idset_add(graph->open_branches,
280 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
281 if (got_object_idset_get(graph->open_branches, qid->id))
283 if (got_object_idset_get(graph->node_ids, qid->id))
284 continue; /* parent already traversed */
285 err = got_object_idset_add(graph->open_branches, qid->id, node);
293 static const struct got_error *
294 add_node(struct got_commit_graph_node **new_node,
295 struct got_commit_graph *graph,
296 struct got_object_id *commit_id,
297 struct got_commit_object *commit,
298 struct got_repository *repo)
300 const struct got_error *err = NULL;
301 struct got_commit_graph_node *node;
305 node = calloc(1, sizeof(*node));
307 return got_error_from_errno("calloc");
309 memcpy(&node->id, commit_id, sizeof(node->id));
310 node->timestamp = commit->committer_time;
312 err = got_object_idset_add(graph->node_ids, &node->id, node);
320 const struct got_error *
321 got_commit_graph_open(struct got_commit_graph **graph,
322 const char *path, int first_parent_traversal)
324 const struct got_error *err = NULL;
326 *graph = calloc(1, sizeof(**graph));
328 return got_error_from_errno("calloc");
330 TAILQ_INIT(&(*graph)->iter_list);
332 (*graph)->path = strdup(path);
333 if ((*graph)->path == NULL) {
334 err = got_error_from_errno("strdup");
338 (*graph)->node_ids = got_object_idset_alloc();
339 if ((*graph)->node_ids == NULL) {
340 err = got_error_from_errno("got_object_idset_alloc");
344 (*graph)->open_branches = got_object_idset_alloc();
345 if ((*graph)->open_branches == NULL) {
346 err = got_error_from_errno("got_object_idset_alloc");
350 if (first_parent_traversal)
351 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
354 got_commit_graph_close(*graph);
360 struct add_branch_tip_arg {
361 struct got_commit_graph_branch_tip *tips;
363 struct got_repository *repo;
364 struct got_commit_graph *graph;
367 static const struct got_error *
368 add_branch_tip(struct got_object_id *commit_id, void *data, void *arg)
370 const struct got_error *err;
371 struct add_branch_tip_arg *a = arg;
372 struct got_commit_graph_node *new_node;
373 struct got_commit_object *commit;
375 err = got_object_open_as_commit(&commit, a->repo, commit_id);
379 err = add_node(&new_node, a->graph, commit_id, commit, a->repo);
383 a->tips[a->ntips].commit_id = new_node ? &new_node->id : NULL;
384 a->tips[a->ntips].commit = commit;
385 a->tips[a->ntips].new_node = new_node;
391 static const struct got_error *
392 fetch_commits_from_open_branches(int *nfetched,
393 struct got_object_id **changed_id, struct got_commit_graph *graph,
394 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
396 const struct got_error *err;
397 struct add_branch_tip_arg arg;
404 ntips = got_object_idset_num_elements(graph->open_branches);
408 /* (Re-)allocate branch tips array if necessary. */
409 if (graph->ntips < ntips) {
410 struct got_commit_graph_branch_tip *tips;
411 tips = recallocarray(graph->tips, graph->ntips, ntips,
414 return got_error_from_errno("recallocarray");
416 graph->ntips = ntips;
418 arg.tips = graph->tips;
419 arg.ntips = 0; /* add_branch_tip() will increment */
422 err = got_object_idset_for_each(graph->open_branches, add_branch_tip,
427 for (i = 0; i < arg.ntips; i++) {
428 struct got_object_id *commit_id;
429 struct got_commit_object *commit;
430 struct got_commit_graph_node *new_node;
434 err = (*cancel_cb)(cancel_arg);
439 commit_id = arg.tips[i].commit_id;
440 commit = arg.tips[i].commit;
441 new_node = arg.tips[i].new_node;
443 err = detect_changed_path(&changed, commit, commit_id,
446 if (err->code != GOT_ERR_NO_OBJ)
449 * History of the path stops here on the current
450 * branch. Keep going on other branches.
452 err = close_branch(graph, commit_id);
458 add_node_to_iter_list(graph, new_node);
459 if (changed_id && *changed_id == NULL)
460 *changed_id = commit_id;
462 err = advance_branch(graph, new_node, commit_id,
468 for (i = 0; i < arg.ntips; i++)
469 got_object_commit_close(arg.tips[i].commit);
470 (*nfetched) = arg.ntips;
474 static const struct got_error *
475 free_node_iter(struct got_object_id *id, void *data, void *arg)
477 struct got_commit_graph_node *node = data;
483 got_commit_graph_close(struct got_commit_graph *graph)
485 if (graph->open_branches)
486 got_object_idset_free(graph->open_branches);
487 got_object_idset_for_each(graph->node_ids, free_node_iter, NULL);
489 got_object_idset_free(graph->node_ids);
495 const struct got_error *
496 got_commit_graph_iter_start(struct got_commit_graph *graph,
497 struct got_object_id *id, struct got_repository *repo,
498 got_cancel_cb cancel_cb, void *cancel_arg)
500 const struct got_error *err = NULL;
501 struct got_commit_graph_node *start_node;
502 struct got_commit_object *commit;
505 if (!TAILQ_EMPTY(&graph->iter_list))
506 return got_error(GOT_ERR_ITER_BUSY);
508 err = got_object_open_as_commit(&commit, repo, id);
512 err = add_node(&start_node, graph, id, commit, repo);
516 err = detect_changed_path(&changed, commit, id, graph->path, repo);
520 add_node_to_iter_list(graph, start_node);
522 err = advance_branch(graph, start_node, id, commit, repo);
527 /* Locate first commit which changed graph->path. */
528 struct got_object_id *changed_id = NULL;
529 while (changed_id == NULL) {
531 err = fetch_commits_from_open_branches(&ncommits,
532 &changed_id, graph, repo, cancel_cb, cancel_arg);
534 got_object_commit_close(commit);
538 start_node = got_object_idset_get(graph->node_ids, changed_id);
541 got_object_commit_close(commit);
543 graph->iter_node = start_node;
547 const struct got_error *
548 got_commit_graph_iter_next(struct got_object_id **id,
549 struct got_commit_graph *graph, struct got_repository *repo,
550 got_cancel_cb cancel_cb, void *cancel_arg)
552 const struct got_error *err = NULL;
556 if (graph->iter_node == NULL) {
557 /* We are done iterating, or iteration was not started. */
558 return got_error(GOT_ERR_ITER_COMPLETED);
561 if (graph->iter_node ==
562 TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
563 got_object_idset_num_elements(graph->open_branches) == 0) {
564 /* We are done iterating. */
565 *id = &graph->iter_node->id;
566 graph->iter_node = NULL;
570 while (TAILQ_NEXT(graph->iter_node, entry) == NULL &&
571 got_object_idset_num_elements(graph->open_branches) > 0) {
573 struct got_object_id *changed_id;
574 err = fetch_commits_from_open_branches(&ncommits,
575 &changed_id, graph, repo, cancel_cb, cancel_arg);
580 *id = &graph->iter_node->id;
581 graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);
585 const struct got_error *
586 got_commit_graph_find_youngest_common_ancestor(struct got_object_id **yca_id,
587 struct got_object_id *commit_id, struct got_object_id *commit_id2,
588 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
590 const struct got_error *err = NULL;
591 struct got_commit_graph *graph = NULL, *graph2 = NULL;
592 int completed = 0, completed2 = 0;
593 struct got_object_idset *commit_ids;
597 commit_ids = got_object_idset_alloc();
598 if (commit_ids == NULL)
599 return got_error_from_errno("got_object_idset_alloc");
601 err = got_commit_graph_open(&graph, "/", 1);
605 err = got_commit_graph_open(&graph2, "/", 1);
609 err = got_commit_graph_iter_start(graph, commit_id, repo,
610 cancel_cb, cancel_arg);
614 err = got_commit_graph_iter_start(graph2, commit_id2, repo,
615 cancel_cb, cancel_arg);
620 struct got_object_id *id = NULL, *id2 = NULL;
623 err = (*cancel_cb)(cancel_arg);
629 err = got_commit_graph_iter_next(&id, graph, repo,
630 cancel_cb, cancel_arg);
632 if (err->code != GOT_ERR_ITER_COMPLETED)
640 err = got_commit_graph_iter_next(&id2, graph2, repo,
641 cancel_cb, cancel_arg);
643 if (err->code != GOT_ERR_ITER_COMPLETED)
651 if (got_object_idset_get(commit_ids, id)) {
652 *yca_id = got_object_id_dup(id);
655 err = got_error_from_errno("got_object_id_dup");
659 err = got_object_idset_add(commit_ids, id, id);
664 if (got_object_idset_get(commit_ids, id2)) {
665 *yca_id = got_object_id_dup(id2);
668 err = got_error_from_errno("got_object_id_dup");
672 err = got_object_idset_add(commit_ids, id2, id2);
677 if (completed && completed2) {
678 err = got_error(GOT_ERR_ANCESTRY);
684 got_object_idset_free(commit_ids);
686 got_commit_graph_close(graph);
688 got_commit_graph_close(graph2);