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 <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sha1.h>
26 #include <zlib.h>
27 #include <ctype.h>
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_commit_graph.h"
33 #include "got_lib_delta.h"
34 #include "got_lib_inflate.h"
35 #include "got_lib_object.h"
36 #include "got_lib_object_idset.h"
37 #include "got_lib_path.h"
39 struct got_commit_graph_node {
40 struct got_object_id id;
42 /*
43 * Each graph node corresponds to a commit object.
44 * Graph vertices are modelled with an adjacency list.
45 * Adjacencies of a graph node are parent (older) commits.
46 */
47 int nparents;
48 struct got_object_id_queue parent_ids;
50 time_t commit_timestamp;
52 /* Used during graph iteration. */
53 TAILQ_ENTRY(got_commit_graph_node) entry;
54 };
56 TAILQ_HEAD(got_commit_graph_iter_list, got_commit_graph_node);
58 struct got_commit_graph_branch_tip {
59 struct got_object_id id;
60 struct got_commit_graph_node *node;
61 };
63 struct got_commit_graph {
64 /* The set of all commits we have traversed. */
65 struct got_object_idset *node_ids;
67 /* The commit at which traversal began (youngest commit in node_ids). */
68 struct got_commit_graph_node *head_node;
70 int flags;
71 #define GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL 0x01
73 /*
74 * A set of object IDs of known parent commits which we have not yet
75 * traversed. Each commit ID in this set represents a branch in commit
76 * history: Either the first-parent branch of the head node, or another
77 * branch corresponding to a traversed merge commit for which we have
78 * not traversed a branch point commit yet.
79 *
80 * Whenever we add a commit with a matching ID to the graph, we remove
81 * its corresponding element from this set, and add new elements for
82 * each of that commit's parent commits which were not traversed yet.
83 *
84 * When API users ask us to fetch more commits, we fetch commits from
85 * all currently open branches. This allows API users to process
86 * commits in linear order even though the history contains branches.
87 */
88 struct got_object_idset *open_branches;
90 /* Copy of known branch tips for fetch_commits_from_open_branches(). */
91 struct got_commit_graph_branch_tip *tips;
92 size_t ntips;
93 #define GOT_COMMIT_GRAPH_MIN_TIPS 100 /* minimum amount of tips to allocate */
95 /* Path of tree entry of interest to the API user. */
96 char *path;
98 /* The next commit to return when the API user asks for one. */
99 struct got_commit_graph_node *iter_node;
101 /* The graph iteration list contains all nodes in sorted order. */
102 struct got_commit_graph_iter_list iter_list;
103 };
105 static struct got_commit_graph *
106 alloc_graph(const char *path)
108 struct got_commit_graph *graph;
110 graph = calloc(1, sizeof(*graph));
111 if (graph == NULL)
112 return NULL;
114 graph->path = strdup(path);
115 if (graph->path == NULL) {
116 free(graph);
117 return NULL;
120 graph->node_ids = got_object_idset_alloc();
121 if (graph->node_ids == NULL) {
122 free(graph->path);
123 free(graph);
124 return NULL;
127 graph->open_branches = got_object_idset_alloc();
128 if (graph->open_branches == NULL) {
129 got_object_idset_free(graph->node_ids);
130 free(graph->path);
131 free(graph);
132 return NULL;
135 TAILQ_INIT(&graph->iter_list);
136 return graph;
139 #if 0
140 static int
141 is_head_node(struct got_commit_graph_node *node)
143 return node->nchildren == 0;
146 int
147 is_branch_point(struct got_commit_graph_node *node)
149 return node->nchildren > 1;
152 static int
153 is_root_node(struct got_commit_graph_node *node)
155 return node->nparents == 0;
157 #endif
159 static int
160 is_merge_point(struct got_commit_graph_node *node)
162 return node->nparents > 1;
165 static const struct got_error *
166 detect_changed_path(int *changed, struct got_commit_object *commit,
167 struct got_object_id *commit_id, const char *path,
168 struct got_repository *repo)
170 const struct got_error *err = NULL;
171 struct got_commit_object *pcommit = NULL;
172 struct got_tree_object *tree = NULL, *ptree = NULL;
173 struct got_object_qid *pid;
175 if (got_path_is_root_dir(path)) {
176 *changed = 1;
177 return NULL;
180 *changed = 0;
182 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
183 if (err)
184 return err;
186 pid = SIMPLEQ_FIRST(&commit->parent_ids);
187 if (pid == NULL) {
188 struct got_object_id *obj_id;
189 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
190 if (err)
191 err = NULL;
192 else
193 *changed = 1;
194 free(obj_id);
195 goto done;
198 err = got_object_open_as_commit(&pcommit, repo, pid->id);
199 if (err)
200 goto done;
202 err = got_object_open_as_tree(&ptree, repo, pcommit->tree_id);
203 if (err)
204 goto done;
206 err = got_object_tree_path_changed(changed, tree, ptree, path, repo);
207 done:
208 if (tree)
209 got_object_tree_close(tree);
210 if (ptree)
211 got_object_tree_close(ptree);
212 if (pcommit)
213 got_object_commit_close(pcommit);
214 return err;
217 static void
218 add_node_to_iter_list(struct got_commit_graph *graph,
219 struct got_commit_graph_node *node,
220 struct got_commit_graph_node *child_node)
222 struct got_commit_graph_node *n, *next;
224 if (TAILQ_EMPTY(&graph->iter_list)) {
225 TAILQ_INSERT_HEAD(&graph->iter_list, node, entry);
226 graph->iter_node = node;
227 return;
230 n = graph->iter_node;
231 /* Ensure that an iteration in progress will see this new commit. */
232 while (n) {
233 next = TAILQ_NEXT(n, entry);
234 if (next && node->commit_timestamp >= next->commit_timestamp) {
235 TAILQ_INSERT_BEFORE(next, node, entry);
236 return;
238 n = next;
240 TAILQ_INSERT_AFTER(&graph->iter_list, graph->iter_node, node, entry);
243 static const struct got_error *
244 add_vertex(struct got_object_id_queue *ids, struct got_object_id *id)
246 struct got_object_qid *qid;
248 qid = calloc(1, sizeof(*qid));
249 if (qid == NULL)
250 return got_error_from_errno();
252 qid->id = got_object_id_dup(id);
253 if (qid->id == NULL) {
254 const struct got_error *err = got_error_from_errno();
255 got_object_qid_free(qid);
256 return err;
259 SIMPLEQ_INSERT_TAIL(ids, qid, entry);
260 return NULL;
263 static const struct got_error *
264 close_branch(struct got_commit_graph *graph, struct got_object_id *commit_id)
266 const struct got_error *err;
268 err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
269 if (err && err->code != GOT_ERR_NO_OBJ)
270 return err;
271 return NULL;
274 static const struct got_error *
275 advance_branch(struct got_commit_graph *graph,
276 struct got_commit_graph_node *node,
277 struct got_object_id *commit_id, struct got_commit_object *commit,
278 struct got_repository *repo)
280 const struct got_error *err;
281 struct got_object_qid *qid;
283 err = close_branch(graph, commit_id);
284 if (err)
285 return err;
287 if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
288 qid = SIMPLEQ_FIRST(&commit->parent_ids);
289 if (qid == NULL)
290 return NULL;
291 err = got_object_idset_add(NULL, graph->open_branches, qid->id,
292 node);
293 if (err && err->code != GOT_ERR_OBJ_EXISTS)
294 return err;
295 return NULL;
298 /*
299 * If we are graphing commits for a specific path, skip branches
300 * which do not contribute any content to this path.
301 */
302 if (is_merge_point(node) && !got_path_is_root_dir(graph->path)) {
303 struct got_object_id *id, *merged_id, *prev_id = NULL;
304 int branches_differ = 0;
305 #if 0
306 int n = 1;
307 #endif
309 err = got_object_id_by_path(&merged_id, repo, commit_id,
310 graph->path);
311 if (err)
312 return err;
314 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
315 if (got_object_idset_get(graph->node_ids, qid->id))
316 continue; /* parent already traversed */
318 err = got_object_id_by_path(&id, repo, qid->id,
319 graph->path);
320 if (err) {
321 if (err->code == GOT_ERR_NO_OBJ) {
322 branches_differ = 1;
323 continue;
325 return err;
328 if (prev_id) {
329 if (!branches_differ &&
330 got_object_id_cmp(merged_id, prev_id) != 0)
331 branches_differ = 1;
332 } else
333 prev_id = id;
335 /*
336 * If a branch has created the merged content we can
337 * skip any other branches.
338 */
339 if (got_object_id_cmp(merged_id, id) == 0) {
340 err = got_object_idset_add(NULL,
341 graph->open_branches, qid->id, node);
342 if (err && err->code != GOT_ERR_OBJ_EXISTS)
343 return err;
344 return NULL;
348 /*
349 * If the path's content is the same on all branches,
350 * follow the first parent only.
351 */
352 if (!branches_differ) {
353 qid = SIMPLEQ_FIRST(&commit->parent_ids);
354 if (qid == NULL)
355 return NULL;
356 if (got_object_idset_get(graph->node_ids, qid->id))
357 return NULL; /* parent already traversed */
358 err = got_object_idset_add(NULL,
359 graph->open_branches, qid->id, node);
360 if (err && err->code != GOT_ERR_OBJ_EXISTS)
361 return err;
362 return NULL;
366 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
367 if (got_object_idset_get(graph->node_ids, qid->id))
368 continue; /* parent already traversed */
369 err = got_object_idset_add(NULL, graph->open_branches,
370 qid->id, node);
371 if (err && err->code != GOT_ERR_OBJ_EXISTS)
372 return err;
375 return NULL;
378 static void
379 free_node(struct got_commit_graph_node *node)
381 while (!SIMPLEQ_EMPTY(&node->parent_ids)) {
382 struct got_object_qid *pid = SIMPLEQ_FIRST(&node->parent_ids);
383 SIMPLEQ_REMOVE_HEAD(&node->parent_ids, entry);
384 got_object_qid_free(pid);
386 free(node);
389 static const struct got_error *
390 add_node(struct got_commit_graph_node **new_node,
391 struct got_commit_graph *graph, struct got_object_id *commit_id,
392 struct got_commit_object *commit, struct got_commit_graph_node *child_node,
393 struct got_repository *repo)
395 const struct got_error *err = NULL;
396 struct got_commit_graph_node *node;
397 struct got_object_qid *pid;
398 int changed = 0, branch_done = 0;
400 *new_node = NULL;
402 node = calloc(1, sizeof(*node));
403 if (node == NULL)
404 return got_error_from_errno();
406 memcpy(&node->id, commit_id, sizeof(node->id));
407 SIMPLEQ_INIT(&node->parent_ids);
408 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
409 err = add_vertex(&node->parent_ids, pid->id);
410 if (err) {
411 free_node(node);
412 return err;
414 node->nparents++;
417 err = got_object_idset_add(NULL, graph->node_ids, &node->id, node);
418 if (err) {
419 if (err->code == GOT_ERR_OBJ_EXISTS)
420 err = NULL;
421 free_node(node);
422 return err;
425 err = detect_changed_path(&changed, commit, commit_id, graph->path,
426 repo);
427 if (err) {
428 if (err->code == GOT_ERR_NO_OBJ) {
429 /*
430 * History of the path stops here on the current
431 * branch. Keep going on other branches.
432 */
433 err = NULL;
434 branch_done = 1;
435 } else {
436 free_node(node);
437 return err;
441 node->commit_timestamp = mktime(&commit->tm_committer);
442 if (node->commit_timestamp == -1) {
443 free_node(node);
444 return got_error_from_errno();
447 if (changed && !is_merge_point(node))
448 add_node_to_iter_list(graph, node, child_node);
450 if (branch_done)
451 err = close_branch(graph, commit_id);
452 else
453 err = advance_branch(graph, node, commit_id, commit, repo);
454 if (err)
455 free_node(node);
456 else
457 *new_node = node;
459 return err;
462 const struct got_error *
463 got_commit_graph_open(struct got_commit_graph **graph,
464 struct got_object_id *commit_id, const char *path,
465 int first_parent_traversal, struct got_repository *repo)
467 const struct got_error *err = NULL;
468 struct got_commit_object *commit;
470 *graph = NULL;
472 err = got_object_open_as_commit(&commit, repo, commit_id);
473 if (err)
474 return err;
476 /* The path must exist in our initial commit. */
477 if (!got_path_is_root_dir(path)) {
478 struct got_object_id *obj_id;
479 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
480 if (err)
481 return err;
482 free(obj_id);
485 *graph = alloc_graph(path);
486 if (*graph == NULL) {
487 got_object_commit_close(commit);
488 return got_error_from_errno();
491 if (first_parent_traversal)
492 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
494 err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL,
495 repo);
496 got_object_commit_close(commit);
497 if (err) {
498 got_commit_graph_close(*graph);
499 *graph = NULL;
500 return err;
503 return NULL;
506 struct gather_branch_tips_arg {
507 struct got_commit_graph_branch_tip *tips;
508 int ntips;
509 };
511 static void
512 gather_branch_tips(struct got_object_id *id, void *data, void *arg)
514 struct gather_branch_tips_arg *a = arg;
515 memcpy(&a->tips[a->ntips].id, id, sizeof(*id));
516 a->tips[a->ntips].node = data;
517 a->ntips++;
520 static const struct got_error *
521 fetch_commits_from_open_branches(int *ncommits, int *wanted_id_added,
522 struct got_object_id **changed_id, struct got_commit_graph *graph,
523 struct got_repository *repo, struct got_object_id *wanted_id)
525 const struct got_error *err;
526 struct gather_branch_tips_arg arg;
527 int i;
529 *ncommits = 0;
530 if (wanted_id_added)
531 *wanted_id_added = 0;
532 if (changed_id)
533 *changed_id = NULL;
535 arg.ntips = got_object_idset_num_elements(graph->open_branches);
536 if (arg.ntips == 0)
537 return NULL;
539 /*
540 * Adding nodes to the graph might change the graph's open
541 * branches state. Create a local copy of the current state.
542 */
543 if (graph->ntips < arg.ntips) {
544 struct got_commit_graph_branch_tip *tips;
545 if (arg.ntips < GOT_COMMIT_GRAPH_MIN_TIPS)
546 arg.ntips = GOT_COMMIT_GRAPH_MIN_TIPS;
547 tips = reallocarray(graph->tips, arg.ntips, sizeof(*tips));
548 if (tips == NULL)
549 return got_error_from_errno();
550 graph->tips = tips;
551 graph->ntips = arg.ntips;
553 arg.ntips = 0; /* reset; gather_branch_tips() will increment */
554 arg.tips = graph->tips;
555 got_object_idset_for_each(graph->open_branches,
556 gather_branch_tips, &arg);
558 for (i = 0; i < arg.ntips; i++) {
559 struct got_object_id *commit_id;
560 struct got_commit_graph_node *child_node, *new_node;
561 struct got_commit_object *commit;
562 int changed;
564 commit_id = &graph->tips[i].id;
565 child_node = graph->tips[i].node;
567 err = got_object_open_as_commit(&commit, repo, commit_id);
568 if (err)
569 break;
571 err = detect_changed_path(&changed, commit, commit_id,
572 graph->path, repo);
573 if (err) {
574 got_object_commit_close(commit);
575 if (err->code != GOT_ERR_NO_OBJ)
576 break;
577 err = close_branch(graph, commit_id);
578 if (err)
579 break;
580 continue;
582 if (changed && changed_id && *changed_id == NULL)
583 *changed_id = commit_id;
584 err = add_node(&new_node, graph, commit_id, commit, child_node,
585 repo);
586 got_object_commit_close(commit);
587 if (err)
588 break;
589 if (new_node)
590 (*ncommits)++;
591 if (wanted_id && got_object_id_cmp(commit_id, wanted_id) == 0)
592 *wanted_id_added = 1;
595 return err;
598 const struct got_error *
599 got_commit_graph_fetch_commits(struct got_commit_graph *graph, int limit,
600 struct got_repository *repo)
602 const struct got_error *err;
603 int nfetched = 0, ncommits;
604 struct got_object_id *changed_id = NULL;
606 while (nfetched < limit) {
607 err = fetch_commits_from_open_branches(&ncommits, NULL,
608 &changed_id, graph, repo, NULL);
609 if (err)
610 return err;
611 if (ncommits == 0)
612 break;
613 if (changed_id)
614 nfetched += ncommits;
617 return NULL;
620 const struct got_error *
621 got_commit_graph_fetch_commits_up_to(int *nfetched,
622 struct got_commit_graph *graph, struct got_object_id *wanted_id,
623 struct got_repository *repo)
625 const struct got_error *err;
626 int ncommits, wanted_id_added = 0;
628 *nfetched = 0;
630 if (got_object_idset_get(graph->node_ids, wanted_id) != NULL)
631 return NULL;
633 while (!wanted_id_added) {
634 err = fetch_commits_from_open_branches(&ncommits,
635 &wanted_id_added, NULL, graph, repo, wanted_id);
636 if (err)
637 return err;
638 if (ncommits == 0)
639 return NULL;
640 *nfetched += ncommits;
643 return NULL;
646 static void
647 free_node_iter(struct got_object_id *id, void *data, void *arg)
649 struct got_commit_graph_node *node = data;
650 free_node(node);
653 void
654 got_commit_graph_close(struct got_commit_graph *graph)
656 got_object_idset_free(graph->open_branches);
657 got_object_idset_for_each(graph->node_ids, free_node_iter, NULL);
658 got_object_idset_free(graph->node_ids);
659 free(graph->tips);
660 free(graph->path);
661 free(graph);
664 const struct got_error *
665 got_commit_graph_iter_start(struct got_commit_graph *graph,
666 struct got_object_id *id, struct got_repository *repo)
668 const struct got_error *err = NULL;
669 struct got_commit_graph_node *start_node;
670 struct got_commit_object *commit;
671 int changed;
673 start_node = got_object_idset_get(graph->node_ids, id);
674 if (start_node == NULL)
675 return got_error(GOT_ERR_NO_OBJ);
678 err = got_object_open_as_commit(&commit, repo, &start_node->id);
679 if (err)
680 return err;
682 err = detect_changed_path(&changed, commit, &start_node->id,
683 graph->path, repo);
684 if (err) {
685 got_object_commit_close(commit);
686 return err;
689 if (!changed) {
690 /* Locate first commit which changed graph->path. */
691 struct got_object_id *changed_id = NULL;
692 while (changed_id == NULL) {
693 int ncommits;
694 err = fetch_commits_from_open_branches(&ncommits, NULL,
695 &changed_id, graph, repo, NULL);
696 if (err) {
697 got_object_commit_close(commit);
698 return err;
701 start_node = got_object_idset_get(graph->node_ids, changed_id);
703 got_object_commit_close(commit);
705 graph->iter_node = start_node;
706 return NULL;
709 const struct got_error *
710 got_commit_graph_iter_next(struct got_object_id **id,
711 struct got_commit_graph *graph)
713 *id = NULL;
715 if (graph->iter_node == NULL) {
716 /* We are done iterating, or iteration was not started. */
717 return got_error(GOT_ERR_ITER_COMPLETED);
720 if (graph->iter_node ==
721 TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
722 got_object_idset_num_elements(graph->open_branches) == 0) {
723 /* We are done iterating. */
724 *id = &graph->iter_node->id;
725 graph->iter_node = NULL;
726 return NULL;
729 if (TAILQ_NEXT(graph->iter_node, entry) == NULL)
730 return got_error(GOT_ERR_ITER_NEED_MORE);
732 *id = &graph->iter_node->id;
733 graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);
734 return NULL;