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>
20 #include <limits.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <zlib.h>
25 #include <ctype.h>
27 #include "got_compat.h"
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_cancel.h"
32 #include "got_commit_graph.h"
33 #include "got_path.h"
35 #include "got_lib_delta.h"
36 #include "got_lib_inflate.h"
37 #include "got_lib_object.h"
38 #include "got_lib_object_idset.h"
40 struct got_commit_graph_node {
41 struct got_object_id id;
43 /* Used only during iteration. */
44 time_t timestamp;
45 TAILQ_ENTRY(got_commit_graph_node) entry;
46 };
48 TAILQ_HEAD(got_commit_graph_iter_list, got_commit_graph_node);
50 struct got_commit_graph_branch_tip {
51 struct got_object_id *commit_id;
52 struct got_commit_object *commit;
53 struct got_commit_graph_node *new_node;
54 };
56 struct got_commit_graph {
57 /* The set of all commits we have traversed. */
58 struct got_object_idset *node_ids;
60 int flags;
61 #define GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL 0x01
63 /*
64 * A set of object IDs of known parent commits which we have not yet
65 * traversed. Each commit ID in this set represents a branch in commit
66 * history: Either the first-parent branch of the head node, or another
67 * branch corresponding to a traversed merge commit for which we have
68 * not traversed a branch point commit yet.
69 *
70 * Whenever we add a commit with a matching ID to the graph, we remove
71 * its corresponding element from this set, and add new elements for
72 * each of that commit's parent commits which were not traversed yet.
73 *
74 * When API users ask us to fetch more commits, we fetch commits from
75 * all currently open branches. This allows API users to process
76 * commits in linear order even though the history contains branches.
77 */
78 struct got_object_idset *open_branches;
80 /* Array of branch tips for fetch_commits_from_open_branches(). */
81 struct got_commit_graph_branch_tip *tips;
82 int ntips;
84 /* Path of tree entry of interest to the API user. */
85 char *path;
87 /*
88 * Nodes which will be passed to the API user next, sorted by
89 * commit timestmap.
90 */
91 struct got_commit_graph_iter_list iter_list;
92 };
94 static const struct got_error *
95 detect_changed_path(int *changed, struct got_commit_object *commit,
96 struct got_object_id *commit_id, const char *path,
97 struct got_repository *repo)
98 {
99 const struct got_error *err = NULL;
100 struct got_commit_object *pcommit = NULL;
101 struct got_tree_object *tree = NULL, *ptree = NULL;
102 struct got_object_qid *pid;
104 if (got_path_is_root_dir(path)) {
105 *changed = 1;
106 return NULL;
109 *changed = 0;
111 pid = STAILQ_FIRST(&commit->parent_ids);
112 if (pid == NULL) {
113 struct got_object_id *obj_id;
114 err = got_object_id_by_path(&obj_id, repo, commit, path);
115 if (err) {
116 if (err->code == GOT_ERR_NO_TREE_ENTRY)
117 err = NULL;
118 } else
119 *changed = 1; /* The path was created in this commit. */
120 free(obj_id);
121 return err;
124 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
125 if (err)
126 return err;
128 err = got_object_open_as_commit(&pcommit, repo, pid->id);
129 if (err)
130 goto done;
132 err = got_object_open_as_tree(&ptree, repo, pcommit->tree_id);
133 if (err)
134 goto done;
136 err = got_object_tree_path_changed(changed, tree, ptree, path, repo);
137 done:
138 if (tree)
139 got_object_tree_close(tree);
140 if (ptree)
141 got_object_tree_close(ptree);
142 if (pcommit)
143 got_object_commit_close(pcommit);
144 return err;
147 static void
148 add_node_to_iter_list(struct got_commit_graph *graph,
149 struct got_commit_graph_node *node, time_t committer_time)
151 struct got_commit_graph_node *n, *next;
153 node->timestamp = committer_time;
155 n = TAILQ_FIRST(&graph->iter_list);
156 while (n) {
157 next = TAILQ_NEXT(n, entry);
158 if (next && node->timestamp >= next->timestamp) {
159 TAILQ_INSERT_BEFORE(next, node, entry);
160 return;
162 n = next;
164 TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
167 static const struct got_error *
168 add_node(struct got_commit_graph_node **new_node,
169 struct got_commit_graph *graph, struct got_object_id *commit_id,
170 struct got_repository *repo)
172 const struct got_error *err = NULL;
173 struct got_commit_graph_node *node;
175 *new_node = NULL;
177 node = calloc(1, sizeof(*node));
178 if (node == NULL)
179 return got_error_from_errno("calloc");
181 memcpy(&node->id, commit_id, sizeof(node->id));
182 err = got_object_idset_add(graph->node_ids, &node->id, NULL);
183 if (err)
184 free(node);
185 else
186 *new_node = node;
187 return err;
190 /*
191 * Ask got-read-pack to traverse first-parent history until a commit is
192 * encountered which modified graph->path, or until the pack file runs
193 * out of relevant commits. This is faster than sending an individual
194 * request for each commit stored in the pack file.
195 */
196 static const struct got_error *
197 packed_first_parent_traversal(int *ncommits_traversed,
198 struct got_commit_graph *graph, struct got_object_id *commit_id,
199 struct got_repository *repo)
201 const struct got_error *err = NULL;
202 struct got_object_id_queue traversed_commits;
203 struct got_object_qid *qid;
205 STAILQ_INIT(&traversed_commits);
206 *ncommits_traversed = 0;
208 err = got_traverse_packed_commits(&traversed_commits,
209 commit_id, graph->path, repo);
210 if (err)
211 return err;
213 /* Add all traversed commits to the graph... */
214 STAILQ_FOREACH(qid, &traversed_commits, entry) {
215 struct got_commit_graph_node *node;
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 = add_node(&node, graph, qid->id, repo);
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;
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 struct got_object_id *merged_id, *prev_id = NULL;
293 int branches_differ = 0;
295 err = got_object_id_by_path(&merged_id, repo, commit,
296 graph->path);
297 if (err)
298 return err;
300 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
301 struct got_object_id *id = NULL;
302 struct got_commit_object *pcommit = NULL;
304 if (got_object_idset_contains(graph->open_branches,
305 qid->id))
306 continue;
308 err = got_object_open_as_commit(&pcommit, repo,
309 qid->id);
310 if (err) {
311 free(merged_id);
312 free(prev_id);
313 return err;
315 err = got_object_id_by_path(&id, repo, pcommit,
316 graph->path);
317 got_object_commit_close(pcommit);
318 pcommit = NULL;
319 if (err) {
320 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
321 branches_differ = 1;
322 continue;
324 free(merged_id);
325 free(prev_id);
326 return err;
329 if (prev_id) {
330 if (!branches_differ &&
331 got_object_id_cmp(id, prev_id) != 0)
332 branches_differ = 1;
333 free(prev_id);
335 prev_id = id;
337 /*
338 * If a branch has created the merged content we can
339 * skip any other branches.
340 */
341 if (got_object_id_cmp(merged_id, id) == 0) {
342 err = got_object_idset_add(graph->open_branches,
343 qid->id, NULL);
344 free(merged_id);
345 free(id);
346 return err;
350 free(prev_id);
351 prev_id = NULL;
352 free(merged_id);
353 merged_id = NULL;
355 /*
356 * If the path's content is the same on all branches,
357 * follow the first parent only.
358 */
359 if (!branches_differ) {
360 qid = STAILQ_FIRST(&commit->parent_ids);
361 if (qid == NULL)
362 return NULL;
363 if (got_object_idset_contains(graph->open_branches,
364 qid->id))
365 return NULL;
366 if (got_object_idset_contains(graph->node_ids,
367 qid->id))
368 return NULL; /* parent already traversed */
369 return got_object_idset_add(graph->open_branches,
370 qid->id, NULL);
374 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
375 if (got_object_idset_contains(graph->open_branches, qid->id))
376 continue;
377 if (got_object_idset_contains(graph->node_ids, qid->id))
378 continue; /* parent already traversed */
379 err = got_object_idset_add(graph->open_branches, qid->id, NULL);
380 if (err)
381 return err;
384 return NULL;
387 const struct got_error *
388 got_commit_graph_open(struct got_commit_graph **graph,
389 const char *path, int first_parent_traversal)
391 const struct got_error *err = NULL;
393 *graph = calloc(1, sizeof(**graph));
394 if (*graph == NULL)
395 return got_error_from_errno("calloc");
397 TAILQ_INIT(&(*graph)->iter_list);
399 (*graph)->path = strdup(path);
400 if ((*graph)->path == NULL) {
401 err = got_error_from_errno("strdup");
402 goto done;
405 (*graph)->node_ids = got_object_idset_alloc();
406 if ((*graph)->node_ids == NULL) {
407 err = got_error_from_errno("got_object_idset_alloc");
408 goto done;
411 (*graph)->open_branches = got_object_idset_alloc();
412 if ((*graph)->open_branches == NULL) {
413 err = got_error_from_errno("got_object_idset_alloc");
414 goto done;
417 if (first_parent_traversal)
418 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
419 done:
420 if (err) {
421 got_commit_graph_close(*graph);
422 *graph = NULL;
424 return err;
427 struct add_branch_tip_arg {
428 struct got_commit_graph_branch_tip *tips;
429 int ntips;
430 struct got_repository *repo;
431 struct got_commit_graph *graph;
432 };
434 static const struct got_error *
435 add_branch_tip(struct got_object_id *commit_id, void *data, void *arg)
437 const struct got_error *err;
438 struct add_branch_tip_arg *a = arg;
439 struct got_commit_graph_node *new_node;
440 struct got_commit_object *commit;
442 err = got_object_open_as_commit(&commit, a->repo, commit_id);
443 if (err)
444 return err;
446 err = add_node(&new_node, a->graph, commit_id, a->repo);
447 if (err)
448 return err;
450 a->tips[a->ntips].commit_id = new_node ? &new_node->id : NULL;
451 a->tips[a->ntips].commit = commit;
452 a->tips[a->ntips].new_node = new_node;
453 a->ntips++;
455 return NULL;
458 static const struct got_error *
459 fetch_commits_from_open_branches(struct got_commit_graph *graph,
460 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
462 const struct got_error *err;
463 struct add_branch_tip_arg arg;
464 int i, ntips;
466 ntips = got_object_idset_num_elements(graph->open_branches);
467 if (ntips == 0)
468 return NULL;
470 /* (Re-)allocate branch tips array if necessary. */
471 if (graph->ntips < ntips) {
472 struct got_commit_graph_branch_tip *tips;
473 tips = recallocarray(graph->tips, graph->ntips, ntips,
474 sizeof(*tips));
475 if (tips == NULL)
476 return got_error_from_errno("recallocarray");
477 graph->tips = tips;
478 graph->ntips = ntips;
480 arg.tips = graph->tips;
481 arg.ntips = 0; /* add_branch_tip() will increment */
482 arg.repo = repo;
483 arg.graph = graph;
484 err = got_object_idset_for_each(graph->open_branches, add_branch_tip,
485 &arg);
486 if (err)
487 goto done;
489 for (i = 0; i < arg.ntips; i++) {
490 struct got_object_id *commit_id;
491 struct got_commit_object *commit;
492 struct got_commit_graph_node *new_node;
493 int changed;
495 if (cancel_cb) {
496 err = (*cancel_cb)(cancel_arg);
497 if (err)
498 break;
501 commit_id = arg.tips[i].commit_id;
502 commit = arg.tips[i].commit;
503 new_node = arg.tips[i].new_node;
505 err = detect_changed_path(&changed, commit, commit_id,
506 graph->path, repo);
507 if (err) {
508 if (err->code != GOT_ERR_NO_OBJ)
509 break;
510 /*
511 * History of the path stops here on the current
512 * branch. Keep going on other branches.
513 */
514 err = close_branch(graph, commit_id);
515 if (err)
516 break;
517 continue;
519 if (changed)
520 add_node_to_iter_list(graph, new_node,
521 got_object_commit_get_committer_time(commit));
522 err = advance_branch(graph, commit_id, commit, repo);
523 if (err)
524 break;
526 done:
527 for (i = 0; i < arg.ntips; i++)
528 got_object_commit_close(arg.tips[i].commit);
529 return err;
532 void
533 got_commit_graph_close(struct got_commit_graph *graph)
535 if (graph->open_branches)
536 got_object_idset_free(graph->open_branches);
537 if (graph->node_ids)
538 got_object_idset_free(graph->node_ids);
539 free(graph->tips);
540 free(graph->path);
541 free(graph);
544 const struct got_error *
545 got_commit_graph_iter_start(struct got_commit_graph *graph,
546 struct got_object_id *id, struct got_repository *repo,
547 got_cancel_cb cancel_cb, void *cancel_arg)
549 const struct got_error *err = NULL;
551 if (!TAILQ_EMPTY(&graph->iter_list))
552 return got_error(GOT_ERR_ITER_BUSY);
554 err = got_object_idset_add(graph->open_branches, id, NULL);
555 if (err)
556 return err;
558 /* Locate first commit which changed graph->path. */
559 while (TAILQ_EMPTY(&graph->iter_list) &&
560 got_object_idset_num_elements(graph->open_branches) > 0) {
561 err = fetch_commits_from_open_branches(graph, repo,
562 cancel_cb, cancel_arg);
563 if (err)
564 return err;
567 if (TAILQ_EMPTY(&graph->iter_list)) {
568 const char *path;
569 if (got_path_is_root_dir(graph->path))
570 return got_error_no_obj(id);
571 path = graph->path;
572 while (path[0] == '/')
573 path++;
574 return got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
577 return NULL;
580 const struct got_error *
581 got_commit_graph_iter_next(struct got_object_id **id,
582 struct got_commit_graph *graph, struct got_repository *repo,
583 got_cancel_cb cancel_cb, void *cancel_arg)
585 const struct got_error *err = NULL;
586 struct got_commit_graph_node *node;
588 *id = NULL;
590 node = TAILQ_FIRST(&graph->iter_list);
591 if (node == NULL) {
592 /* We are done iterating, or iteration was not started. */
593 return got_error(GOT_ERR_ITER_COMPLETED);
596 while (TAILQ_NEXT(node, entry) == NULL &&
597 got_object_idset_num_elements(graph->open_branches) > 0) {
598 err = fetch_commits_from_open_branches(graph, repo,
599 cancel_cb, cancel_arg);
600 if (err)
601 return err;
604 *id = &node->id;
605 TAILQ_REMOVE(&graph->iter_list, node, entry);
606 return NULL;
609 const struct got_error *
610 got_commit_graph_find_youngest_common_ancestor(struct got_object_id **yca_id,
611 struct got_object_id *commit_id, struct got_object_id *commit_id2,
612 int first_parent_traversal,
613 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
615 const struct got_error *err = NULL;
616 struct got_commit_graph *graph = NULL, *graph2 = NULL;
617 int completed = 0, completed2 = 0;
618 struct got_object_idset *commit_ids;
620 *yca_id = NULL;
622 commit_ids = got_object_idset_alloc();
623 if (commit_ids == NULL)
624 return got_error_from_errno("got_object_idset_alloc");
626 err = got_commit_graph_open(&graph, "/", first_parent_traversal);
627 if (err)
628 goto done;
630 err = got_commit_graph_open(&graph2, "/", first_parent_traversal);
631 if (err)
632 goto done;
634 err = got_commit_graph_iter_start(graph, commit_id, repo,
635 cancel_cb, cancel_arg);
636 if (err)
637 goto done;
639 err = got_commit_graph_iter_start(graph2, commit_id2, repo,
640 cancel_cb, cancel_arg);
641 if (err)
642 goto done;
644 for (;;) {
645 struct got_object_id *id = NULL, *id2 = NULL;
647 if (cancel_cb) {
648 err = (*cancel_cb)(cancel_arg);
649 if (err)
650 break;
653 if (!completed) {
654 err = got_commit_graph_iter_next(&id, graph, repo,
655 cancel_cb, cancel_arg);
656 if (err) {
657 if (err->code != GOT_ERR_ITER_COMPLETED)
658 break;
659 err = NULL;
660 completed = 1;
664 if (!completed2) {
665 err = got_commit_graph_iter_next(&id2, graph2, repo,
666 cancel_cb, cancel_arg);
667 if (err) {
668 if (err->code != GOT_ERR_ITER_COMPLETED)
669 break;
670 err = NULL;
671 completed2 = 1;
675 if (id) {
676 if (got_object_idset_contains(commit_ids, id)) {
677 *yca_id = got_object_id_dup(id);
678 if (*yca_id)
679 break;
680 err = got_error_from_errno("got_object_id_dup");
681 break;
684 err = got_object_idset_add(commit_ids, id, NULL);
685 if (err)
686 break;
688 if (id2) {
689 if (got_object_idset_contains(commit_ids, id2)) {
690 *yca_id = got_object_id_dup(id2);
691 if (*yca_id)
692 break;
693 err = got_error_from_errno("got_object_id_dup");
694 break;
697 err = got_object_idset_add(commit_ids, id2, NULL);
698 if (err)
699 break;
702 if (completed && completed2) {
703 err = got_error(GOT_ERR_ANCESTRY);
704 break;
708 done:
709 got_object_idset_free(commit_ids);
710 if (graph)
711 got_commit_graph_close(graph);
712 if (graph2)
713 got_commit_graph_close(graph2);
714 return err;