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, int *changed,
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 branch_done = 0;
393 *new_node = NULL;
394 *changed = 0;
396 node = calloc(1, sizeof(*node));
397 if (node == NULL)
398 return got_error_from_errno();
400 memcpy(&node->id, commit_id, sizeof(node->id));
401 SIMPLEQ_INIT(&node->parent_ids);
402 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
403 err = add_vertex(&node->parent_ids, pid->id);
404 if (err) {
405 free_node(node);
406 return err;
408 node->nparents++;
410 node->commit_timestamp = commit->committer_time;
412 err = got_object_idset_add(graph->node_ids, &node->id, node);
413 if (err) {
414 free_node(node);
415 return err;
418 err = detect_changed_path(changed, commit, commit_id, graph->path,
419 repo);
420 if (err) {
421 if (err->code == GOT_ERR_NO_OBJ) {
422 /*
423 * History of the path stops here on the current
424 * branch. Keep going on other branches.
425 */
426 err = NULL;
427 branch_done = 1;
428 } else {
429 free_node(node);
430 return err;
434 if (*changed)
435 add_node_to_iter_list(graph, node, child_node);
437 if (branch_done)
438 err = close_branch(graph, commit_id);
439 else
440 err = advance_branch(graph, node, commit_id, commit, repo);
441 if (err)
442 free_node(node);
443 else
444 *new_node = node;
446 return err;
449 const struct got_error *
450 got_commit_graph_open(struct got_commit_graph **graph,
451 struct got_object_id *commit_id, const char *path,
452 int first_parent_traversal, struct got_repository *repo)
454 const struct got_error *err = NULL;
455 struct got_commit_object *commit;
456 int changed;
458 *graph = NULL;
460 err = got_object_open_as_commit(&commit, repo, commit_id);
461 if (err)
462 return err;
464 /* The path must exist in our initial commit. */
465 if (!got_path_is_root_dir(path)) {
466 struct got_object_id *obj_id;
467 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
468 if (err)
469 return err;
470 free(obj_id);
473 *graph = alloc_graph(path);
474 if (*graph == NULL) {
475 got_object_commit_close(commit);
476 return got_error_from_errno();
479 if (first_parent_traversal)
480 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
482 err = add_node(&(*graph)->head_node, &changed, *graph, commit_id,
483 commit, NULL, repo);
484 got_object_commit_close(commit);
485 if (err) {
486 got_commit_graph_close(*graph);
487 *graph = NULL;
488 return err;
491 return NULL;
494 struct gather_branch_tips_arg {
495 struct got_commit_graph_branch_tip *tips;
496 int ntips;
497 };
499 static void
500 gather_branch_tips(struct got_object_id *id, void *data, void *arg)
502 struct gather_branch_tips_arg *a = arg;
503 memcpy(&a->tips[a->ntips].id, id, sizeof(*id));
504 a->tips[a->ntips].node = data;
505 a->ntips++;
508 static const struct got_error *
509 fetch_commits_from_open_branches(int *ncommits,
510 struct got_object_id **changed_id, struct got_commit_graph *graph,
511 struct got_repository *repo)
513 const struct got_error *err;
514 struct gather_branch_tips_arg arg;
515 int i;
517 *ncommits = 0;
518 *changed_id = NULL;
520 arg.ntips = got_object_idset_num_elements(graph->open_branches);
521 if (arg.ntips == 0)
522 return NULL;
524 /*
525 * Adding nodes to the graph might change the graph's open
526 * branches state. Create a local copy of the current state.
527 */
528 if (graph->ntips < arg.ntips) {
529 struct got_commit_graph_branch_tip *tips;
530 if (arg.ntips < GOT_COMMIT_GRAPH_MIN_TIPS)
531 arg.ntips = GOT_COMMIT_GRAPH_MIN_TIPS;
532 tips = reallocarray(graph->tips, arg.ntips, sizeof(*tips));
533 if (tips == NULL)
534 return got_error_from_errno();
535 graph->tips = tips;
536 graph->ntips = arg.ntips;
538 arg.ntips = 0; /* reset; gather_branch_tips() will increment */
539 arg.tips = graph->tips;
540 got_object_idset_for_each(graph->open_branches,
541 gather_branch_tips, &arg);
543 for (i = 0; i < arg.ntips; i++) {
544 struct got_object_id *commit_id;
545 struct got_commit_graph_node *child_node, *new_node;
546 struct got_commit_object *commit;
547 int changed;
549 commit_id = &graph->tips[i].id;
550 child_node = graph->tips[i].node;
552 err = got_object_open_as_commit(&commit, repo, commit_id);
553 if (err)
554 break;
556 err = add_node(&new_node, &changed, graph, commit_id, commit,
557 child_node, repo);
558 if (changed && *changed_id == NULL)
559 *changed_id = commit_id;
560 got_object_commit_close(commit);
561 if (err)
562 break;
563 if (new_node)
564 (*ncommits)++;
567 return err;
570 const struct got_error *
571 got_commit_graph_fetch_commits(struct got_commit_graph *graph, int limit,
572 struct got_repository *repo)
574 const struct got_error *err;
575 int nfetched = 0, ncommits;
576 struct got_object_id *changed_id = NULL;
578 while (nfetched < limit) {
579 err = fetch_commits_from_open_branches(&ncommits,
580 &changed_id, graph, repo);
581 if (err)
582 return err;
583 if (ncommits == 0)
584 break;
585 if (changed_id)
586 nfetched += ncommits;
589 return NULL;
592 static void
593 free_node_iter(struct got_object_id *id, void *data, void *arg)
595 struct got_commit_graph_node *node = data;
596 free_node(node);
599 void
600 got_commit_graph_close(struct got_commit_graph *graph)
602 got_object_idset_free(graph->open_branches);
603 got_object_idset_for_each(graph->node_ids, free_node_iter, NULL);
604 got_object_idset_free(graph->node_ids);
605 free(graph->tips);
606 free(graph->path);
607 free(graph);
610 const struct got_error *
611 got_commit_graph_iter_start(struct got_commit_graph *graph,
612 struct got_object_id *id, struct got_repository *repo)
614 const struct got_error *err = NULL;
615 struct got_commit_graph_node *start_node;
616 struct got_commit_object *commit;
617 int changed;
619 start_node = got_object_idset_get(graph->node_ids, id);
620 if (start_node == NULL)
621 return got_error(GOT_ERR_NO_OBJ);
623 err = got_object_open_as_commit(&commit, repo, &start_node->id);
624 if (err)
625 return err;
627 err = detect_changed_path(&changed, commit, &start_node->id,
628 graph->path, repo);
629 if (err) {
630 got_object_commit_close(commit);
631 return err;
634 if (!changed) {
635 /* Locate first commit which changed graph->path. */
636 struct got_object_id *changed_id = NULL;
637 while (changed_id == NULL) {
638 int ncommits;
639 err = fetch_commits_from_open_branches(&ncommits,
640 &changed_id, graph, repo);
641 if (err) {
642 got_object_commit_close(commit);
643 return err;
646 start_node = got_object_idset_get(graph->node_ids, changed_id);
648 got_object_commit_close(commit);
650 graph->iter_node = start_node;
651 return NULL;
654 const struct got_error *
655 got_commit_graph_iter_next(struct got_object_id **id,
656 struct got_commit_graph *graph)
658 *id = NULL;
660 if (graph->iter_node == NULL) {
661 /* We are done iterating, or iteration was not started. */
662 return got_error(GOT_ERR_ITER_COMPLETED);
665 if (graph->iter_node ==
666 TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
667 got_object_idset_num_elements(graph->open_branches) == 0) {
668 /* We are done iterating. */
669 *id = &graph->iter_node->id;
670 graph->iter_node = NULL;
671 return NULL;
674 if (TAILQ_NEXT(graph->iter_node, entry) == NULL)
675 return got_error(GOT_ERR_ITER_NEED_MORE);
677 *id = &graph->iter_node->id;
678 graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);
679 return NULL;