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 <zlib.h>
28 #include <ctype.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;
94 /*
95 * Temporary storage for the id returned by
96 * got_commit_graph_iter_next.
97 */
98 struct got_object_id id;
99 };
101 static const struct got_error *
102 detect_changed_path(int *changed, struct got_commit_object *commit,
103 struct got_object_id *commit_id, const char *path,
104 struct got_repository *repo)
106 const struct got_error *err = NULL;
107 struct got_commit_object *pcommit = NULL;
108 struct got_tree_object *tree = NULL, *ptree = NULL;
109 struct got_object_qid *pid;
111 if (got_path_is_root_dir(path)) {
112 *changed = 1;
113 return NULL;
116 *changed = 0;
118 pid = STAILQ_FIRST(&commit->parent_ids);
119 if (pid == NULL) {
120 struct got_object_id *obj_id;
121 err = got_object_id_by_path(&obj_id, repo, commit, path);
122 if (err) {
123 if (err->code == GOT_ERR_NO_TREE_ENTRY)
124 err = NULL;
125 } else
126 *changed = 1; /* The path was created in this commit. */
127 free(obj_id);
128 return err;
131 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
132 if (err)
133 return err;
135 err = got_object_open_as_commit(&pcommit, repo, &pid->id);
136 if (err)
137 goto done;
139 err = got_object_open_as_tree(&ptree, repo, pcommit->tree_id);
140 if (err)
141 goto done;
143 err = got_object_tree_path_changed(changed, tree, ptree, path, repo);
144 done:
145 if (tree)
146 got_object_tree_close(tree);
147 if (ptree)
148 got_object_tree_close(ptree);
149 if (pcommit)
150 got_object_commit_close(pcommit);
151 return err;
154 static void
155 add_node_to_iter_list(struct got_commit_graph *graph,
156 struct got_commit_graph_node *node, time_t committer_time)
158 struct got_commit_graph_node *n, *next;
160 node->timestamp = committer_time;
162 n = TAILQ_FIRST(&graph->iter_list);
163 while (n) {
164 next = TAILQ_NEXT(n, entry);
165 if (next && node->timestamp >= next->timestamp) {
166 TAILQ_INSERT_BEFORE(next, node, entry);
167 return;
169 n = next;
171 TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
174 static const struct got_error *
175 add_node(struct got_commit_graph_node **new_node,
176 struct got_commit_graph *graph, struct got_object_id *commit_id,
177 struct got_repository *repo)
179 const struct got_error *err = NULL;
180 struct got_commit_graph_node *node;
182 *new_node = NULL;
184 node = calloc(1, sizeof(*node));
185 if (node == NULL)
186 return got_error_from_errno("calloc");
188 memcpy(&node->id, commit_id, sizeof(node->id));
189 err = got_object_idset_add(graph->node_ids, &node->id, NULL);
190 if (err)
191 free(node);
192 else
193 *new_node = node;
194 return err;
197 /*
198 * Ask got-read-pack to traverse first-parent history until a commit is
199 * encountered which modified graph->path, or until the pack file runs
200 * out of relevant commits. This is faster than sending an individual
201 * request for each commit stored in the pack file.
202 */
203 static const struct got_error *
204 packed_first_parent_traversal(int *ncommits_traversed,
205 struct got_commit_graph *graph, struct got_object_id *commit_id,
206 struct got_repository *repo)
208 const struct got_error *err = NULL;
209 struct got_object_id_queue traversed_commits;
210 struct got_object_qid *qid;
212 STAILQ_INIT(&traversed_commits);
213 *ncommits_traversed = 0;
215 err = got_traverse_packed_commits(&traversed_commits,
216 commit_id, graph->path, repo);
217 if (err)
218 return err;
220 /* Add all traversed commits to the graph... */
221 STAILQ_FOREACH(qid, &traversed_commits, entry) {
222 if (got_object_idset_contains(graph->open_branches, &qid->id))
223 continue;
224 if (got_object_idset_contains(graph->node_ids, &qid->id))
225 continue;
227 (*ncommits_traversed)++;
229 /* ... except the last commit is the new branch tip. */
230 if (STAILQ_NEXT(qid, entry) == NULL) {
231 err = got_object_idset_add(graph->open_branches,
232 &qid->id, NULL);
233 break;
236 err = got_object_idset_add(graph->node_ids, &qid->id, NULL);
237 if (err)
238 break;
241 got_object_id_queue_free(&traversed_commits);
242 return err;
245 static const struct got_error *
246 close_branch(struct got_commit_graph *graph, struct got_object_id *commit_id)
248 const struct got_error *err;
250 err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
251 if (err && err->code != GOT_ERR_NO_OBJ)
252 return err;
253 return NULL;
256 static const struct got_error *
257 advance_branch(struct got_commit_graph *graph, struct got_object_id *commit_id,
258 struct got_commit_object *commit, struct got_repository *repo)
260 const struct got_error *err;
261 struct got_object_qid *qid;
263 err = close_branch(graph, commit_id);
264 if (err)
265 return err;
267 if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
268 qid = STAILQ_FIRST(&commit->parent_ids);
269 if (qid == NULL ||
270 got_object_idset_contains(graph->open_branches, &qid->id))
271 return NULL;
272 /*
273 * The root directory always changes by definition, and when
274 * logging the root we want to traverse consecutive commits
275 * even if they point at the same tree.
276 * But if we are looking for a specific path then we can avoid
277 * fetching packed commits which did not modify the path and
278 * only fetch their IDs. This speeds up 'got blame'.
279 */
280 if (!got_path_is_root_dir(graph->path) &&
281 (commit->flags & GOT_COMMIT_FLAG_PACKED)) {
282 int ncommits = 0;
283 err = packed_first_parent_traversal(&ncommits,
284 graph, &qid->id, repo);
285 if (err || ncommits > 0)
286 return err;
288 return got_object_idset_add(graph->open_branches,
289 &qid->id, NULL);
292 /*
293 * If we are graphing commits for a specific path, skip branches
294 * which do not contribute any content to this path.
295 */
296 if (commit->nparents > 1 && !got_path_is_root_dir(graph->path)) {
297 struct got_object_id *merged_id, *prev_id = NULL;
298 int branches_differ = 0;
300 err = got_object_id_by_path(&merged_id, repo, commit,
301 graph->path);
302 if (err)
303 return err;
305 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
306 struct got_object_id *id = NULL;
307 struct got_commit_object *pcommit = NULL;
309 if (got_object_idset_contains(graph->open_branches,
310 &qid->id))
311 continue;
313 err = got_object_open_as_commit(&pcommit, repo,
314 &qid->id);
315 if (err) {
316 free(merged_id);
317 free(prev_id);
318 return err;
320 err = got_object_id_by_path(&id, repo, pcommit,
321 graph->path);
322 got_object_commit_close(pcommit);
323 pcommit = NULL;
324 if (err) {
325 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
326 branches_differ = 1;
327 continue;
329 free(merged_id);
330 free(prev_id);
331 return err;
334 if (prev_id) {
335 if (!branches_differ &&
336 got_object_id_cmp(id, prev_id) != 0)
337 branches_differ = 1;
338 free(prev_id);
340 prev_id = id;
342 /*
343 * If a branch has created the merged content we can
344 * skip any other branches.
345 */
346 if (got_object_id_cmp(merged_id, id) == 0) {
347 err = got_object_idset_add(graph->open_branches,
348 &qid->id, NULL);
349 free(merged_id);
350 free(id);
351 return err;
355 free(prev_id);
356 prev_id = NULL;
357 free(merged_id);
358 merged_id = NULL;
360 /*
361 * If the path's content is the same on all branches,
362 * follow the first parent only.
363 */
364 if (!branches_differ) {
365 qid = STAILQ_FIRST(&commit->parent_ids);
366 if (qid == NULL)
367 return NULL;
368 if (got_object_idset_contains(graph->open_branches,
369 &qid->id))
370 return NULL;
371 if (got_object_idset_contains(graph->node_ids,
372 &qid->id))
373 return NULL; /* parent already traversed */
374 return got_object_idset_add(graph->open_branches,
375 &qid->id, NULL);
379 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
380 if (got_object_idset_contains(graph->open_branches, &qid->id))
381 continue;
382 if (got_object_idset_contains(graph->node_ids, &qid->id))
383 continue; /* parent already traversed */
384 err = got_object_idset_add(graph->open_branches, &qid->id,
385 NULL);
386 if (err)
387 return err;
390 return NULL;
393 const struct got_error *
394 got_commit_graph_open(struct got_commit_graph **graph,
395 const char *path, int first_parent_traversal)
397 const struct got_error *err = NULL;
399 *graph = calloc(1, sizeof(**graph));
400 if (*graph == NULL)
401 return got_error_from_errno("calloc");
403 TAILQ_INIT(&(*graph)->iter_list);
405 (*graph)->path = strdup(path);
406 if ((*graph)->path == NULL) {
407 err = got_error_from_errno("strdup");
408 goto done;
411 (*graph)->node_ids = got_object_idset_alloc();
412 if ((*graph)->node_ids == NULL) {
413 err = got_error_from_errno("got_object_idset_alloc");
414 goto done;
417 (*graph)->open_branches = got_object_idset_alloc();
418 if ((*graph)->open_branches == NULL) {
419 err = got_error_from_errno("got_object_idset_alloc");
420 goto done;
423 if (first_parent_traversal)
424 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
425 done:
426 if (err) {
427 got_commit_graph_close(*graph);
428 *graph = NULL;
430 return err;
433 struct add_branch_tip_arg {
434 struct got_commit_graph_branch_tip *tips;
435 int ntips;
436 struct got_repository *repo;
437 struct got_commit_graph *graph;
438 };
440 static const struct got_error *
441 add_branch_tip(struct got_object_id *commit_id, void *data, void *arg)
443 const struct got_error *err;
444 struct add_branch_tip_arg *a = arg;
445 struct got_commit_graph_node *new_node;
446 struct got_commit_object *commit;
448 err = got_object_open_as_commit(&commit, a->repo, commit_id);
449 if (err)
450 return err;
452 err = add_node(&new_node, a->graph, commit_id, a->repo);
453 if (err) {
454 got_object_commit_close(commit);
455 return err;
458 a->tips[a->ntips].commit_id = &new_node->id;
459 a->tips[a->ntips].commit = commit;
460 a->tips[a->ntips].new_node = new_node;
461 a->ntips++;
463 return NULL;
466 static const struct got_error *
467 fetch_commits_from_open_branches(struct got_commit_graph *graph,
468 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
470 const struct got_error *err;
471 struct add_branch_tip_arg arg;
472 int i, ntips;
474 ntips = got_object_idset_num_elements(graph->open_branches);
475 if (ntips == 0)
476 return NULL;
478 /* (Re-)allocate branch tips array if necessary. */
479 if (graph->ntips < ntips) {
480 struct got_commit_graph_branch_tip *tips;
481 tips = recallocarray(graph->tips, graph->ntips, ntips,
482 sizeof(*tips));
483 if (tips == NULL)
484 return got_error_from_errno("recallocarray");
485 graph->tips = tips;
486 graph->ntips = ntips;
488 arg.tips = graph->tips;
489 arg.ntips = 0; /* add_branch_tip() will increment */
490 arg.repo = repo;
491 arg.graph = graph;
492 err = got_object_idset_for_each(graph->open_branches, add_branch_tip,
493 &arg);
494 if (err)
495 goto done;
497 for (i = 0; i < arg.ntips; i++) {
498 struct got_object_id *commit_id;
499 struct got_commit_object *commit;
500 struct got_commit_graph_node *new_node;
501 int changed;
503 if (cancel_cb) {
504 err = (*cancel_cb)(cancel_arg);
505 if (err)
506 break;
509 commit_id = arg.tips[i].commit_id;
510 commit = arg.tips[i].commit;
511 new_node = arg.tips[i].new_node;
513 err = detect_changed_path(&changed, commit, commit_id,
514 graph->path, repo);
515 if (err) {
516 if (err->code != GOT_ERR_NO_OBJ)
517 break;
518 /*
519 * History of the path stops here on the current
520 * branch. Keep going on other branches.
521 */
522 err = close_branch(graph, commit_id);
523 if (err)
524 break;
525 continue;
527 if (changed) {
528 add_node_to_iter_list(graph, new_node,
529 got_object_commit_get_committer_time(commit));
530 arg.tips[i].new_node = NULL;
532 err = advance_branch(graph, commit_id, commit, repo);
533 if (err)
534 break;
536 done:
537 for (i = 0; i < arg.ntips; i++) {
538 got_object_commit_close(arg.tips[i].commit);
539 free(arg.tips[i].new_node);
541 return err;
544 void
545 got_commit_graph_close(struct got_commit_graph *graph)
547 struct got_commit_graph_node *node;
549 while ((node = TAILQ_FIRST(&graph->iter_list))) {
550 TAILQ_REMOVE(&graph->iter_list, node, entry);
551 free(node);
554 if (graph->open_branches)
555 got_object_idset_free(graph->open_branches);
556 if (graph->node_ids)
557 got_object_idset_free(graph->node_ids);
558 free(graph->tips);
559 free(graph->path);
560 free(graph);
563 const struct got_error *
564 got_commit_graph_iter_start(struct got_commit_graph *graph,
565 struct got_object_id *id, struct got_repository *repo,
566 got_cancel_cb cancel_cb, void *cancel_arg)
568 const struct got_error *err = NULL;
570 if (!TAILQ_EMPTY(&graph->iter_list))
571 return got_error(GOT_ERR_ITER_BUSY);
573 err = got_object_idset_add(graph->open_branches, id, NULL);
574 if (err)
575 return err;
577 /* Locate first commit which changed graph->path. */
578 while (TAILQ_EMPTY(&graph->iter_list) &&
579 got_object_idset_num_elements(graph->open_branches) > 0) {
580 err = fetch_commits_from_open_branches(graph, repo,
581 cancel_cb, cancel_arg);
582 if (err)
583 return err;
586 if (TAILQ_EMPTY(&graph->iter_list)) {
587 const char *path;
588 if (got_path_is_root_dir(graph->path))
589 return got_error_no_obj(id);
590 path = graph->path;
591 while (path[0] == '/')
592 path++;
593 return got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
596 return NULL;
599 const struct got_error *
600 got_commit_graph_iter_next(struct got_object_id **id,
601 struct got_commit_graph *graph, struct got_repository *repo,
602 got_cancel_cb cancel_cb, void *cancel_arg)
604 const struct got_error *err = NULL;
605 struct got_commit_graph_node *node;
607 *id = NULL;
609 node = TAILQ_FIRST(&graph->iter_list);
610 if (node == NULL) {
611 /* We are done iterating, or iteration was not started. */
612 return got_error(GOT_ERR_ITER_COMPLETED);
615 while (TAILQ_NEXT(node, entry) == NULL &&
616 got_object_idset_num_elements(graph->open_branches) > 0) {
617 err = fetch_commits_from_open_branches(graph, repo,
618 cancel_cb, cancel_arg);
619 if (err)
620 return err;
623 memcpy(&graph->id, &node->id, sizeof(graph->id));
624 *id = &graph->id;
626 TAILQ_REMOVE(&graph->iter_list, node, entry);
627 free(node);
628 return NULL;
631 const struct got_error *
632 got_commit_graph_find_youngest_common_ancestor(struct got_object_id **yca_id,
633 struct got_object_id *commit_id, struct got_object_id *commit_id2,
634 int first_parent_traversal,
635 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
637 const struct got_error *err = NULL;
638 struct got_commit_graph *graph = NULL, *graph2 = NULL;
639 int completed = 0, completed2 = 0;
640 struct got_object_idset *commit_ids;
642 *yca_id = NULL;
644 commit_ids = got_object_idset_alloc();
645 if (commit_ids == NULL)
646 return got_error_from_errno("got_object_idset_alloc");
648 err = got_commit_graph_open(&graph, "/", first_parent_traversal);
649 if (err)
650 goto done;
652 err = got_commit_graph_open(&graph2, "/", first_parent_traversal);
653 if (err)
654 goto done;
656 err = got_commit_graph_iter_start(graph, commit_id, repo,
657 cancel_cb, cancel_arg);
658 if (err)
659 goto done;
661 err = got_commit_graph_iter_start(graph2, commit_id2, repo,
662 cancel_cb, cancel_arg);
663 if (err)
664 goto done;
666 for (;;) {
667 struct got_object_id *id = NULL, *id2 = NULL;
669 if (cancel_cb) {
670 err = (*cancel_cb)(cancel_arg);
671 if (err)
672 break;
675 if (!completed) {
676 err = got_commit_graph_iter_next(&id, graph, repo,
677 cancel_cb, cancel_arg);
678 if (err) {
679 if (err->code != GOT_ERR_ITER_COMPLETED)
680 break;
681 err = NULL;
682 completed = 1;
686 if (!completed2) {
687 err = got_commit_graph_iter_next(&id2, graph2, repo,
688 cancel_cb, cancel_arg);
689 if (err) {
690 if (err->code != GOT_ERR_ITER_COMPLETED)
691 break;
692 err = NULL;
693 completed2 = 1;
697 if (id) {
698 if (got_object_idset_contains(commit_ids, id)) {
699 *yca_id = got_object_id_dup(id);
700 if (*yca_id)
701 break;
702 err = got_error_from_errno("got_object_id_dup");
703 break;
706 err = got_object_idset_add(commit_ids, id, NULL);
707 if (err)
708 break;
710 if (id2) {
711 if (got_object_idset_contains(commit_ids, id2)) {
712 *yca_id = got_object_id_dup(id2);
713 if (*yca_id)
714 break;
715 err = got_error_from_errno("got_object_id_dup");
716 break;
719 err = got_object_idset_add(commit_ids, id2, NULL);
720 if (err)
721 break;
724 if (completed && completed2) {
725 err = got_error(GOT_ERR_ANCESTRY);
726 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;