Blob


1 /*
2 * Copyright (c) 2018 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;
43 time_t timestamp;
45 /* Used during graph iteration. */
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 int changed;
56 int branch_done;
57 };
59 struct got_commit_graph {
60 /* The set of all commits we have traversed. */
61 struct got_object_idset *node_ids;
63 int flags;
64 #define GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL 0x01
66 /*
67 * A set of object IDs of known parent commits which we have not yet
68 * traversed. Each commit ID in this set represents a branch in commit
69 * history: Either the first-parent branch of the head node, or another
70 * branch corresponding to a traversed merge commit for which we have
71 * not traversed a branch point commit yet.
72 *
73 * Whenever we add a commit with a matching ID to the graph, we remove
74 * its corresponding element from this set, and add new elements for
75 * each of that commit's parent commits which were not traversed yet.
76 *
77 * When API users ask us to fetch more commits, we fetch commits from
78 * all currently open branches. This allows API users to process
79 * commits in linear order even though the history contains branches.
80 */
81 struct got_object_idset *open_branches;
83 /* Array of branch tips for fetch_commits_from_open_branches(). */
84 struct got_commit_graph_branch_tip *tips;
85 int ntips;
87 /* Path of tree entry of interest to the API user. */
88 char *path;
90 /* The next commit to return when the API user asks for one. */
91 struct got_commit_graph_node *iter_node;
93 /* The graph iteration list contains all nodes in sorted order. */
94 struct got_commit_graph_iter_list iter_list;
95 };
97 static struct got_commit_graph *
98 alloc_graph(const char *path)
99 {
100 struct got_commit_graph *graph;
102 graph = calloc(1, sizeof(*graph));
103 if (graph == NULL)
104 return NULL;
106 graph->path = strdup(path);
107 if (graph->path == NULL) {
108 free(graph);
109 return NULL;
112 graph->node_ids = got_object_idset_alloc();
113 if (graph->node_ids == NULL) {
114 free(graph->path);
115 free(graph);
116 return NULL;
119 graph->open_branches = got_object_idset_alloc();
120 if (graph->open_branches == NULL) {
121 got_object_idset_free(graph->node_ids);
122 free(graph->path);
123 free(graph);
124 return NULL;
127 TAILQ_INIT(&graph->iter_list);
128 return graph;
131 static const struct got_error *
132 detect_changed_path(int *changed, struct got_commit_object *commit,
133 struct got_object_id *commit_id, const char *path,
134 struct got_repository *repo)
136 const struct got_error *err = NULL;
137 struct got_commit_object *pcommit = NULL;
138 struct got_tree_object *tree = NULL, *ptree = NULL;
139 struct got_object_qid *pid;
141 if (got_path_is_root_dir(path)) {
142 *changed = 1;
143 return NULL;
146 *changed = 0;
148 pid = SIMPLEQ_FIRST(&commit->parent_ids);
149 if (pid == NULL) {
150 struct got_object_id *obj_id;
151 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
152 if (err) {
153 if (err->code == GOT_ERR_NO_TREE_ENTRY)
154 err = NULL;
155 } else
156 *changed = 1; /* The path was created in this commit. */
157 free(obj_id);
158 return err;
161 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
162 if (err)
163 return err;
165 err = got_object_open_as_commit(&pcommit, repo, pid->id);
166 if (err)
167 goto done;
169 err = got_object_open_as_tree(&ptree, repo, pcommit->tree_id);
170 if (err)
171 goto done;
173 err = got_object_tree_path_changed(changed, tree, ptree, path, repo);
174 done:
175 if (tree)
176 got_object_tree_close(tree);
177 if (ptree)
178 got_object_tree_close(ptree);
179 if (pcommit)
180 got_object_commit_close(pcommit);
181 return err;
184 static void
185 add_node_to_iter_list(struct got_commit_graph *graph,
186 struct got_commit_graph_node *node,
187 struct got_commit_graph_node *child_node)
189 struct got_commit_graph_node *n, *next;
191 if (TAILQ_EMPTY(&graph->iter_list)) {
192 TAILQ_INSERT_HEAD(&graph->iter_list, node, entry);
193 graph->iter_node = node;
194 return;
197 n = graph->iter_node;
198 /* Ensure that an iteration in progress will see this new commit. */
199 while (n) {
200 next = TAILQ_NEXT(n, entry);
201 if (next && node->timestamp >= next->timestamp) {
202 TAILQ_INSERT_BEFORE(next, node, entry);
203 return;
205 n = next;
207 TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
210 static const struct got_error *
211 close_branch(struct got_commit_graph *graph, struct got_object_id *commit_id)
213 const struct got_error *err;
215 err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
216 if (err && err->code != GOT_ERR_NO_OBJ)
217 return err;
218 return NULL;
221 static const struct got_error *
222 advance_branch(struct got_commit_graph *graph,
223 struct got_commit_graph_node *node,
224 struct got_object_id *commit_id, struct got_commit_object *commit,
225 struct got_repository *repo)
227 const struct got_error *err;
228 struct got_object_qid *qid;
230 err = close_branch(graph, commit_id);
231 if (err)
232 return err;
234 if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
235 qid = SIMPLEQ_FIRST(&commit->parent_ids);
236 if (qid == NULL ||
237 got_object_idset_get(graph->open_branches, qid->id))
238 return NULL;
239 return got_object_idset_add(graph->open_branches,
240 qid->id, node);
243 /*
244 * If we are graphing commits for a specific path, skip branches
245 * which do not contribute any content to this path.
246 */
247 if (commit->nparents > 1 && !got_path_is_root_dir(graph->path)) {
248 struct got_object_id *merged_id, *prev_id = NULL;
249 int branches_differ = 0;
251 err = got_object_id_by_path(&merged_id, repo, commit_id,
252 graph->path);
253 if (err)
254 return err;
256 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
257 struct got_object_id *id;
259 if (got_object_idset_get(graph->open_branches, qid->id))
260 continue;
262 err = got_object_id_by_path(&id, repo, qid->id,
263 graph->path);
264 if (err) {
265 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
266 branches_differ = 1;
267 continue;
269 free(merged_id);
270 free(prev_id);
271 return err;
274 if (prev_id) {
275 if (!branches_differ &&
276 got_object_id_cmp(id, prev_id) != 0)
277 branches_differ = 1;
278 free(prev_id);
280 prev_id = id;
282 /*
283 * If a branch has created the merged content we can
284 * skip any other branches.
285 */
286 if (got_object_id_cmp(merged_id, id) == 0) {
287 err = got_object_idset_add(graph->open_branches,
288 qid->id, node);
289 free(merged_id);
290 free(id);
291 return err;
295 free(prev_id);
296 prev_id = NULL;
297 free(merged_id);
298 merged_id = NULL;
300 /*
301 * If the path's content is the same on all branches,
302 * follow the first parent only.
303 */
304 if (!branches_differ) {
305 qid = SIMPLEQ_FIRST(&commit->parent_ids);
306 if (qid == NULL)
307 return NULL;
308 if (got_object_idset_get(graph->open_branches, qid->id))
309 return NULL;
310 if (got_object_idset_get(graph->node_ids, qid->id))
311 return NULL; /* parent already traversed */
312 return got_object_idset_add(graph->open_branches,
313 qid->id, node);
317 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
318 if (got_object_idset_get(graph->open_branches, qid->id))
319 continue;
320 if (got_object_idset_get(graph->node_ids, qid->id))
321 continue; /* parent already traversed */
322 err = got_object_idset_add(graph->open_branches, qid->id, node);
323 if (err)
324 return err;
327 return NULL;
330 static const struct got_error *
331 add_node(struct got_commit_graph_node **new_node, int *changed,
332 int *branch_done, struct got_commit_graph *graph,
333 struct got_object_id *commit_id, struct got_commit_object *commit,
334 struct got_commit_graph_node *child_node, struct got_repository *repo)
336 const struct got_error *err = NULL;
337 struct got_commit_graph_node *node;
339 *new_node = NULL;
340 *changed = 0;
341 *branch_done = 0;
343 node = calloc(1, sizeof(*node));
344 if (node == NULL)
345 return got_error_from_errno("calloc");
347 memcpy(&node->id, commit_id, sizeof(node->id));
348 node->timestamp = commit->committer_time;
350 err = got_object_idset_add(graph->node_ids, &node->id, node);
351 if (err) {
352 free(node);
353 return err;
356 err = detect_changed_path(changed, commit, commit_id, graph->path,
357 repo);
358 if (err) {
359 if (err->code == GOT_ERR_NO_OBJ) {
360 /*
361 * History of the path stops here on the current
362 * branch. Keep going on other branches.
363 */
364 err = NULL;
365 *branch_done = 1;
366 } else {
367 got_object_idset_remove(NULL, graph->node_ids,
368 &node->id);
369 free(node);
370 return err;
374 if (*changed)
375 add_node_to_iter_list(graph, node, child_node);
376 *new_node = node;
377 return NULL;
380 const struct got_error *
381 got_commit_graph_open(struct got_commit_graph **graph,
382 struct got_object_id *commit_id, const char *path,
383 int first_parent_traversal, struct got_repository *repo)
385 const struct got_error *err = NULL;
386 struct got_commit_object *commit;
387 int changed, branch_done;
388 struct got_commit_graph_node *node;
390 *graph = NULL;
392 err = got_object_open_as_commit(&commit, repo, commit_id);
393 if (err)
394 return err;
396 /* The path must exist in our initial commit. */
397 if (!got_path_is_root_dir(path)) {
398 struct got_object_id *obj_id;
399 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
400 if (err)
401 return err;
402 free(obj_id);
405 *graph = alloc_graph(path);
406 if (*graph == NULL) {
407 err = got_error_from_errno("alloc_graph");
408 got_object_commit_close(commit);
409 return err;
412 if (first_parent_traversal)
413 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
415 err = add_node(&node, &changed, &branch_done, *graph,
416 commit_id, commit, NULL, repo);
417 if (err == NULL) {
418 err = advance_branch(*graph, node, commit_id,
419 commit, repo);
421 got_object_commit_close(commit);
422 if (err) {
423 got_commit_graph_close(*graph);
424 *graph = NULL;
425 return err;
428 return NULL;
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 got_commit_graph_node *child_node = data;
443 struct add_branch_tip_arg *a = arg;
444 struct got_commit_graph_node *new_node;
445 struct got_commit_object *commit;
446 int changed, branch_done;
448 err = got_object_open_as_commit(&commit, a->repo, commit_id);
449 if (err)
450 return err;
452 err = add_node(&new_node, &changed, &branch_done, a->graph,
453 commit_id, commit, child_node, a->repo);
454 if (err)
455 return err;
457 a->tips[a->ntips].commit_id = new_node ? &new_node->id : NULL;
458 a->tips[a->ntips].commit = commit;
459 a->tips[a->ntips].new_node = new_node;
460 a->tips[a->ntips].changed = changed;
461 a->tips[a->ntips].branch_done = branch_done;
462 a->ntips++;
464 return NULL;
467 static const struct got_error *
468 fetch_commits_from_open_branches(int *nfetched,
469 struct got_object_id **changed_id, struct got_commit_graph *graph,
470 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
472 const struct got_error *err;
473 struct add_branch_tip_arg arg;
474 int i, ntips;
476 *nfetched = 0;
477 if (changed_id)
478 *changed_id = NULL;
480 ntips = got_object_idset_num_elements(graph->open_branches);
481 if (ntips == 0)
482 return NULL;
484 /* (Re-)allocate branch tips array if necessary. */
485 if (graph->ntips < ntips) {
486 struct got_commit_graph_branch_tip *tips;
487 tips = recallocarray(graph->tips, graph->ntips, ntips,
488 sizeof(*tips));
489 if (tips == NULL)
490 return got_error_from_errno("recallocarray");
491 graph->tips = tips;
492 graph->ntips = ntips;
494 arg.tips = graph->tips;
495 arg.ntips = 0; /* add_branch_tip() will increment */
496 arg.repo = repo;
497 arg.graph = graph;
498 err = got_object_idset_for_each(graph->open_branches, add_branch_tip,
499 &arg);
500 if (err)
501 goto done;
503 for (i = 0; i < arg.ntips; i++) {
504 struct got_object_id *commit_id;
505 struct got_commit_object *commit;
506 struct got_commit_graph_node *new_node;
507 int branch_done, changed;
509 if (cancel_cb) {
510 err = (*cancel_cb)(cancel_arg);
511 if (err)
512 break;
515 commit_id = arg.tips[i].commit_id;
516 commit = arg.tips[i].commit;
517 new_node = arg.tips[i].new_node;
518 branch_done = arg.tips[i].branch_done;
519 changed = arg.tips[i].changed;
521 if (branch_done)
522 err = close_branch(graph, commit_id);
523 else
524 err = advance_branch(graph, new_node, commit_id,
525 commit, repo);
526 if (err)
527 break;
528 if (changed && changed_id && *changed_id == NULL)
529 *changed_id = commit_id;
531 done:
532 for (i = 0; i < arg.ntips; i++)
533 got_object_commit_close(arg.tips[i].commit);
534 (*nfetched) = arg.ntips;
535 return err;
538 static const struct got_error *
539 free_node_iter(struct got_object_id *id, void *data, void *arg)
541 struct got_commit_graph_node *node = data;
542 free(node);
543 return NULL;
546 void
547 got_commit_graph_close(struct got_commit_graph *graph)
549 got_object_idset_free(graph->open_branches);
550 got_object_idset_for_each(graph->node_ids, free_node_iter, NULL);
551 got_object_idset_free(graph->node_ids);
552 free(graph->tips);
553 free(graph->path);
554 free(graph);
557 const struct got_error *
558 got_commit_graph_iter_start(struct got_commit_graph *graph,
559 struct got_object_id *id, struct got_repository *repo,
560 got_cancel_cb cancel_cb, void *cancel_arg)
562 const struct got_error *err = NULL;
563 struct got_commit_graph_node *start_node;
564 struct got_commit_object *commit;
565 int changed;
567 start_node = got_object_idset_get(graph->node_ids, id);
568 while (start_node == NULL) {
569 int ncommits;
570 err = fetch_commits_from_open_branches(&ncommits, NULL, graph,
571 repo, cancel_cb, cancel_arg);
572 if (err)
573 return err;
574 if (ncommits == 0)
575 return got_error_no_obj(id);
576 start_node = got_object_idset_get(graph->node_ids, id);
579 err = got_object_open_as_commit(&commit, repo, &start_node->id);
580 if (err)
581 return err;
583 err = detect_changed_path(&changed, commit, &start_node->id,
584 graph->path, repo);
585 if (err) {
586 got_object_commit_close(commit);
587 return err;
590 if (!changed) {
591 /* Locate first commit which changed graph->path. */
592 struct got_object_id *changed_id = NULL;
593 while (changed_id == NULL) {
594 int ncommits;
595 err = fetch_commits_from_open_branches(&ncommits,
596 &changed_id, graph, repo, cancel_cb, cancel_arg);
597 if (err) {
598 got_object_commit_close(commit);
599 return err;
602 start_node = got_object_idset_get(graph->node_ids, changed_id);
604 got_object_commit_close(commit);
606 graph->iter_node = start_node;
607 return NULL;
610 const struct got_error *
611 got_commit_graph_iter_next(struct got_object_id **id,
612 struct got_commit_graph *graph, struct got_repository *repo,
613 got_cancel_cb cancel_cb, void *cancel_arg)
615 const struct got_error *err = NULL;
617 *id = NULL;
619 if (graph->iter_node == NULL) {
620 /* We are done iterating, or iteration was not started. */
621 return got_error(GOT_ERR_ITER_COMPLETED);
624 if (graph->iter_node ==
625 TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
626 got_object_idset_num_elements(graph->open_branches) == 0) {
627 /* We are done iterating. */
628 *id = &graph->iter_node->id;
629 graph->iter_node = NULL;
630 return NULL;
633 while (TAILQ_NEXT(graph->iter_node, entry) == NULL &&
634 got_object_idset_num_elements(graph->open_branches) > 0) {
635 int ncommits;
636 struct got_object_id *changed_id;
637 err = fetch_commits_from_open_branches(&ncommits,
638 &changed_id, graph, repo, cancel_cb, cancel_arg);
639 if (err)
640 return err;
643 *id = &graph->iter_node->id;
644 graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);
645 return NULL;
648 const struct got_error *
649 got_commit_graph_find_youngest_common_ancestor(struct got_object_id **yca_id,
650 struct got_object_id *commit_id, struct got_object_id *commit_id2,
651 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
653 const struct got_error *err = NULL;
654 struct got_commit_graph *graph = NULL, *graph2 = NULL;
655 int completed = 0, completed2 = 0;
656 struct got_object_idset *commit_ids;
658 *yca_id = NULL;
660 commit_ids = got_object_idset_alloc();
661 if (commit_ids == NULL)
662 return got_error_from_errno("got_object_idset_alloc");
664 err = got_commit_graph_open(&graph, commit_id, "/", 1, repo);
665 if (err)
666 goto done;
668 err = got_commit_graph_open(&graph2, commit_id2, "/", 1, repo);
669 if (err)
670 goto done;
672 err = got_commit_graph_iter_start(graph, commit_id, repo,
673 cancel_cb, cancel_arg);
674 if (err)
675 goto done;
677 err = got_commit_graph_iter_start(graph2, commit_id2, repo,
678 cancel_cb, cancel_arg);
679 if (err)
680 goto done;
682 for (;;) {
683 struct got_object_id *id = NULL, *id2 = NULL;
685 if (cancel_cb) {
686 err = (*cancel_cb)(cancel_arg);
687 if (err)
688 break;
691 if (!completed) {
692 err = got_commit_graph_iter_next(&id, graph, repo,
693 cancel_cb, cancel_arg);
694 if (err) {
695 if (err->code != GOT_ERR_ITER_COMPLETED)
696 break;
697 err = NULL;
698 completed = 1;
702 if (!completed2) {
703 err = got_commit_graph_iter_next(&id2, graph2, repo,
704 cancel_cb, cancel_arg);
705 if (err) {
706 if (err->code != GOT_ERR_ITER_COMPLETED)
707 break;
708 err = NULL;
709 completed2 = 1;
713 if (id) {
714 if (got_object_idset_get(commit_ids, id)) {
715 *yca_id = got_object_id_dup(id);
716 if (*yca_id)
717 break;
718 err = got_error_from_errno("got_object_id_dup");
719 break;
722 err = got_object_idset_add(commit_ids, id, id);
723 if (err)
724 break;
726 if (id2) {
727 if (got_object_idset_get(commit_ids, id2)) {
728 *yca_id = got_object_id_dup(id2);
729 if (*yca_id)
730 break;
731 err = got_error_from_errno("got_object_id_dup");
732 break;
735 err = got_object_idset_add(commit_ids, id2, id2);
736 if (err)
737 break;
740 if (completed && completed2) {
741 err = got_error(GOT_ERR_ANCESTRY);
742 break;
746 done:
747 got_object_idset_free(commit_ids);
748 if (graph)
749 got_commit_graph_close(graph);
750 if (graph2)
751 got_commit_graph_close(graph2);
752 return err;