2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/stdint.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_zbuf.h"
35 #include "got_lib_object.h"
36 #include "got_lib_object_idset.h"
38 struct got_commit_graph_node {
39 struct got_object_id id;
42 * Each graph node corresponds to a commit object.
43 * Graph vertices are modelled with two separate adjacency lists:
44 * Adjacencies of a graph node are either parent (older) commits,
45 * and child (younger) commits.
47 struct got_commit_object *commit; /* contains list of parents */
49 SIMPLEQ_HEAD(, got_parent_id) child_ids;
51 /* Used during graph iteration. */
52 TAILQ_ENTRY(got_commit_graph_node) entry;
55 struct got_commit_graph {
56 /* The set of all commits we have traversed. */
57 struct got_object_idset *node_ids;
59 /* The commit at which traversal began (youngest commit in node_ids). */
60 struct got_commit_graph_node *head_node;
63 * A set of object IDs of known parent commits which we have not yet
64 * traversed. Each commit ID in this set represents a branch in commit
65 * history: Either the first-parent branch of the head node, or another
66 * branch corresponding to a traversed merge commit for which we have
67 * not traversed a branch point commit yet.
69 * Whenever we add a commit with a matching ID to the graph, we remove
70 * its corresponding element from this set, and add new elements for
71 * each of that commit's parent commits which were not traversed yet.
73 * When API users ask us to fetch more commits, we fetch commits from
74 * all currently open branches. This allows API users to process
75 * commits in linear order even though the history contains branches.
77 struct got_object_idset *open_branches;
79 /* The next commit to return when the API user asks for one. */
80 struct got_commit_graph_node *iter_node;
82 TAILQ_HEAD(, got_commit_graph_node) iter_candidates;
85 static struct got_commit_graph *
88 struct got_commit_graph *graph;
90 graph = calloc(1, sizeof(*graph));
94 graph->node_ids = got_object_idset_alloc();
95 if (graph->node_ids == NULL) {
100 graph->open_branches = got_object_idset_alloc();
101 if (graph->open_branches == NULL) {
102 got_object_idset_free(graph->node_ids);
107 TAILQ_INIT(&graph->iter_candidates);
113 is_head_node(struct got_commit_graph_node *node)
115 return node->nchildren == 0;
119 is_merge_point(struct got_commit_graph_node *node)
121 return node->commit->nparents > 1;
125 is_branch_point(struct got_commit_graph_node *node)
127 return node->nchildren > 1;
132 is_root_node(struct got_commit_graph_node *node)
134 return node->commit->nparents == 0;
137 static const struct got_error *
138 parse_commit_time(int64_t *time, struct got_commit_object *commit)
140 const struct got_error *err = NULL;
142 char *committer, *space;
146 committer = strdup(commit->committer);
147 if (committer == NULL)
148 return got_error_from_errno();
150 /* Strip off trailing timezone indicator. */
151 space = strrchr(committer, ' ');
153 err = got_error(GOT_ERR_BAD_OBJ_DATA);
158 /* Timestamp is separated from committer name + email by space. */
159 space = strrchr(committer, ' ');
161 err = got_error(GOT_ERR_BAD_OBJ_DATA);
165 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
167 err = got_error(GOT_ERR_BAD_OBJ_DATA);
174 static const struct got_error *
175 compare_commits(int *cmp, struct got_commit_object *c1,
176 struct got_commit_object *c2)
178 const struct got_error *err;
181 err = parse_commit_time(&t1, c1);
184 err = parse_commit_time(&t2, c2);
198 static const struct got_error *
199 add_iteration_candidate(struct got_commit_graph *graph,
200 struct got_commit_graph_node *node)
202 struct got_commit_graph_node *n;
204 if (TAILQ_EMPTY(&graph->iter_candidates)) {
205 TAILQ_INSERT_TAIL(&graph->iter_candidates, node, entry);
209 TAILQ_FOREACH(n, &graph->iter_candidates, entry) {
210 const struct got_error *err;
212 err = compare_commits(&cmp, node->commit, n->commit);
217 TAILQ_INSERT_BEFORE(n, node, entry);
224 static const struct got_error *
225 add_node(struct got_commit_graph_node **new_node,
226 struct got_commit_graph *graph, struct got_object_id *commit_id,
227 struct got_commit_object *commit, struct got_object_id *child_commit_id)
229 const struct got_error *err = NULL;
230 struct got_commit_graph_node *node, *existing_node;
234 node = calloc(1, sizeof(*node));
236 return got_error_from_errno();
238 memcpy(&node->id, commit_id, sizeof(node->id));
239 node->commit = commit;
240 SIMPLEQ_INIT(&node->child_ids);
242 err = got_object_idset_add((void **)(&existing_node),
243 graph->node_ids, &node->id, node);
245 struct got_parent_id *pid;
247 err = add_iteration_candidate(graph, node);
251 err = got_object_idset_remove(graph->open_branches, commit_id);
252 if (err && err->code != GOT_ERR_NO_OBJ)
254 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
255 if (got_object_idset_get(graph->node_ids, pid->id))
256 continue; /* parent already traversed */
257 err = got_object_idset_add(NULL, graph->open_branches,
259 if (err && err->code != GOT_ERR_OBJ_EXISTS)
263 } else if (err->code == GOT_ERR_OBJ_EXISTS) {
266 node = existing_node;
272 if (child_commit_id) {
273 struct got_parent_id *child, *cid;
275 /* Prevent linking to self. */
276 if (got_object_id_cmp(commit_id, child_commit_id) == 0)
277 return got_error(GOT_ERR_BAD_OBJ_ID);
279 /* Prevent double-linking to the same child. */
280 SIMPLEQ_FOREACH(cid, &node->child_ids, entry) {
281 if (got_object_id_cmp(cid->id, child_commit_id) == 0)
282 return got_error(GOT_ERR_BAD_OBJ_ID);
285 child = calloc(1, sizeof(*child));
287 return got_error_from_errno();
288 child->id = got_object_id_dup(child_commit_id);
289 if (child->id == NULL) {
290 err = got_error_from_errno();
294 SIMPLEQ_INSERT_TAIL(&node->child_ids, child, entry);
301 const struct got_error *
302 got_commit_graph_open(struct got_commit_graph **graph,
303 struct got_object_id *commit_id, struct got_repository *repo)
305 const struct got_error *err = NULL;
306 struct got_object *obj;
307 struct got_commit_object *commit;
311 err = got_object_open(&obj, repo, commit_id);
314 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
315 err = got_error(GOT_ERR_OBJ_TYPE);
316 got_object_close(obj);
320 err = got_object_commit_open(&commit, repo, obj);
321 got_object_close(obj);
325 *graph = alloc_graph();
326 if (*graph == NULL) {
327 got_object_commit_close(commit);
328 return got_error_from_errno();
331 err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL);
333 got_commit_graph_close(*graph);
341 static const struct got_error *
342 open_commit(struct got_commit_object **commit, struct got_object_id *id,
343 struct got_repository *repo)
345 const struct got_error *err;
346 struct got_object *obj;
348 err = got_object_open(&obj, repo, id);
351 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
352 err = got_error(GOT_ERR_OBJ_TYPE);
356 err = got_object_commit_open(commit, repo, obj);
358 got_object_close(obj);
362 struct got_commit_graph_branch {
363 struct got_object_id parent_id;
364 struct got_commit_graph_node *node;
367 struct gather_branches_arg {
368 struct got_commit_graph_branch *branches;
373 gather_branches(struct got_object_id *id, void *data, void *arg)
375 struct gather_branches_arg *a = arg;
376 memcpy(&a->branches[a->nbranches].parent_id, id, sizeof(*id));
377 a->branches[a->nbranches].node = data;
381 const struct got_error *
382 fetch_commits_from_open_branches(int *ncommits,
383 struct got_commit_graph *graph, struct got_repository *repo)
385 const struct got_error *err;
386 struct got_commit_graph_branch *branches;
387 struct gather_branches_arg arg;
392 arg.nbranches = got_object_idset_num_elements(graph->open_branches);
393 if (arg.nbranches == 0)
397 * Adding nodes to the graph might change the graph's open
398 * branches state. Create a local copy of the current state.
400 branches = calloc(arg.nbranches, sizeof(*branches));
401 if (branches == NULL)
402 return got_error_from_errno();
403 arg.nbranches = 0; /* reset; gather_branches() will increment */
404 arg.branches = branches;
405 got_object_idset_for_each(graph->open_branches, gather_branches, &arg);
407 for (i = 0; i < arg.nbranches; i++) {
408 struct got_object_id *commit_id;
409 struct got_commit_graph_node *child_node, *new_node;
410 struct got_commit_object *commit;
412 commit_id = &branches[i].parent_id;
413 child_node = branches[i].node;
415 err = open_commit(&commit, commit_id, repo);
419 err = add_node(&new_node, graph, commit_id, commit,
422 if (err->code != GOT_ERR_OBJ_EXISTS)
429 got_object_commit_close(commit);
436 const struct got_error *
437 got_commit_graph_fetch_commits(int *nfetched, struct got_commit_graph *graph,
438 int limit, struct got_repository *repo)
440 const struct got_error *err;
441 int total = 0, ncommits;
445 while (total < limit) {
446 err = fetch_commits_from_open_branches(&ncommits, graph, repo);
459 free_graph_node(struct got_object_id *id, void *data, void *arg)
461 struct got_commit_graph_node *node = data;
462 got_object_commit_close(node->commit);
463 while (!SIMPLEQ_EMPTY(&node->child_ids)) {
464 struct got_parent_id *child = SIMPLEQ_FIRST(&node->child_ids);
465 SIMPLEQ_REMOVE_HEAD(&node->child_ids, entry);
472 got_commit_graph_close(struct got_commit_graph *graph)
474 got_object_idset_free(graph->open_branches);
475 got_object_idset_for_each(graph->node_ids, free_graph_node, NULL);
476 got_object_idset_free(graph->node_ids);
480 const struct got_error *
481 got_commit_graph_iter_start(struct got_commit_graph *graph,
482 struct got_object_id *id)
484 struct got_commit_graph_node *start_node, *node;
485 struct got_parent_id *pid;
487 start_node = got_object_idset_get(graph->node_ids, id);
488 if (start_node == NULL)
489 return got_error(GOT_ERR_NO_OBJ);
491 graph->iter_node = start_node;
493 while (!TAILQ_EMPTY(&graph->iter_candidates)) {
494 node = TAILQ_FIRST(&graph->iter_candidates);
495 TAILQ_REMOVE(&graph->iter_candidates, node, entry);
498 /* Put all known parents of this commit on the candidate list. */
499 SIMPLEQ_FOREACH(pid, &start_node->commit->parent_ids, entry) {
500 node = got_object_idset_get(graph->node_ids, pid->id);
502 const struct got_error *err;
503 err = add_iteration_candidate(graph, node);
512 const struct got_error *
513 got_commit_graph_iter_next(struct got_commit_object **commit,
514 struct got_object_id **id, struct got_commit_graph *graph)
516 struct got_commit_graph_node *node;
518 if (graph->iter_node == NULL) {
519 /* We are done interating, or iteration was not started. */
525 if (TAILQ_EMPTY(&graph->iter_candidates)) {
526 if (is_root_node(graph->iter_node) &&
527 got_object_idset_num_elements(graph->open_branches) == 0) {
528 *commit = graph->iter_node->commit;
529 *id = &graph->iter_node->id;
530 /* We are done interating. */
531 graph->iter_node = NULL;
534 return got_error(GOT_ERR_ITER_NEED_MORE);
537 *commit = graph->iter_node->commit;
538 *id = &graph->iter_node->id;
539 node = TAILQ_FIRST(&graph->iter_candidates);
540 TAILQ_REMOVE(&graph->iter_candidates, node, entry);
541 graph->iter_node = node;
545 const struct got_commit_object *
546 got_commit_graph_get_commit(struct got_commit_graph *graph,
547 struct got_object_id *id)
549 struct got_commit_graph_node *node;
550 node = got_object_idset_get(graph->node_ids, id);
551 return node ? node->commit : NULL;