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;
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_id, 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 struct got_commit_graph_node *node;
218 if (got_object_idset_contains(graph->open_branches, qid->id))
219 continue;
220 if (got_object_idset_contains(graph->node_ids, qid->id))
221 continue;
223 (*ncommits_traversed)++;
225 /* ... except the last commit is the new branch tip. */
226 if (STAILQ_NEXT(qid, entry) == NULL) {
227 err = got_object_idset_add(graph->open_branches,
228 qid->id, NULL);
229 break;
232 err = add_node(&node, graph, qid->id, repo);
233 if (err)
234 break;
237 got_object_id_queue_free(&traversed_commits);
238 return err;
241 static const struct got_error *
242 close_branch(struct got_commit_graph *graph, struct got_object_id *commit_id)
244 const struct got_error *err;
246 err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
247 if (err && err->code != GOT_ERR_NO_OBJ)
248 return err;
249 return NULL;
252 static const struct got_error *
253 advance_branch(struct got_commit_graph *graph, struct got_object_id *commit_id,
254 struct got_commit_object *commit, struct got_repository *repo)
256 const struct got_error *err;
257 struct got_object_qid *qid;
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 struct got_object_id *merged_id, *prev_id = NULL;
294 int branches_differ = 0;
296 err = got_object_id_by_path(&merged_id, repo, commit_id,
297 graph->path);
298 if (err)
299 return err;
301 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
302 struct got_object_id *id;
304 if (got_object_idset_contains(graph->open_branches,
305 qid->id))
306 continue;
308 err = got_object_id_by_path(&id, repo, qid->id,
309 graph->path);
310 if (err) {
311 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
312 branches_differ = 1;
313 continue;
315 free(merged_id);
316 free(prev_id);
317 return err;
320 if (prev_id) {
321 if (!branches_differ &&
322 got_object_id_cmp(id, prev_id) != 0)
323 branches_differ = 1;
324 free(prev_id);
326 prev_id = id;
328 /*
329 * If a branch has created the merged content we can
330 * skip any other branches.
331 */
332 if (got_object_id_cmp(merged_id, id) == 0) {
333 err = got_object_idset_add(graph->open_branches,
334 qid->id, NULL);
335 free(merged_id);
336 free(id);
337 return err;
341 free(prev_id);
342 prev_id = NULL;
343 free(merged_id);
344 merged_id = NULL;
346 /*
347 * If the path's content is the same on all branches,
348 * follow the first parent only.
349 */
350 if (!branches_differ) {
351 qid = STAILQ_FIRST(&commit->parent_ids);
352 if (qid == NULL)
353 return NULL;
354 if (got_object_idset_contains(graph->open_branches,
355 qid->id))
356 return NULL;
357 if (got_object_idset_contains(graph->node_ids,
358 qid->id))
359 return NULL; /* parent already traversed */
360 return got_object_idset_add(graph->open_branches,
361 qid->id, NULL);
365 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
366 if (got_object_idset_contains(graph->open_branches, qid->id))
367 continue;
368 if (got_object_idset_contains(graph->node_ids, qid->id))
369 continue; /* parent already traversed */
370 err = got_object_idset_add(graph->open_branches, qid->id, NULL);
371 if (err)
372 return err;
375 return NULL;
378 const struct got_error *
379 got_commit_graph_open(struct got_commit_graph **graph,
380 const char *path, int first_parent_traversal)
382 const struct got_error *err = NULL;
384 *graph = calloc(1, sizeof(**graph));
385 if (*graph == NULL)
386 return got_error_from_errno("calloc");
388 TAILQ_INIT(&(*graph)->iter_list);
390 (*graph)->path = strdup(path);
391 if ((*graph)->path == NULL) {
392 err = got_error_from_errno("strdup");
393 goto done;
396 (*graph)->node_ids = got_object_idset_alloc();
397 if ((*graph)->node_ids == NULL) {
398 err = got_error_from_errno("got_object_idset_alloc");
399 goto done;
402 (*graph)->open_branches = got_object_idset_alloc();
403 if ((*graph)->open_branches == NULL) {
404 err = got_error_from_errno("got_object_idset_alloc");
405 goto done;
408 if (first_parent_traversal)
409 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
410 done:
411 if (err) {
412 got_commit_graph_close(*graph);
413 *graph = NULL;
415 return err;
418 struct add_branch_tip_arg {
419 struct got_commit_graph_branch_tip *tips;
420 int ntips;
421 struct got_repository *repo;
422 struct got_commit_graph *graph;
423 };
425 static const struct got_error *
426 add_branch_tip(struct got_object_id *commit_id, void *data, void *arg)
428 const struct got_error *err;
429 struct add_branch_tip_arg *a = arg;
430 struct got_commit_graph_node *new_node;
431 struct got_commit_object *commit;
433 err = got_object_open_as_commit(&commit, a->repo, commit_id);
434 if (err)
435 return err;
437 err = add_node(&new_node, a->graph, commit_id, a->repo);
438 if (err)
439 return err;
441 a->tips[a->ntips].commit_id = new_node ? &new_node->id : NULL;
442 a->tips[a->ntips].commit = commit;
443 a->tips[a->ntips].new_node = new_node;
444 a->ntips++;
446 return NULL;
449 static const struct got_error *
450 fetch_commits_from_open_branches(struct got_commit_graph *graph,
451 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
453 const struct got_error *err;
454 struct add_branch_tip_arg arg;
455 int i, ntips;
457 ntips = got_object_idset_num_elements(graph->open_branches);
458 if (ntips == 0)
459 return NULL;
461 /* (Re-)allocate branch tips array if necessary. */
462 if (graph->ntips < ntips) {
463 struct got_commit_graph_branch_tip *tips;
464 tips = recallocarray(graph->tips, graph->ntips, ntips,
465 sizeof(*tips));
466 if (tips == NULL)
467 return got_error_from_errno("recallocarray");
468 graph->tips = tips;
469 graph->ntips = ntips;
471 arg.tips = graph->tips;
472 arg.ntips = 0; /* add_branch_tip() will increment */
473 arg.repo = repo;
474 arg.graph = graph;
475 err = got_object_idset_for_each(graph->open_branches, add_branch_tip,
476 &arg);
477 if (err)
478 goto done;
480 for (i = 0; i < arg.ntips; i++) {
481 struct got_object_id *commit_id;
482 struct got_commit_object *commit;
483 struct got_commit_graph_node *new_node;
484 int changed;
486 if (cancel_cb) {
487 err = (*cancel_cb)(cancel_arg);
488 if (err)
489 break;
492 commit_id = arg.tips[i].commit_id;
493 commit = arg.tips[i].commit;
494 new_node = arg.tips[i].new_node;
496 err = detect_changed_path(&changed, commit, commit_id,
497 graph->path, repo);
498 if (err) {
499 if (err->code != GOT_ERR_NO_OBJ)
500 break;
501 /*
502 * History of the path stops here on the current
503 * branch. Keep going on other branches.
504 */
505 err = close_branch(graph, commit_id);
506 if (err)
507 break;
508 continue;
510 if (changed)
511 add_node_to_iter_list(graph, new_node,
512 got_object_commit_get_committer_time(commit));
513 err = advance_branch(graph, commit_id, commit, repo);
514 if (err)
515 break;
517 done:
518 for (i = 0; i < arg.ntips; i++)
519 got_object_commit_close(arg.tips[i].commit);
520 return err;
523 void
524 got_commit_graph_close(struct got_commit_graph *graph)
526 if (graph->open_branches)
527 got_object_idset_free(graph->open_branches);
528 if (graph->node_ids)
529 got_object_idset_free(graph->node_ids);
530 free(graph->tips);
531 free(graph->path);
532 free(graph);
535 const struct got_error *
536 got_commit_graph_iter_start(struct got_commit_graph *graph,
537 struct got_object_id *id, struct got_repository *repo,
538 got_cancel_cb cancel_cb, void *cancel_arg)
540 const struct got_error *err = NULL;
542 if (!TAILQ_EMPTY(&graph->iter_list))
543 return got_error(GOT_ERR_ITER_BUSY);
545 err = got_object_idset_add(graph->open_branches, id, NULL);
546 if (err)
547 return err;
549 /* Locate first commit which changed graph->path. */
550 while (TAILQ_EMPTY(&graph->iter_list) &&
551 got_object_idset_num_elements(graph->open_branches) > 0) {
552 err = fetch_commits_from_open_branches(graph, repo,
553 cancel_cb, cancel_arg);
554 if (err)
555 return err;
558 if (TAILQ_EMPTY(&graph->iter_list)) {
559 const char *path;
560 if (got_path_is_root_dir(graph->path))
561 return got_error_no_obj(id);
562 path = graph->path;
563 while (path[0] == '/')
564 path++;
565 return got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
568 return NULL;
571 const struct got_error *
572 got_commit_graph_iter_next(struct got_object_id **id,
573 struct got_commit_graph *graph, struct got_repository *repo,
574 got_cancel_cb cancel_cb, void *cancel_arg)
576 const struct got_error *err = NULL;
577 struct got_commit_graph_node *node;
579 *id = NULL;
581 node = TAILQ_FIRST(&graph->iter_list);
582 if (node == NULL) {
583 /* We are done iterating, or iteration was not started. */
584 return got_error(GOT_ERR_ITER_COMPLETED);
587 while (TAILQ_NEXT(node, entry) == NULL &&
588 got_object_idset_num_elements(graph->open_branches) > 0) {
589 err = fetch_commits_from_open_branches(graph, repo,
590 cancel_cb, cancel_arg);
591 if (err)
592 return err;
595 *id = &node->id;
596 TAILQ_REMOVE(&graph->iter_list, node, entry);
597 return NULL;
600 const struct got_error *
601 got_commit_graph_find_youngest_common_ancestor(struct got_object_id **yca_id,
602 struct got_object_id *commit_id, struct got_object_id *commit_id2,
603 int first_parent_traversal,
604 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
606 const struct got_error *err = NULL;
607 struct got_commit_graph *graph = NULL, *graph2 = NULL;
608 int completed = 0, completed2 = 0;
609 struct got_object_idset *commit_ids;
611 *yca_id = NULL;
613 commit_ids = got_object_idset_alloc();
614 if (commit_ids == NULL)
615 return got_error_from_errno("got_object_idset_alloc");
617 err = got_commit_graph_open(&graph, "/", first_parent_traversal);
618 if (err)
619 goto done;
621 err = got_commit_graph_open(&graph2, "/", first_parent_traversal);
622 if (err)
623 goto done;
625 err = got_commit_graph_iter_start(graph, commit_id, repo,
626 cancel_cb, cancel_arg);
627 if (err)
628 goto done;
630 err = got_commit_graph_iter_start(graph2, commit_id2, repo,
631 cancel_cb, cancel_arg);
632 if (err)
633 goto done;
635 for (;;) {
636 struct got_object_id *id = NULL, *id2 = NULL;
638 if (cancel_cb) {
639 err = (*cancel_cb)(cancel_arg);
640 if (err)
641 break;
644 if (!completed) {
645 err = got_commit_graph_iter_next(&id, graph, repo,
646 cancel_cb, cancel_arg);
647 if (err) {
648 if (err->code != GOT_ERR_ITER_COMPLETED)
649 break;
650 err = NULL;
651 completed = 1;
655 if (!completed2) {
656 err = got_commit_graph_iter_next(&id2, graph2, repo,
657 cancel_cb, cancel_arg);
658 if (err) {
659 if (err->code != GOT_ERR_ITER_COMPLETED)
660 break;
661 err = NULL;
662 completed2 = 1;
666 if (id) {
667 if (got_object_idset_contains(commit_ids, id)) {
668 *yca_id = got_object_id_dup(id);
669 if (*yca_id)
670 break;
671 err = got_error_from_errno("got_object_id_dup");
672 break;
675 err = got_object_idset_add(commit_ids, id, NULL);
676 if (err)
677 break;
679 if (id2) {
680 if (got_object_idset_contains(commit_ids, id2)) {
681 *yca_id = got_object_id_dup(id2);
682 if (*yca_id)
683 break;
684 err = got_error_from_errno("got_object_id_dup");
685 break;
688 err = got_object_idset_add(commit_ids, id2, NULL);
689 if (err)
690 break;
693 if (completed && completed2) {
694 err = got_error(GOT_ERR_ANCESTRY);
695 break;
699 done:
700 got_object_idset_free(commit_ids);
701 if (graph)
702 got_commit_graph_close(graph);
703 if (graph2)
704 got_commit_graph_close(graph2);
705 return err;