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 static int
147 is_merge_point(struct got_commit_graph_node *node)
149 return node->nparents > 1;
152 int
153 is_branch_point(struct got_commit_graph_node *node)
155 return node->nchildren > 1;
158 static int
159 is_root_node(struct got_commit_graph_node *node)
161 return node->nparents == 0;
163 #endif
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_TAIL(&graph->iter_list, node, entry);
226 return;
229 /* Ensure that an iteration in progress will see this new commit. */
230 if (graph->iter_node) {
231 n = graph->iter_node;
232 while (n) {
233 next = TAILQ_NEXT(n, entry);
234 if (next &&
235 node->commit_timestamp >= next->commit_timestamp) {
236 TAILQ_INSERT_BEFORE(next, node, entry);
237 return;
239 n = next;
241 TAILQ_INSERT_AFTER(&graph->iter_list, graph->iter_node,
242 node, entry);
243 return;
246 /*
247 * If a child node is known, begin looping over the list there
248 * instead of beginning from the list head.
249 */
250 n = child_node ? child_node : TAILQ_FIRST(&graph->iter_list);
252 /* Insert into list based on committer timestamp. */
253 do {
254 if (node->commit_timestamp == n->commit_timestamp) {
255 TAILQ_INSERT_AFTER(&graph->iter_list, n, node, entry);
256 break;
257 } else if (node->commit_timestamp < n->commit_timestamp) {
258 next = TAILQ_NEXT(n, entry);
259 if (next == NULL) {
260 TAILQ_INSERT_AFTER(&graph->iter_list, n,
261 node, entry);
262 break;
264 if (node->commit_timestamp >= next->commit_timestamp) {
265 TAILQ_INSERT_BEFORE(next, node, entry);
266 break;
268 } else {
269 TAILQ_INSERT_BEFORE(n, node, entry);
270 break;
272 n = TAILQ_NEXT(n, entry);
273 } while (n);
276 static const struct got_error *
277 add_vertex(struct got_object_id_queue *ids, struct got_object_id *id)
279 struct got_object_qid *qid;
281 qid = calloc(1, sizeof(*qid));
282 if (qid == NULL)
283 return got_error_from_errno();
285 qid->id = got_object_id_dup(id);
286 if (qid->id == NULL) {
287 const struct got_error *err = got_error_from_errno();
288 got_object_qid_free(qid);
289 return err;
292 SIMPLEQ_INSERT_TAIL(ids, qid, entry);
293 return NULL;
296 static const struct got_error *
297 close_branch(struct got_commit_graph *graph, struct got_object_id *commit_id)
299 const struct got_error *err;
301 err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
302 if (err && err->code != GOT_ERR_NO_OBJ)
303 return err;
304 return NULL;
307 static const struct got_error *
308 advance_branch(struct got_commit_graph *graph,
309 struct got_commit_graph_node *node,
310 struct got_object_id *commit_id, struct got_commit_object *commit)
312 const struct got_error *err;
313 struct got_object_qid *qid;
315 err = close_branch(graph, commit_id);
316 if (err)
317 return err;
319 if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
320 qid = SIMPLEQ_FIRST(&commit->parent_ids);
321 if (qid == NULL)
322 return NULL;
323 err = got_object_idset_add(NULL, graph->open_branches, qid->id,
324 node);
325 if (err && err->code != GOT_ERR_OBJ_EXISTS)
326 return err;
327 return NULL;
330 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
331 if (got_object_idset_get(graph->node_ids, qid->id))
332 continue; /* parent already traversed */
333 err = got_object_idset_add(NULL, graph->open_branches,
334 qid->id, node);
335 if (err && err->code != GOT_ERR_OBJ_EXISTS)
336 return err;
339 return NULL;
342 static void
343 free_node(struct got_commit_graph_node *node)
345 while (!SIMPLEQ_EMPTY(&node->parent_ids)) {
346 struct got_object_qid *pid = SIMPLEQ_FIRST(&node->parent_ids);
347 SIMPLEQ_REMOVE_HEAD(&node->parent_ids, entry);
348 got_object_qid_free(pid);
350 free(node);
353 static const struct got_error *
354 add_node(struct got_commit_graph_node **new_node,
355 struct got_commit_graph *graph, struct got_object_id *commit_id,
356 struct got_commit_object *commit, struct got_commit_graph_node *child_node,
357 struct got_repository *repo)
359 const struct got_error *err = NULL;
360 struct got_commit_graph_node *node;
361 struct got_object_qid *pid;
362 int changed = 0, branch_done = 0;
364 *new_node = NULL;
366 node = calloc(1, sizeof(*node));
367 if (node == NULL)
368 return got_error_from_errno();
370 memcpy(&node->id, commit_id, sizeof(node->id));
371 SIMPLEQ_INIT(&node->parent_ids);
372 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
373 err = add_vertex(&node->parent_ids, pid->id);
374 if (err) {
375 free_node(node);
376 return err;
378 node->nparents++;
380 node->commit_timestamp = mktime(&commit->tm_committer);
381 if (node->commit_timestamp == -1) {
382 free_node(node);
383 return got_error_from_errno();
386 err = got_object_idset_add(NULL, graph->node_ids, &node->id, node);
387 if (err) {
388 if (err->code == GOT_ERR_OBJ_EXISTS)
389 err = NULL;
390 free_node(node);
391 return err;
394 err = detect_changed_path(&changed, commit, commit_id, graph->path,
395 repo);
396 if (err) {
397 if (err->code == GOT_ERR_NO_OBJ) {
398 /*
399 * History of the path stops here on the current
400 * branch. Keep going on other branches.
401 */
402 err = NULL;
403 branch_done = 1;
404 } else {
405 free_node(node);
406 return err;
410 if (changed)
411 add_node_to_iter_list(graph, node, child_node);
413 if (branch_done)
414 err = close_branch(graph, commit_id);
415 else
416 err = advance_branch(graph, node, commit_id, commit);
417 if (err)
418 free_node(node);
419 else
420 *new_node = node;
422 return err;
425 const struct got_error *
426 got_commit_graph_open(struct got_commit_graph **graph,
427 struct got_object_id *commit_id, const char *path,
428 int first_parent_traversal, struct got_repository *repo)
430 const struct got_error *err = NULL;
431 struct got_commit_object *commit;
433 *graph = NULL;
435 err = got_object_open_as_commit(&commit, repo, commit_id);
436 if (err)
437 return err;
439 /* The path must exist in our initial commit. */
440 if (!got_path_is_root_dir(path)) {
441 struct got_object_id *obj_id;
442 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
443 if (err)
444 return err;
445 free(obj_id);
448 *graph = alloc_graph(path);
449 if (*graph == NULL) {
450 got_object_commit_close(commit);
451 return got_error_from_errno();
454 if (first_parent_traversal)
455 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
457 err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL,
458 repo);
459 got_object_commit_close(commit);
460 if (err) {
461 got_commit_graph_close(*graph);
462 *graph = NULL;
463 return err;
466 return NULL;
469 struct gather_branch_tips_arg {
470 struct got_commit_graph_branch_tip *tips;
471 int ntips;
472 };
474 static void
475 gather_branch_tips(struct got_object_id *id, void *data, void *arg)
477 struct gather_branch_tips_arg *a = arg;
478 memcpy(&a->tips[a->ntips].id, id, sizeof(*id));
479 a->tips[a->ntips].node = data;
480 a->ntips++;
483 static const struct got_error *
484 fetch_commits_from_open_branches(int *ncommits, int *wanted_id_added,
485 struct got_object_id **changed_id, struct got_commit_graph *graph,
486 struct got_repository *repo, struct got_object_id *wanted_id)
488 const struct got_error *err;
489 struct gather_branch_tips_arg arg;
490 int i;
492 *ncommits = 0;
493 if (wanted_id_added)
494 *wanted_id_added = 0;
495 if (changed_id)
496 *changed_id = NULL;
498 arg.ntips = got_object_idset_num_elements(graph->open_branches);
499 if (arg.ntips == 0)
500 return NULL;
502 /*
503 * Adding nodes to the graph might change the graph's open
504 * branches state. Create a local copy of the current state.
505 */
506 if (graph->ntips < arg.ntips) {
507 struct got_commit_graph_branch_tip *tips;
508 if (arg.ntips < GOT_COMMIT_GRAPH_MIN_TIPS)
509 arg.ntips = GOT_COMMIT_GRAPH_MIN_TIPS;
510 tips = reallocarray(graph->tips, arg.ntips, sizeof(*tips));
511 if (tips == NULL)
512 return got_error_from_errno();
513 graph->tips = tips;
514 graph->ntips = arg.ntips;
516 arg.ntips = 0; /* reset; gather_branch_tips() will increment */
517 arg.tips = graph->tips;
518 got_object_idset_for_each(graph->open_branches,
519 gather_branch_tips, &arg);
521 for (i = 0; i < arg.ntips; i++) {
522 struct got_object_id *commit_id;
523 struct got_commit_graph_node *child_node, *new_node;
524 struct got_commit_object *commit;
525 int changed;
527 commit_id = &graph->tips[i].id;
528 child_node = graph->tips[i].node;
530 err = got_object_open_as_commit(&commit, repo, commit_id);
531 if (err)
532 break;
534 err = detect_changed_path(&changed, commit, commit_id,
535 graph->path, repo);
536 if (err) {
537 got_object_commit_close(commit);
538 if (err->code != GOT_ERR_NO_OBJ)
539 break;
540 err = close_branch(graph, commit_id);
541 if (err)
542 break;
543 continue;
545 if (changed && changed_id && *changed_id == NULL)
546 *changed_id = commit_id;
547 err = add_node(&new_node, graph, commit_id, commit, child_node,
548 repo);
549 got_object_commit_close(commit);
550 if (err)
551 break;
552 if (new_node)
553 (*ncommits)++;
554 if (wanted_id && got_object_id_cmp(commit_id, wanted_id) == 0)
555 *wanted_id_added = 1;
558 return err;
561 const struct got_error *
562 got_commit_graph_fetch_commits(struct got_commit_graph *graph, int limit,
563 struct got_repository *repo)
565 const struct got_error *err;
566 int nfetched = 0, ncommits;
567 struct got_object_id *changed_id = NULL;
569 while (nfetched < limit) {
570 err = fetch_commits_from_open_branches(&ncommits, NULL,
571 &changed_id, graph, repo, NULL);
572 if (err)
573 return err;
574 if (ncommits == 0)
575 break;
576 if (changed_id)
577 nfetched += ncommits;
580 return NULL;
583 const struct got_error *
584 got_commit_graph_fetch_commits_up_to(int *nfetched,
585 struct got_commit_graph *graph, struct got_object_id *wanted_id,
586 struct got_repository *repo)
588 const struct got_error *err;
589 int ncommits, wanted_id_added = 0;
591 *nfetched = 0;
593 if (got_object_idset_get(graph->node_ids, wanted_id) != NULL)
594 return NULL;
596 while (!wanted_id_added) {
597 err = fetch_commits_from_open_branches(&ncommits,
598 &wanted_id_added, NULL, graph, repo, wanted_id);
599 if (err)
600 return err;
601 if (ncommits == 0)
602 return NULL;
603 *nfetched += ncommits;
606 return NULL;
609 static void
610 free_node_iter(struct got_object_id *id, void *data, void *arg)
612 struct got_commit_graph_node *node = data;
613 free_node(node);
616 void
617 got_commit_graph_close(struct got_commit_graph *graph)
619 got_object_idset_free(graph->open_branches);
620 got_object_idset_for_each(graph->node_ids, free_node_iter, NULL);
621 got_object_idset_free(graph->node_ids);
622 free(graph->tips);
623 free(graph->path);
624 free(graph);
627 const struct got_error *
628 got_commit_graph_iter_start(struct got_commit_graph *graph,
629 struct got_object_id *id, struct got_repository *repo)
631 const struct got_error *err = NULL;
632 struct got_commit_graph_node *start_node;
633 struct got_commit_object *commit;
634 int changed;
636 start_node = got_object_idset_get(graph->node_ids, id);
637 if (start_node == NULL)
638 return got_error(GOT_ERR_NO_OBJ);
641 err = got_object_open_as_commit(&commit, repo, &start_node->id);
642 if (err)
643 return err;
645 err = detect_changed_path(&changed, commit, &start_node->id,
646 graph->path, repo);
647 if (err) {
648 got_object_commit_close(commit);
649 return err;
652 if (!changed) {
653 /* Locate first commit which changed graph->path. */
654 struct got_object_id *changed_id = NULL;
655 while (changed_id == NULL) {
656 int ncommits;
657 err = fetch_commits_from_open_branches(&ncommits, NULL,
658 &changed_id, graph, repo, NULL);
659 if (err) {
660 got_object_commit_close(commit);
661 return err;
664 start_node = got_object_idset_get(graph->node_ids, changed_id);
666 got_object_commit_close(commit);
668 graph->iter_node = start_node;
669 return NULL;
672 const struct got_error *
673 got_commit_graph_iter_next(struct got_object_id **id,
674 struct got_commit_graph *graph)
676 *id = NULL;
678 if (graph->iter_node == NULL) {
679 /* We are done interating, or iteration was not started. */
680 return got_error(GOT_ERR_ITER_COMPLETED);
683 if (graph->iter_node ==
684 TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
685 got_object_idset_num_elements(graph->open_branches) == 0) {
686 *id = &graph->iter_node->id;
687 /* We are done interating. */
688 graph->iter_node = NULL;
689 return NULL;
692 if (TAILQ_NEXT(graph->iter_node, entry) == NULL)
693 return got_error(GOT_ERR_ITER_NEED_MORE);
695 *id = &graph->iter_node->id;
696 graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);
697 return NULL;