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>
20 #include <sys/stdint.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sha1.h>
27 #include <sha2.h>
28 #include <zlib.h>
29 #include <ctype.h>
31 #include "got_error.h"
32 #include "got_object.h"
33 #include "got_cancel.h"
34 #include "got_commit_graph.h"
35 #include "got_path.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. */
46 time_t timestamp;
47 TAILQ_ENTRY(got_commit_graph_node) entry;
48 };
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;
56 };
58 struct got_commit_graph {
59 /* The set of all commits we have traversed. */
60 struct got_object_idset *node_ids;
62 int flags;
63 #define GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL 0x01
65 /*
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.
71 *
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.
75 *
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.
79 */
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;
84 int ntips;
86 /* Path of tree entry of interest to the API user. */
87 char *path;
89 /*
90 * Nodes which will be passed to the API user next, sorted by
91 * commit timestmap.
92 */
93 struct got_commit_graph_iter_list iter_list;
94 };
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)) {
107 *changed = 1;
108 return NULL;
111 *changed = 0;
113 pid = STAILQ_FIRST(&commit->parent_ids);
114 if (pid == NULL) {
115 struct got_object_id *obj_id;
116 err = got_object_id_by_path(&obj_id, repo, commit, path);
117 if (err) {
118 if (err->code == GOT_ERR_NO_TREE_ENTRY)
119 err = NULL;
120 } else
121 *changed = 1; /* The path was created in this commit. */
122 free(obj_id);
123 return err;
126 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
127 if (err)
128 return err;
130 err = got_object_open_as_commit(&pcommit, repo, &pid->id);
131 if (err)
132 goto done;
134 err = got_object_open_as_tree(&ptree, repo, pcommit->tree_id);
135 if (err)
136 goto done;
138 err = got_object_tree_path_changed(changed, tree, ptree, path, repo);
139 done:
140 if (tree)
141 got_object_tree_close(tree);
142 if (ptree)
143 got_object_tree_close(ptree);
144 if (pcommit)
145 got_object_commit_close(pcommit);
146 return err;
149 static void
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);
158 while (n) {
159 next = TAILQ_NEXT(n, entry);
160 if (next && node->timestamp >= next->timestamp) {
161 TAILQ_INSERT_BEFORE(next, node, entry);
162 return;
164 n = next;
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;
177 *new_node = NULL;
179 node = calloc(1, sizeof(*node));
180 if (node == NULL)
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);
185 if (err)
186 free(node);
187 else
188 *new_node = node;
189 return err;
192 /*
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.
197 */
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);
212 if (err)
213 return err;
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))
218 continue;
219 if (got_object_idset_contains(graph->node_ids, &qid->id))
220 continue;
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,
227 &qid->id, NULL);
228 break;
231 err = got_object_idset_add(graph->node_ids, &qid->id, NULL);
232 if (err)
233 break;
236 got_object_id_queue_free(&traversed_commits);
237 return err;
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)
247 return err;
248 return NULL;
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;
257 struct got_object_id *merged_id = NULL;
259 err = close_branch(graph, commit_id);
260 if (err)
261 return err;
263 if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
264 qid = STAILQ_FIRST(&commit->parent_ids);
265 if (qid == NULL ||
266 got_object_idset_contains(graph->open_branches, &qid->id))
267 return NULL;
268 /*
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'.
275 */
276 if (!got_path_is_root_dir(graph->path) &&
277 (commit->flags & GOT_COMMIT_FLAG_PACKED)) {
278 int ncommits = 0;
279 err = packed_first_parent_traversal(&ncommits,
280 graph, &qid->id, repo);
281 if (err || ncommits > 0)
282 return err;
284 return got_object_idset_add(graph->open_branches,
285 &qid->id, NULL);
288 /*
289 * If we are graphing commits for a specific path, skip branches
290 * which do not contribute any content to this path.
291 */
292 if (commit->nparents > 1 && !got_path_is_root_dir(graph->path)) {
293 err = got_object_id_by_path(&merged_id, repo, commit, graph->path);
294 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
295 return err;
296 /* The requested path does not exist in this merge commit. */
298 if (commit->nparents > 1 && !got_path_is_root_dir(graph->path) &&
299 merged_id != NULL) {
300 struct got_object_id *prev_id = NULL;
301 int branches_differ = 0;
304 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
305 struct got_object_id *id = NULL;
306 struct got_commit_object *pcommit = NULL;
308 if (got_object_idset_contains(graph->open_branches,
309 &qid->id))
310 continue;
312 err = got_object_open_as_commit(&pcommit, repo,
313 &qid->id);
314 if (err) {
315 free(merged_id);
316 free(prev_id);
317 return err;
319 err = got_object_id_by_path(&id, repo, pcommit,
320 graph->path);
321 got_object_commit_close(pcommit);
322 pcommit = NULL;
323 if (err) {
324 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
325 branches_differ = 1;
326 continue;
328 free(merged_id);
329 free(prev_id);
330 return err;
333 if (prev_id) {
334 if (!branches_differ &&
335 got_object_id_cmp(id, prev_id) != 0)
336 branches_differ = 1;
337 free(prev_id);
339 prev_id = id;
341 /*
342 * If a branch has created the merged content we can
343 * skip any other branches.
344 */
345 if (got_object_id_cmp(merged_id, id) == 0) {
346 err = got_object_idset_add(graph->open_branches,
347 &qid->id, NULL);
348 free(merged_id);
349 free(id);
350 return err;
354 free(prev_id);
355 prev_id = NULL;
356 free(merged_id);
357 merged_id = NULL;
359 /*
360 * If the path's content is the same on all branches,
361 * follow the first parent only.
362 */
363 if (!branches_differ) {
364 qid = STAILQ_FIRST(&commit->parent_ids);
365 if (qid == NULL)
366 return NULL;
367 if (got_object_idset_contains(graph->open_branches,
368 &qid->id))
369 return NULL;
370 if (got_object_idset_contains(graph->node_ids,
371 &qid->id))
372 return NULL; /* parent already traversed */
373 return got_object_idset_add(graph->open_branches,
374 &qid->id, NULL);
378 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
379 if (got_object_idset_contains(graph->open_branches, &qid->id))
380 continue;
381 if (got_object_idset_contains(graph->node_ids, &qid->id))
382 continue; /* parent already traversed */
383 err = got_object_idset_add(graph->open_branches, &qid->id,
384 NULL);
385 if (err)
386 return err;
389 return NULL;
392 const struct got_error *
393 got_commit_graph_open(struct got_commit_graph **graph,
394 const char *path, int first_parent_traversal)
396 const struct got_error *err = NULL;
398 *graph = calloc(1, sizeof(**graph));
399 if (*graph == NULL)
400 return got_error_from_errno("calloc");
402 TAILQ_INIT(&(*graph)->iter_list);
404 (*graph)->path = strdup(path);
405 if ((*graph)->path == NULL) {
406 err = got_error_from_errno("strdup");
407 goto done;
410 (*graph)->node_ids = got_object_idset_alloc();
411 if ((*graph)->node_ids == NULL) {
412 err = got_error_from_errno("got_object_idset_alloc");
413 goto done;
416 (*graph)->open_branches = got_object_idset_alloc();
417 if ((*graph)->open_branches == NULL) {
418 err = got_error_from_errno("got_object_idset_alloc");
419 goto done;
422 if (first_parent_traversal)
423 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
424 done:
425 if (err) {
426 got_commit_graph_close(*graph);
427 *graph = NULL;
429 return err;
432 struct add_branch_tip_arg {
433 struct got_commit_graph_branch_tip *tips;
434 int ntips;
435 struct got_repository *repo;
436 struct got_commit_graph *graph;
437 };
439 static const struct got_error *
440 add_branch_tip(struct got_object_id *commit_id, void *data, void *arg)
442 const struct got_error *err;
443 struct add_branch_tip_arg *a = arg;
444 struct got_commit_graph_node *new_node;
445 struct got_commit_object *commit;
447 err = got_object_open_as_commit(&commit, a->repo, commit_id);
448 if (err)
449 return err;
451 err = add_node(&new_node, a->graph, commit_id, a->repo);
452 if (err) {
453 got_object_commit_close(commit);
454 return err;
457 a->tips[a->ntips].commit_id = &new_node->id;
458 a->tips[a->ntips].commit = commit;
459 a->tips[a->ntips].new_node = new_node;
460 a->ntips++;
462 return NULL;
465 static const struct got_error *
466 fetch_commits_from_open_branches(struct got_commit_graph *graph,
467 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
469 const struct got_error *err;
470 struct add_branch_tip_arg arg;
471 int i, ntips;
473 ntips = got_object_idset_num_elements(graph->open_branches);
474 if (ntips == 0)
475 return NULL;
477 /* (Re-)allocate branch tips array if necessary. */
478 if (graph->ntips < ntips) {
479 struct got_commit_graph_branch_tip *tips;
480 tips = recallocarray(graph->tips, graph->ntips, ntips,
481 sizeof(*tips));
482 if (tips == NULL)
483 return got_error_from_errno("recallocarray");
484 graph->tips = tips;
485 graph->ntips = ntips;
487 arg.tips = graph->tips;
488 arg.ntips = 0; /* add_branch_tip() will increment */
489 arg.repo = repo;
490 arg.graph = graph;
491 err = got_object_idset_for_each(graph->open_branches, add_branch_tip,
492 &arg);
493 if (err)
494 goto done;
496 for (i = 0; i < arg.ntips; i++) {
497 struct got_object_id *commit_id;
498 struct got_commit_object *commit;
499 struct got_commit_graph_node *new_node;
500 int changed;
502 if (cancel_cb) {
503 err = (*cancel_cb)(cancel_arg);
504 if (err)
505 break;
508 commit_id = arg.tips[i].commit_id;
509 commit = arg.tips[i].commit;
510 new_node = arg.tips[i].new_node;
512 err = detect_changed_path(&changed, commit, commit_id,
513 graph->path, repo);
514 if (err) {
515 if (err->code != GOT_ERR_NO_OBJ)
516 break;
517 /*
518 * History of the path stops here on the current
519 * branch. Keep going on other branches.
520 */
521 err = close_branch(graph, commit_id);
522 if (err)
523 break;
524 continue;
526 if (changed) {
527 add_node_to_iter_list(graph, new_node,
528 got_object_commit_get_committer_time(commit));
529 arg.tips[i].new_node = NULL;
531 err = advance_branch(graph, commit_id, commit, repo);
532 if (err)
533 break;
535 done:
536 for (i = 0; i < arg.ntips; i++) {
537 got_object_commit_close(arg.tips[i].commit);
538 free(arg.tips[i].new_node);
540 return err;
543 void
544 got_commit_graph_close(struct got_commit_graph *graph)
546 struct got_commit_graph_node *node;
548 while ((node = TAILQ_FIRST(&graph->iter_list))) {
549 TAILQ_REMOVE(&graph->iter_list, node, entry);
550 free(node);
553 if (graph->open_branches)
554 got_object_idset_free(graph->open_branches);
555 if (graph->node_ids)
556 got_object_idset_free(graph->node_ids);
557 free(graph->tips);
558 free(graph->path);
559 free(graph);
562 const struct got_error *
563 got_commit_graph_iter_start(struct got_commit_graph *graph,
564 struct got_object_id *id, struct got_repository *repo,
565 got_cancel_cb cancel_cb, void *cancel_arg)
567 const struct got_error *err = NULL;
569 if (!TAILQ_EMPTY(&graph->iter_list))
570 return got_error(GOT_ERR_ITER_BUSY);
572 err = got_object_idset_add(graph->open_branches, id, NULL);
573 if (err)
574 return err;
576 /* Locate first commit which changed graph->path. */
577 while (TAILQ_EMPTY(&graph->iter_list) &&
578 got_object_idset_num_elements(graph->open_branches) > 0) {
579 err = fetch_commits_from_open_branches(graph, repo,
580 cancel_cb, cancel_arg);
581 if (err)
582 return err;
585 if (TAILQ_EMPTY(&graph->iter_list)) {
586 const char *path;
587 if (got_path_is_root_dir(graph->path))
588 return got_error_no_obj(id);
589 path = graph->path;
590 while (path[0] == '/')
591 path++;
592 return got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
595 return NULL;
598 const struct got_error *
599 got_commit_graph_iter_next(struct got_object_id *id,
600 struct got_commit_graph *graph, struct got_repository *repo,
601 got_cancel_cb cancel_cb, void *cancel_arg)
603 const struct got_error *err = NULL;
604 struct got_commit_graph_node *node;
606 node = TAILQ_FIRST(&graph->iter_list);
607 if (node == NULL) {
608 /* We are done iterating, or iteration was not started. */
609 return got_error(GOT_ERR_ITER_COMPLETED);
612 while (TAILQ_NEXT(node, entry) == NULL &&
613 got_object_idset_num_elements(graph->open_branches) > 0) {
614 err = fetch_commits_from_open_branches(graph, repo,
615 cancel_cb, cancel_arg);
616 if (err)
617 return err;
620 memcpy(id, &node->id, sizeof(*id));
622 TAILQ_REMOVE(&graph->iter_list, node, entry);
623 free(node);
624 return NULL;
627 static const struct got_error *
628 find_yca_add_id(struct got_object_id **yca_id, struct got_commit_graph *graph,
629 struct got_object_idset *commit_ids, struct got_repository *repo,
630 got_cancel_cb cancel_cb, void *cancel_arg)
632 const struct got_error *err = NULL;
633 struct got_object_id id;
635 err = got_commit_graph_iter_next(&id, graph, repo, cancel_cb,
636 cancel_arg);
637 if (err)
638 return err;
640 if (got_object_idset_contains(commit_ids, &id)) {
641 *yca_id = got_object_id_dup(&id);
642 if (*yca_id == NULL)
643 err = got_error_from_errno("got_object_id_dup");
644 return err;
647 return got_object_idset_add(commit_ids, &id, NULL);
650 /*
651 * Sets *yca_id to the youngest common ancestor of commit_id and
652 * commit_id2. Returns got_error(GOT_ERR_ANCESTRY) if they have no
653 * common ancestors.
655 * If first_parent_traversal is nonzero, only linear history is considered.
656 */
657 const struct got_error *
658 got_commit_graph_find_youngest_common_ancestor(struct got_object_id **yca_id,
659 struct got_object_id *commit_id, struct got_object_id *commit_id2,
660 int first_parent_traversal,
661 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
663 const struct got_error *err = NULL;
664 struct got_commit_graph *graph = NULL, *graph2 = NULL;
665 int completed = 0, completed2 = 0;
666 struct got_object_idset *commit_ids;
668 *yca_id = NULL;
670 commit_ids = got_object_idset_alloc();
671 if (commit_ids == NULL)
672 return got_error_from_errno("got_object_idset_alloc");
674 err = got_commit_graph_open(&graph, "/", first_parent_traversal);
675 if (err)
676 goto done;
678 err = got_commit_graph_open(&graph2, "/", first_parent_traversal);
679 if (err)
680 goto done;
682 err = got_commit_graph_iter_start(graph, commit_id, repo,
683 cancel_cb, cancel_arg);
684 if (err)
685 goto done;
687 err = got_commit_graph_iter_start(graph2, commit_id2, repo,
688 cancel_cb, cancel_arg);
689 if (err)
690 goto done;
692 for (;;) {
693 if (cancel_cb) {
694 err = (*cancel_cb)(cancel_arg);
695 if (err)
696 break;
699 if (!completed) {
700 err = find_yca_add_id(yca_id, graph, commit_ids, repo,
701 cancel_cb, cancel_arg);
702 if (err) {
703 if (err->code != GOT_ERR_ITER_COMPLETED)
704 break;
705 err = NULL;
706 completed = 1;
708 if (*yca_id)
709 break;
712 if (!completed2) {
713 err = find_yca_add_id(yca_id, graph2, commit_ids, repo,
714 cancel_cb, cancel_arg);
715 if (err) {
716 if (err->code != GOT_ERR_ITER_COMPLETED)
717 break;
718 err = NULL;
719 completed2 = 1;
721 if (*yca_id)
722 break;
725 if (completed && completed2) {
726 err = got_error(GOT_ERR_ANCESTRY);
727 break;
730 done:
731 got_object_idset_free(commit_ids);
732 if (graph)
733 got_commit_graph_close(graph);
734 if (graph2)
735 got_commit_graph_close(graph2);
736 return err;