Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/queue.h>
21 #include <limits.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <zlib.h>
26 #include <ctype.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"
34 #include "got_path.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. */
45 time_t timestamp;
46 TAILQ_ENTRY(got_commit_graph_node) entry;
47 };
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;
55 };
57 struct got_commit_graph {
58 /* The set of all commits we have traversed. */
59 struct got_object_idset *node_ids;
61 int flags;
62 #define GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL 0x01
64 /*
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.
70 *
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.
74 *
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.
78 */
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;
83 int ntips;
85 /* Path of tree entry of interest to the API user. */
86 char *path;
88 /*
89 * Nodes which will be passed to the API user next, sorted by
90 * commit timestmap.
91 */
92 struct got_commit_graph_iter_list iter_list;
93 };
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)
99 {
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)) {
106 *changed = 1;
107 return NULL;
110 *changed = 0;
112 pid = STAILQ_FIRST(&commit->parent_ids);
113 if (pid == NULL) {
114 struct got_object_id *obj_id;
115 err = got_object_id_by_path(&obj_id, repo, commit, path);
116 if (err) {
117 if (err->code == GOT_ERR_NO_TREE_ENTRY)
118 err = NULL;
119 } else
120 *changed = 1; /* The path was created in this commit. */
121 free(obj_id);
122 return err;
125 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
126 if (err)
127 return err;
129 err = got_object_open_as_commit(&pcommit, repo, &pid->id);
130 if (err)
131 goto done;
133 err = got_object_open_as_tree(&ptree, repo, pcommit->tree_id);
134 if (err)
135 goto done;
137 err = got_object_tree_path_changed(changed, tree, ptree, path, repo);
138 done:
139 if (tree)
140 got_object_tree_close(tree);
141 if (ptree)
142 got_object_tree_close(ptree);
143 if (pcommit)
144 got_object_commit_close(pcommit);
145 return err;
148 static void
149 add_node_to_iter_list(struct got_commit_graph *graph,
150 struct got_commit_graph_node *node, time_t committer_time)
152 struct got_commit_graph_node *n, *next;
154 node->timestamp = committer_time;
156 n = TAILQ_FIRST(&graph->iter_list);
157 while (n) {
158 next = TAILQ_NEXT(n, entry);
159 if (next && node->timestamp >= next->timestamp) {
160 TAILQ_INSERT_BEFORE(next, node, entry);
161 return;
163 n = next;
165 TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
168 static const struct got_error *
169 add_node(struct got_commit_graph_node **new_node,
170 struct got_commit_graph *graph, struct got_object_id *commit_id,
171 struct got_repository *repo)
173 const struct got_error *err = NULL;
174 struct got_commit_graph_node *node;
176 *new_node = NULL;
178 node = calloc(1, sizeof(*node));
179 if (node == NULL)
180 return got_error_from_errno("calloc");
182 memcpy(&node->id, commit_id, sizeof(node->id));
183 err = got_object_idset_add(graph->node_ids, &node->id, NULL);
184 if (err)
185 free(node);
186 else
187 *new_node = node;
188 return err;
191 /*
192 * Ask got-read-pack to traverse first-parent history until a commit is
193 * encountered which modified graph->path, or until the pack file runs
194 * out of relevant commits. This is faster than sending an individual
195 * request for each commit stored in the pack file.
196 */
197 static const struct got_error *
198 packed_first_parent_traversal(int *ncommits_traversed,
199 struct got_commit_graph *graph, struct got_object_id *commit_id,
200 struct got_repository *repo)
202 const struct got_error *err = NULL;
203 struct got_object_id_queue traversed_commits;
204 struct got_object_qid *qid;
206 STAILQ_INIT(&traversed_commits);
207 *ncommits_traversed = 0;
209 err = got_traverse_packed_commits(&traversed_commits,
210 commit_id, graph->path, repo);
211 if (err)
212 return err;
214 /* Add all traversed commits to the graph... */
215 STAILQ_FOREACH(qid, &traversed_commits, entry) {
216 if (got_object_idset_contains(graph->open_branches, &qid->id))
217 continue;
218 if (got_object_idset_contains(graph->node_ids, &qid->id))
219 continue;
221 (*ncommits_traversed)++;
223 /* ... except the last commit is the new branch tip. */
224 if (STAILQ_NEXT(qid, entry) == NULL) {
225 err = got_object_idset_add(graph->open_branches,
226 &qid->id, NULL);
227 break;
230 err = got_object_idset_add(graph->node_ids, &qid->id, NULL);
231 if (err)
232 break;
235 got_object_id_queue_free(&traversed_commits);
236 return err;
239 static const struct got_error *
240 close_branch(struct got_commit_graph *graph, struct got_object_id *commit_id)
242 const struct got_error *err;
244 err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
245 if (err && err->code != GOT_ERR_NO_OBJ)
246 return err;
247 return NULL;
250 static const struct got_error *
251 advance_branch(struct got_commit_graph *graph, struct got_object_id *commit_id,
252 struct got_commit_object *commit, struct got_repository *repo)
254 const struct got_error *err;
255 struct got_object_qid *qid;
256 struct got_object_id *merged_id = NULL;
258 err = close_branch(graph, commit_id);
259 if (err)
260 return err;
262 if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
263 qid = STAILQ_FIRST(&commit->parent_ids);
264 if (qid == NULL ||
265 got_object_idset_contains(graph->open_branches, &qid->id))
266 return NULL;
267 /*
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'.
274 */
275 if (!got_path_is_root_dir(graph->path) &&
276 (commit->flags & GOT_COMMIT_FLAG_PACKED)) {
277 int ncommits = 0;
278 err = packed_first_parent_traversal(&ncommits,
279 graph, &qid->id, repo);
280 if (err || ncommits > 0)
281 return err;
283 return got_object_idset_add(graph->open_branches,
284 &qid->id, NULL);
287 /*
288 * If we are graphing commits for a specific path, skip branches
289 * which do not contribute any content to this path.
290 */
291 if (commit->nparents > 1 && !got_path_is_root_dir(graph->path)) {
292 err = got_object_id_by_path(&merged_id, repo, commit, graph->path);
293 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
294 return err;
295 /* The requested path does not exist in this merge commit. */
297 if (commit->nparents > 1 && !got_path_is_root_dir(graph->path) &&
298 merged_id != NULL) {
299 struct got_object_id *prev_id = NULL;
300 int branches_differ = 0;
303 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
304 struct got_object_id *id = NULL;
305 struct got_commit_object *pcommit = NULL;
307 if (got_object_idset_contains(graph->open_branches,
308 &qid->id))
309 continue;
311 err = got_object_open_as_commit(&pcommit, repo,
312 &qid->id);
313 if (err) {
314 free(merged_id);
315 free(prev_id);
316 return err;
318 err = got_object_id_by_path(&id, repo, pcommit,
319 graph->path);
320 got_object_commit_close(pcommit);
321 pcommit = NULL;
322 if (err) {
323 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
324 branches_differ = 1;
325 continue;
327 free(merged_id);
328 free(prev_id);
329 return err;
332 if (prev_id) {
333 if (!branches_differ &&
334 got_object_id_cmp(id, prev_id) != 0)
335 branches_differ = 1;
336 free(prev_id);
338 prev_id = id;
340 /*
341 * If a branch has created the merged content we can
342 * skip any other branches.
343 */
344 if (got_object_id_cmp(merged_id, id) == 0) {
345 err = got_object_idset_add(graph->open_branches,
346 &qid->id, NULL);
347 free(merged_id);
348 free(id);
349 return err;
353 free(prev_id);
354 prev_id = NULL;
355 free(merged_id);
356 merged_id = NULL;
358 /*
359 * If the path's content is the same on all branches,
360 * follow the first parent only.
361 */
362 if (!branches_differ) {
363 qid = STAILQ_FIRST(&commit->parent_ids);
364 if (qid == NULL)
365 return NULL;
366 if (got_object_idset_contains(graph->open_branches,
367 &qid->id))
368 return NULL;
369 if (got_object_idset_contains(graph->node_ids,
370 &qid->id))
371 return NULL; /* parent already traversed */
372 return got_object_idset_add(graph->open_branches,
373 &qid->id, NULL);
377 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
378 if (got_object_idset_contains(graph->open_branches, &qid->id))
379 continue;
380 if (got_object_idset_contains(graph->node_ids, &qid->id))
381 continue; /* parent already traversed */
382 err = got_object_idset_add(graph->open_branches, &qid->id,
383 NULL);
384 if (err)
385 return err;
388 return NULL;
391 const struct got_error *
392 got_commit_graph_open(struct got_commit_graph **graph,
393 const char *path, int first_parent_traversal)
395 const struct got_error *err = NULL;
397 *graph = calloc(1, sizeof(**graph));
398 if (*graph == NULL)
399 return got_error_from_errno("calloc");
401 TAILQ_INIT(&(*graph)->iter_list);
403 (*graph)->path = strdup(path);
404 if ((*graph)->path == NULL) {
405 err = got_error_from_errno("strdup");
406 goto done;
409 (*graph)->node_ids = got_object_idset_alloc();
410 if ((*graph)->node_ids == NULL) {
411 err = got_error_from_errno("got_object_idset_alloc");
412 goto done;
415 (*graph)->open_branches = got_object_idset_alloc();
416 if ((*graph)->open_branches == NULL) {
417 err = got_error_from_errno("got_object_idset_alloc");
418 goto done;
421 if (first_parent_traversal)
422 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
423 done:
424 if (err) {
425 got_commit_graph_close(*graph);
426 *graph = NULL;
428 return err;
431 struct add_branch_tip_arg {
432 struct got_commit_graph_branch_tip *tips;
433 int ntips;
434 struct got_repository *repo;
435 struct got_commit_graph *graph;
436 };
438 static const struct got_error *
439 add_branch_tip(struct got_object_id *commit_id, void *data, void *arg)
441 const struct got_error *err;
442 struct add_branch_tip_arg *a = arg;
443 struct got_commit_graph_node *new_node;
444 struct got_commit_object *commit;
446 err = got_object_open_as_commit(&commit, a->repo, commit_id);
447 if (err)
448 return err;
450 err = add_node(&new_node, a->graph, commit_id, a->repo);
451 if (err) {
452 got_object_commit_close(commit);
453 return err;
456 a->tips[a->ntips].commit_id = &new_node->id;
457 a->tips[a->ntips].commit = commit;
458 a->tips[a->ntips].new_node = new_node;
459 a->ntips++;
461 return NULL;
464 static const struct got_error *
465 fetch_commits_from_open_branches(struct got_commit_graph *graph,
466 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
468 const struct got_error *err;
469 struct add_branch_tip_arg arg;
470 int i, ntips;
472 ntips = got_object_idset_num_elements(graph->open_branches);
473 if (ntips == 0)
474 return NULL;
476 /* (Re-)allocate branch tips array if necessary. */
477 if (graph->ntips < ntips) {
478 struct got_commit_graph_branch_tip *tips;
479 tips = recallocarray(graph->tips, graph->ntips, ntips,
480 sizeof(*tips));
481 if (tips == NULL)
482 return got_error_from_errno("recallocarray");
483 graph->tips = tips;
484 graph->ntips = ntips;
486 arg.tips = graph->tips;
487 arg.ntips = 0; /* add_branch_tip() will increment */
488 arg.repo = repo;
489 arg.graph = graph;
490 err = got_object_idset_for_each(graph->open_branches, add_branch_tip,
491 &arg);
492 if (err)
493 goto done;
495 for (i = 0; i < arg.ntips; i++) {
496 struct got_object_id *commit_id;
497 struct got_commit_object *commit;
498 struct got_commit_graph_node *new_node;
499 int changed;
501 if (cancel_cb) {
502 err = (*cancel_cb)(cancel_arg);
503 if (err)
504 break;
507 commit_id = arg.tips[i].commit_id;
508 commit = arg.tips[i].commit;
509 new_node = arg.tips[i].new_node;
511 err = detect_changed_path(&changed, commit, commit_id,
512 graph->path, repo);
513 if (err) {
514 if (err->code != GOT_ERR_NO_OBJ)
515 break;
516 /*
517 * History of the path stops here on the current
518 * branch. Keep going on other branches.
519 */
520 err = close_branch(graph, commit_id);
521 if (err)
522 break;
523 continue;
525 if (changed) {
526 add_node_to_iter_list(graph, new_node,
527 got_object_commit_get_committer_time(commit));
528 arg.tips[i].new_node = NULL;
530 err = advance_branch(graph, commit_id, commit, repo);
531 if (err)
532 break;
534 done:
535 for (i = 0; i < arg.ntips; i++) {
536 got_object_commit_close(arg.tips[i].commit);
537 free(arg.tips[i].new_node);
539 return err;
542 void
543 got_commit_graph_close(struct got_commit_graph *graph)
545 struct got_commit_graph_node *node;
547 while ((node = TAILQ_FIRST(&graph->iter_list))) {
548 TAILQ_REMOVE(&graph->iter_list, node, entry);
549 free(node);
552 if (graph->open_branches)
553 got_object_idset_free(graph->open_branches);
554 if (graph->node_ids)
555 got_object_idset_free(graph->node_ids);
556 free(graph->tips);
557 free(graph->path);
558 free(graph);
561 const struct got_error *
562 got_commit_graph_iter_start(struct got_commit_graph *graph,
563 struct got_object_id *id, struct got_repository *repo,
564 got_cancel_cb cancel_cb, void *cancel_arg)
566 const struct got_error *err = NULL;
568 if (!TAILQ_EMPTY(&graph->iter_list))
569 return got_error(GOT_ERR_ITER_BUSY);
571 err = got_object_idset_add(graph->open_branches, id, NULL);
572 if (err)
573 return err;
575 /* Locate first commit which changed graph->path. */
576 while (TAILQ_EMPTY(&graph->iter_list) &&
577 got_object_idset_num_elements(graph->open_branches) > 0) {
578 err = fetch_commits_from_open_branches(graph, repo,
579 cancel_cb, cancel_arg);
580 if (err)
581 return err;
584 if (TAILQ_EMPTY(&graph->iter_list)) {
585 const char *path;
586 if (got_path_is_root_dir(graph->path))
587 return got_error_no_obj(id);
588 path = graph->path;
589 while (path[0] == '/')
590 path++;
591 return got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
594 return NULL;
597 const struct got_error *
598 got_commit_graph_iter_next(struct got_object_id *id,
599 struct got_commit_graph *graph, struct got_repository *repo,
600 got_cancel_cb cancel_cb, void *cancel_arg)
602 const struct got_error *err = NULL;
603 struct got_commit_graph_node *node;
605 node = TAILQ_FIRST(&graph->iter_list);
606 if (node == NULL) {
607 /* We are done iterating, or iteration was not started. */
608 return got_error(GOT_ERR_ITER_COMPLETED);
611 while (TAILQ_NEXT(node, entry) == NULL &&
612 got_object_idset_num_elements(graph->open_branches) > 0) {
613 err = fetch_commits_from_open_branches(graph, repo,
614 cancel_cb, cancel_arg);
615 if (err)
616 return err;
619 memcpy(id, &node->id, sizeof(*id));
621 TAILQ_REMOVE(&graph->iter_list, node, entry);
622 free(node);
623 return NULL;
626 static const struct got_error *
627 find_yca_add_id(struct got_object_id **yca_id, struct got_commit_graph *graph,
628 struct got_object_idset *commit_ids, struct got_repository *repo,
629 got_cancel_cb cancel_cb, void *cancel_arg)
631 const struct got_error *err = NULL;
632 struct got_object_id id;
634 err = got_commit_graph_iter_next(&id, graph, repo, cancel_cb,
635 cancel_arg);
636 if (err)
637 return err;
639 if (got_object_idset_contains(commit_ids, &id)) {
640 *yca_id = got_object_id_dup(&id);
641 if (*yca_id == NULL)
642 err = got_error_from_errno("got_object_id_dup");
643 return err;
646 return got_object_idset_add(commit_ids, &id, NULL);
649 /*
650 * Sets *yca_id to the youngest common ancestor of commit_id and
651 * commit_id2. Returns got_error(GOT_ERR_ANCESTRY) if they have no
652 * common ancestors.
654 * If first_parent_traversal is nonzero, only linear history is considered.
655 */
656 const struct got_error *
657 got_commit_graph_find_youngest_common_ancestor(struct got_object_id **yca_id,
658 struct got_object_id *commit_id, struct got_object_id *commit_id2,
659 int first_parent_traversal,
660 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
662 const struct got_error *err = NULL;
663 struct got_commit_graph *graph = NULL, *graph2 = NULL;
664 int completed = 0, completed2 = 0;
665 struct got_object_idset *commit_ids;
667 *yca_id = NULL;
669 commit_ids = got_object_idset_alloc();
670 if (commit_ids == NULL)
671 return got_error_from_errno("got_object_idset_alloc");
673 err = got_commit_graph_open(&graph, "/", first_parent_traversal);
674 if (err)
675 goto done;
677 err = got_commit_graph_open(&graph2, "/", first_parent_traversal);
678 if (err)
679 goto done;
681 err = got_commit_graph_iter_start(graph, commit_id, repo,
682 cancel_cb, cancel_arg);
683 if (err)
684 goto done;
686 err = got_commit_graph_iter_start(graph2, commit_id2, repo,
687 cancel_cb, cancel_arg);
688 if (err)
689 goto done;
691 for (;;) {
692 if (cancel_cb) {
693 err = (*cancel_cb)(cancel_arg);
694 if (err)
695 break;
698 if (!completed) {
699 err = find_yca_add_id(yca_id, graph, commit_ids, repo,
700 cancel_cb, cancel_arg);
701 if (err) {
702 if (err->code != GOT_ERR_ITER_COMPLETED)
703 break;
704 err = NULL;
705 completed = 1;
707 if (*yca_id)
708 break;
711 if (!completed2) {
712 err = find_yca_add_id(yca_id, graph2, commit_ids, repo,
713 cancel_cb, cancel_arg);
714 if (err) {
715 if (err->code != GOT_ERR_ITER_COMPLETED)
716 break;
717 err = NULL;
718 completed2 = 1;
720 if (*yca_id)
721 break;
724 if (completed && completed2) {
725 err = got_error(GOT_ERR_ANCESTRY);
726 break;
729 done:
730 got_object_idset_free(commit_ids);
731 if (graph)
732 got_commit_graph_close(graph);
733 if (graph2)
734 got_commit_graph_close(graph2);
735 return err;