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 10 /* 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 if (err->code == GOT_ERR_NO_OBJ)
192 err = NULL;
193 } else
194 *changed = 1; /* The path was created in this commit. */
195 free(obj_id);
196 } else {
197 err = got_object_open_as_commit(&pcommit, repo, pid->id);
198 if (err)
199 goto done;
201 err = got_object_open_as_tree(&ptree, repo, pcommit->tree_id);
202 if (err)
203 goto done;
205 err = got_object_tree_path_changed(changed, tree, ptree, path,
206 repo);
208 done:
209 if (tree)
210 got_object_tree_close(tree);
211 if (ptree)
212 got_object_tree_close(ptree);
213 if (pcommit)
214 got_object_commit_close(pcommit);
215 return err;
218 static void
219 add_node_to_iter_list(struct got_commit_graph *graph,
220 struct got_commit_graph_node *node,
221 struct got_commit_graph_node *child_node)
223 struct got_commit_graph_node *n, *next;
225 if (TAILQ_EMPTY(&graph->iter_list)) {
226 TAILQ_INSERT_HEAD(&graph->iter_list, node, entry);
227 graph->iter_node = node;
228 return;
231 n = graph->iter_node;
232 /* Ensure that an iteration in progress will see this new commit. */
233 while (n) {
234 next = TAILQ_NEXT(n, entry);
235 if (next && node->commit_timestamp >= next->commit_timestamp) {
236 TAILQ_INSERT_BEFORE(next, node, entry);
237 return;
239 n = next;
241 TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
244 static const struct got_error *
245 add_vertex(struct got_object_id_queue *ids, struct got_object_id *id)
247 struct got_object_qid *qid;
249 qid = calloc(1, sizeof(*qid));
250 if (qid == NULL)
251 return got_error_from_errno();
253 qid->id = got_object_id_dup(id);
254 if (qid->id == NULL) {
255 const struct got_error *err = got_error_from_errno();
256 got_object_qid_free(qid);
257 return err;
260 SIMPLEQ_INSERT_TAIL(ids, qid, entry);
261 return NULL;
264 static const struct got_error *
265 close_branch(struct got_commit_graph *graph, struct got_object_id *commit_id)
267 const struct got_error *err;
269 err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
270 if (err && err->code != GOT_ERR_NO_OBJ)
271 return err;
272 return NULL;
275 static const struct got_error *
276 advance_branch(struct got_commit_graph *graph,
277 struct got_commit_graph_node *node,
278 struct got_object_id *commit_id, struct got_commit_object *commit,
279 struct got_repository *repo)
281 const struct got_error *err;
282 struct got_object_qid *qid;
284 err = close_branch(graph, commit_id);
285 if (err)
286 return err;
288 if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
289 qid = SIMPLEQ_FIRST(&commit->parent_ids);
290 if (qid == NULL)
291 return NULL;
292 err = got_object_idset_add(graph->open_branches, qid->id, node);
293 return err;
296 /*
297 * If we are graphing commits for a specific path, skip branches
298 * which do not contribute any content to this path.
299 */
300 if (is_merge_point(node) && !got_path_is_root_dir(graph->path)) {
301 struct got_object_id *id, *merged_id, *prev_id = NULL;
302 int branches_differ = 0;
304 err = got_object_id_by_path(&merged_id, repo, commit_id,
305 graph->path);
306 if (err)
307 return err;
309 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
310 if (got_object_idset_get(graph->node_ids, qid->id))
311 continue; /* parent already traversed */
313 err = got_object_id_by_path(&id, repo, qid->id,
314 graph->path);
315 if (err) {
316 if (err->code == GOT_ERR_NO_OBJ) {
317 branches_differ = 1;
318 continue;
320 return err;
323 if (prev_id) {
324 if (!branches_differ &&
325 got_object_id_cmp(merged_id, prev_id) != 0)
326 branches_differ = 1;
327 } else
328 prev_id = id;
330 /*
331 * If a branch has created the merged content we can
332 * skip any other branches.
333 */
334 if (got_object_id_cmp(merged_id, id) == 0) {
335 err = got_object_idset_add(graph->open_branches,
336 qid->id, node);
337 return err;
341 /*
342 * If the path's content is the same on all branches,
343 * follow the first parent only.
344 */
345 if (!branches_differ) {
346 qid = SIMPLEQ_FIRST(&commit->parent_ids);
347 if (qid == NULL)
348 return NULL;
349 if (got_object_idset_get(graph->node_ids, qid->id))
350 return NULL; /* parent already traversed */
351 if (got_object_idset_get(graph->open_branches, qid->id))
352 return NULL;
353 return got_object_idset_add(graph->open_branches,
354 qid->id, node);
358 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
359 if (got_object_idset_get(graph->node_ids, qid->id))
360 continue; /* parent already traversed */
361 if (got_object_idset_get(graph->open_branches, qid->id))
362 continue;
363 err = got_object_idset_add(graph->open_branches, qid->id, node);
364 if (err)
365 return err;
368 return NULL;
371 static void
372 free_node(struct got_commit_graph_node *node)
374 while (!SIMPLEQ_EMPTY(&node->parent_ids)) {
375 struct got_object_qid *pid = SIMPLEQ_FIRST(&node->parent_ids);
376 SIMPLEQ_REMOVE_HEAD(&node->parent_ids, entry);
377 got_object_qid_free(pid);
379 free(node);
382 static const struct got_error *
383 add_node(struct got_commit_graph_node **new_node,
384 struct got_commit_graph *graph, struct got_object_id *commit_id,
385 struct got_commit_object *commit, struct got_commit_graph_node *child_node,
386 struct got_repository *repo)
388 const struct got_error *err = NULL;
389 struct got_commit_graph_node *node;
390 struct got_object_qid *pid;
391 int changed = 0, branch_done = 0;
393 *new_node = NULL;
395 node = calloc(1, sizeof(*node));
396 if (node == NULL)
397 return got_error_from_errno();
399 memcpy(&node->id, commit_id, sizeof(node->id));
400 SIMPLEQ_INIT(&node->parent_ids);
401 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
402 err = add_vertex(&node->parent_ids, pid->id);
403 if (err) {
404 free_node(node);
405 return err;
407 node->nparents++;
410 err = got_object_idset_add(graph->node_ids, &node->id, node);
411 if (err) {
412 free_node(node);
413 return err;
416 err = detect_changed_path(&changed, commit, commit_id, graph->path,
417 repo);
418 if (err) {
419 if (err->code == GOT_ERR_NO_OBJ) {
420 /*
421 * History of the path stops here on the current
422 * branch. Keep going on other branches.
423 */
424 err = NULL;
425 branch_done = 1;
426 } else {
427 free_node(node);
428 return err;
432 node->commit_timestamp = mktime(&commit->tm_committer);
433 if (node->commit_timestamp == -1) {
434 free_node(node);
435 return got_error_from_errno();
438 if (changed)
439 add_node_to_iter_list(graph, node, child_node);
441 if (branch_done)
442 err = close_branch(graph, commit_id);
443 else
444 err = advance_branch(graph, node, commit_id, commit, repo);
445 if (err)
446 free_node(node);
447 else
448 *new_node = node;
450 return err;
453 const struct got_error *
454 got_commit_graph_open(struct got_commit_graph **graph,
455 struct got_object_id *commit_id, const char *path,
456 int first_parent_traversal, struct got_repository *repo)
458 const struct got_error *err = NULL;
459 struct got_commit_object *commit;
461 *graph = NULL;
463 err = got_object_open_as_commit(&commit, repo, commit_id);
464 if (err)
465 return err;
467 /* The path must exist in our initial commit. */
468 if (!got_path_is_root_dir(path)) {
469 struct got_object_id *obj_id;
470 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
471 if (err)
472 return err;
473 free(obj_id);
476 *graph = alloc_graph(path);
477 if (*graph == NULL) {
478 got_object_commit_close(commit);
479 return got_error_from_errno();
482 if (first_parent_traversal)
483 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
485 err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL,
486 repo);
487 got_object_commit_close(commit);
488 if (err) {
489 got_commit_graph_close(*graph);
490 *graph = NULL;
491 return err;
494 return NULL;
497 struct gather_branch_tips_arg {
498 struct got_commit_graph_branch_tip *tips;
499 int ntips;
500 };
502 static void
503 gather_branch_tips(struct got_object_id *id, void *data, void *arg)
505 struct gather_branch_tips_arg *a = arg;
506 memcpy(&a->tips[a->ntips].id, id, sizeof(*id));
507 a->tips[a->ntips].node = data;
508 a->ntips++;
511 static const struct got_error *
512 fetch_commits_from_open_branches(int *ncommits,
513 struct got_object_id **changed_id, struct got_commit_graph *graph,
514 struct got_repository *repo)
516 const struct got_error *err;
517 struct gather_branch_tips_arg arg;
518 int i;
520 *ncommits = 0;
521 if (changed_id)
522 *changed_id = NULL;
524 arg.ntips = got_object_idset_num_elements(graph->open_branches);
525 if (arg.ntips == 0)
526 return NULL;
528 /*
529 * Adding nodes to the graph might change the graph's open
530 * branches state. Create a local copy of the current state.
531 */
532 if (graph->ntips < arg.ntips) {
533 struct got_commit_graph_branch_tip *tips;
534 if (arg.ntips < GOT_COMMIT_GRAPH_MIN_TIPS)
535 arg.ntips = GOT_COMMIT_GRAPH_MIN_TIPS;
536 tips = reallocarray(graph->tips, arg.ntips, sizeof(*tips));
537 if (tips == NULL)
538 return got_error_from_errno();
539 graph->tips = tips;
540 graph->ntips = arg.ntips;
542 arg.ntips = 0; /* reset; gather_branch_tips() will increment */
543 arg.tips = graph->tips;
544 got_object_idset_for_each(graph->open_branches,
545 gather_branch_tips, &arg);
547 for (i = 0; i < arg.ntips; i++) {
548 struct got_object_id *commit_id;
549 struct got_commit_graph_node *child_node, *new_node;
550 struct got_commit_object *commit;
551 int changed;
553 commit_id = &graph->tips[i].id;
554 child_node = graph->tips[i].node;
556 err = got_object_open_as_commit(&commit, repo, commit_id);
557 if (err)
558 break;
560 err = detect_changed_path(&changed, commit, commit_id,
561 graph->path, repo);
562 if (err) {
563 got_object_commit_close(commit);
564 if (err->code != GOT_ERR_NO_OBJ)
565 break;
566 err = close_branch(graph, commit_id);
567 if (err)
568 break;
569 continue;
571 if (changed && changed_id && *changed_id == NULL)
572 *changed_id = commit_id;
573 err = add_node(&new_node, graph, commit_id, commit, child_node,
574 repo);
575 got_object_commit_close(commit);
576 if (err)
577 break;
578 if (new_node)
579 (*ncommits)++;
582 return err;
585 const struct got_error *
586 got_commit_graph_fetch_commits(struct got_commit_graph *graph, int limit,
587 struct got_repository *repo)
589 const struct got_error *err;
590 int nfetched = 0, ncommits;
591 struct got_object_id *changed_id = NULL;
593 while (nfetched < limit) {
594 err = fetch_commits_from_open_branches(&ncommits,
595 &changed_id, graph, repo);
596 if (err)
597 return err;
598 if (ncommits == 0)
599 break;
600 if (changed_id)
601 nfetched += ncommits;
604 return NULL;
607 static void
608 free_node_iter(struct got_object_id *id, void *data, void *arg)
610 struct got_commit_graph_node *node = data;
611 free_node(node);
614 void
615 got_commit_graph_close(struct got_commit_graph *graph)
617 got_object_idset_free(graph->open_branches);
618 got_object_idset_for_each(graph->node_ids, free_node_iter, NULL);
619 got_object_idset_free(graph->node_ids);
620 free(graph->tips);
621 free(graph->path);
622 free(graph);
625 const struct got_error *
626 got_commit_graph_iter_start(struct got_commit_graph *graph,
627 struct got_object_id *id, struct got_repository *repo)
629 const struct got_error *err = NULL;
630 struct got_commit_graph_node *start_node;
631 struct got_commit_object *commit;
632 int changed;
634 start_node = got_object_idset_get(graph->node_ids, id);
635 if (start_node == NULL)
636 return got_error(GOT_ERR_NO_OBJ);
638 err = got_object_open_as_commit(&commit, repo, &start_node->id);
639 if (err)
640 return err;
642 err = detect_changed_path(&changed, commit, &start_node->id,
643 graph->path, repo);
644 if (err) {
645 got_object_commit_close(commit);
646 return err;
649 if (!changed) {
650 /* Locate first commit which changed graph->path. */
651 struct got_object_id *changed_id = NULL;
652 while (changed_id == NULL) {
653 int ncommits;
654 err = fetch_commits_from_open_branches(&ncommits,
655 &changed_id, graph, repo);
656 if (err) {
657 got_object_commit_close(commit);
658 return err;
661 start_node = got_object_idset_get(graph->node_ids, changed_id);
663 got_object_commit_close(commit);
665 graph->iter_node = start_node;
666 return NULL;
669 const struct got_error *
670 got_commit_graph_iter_next(struct got_object_id **id,
671 struct got_commit_graph *graph)
673 *id = NULL;
675 if (graph->iter_node == NULL) {
676 /* We are done iterating, or iteration was not started. */
677 return got_error(GOT_ERR_ITER_COMPLETED);
680 if (graph->iter_node ==
681 TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
682 got_object_idset_num_elements(graph->open_branches) == 0) {
683 /* We are done iterating. */
684 *id = &graph->iter_node->id;
685 graph->iter_node = NULL;
686 return NULL;
689 if (TAILQ_NEXT(graph->iter_node, entry) == NULL)
690 return got_error(GOT_ERR_ITER_NEED_MORE);
692 *id = &graph->iter_node->id;
693 graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);
694 return NULL;