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 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_TAIL(&graph->iter_list, 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;
306 err = got_object_id_by_path(&merged_id, repo, commit_id,
307 graph->path);
308 if (err)
309 return err;
311 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
312 if (got_object_idset_get(graph->node_ids, qid->id))
313 continue; /* parent already traversed */
315 err = got_object_id_by_path(&id, repo, qid->id,
316 graph->path);
317 if (err) {
318 if (err->code == GOT_ERR_NO_OBJ) {
319 branches_differ = 1;
320 continue;
322 return err;
325 if (prev_id) {
326 if (!branches_differ &&
327 got_object_id_cmp(merged_id, prev_id) != 0)
328 branches_differ = 1;
329 } else
330 prev_id = id;
332 /*
333 * If a branch has created the merged content we can
334 * skip any other branches.
335 */
336 if (got_object_id_cmp(merged_id, id) == 0) {
337 err = got_object_idset_add(NULL,
338 graph->open_branches, qid->id, node);
339 if (err && err->code != GOT_ERR_OBJ_EXISTS)
340 return err;
341 return NULL;
345 /*
346 * If the path's content is the same on all branches,
347 * follow the first parent only.
348 */
349 if (!branches_differ) {
350 qid = SIMPLEQ_FIRST(&commit->parent_ids);
351 if (qid == NULL)
352 return NULL;
353 if (got_object_idset_get(graph->node_ids, qid->id))
354 return NULL; /* parent already traversed */
355 err = got_object_idset_add(NULL,
356 graph->open_branches, qid->id, node);
357 if (err && err->code != GOT_ERR_OBJ_EXISTS)
358 return err;
359 return NULL;
363 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
364 if (got_object_idset_get(graph->node_ids, qid->id))
365 continue; /* parent already traversed */
366 err = got_object_idset_add(NULL, graph->open_branches,
367 qid->id, node);
368 if (err && err->code != GOT_ERR_OBJ_EXISTS)
369 return err;
372 return NULL;
375 static void
376 free_node(struct got_commit_graph_node *node)
378 while (!SIMPLEQ_EMPTY(&node->parent_ids)) {
379 struct got_object_qid *pid = SIMPLEQ_FIRST(&node->parent_ids);
380 SIMPLEQ_REMOVE_HEAD(&node->parent_ids, entry);
381 got_object_qid_free(pid);
383 free(node);
386 static const struct got_error *
387 add_node(struct got_commit_graph_node **new_node,
388 struct got_commit_graph *graph, struct got_object_id *commit_id,
389 struct got_commit_object *commit, struct got_commit_graph_node *child_node,
390 struct got_repository *repo)
392 const struct got_error *err = NULL;
393 struct got_commit_graph_node *node;
394 struct got_object_qid *pid;
395 int changed = 0, branch_done = 0;
397 *new_node = NULL;
399 node = calloc(1, sizeof(*node));
400 if (node == NULL)
401 return got_error_from_errno();
403 memcpy(&node->id, commit_id, sizeof(node->id));
404 SIMPLEQ_INIT(&node->parent_ids);
405 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
406 err = add_vertex(&node->parent_ids, pid->id);
407 if (err) {
408 free_node(node);
409 return err;
411 node->nparents++;
414 err = got_object_idset_add(NULL, graph->node_ids, &node->id, node);
415 if (err) {
416 if (err->code == GOT_ERR_OBJ_EXISTS)
417 err = NULL;
418 free_node(node);
419 return err;
422 err = detect_changed_path(&changed, commit, commit_id, graph->path,
423 repo);
424 if (err) {
425 if (err->code == GOT_ERR_NO_OBJ) {
426 /*
427 * History of the path stops here on the current
428 * branch. Keep going on other branches.
429 */
430 err = NULL;
431 branch_done = 1;
432 } else {
433 free_node(node);
434 return err;
438 node->commit_timestamp = mktime(&commit->tm_committer);
439 if (node->commit_timestamp == -1) {
440 free_node(node);
441 return got_error_from_errno();
444 if (changed)
445 add_node_to_iter_list(graph, node, child_node);
447 if (branch_done)
448 err = close_branch(graph, commit_id);
449 else
450 err = advance_branch(graph, node, commit_id, commit, repo);
451 if (err)
452 free_node(node);
453 else
454 *new_node = node;
456 return err;
459 const struct got_error *
460 got_commit_graph_open(struct got_commit_graph **graph,
461 struct got_object_id *commit_id, const char *path,
462 int first_parent_traversal, struct got_repository *repo)
464 const struct got_error *err = NULL;
465 struct got_commit_object *commit;
467 *graph = NULL;
469 err = got_object_open_as_commit(&commit, repo, commit_id);
470 if (err)
471 return err;
473 /* The path must exist in our initial commit. */
474 if (!got_path_is_root_dir(path)) {
475 struct got_object_id *obj_id;
476 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
477 if (err)
478 return err;
479 free(obj_id);
482 *graph = alloc_graph(path);
483 if (*graph == NULL) {
484 got_object_commit_close(commit);
485 return got_error_from_errno();
488 if (first_parent_traversal)
489 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
491 err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL,
492 repo);
493 got_object_commit_close(commit);
494 if (err) {
495 got_commit_graph_close(*graph);
496 *graph = NULL;
497 return err;
500 return NULL;
503 struct gather_branch_tips_arg {
504 struct got_commit_graph_branch_tip *tips;
505 int ntips;
506 };
508 static void
509 gather_branch_tips(struct got_object_id *id, void *data, void *arg)
511 struct gather_branch_tips_arg *a = arg;
512 memcpy(&a->tips[a->ntips].id, id, sizeof(*id));
513 a->tips[a->ntips].node = data;
514 a->ntips++;
517 static const struct got_error *
518 fetch_commits_from_open_branches(int *ncommits,
519 struct got_object_id **changed_id, struct got_commit_graph *graph,
520 struct got_repository *repo)
522 const struct got_error *err;
523 struct gather_branch_tips_arg arg;
524 int i;
526 *ncommits = 0;
527 if (changed_id)
528 *changed_id = NULL;
530 arg.ntips = got_object_idset_num_elements(graph->open_branches);
531 if (arg.ntips == 0)
532 return NULL;
534 /*
535 * Adding nodes to the graph might change the graph's open
536 * branches state. Create a local copy of the current state.
537 */
538 if (graph->ntips < arg.ntips) {
539 struct got_commit_graph_branch_tip *tips;
540 if (arg.ntips < GOT_COMMIT_GRAPH_MIN_TIPS)
541 arg.ntips = GOT_COMMIT_GRAPH_MIN_TIPS;
542 tips = reallocarray(graph->tips, arg.ntips, sizeof(*tips));
543 if (tips == NULL)
544 return got_error_from_errno();
545 graph->tips = tips;
546 graph->ntips = arg.ntips;
548 arg.ntips = 0; /* reset; gather_branch_tips() will increment */
549 arg.tips = graph->tips;
550 got_object_idset_for_each(graph->open_branches,
551 gather_branch_tips, &arg);
553 for (i = 0; i < arg.ntips; i++) {
554 struct got_object_id *commit_id;
555 struct got_commit_graph_node *child_node, *new_node;
556 struct got_commit_object *commit;
557 int changed;
559 commit_id = &graph->tips[i].id;
560 child_node = graph->tips[i].node;
562 err = got_object_open_as_commit(&commit, repo, commit_id);
563 if (err)
564 break;
566 err = detect_changed_path(&changed, commit, commit_id,
567 graph->path, repo);
568 if (err) {
569 got_object_commit_close(commit);
570 if (err->code != GOT_ERR_NO_OBJ)
571 break;
572 err = close_branch(graph, commit_id);
573 if (err)
574 break;
575 continue;
577 if (changed && changed_id && *changed_id == NULL)
578 *changed_id = commit_id;
579 err = add_node(&new_node, graph, commit_id, commit, child_node,
580 repo);
581 got_object_commit_close(commit);
582 if (err)
583 break;
584 if (new_node)
585 (*ncommits)++;
588 return err;
591 const struct got_error *
592 got_commit_graph_fetch_commits(struct got_commit_graph *graph, int limit,
593 struct got_repository *repo)
595 const struct got_error *err;
596 int nfetched = 0, ncommits;
597 struct got_object_id *changed_id = NULL;
599 while (nfetched < limit) {
600 err = fetch_commits_from_open_branches(&ncommits,
601 &changed_id, graph, repo);
602 if (err)
603 return err;
604 if (ncommits == 0)
605 break;
606 if (changed_id)
607 nfetched += ncommits;
610 return NULL;
613 static void
614 free_node_iter(struct got_object_id *id, void *data, void *arg)
616 struct got_commit_graph_node *node = data;
617 free_node(node);
620 void
621 got_commit_graph_close(struct got_commit_graph *graph)
623 got_object_idset_free(graph->open_branches);
624 got_object_idset_for_each(graph->node_ids, free_node_iter, NULL);
625 got_object_idset_free(graph->node_ids);
626 free(graph->tips);
627 free(graph->path);
628 free(graph);
631 const struct got_error *
632 got_commit_graph_iter_start(struct got_commit_graph *graph,
633 struct got_object_id *id, struct got_repository *repo)
635 const struct got_error *err = NULL;
636 struct got_commit_graph_node *start_node;
637 struct got_commit_object *commit;
638 int changed;
640 start_node = got_object_idset_get(graph->node_ids, id);
641 if (start_node == NULL)
642 return got_error(GOT_ERR_NO_OBJ);
644 err = got_object_open_as_commit(&commit, repo, &start_node->id);
645 if (err)
646 return err;
648 err = detect_changed_path(&changed, commit, &start_node->id,
649 graph->path, repo);
650 if (err) {
651 got_object_commit_close(commit);
652 return err;
655 if (!changed) {
656 /* Locate first commit which changed graph->path. */
657 struct got_object_id *changed_id = NULL;
658 while (changed_id == NULL) {
659 int ncommits;
660 err = fetch_commits_from_open_branches(&ncommits,
661 &changed_id, graph, repo);
662 if (err) {
663 got_object_commit_close(commit);
664 return err;
667 start_node = got_object_idset_get(graph->node_ids, changed_id);
669 got_object_commit_close(commit);
671 graph->iter_node = start_node;
672 return NULL;
675 const struct got_error *
676 got_commit_graph_iter_next(struct got_object_id **id,
677 struct got_commit_graph *graph)
679 *id = NULL;
681 if (graph->iter_node == NULL) {
682 /* We are done iterating, or iteration was not started. */
683 return got_error(GOT_ERR_ITER_COMPLETED);
686 if (graph->iter_node ==
687 TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
688 got_object_idset_num_elements(graph->open_branches) == 0) {
689 /* We are done iterating. */
690 *id = &graph->iter_node->id;
691 graph->iter_node = NULL;
692 return NULL;
695 if (TAILQ_NEXT(graph->iter_node, entry) == NULL)
696 return got_error(GOT_ERR_ITER_NEED_MORE);
698 *id = &graph->iter_node->id;
699 graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);
700 return NULL;