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.
48 struct got_object_id_queue parent_ids;
50 struct got_object_id_queue child_ids;
52 time_t commit_timestamp;
54 /* Used during graph iteration. */
55 TAILQ_ENTRY(got_commit_graph_node) entry;
58 TAILQ_HEAD(got_commit_graph_iter_list, got_commit_graph_node);
60 struct got_commit_graph {
61 /* The set of all commits we have traversed. */
62 struct got_object_idset *node_ids;
64 /* The commit at which traversal began (youngest commit in node_ids). */
65 struct got_commit_graph_node *head_node;
68 * A set of object IDs of known parent commits which we have not yet
69 * traversed. Each commit ID in this set represents a branch in commit
70 * history: Either the first-parent branch of the head node, or another
71 * branch corresponding to a traversed merge commit for which we have
72 * not traversed a branch point commit yet.
74 * Whenever we add a commit with a matching ID to the graph, we remove
75 * its corresponding element from this set, and add new elements for
76 * each of that commit's parent commits which were not traversed yet.
78 * When API users ask us to fetch more commits, we fetch commits from
79 * all currently open branches. This allows API users to process
80 * commits in linear order even though the history contains branches.
82 struct got_object_idset *open_branches;
84 /* The next commit to return when the API user asks for one. */
85 struct got_commit_graph_node *iter_node;
87 /* The graph iteration list contains all nodes in sorted order. */
88 struct got_commit_graph_iter_list iter_list;
91 static struct got_commit_graph *
94 struct got_commit_graph *graph;
96 graph = calloc(1, sizeof(*graph));
100 graph->node_ids = got_object_idset_alloc();
101 if (graph->node_ids == NULL) {
106 graph->open_branches = got_object_idset_alloc();
107 if (graph->open_branches == NULL) {
108 got_object_idset_free(graph->node_ids);
113 TAILQ_INIT(&graph->iter_list);
119 is_head_node(struct got_commit_graph_node *node)
121 return node->nchildren == 0;
125 is_merge_point(struct got_commit_graph_node *node)
127 return node->nparents > 1;
131 is_branch_point(struct got_commit_graph_node *node)
133 return node->nchildren > 1;
137 is_root_node(struct got_commit_graph_node *node)
139 return node->nparents == 0;
144 add_node_to_iter_list(struct got_commit_graph *graph,
145 struct got_commit_graph_node *node,
146 struct got_commit_graph_node *child_node)
148 struct got_commit_graph_node *n, *next;
150 if (TAILQ_EMPTY(&graph->iter_list)) {
151 TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
156 * If a child node is known, being looping over the list there
157 * instead of from the list head.
158 * All parent commits *should* appear before their children unless
159 * commit timestamps are broken (in which case the ordering of
160 * commits will be broken in some way in any case).
162 n = child_node ? child_node : TAILQ_FIRST(&graph->iter_list);
164 /* Ensure that an iteration in progress will see this new commit. */
165 if (graph->iter_node) {
166 n = graph->iter_node;
168 next = TAILQ_NEXT(n, entry);
170 node->commit_timestamp >= next->commit_timestamp) {
171 TAILQ_INSERT_BEFORE(next, node, entry);
177 TAILQ_INSERT_AFTER(&graph->iter_list, graph->iter_node,
182 /* Insert into list based on committer timestamp. */
184 if (node->commit_timestamp == n->commit_timestamp) {
185 TAILQ_INSERT_AFTER(&graph->iter_list, n, node, entry);
187 } else if (node->commit_timestamp < n->commit_timestamp) {
188 next = TAILQ_NEXT(n, entry);
190 TAILQ_INSERT_AFTER(&graph->iter_list, n,
194 if (node->commit_timestamp >= next->commit_timestamp) {
195 TAILQ_INSERT_BEFORE(next, node, entry);
199 TAILQ_INSERT_BEFORE(n, node, entry);
202 n = TAILQ_NEXT(n, entry);
206 static const struct got_error *
207 add_vertex(struct got_object_id_queue *ids, struct got_object_id *id)
209 struct got_object_qid *qid;
211 qid = calloc(1, sizeof(*qid));
213 return got_error_from_errno();
215 qid->id = got_object_id_dup(id);
216 if (qid->id == NULL) {
217 const struct got_error *err = got_error_from_errno();
222 SIMPLEQ_INSERT_TAIL(ids, qid, entry);
226 static const struct got_error *
227 add_node(struct got_commit_graph_node **new_node,
228 struct got_commit_graph *graph, struct got_object_id *commit_id,
229 struct got_commit_object *commit, struct got_commit_graph_node *child_node)
231 const struct got_error *err = NULL;
232 struct got_commit_graph_node *node, *existing_node;
233 struct got_object_qid *qid;
237 node = calloc(1, sizeof(*node));
239 return got_error_from_errno();
241 memcpy(&node->id, commit_id, sizeof(node->id));
242 SIMPLEQ_INIT(&node->parent_ids);
243 SIMPLEQ_INIT(&node->child_ids);
244 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
245 err = add_vertex(&node->parent_ids, qid->id);
250 node->commit_timestamp = commit->committer_time; /* XXX not UTC! */
252 err = got_object_idset_add((void **)(&existing_node),
253 graph->node_ids, &node->id, node);
255 struct got_object_qid *qid;
257 add_node_to_iter_list(graph, node, child_node);
258 err = got_object_idset_remove(graph->open_branches, commit_id);
259 if (err && err->code != GOT_ERR_NO_OBJ)
261 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
262 if (got_object_idset_get(graph->node_ids, qid->id))
263 continue; /* parent already traversed */
264 err = got_object_idset_add(NULL, graph->open_branches,
266 if (err && err->code != GOT_ERR_OBJ_EXISTS)
270 } else if (err->code == GOT_ERR_OBJ_EXISTS) {
273 node = existing_node;
280 struct got_object_qid *cid;
282 /* Prevent linking to self. */
283 if (got_object_id_cmp(commit_id, &child_node->id) == 0)
284 return got_error(GOT_ERR_BAD_OBJ_ID);
286 /* Prevent double-linking to the same child. */
287 SIMPLEQ_FOREACH(cid, &node->child_ids, entry) {
288 if (got_object_id_cmp(cid->id, &child_node->id) == 0)
289 return got_error(GOT_ERR_BAD_OBJ_ID);
292 err = add_vertex(&node->child_ids, &child_node->id);
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_commit_object *commit;
310 err = got_object_open_as_commit(&commit, repo, commit_id);
314 *graph = alloc_graph();
315 if (*graph == NULL) {
316 got_object_commit_close(commit);
317 return got_error_from_errno();
320 err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL);
321 got_object_commit_close(commit);
323 got_commit_graph_close(*graph);
331 struct got_commit_graph_branch {
332 struct got_object_id parent_id;
333 struct got_commit_graph_node *node;
336 struct gather_branches_arg {
337 struct got_commit_graph_branch *branches;
342 gather_branches(struct got_object_id *id, void *data, void *arg)
344 struct gather_branches_arg *a = arg;
345 memcpy(&a->branches[a->nbranches].parent_id, id, sizeof(*id));
346 a->branches[a->nbranches].node = data;
350 static const struct got_error *
351 fetch_commits_from_open_branches(int *ncommits, int *wanted_id_added,
352 struct got_commit_graph *graph, struct got_repository *repo,
353 struct got_object_id *wanted_id)
355 const struct got_error *err;
356 struct got_commit_graph_branch *branches;
357 struct gather_branches_arg arg;
362 *wanted_id_added = 0;
364 arg.nbranches = got_object_idset_num_elements(graph->open_branches);
365 if (arg.nbranches == 0)
369 * Adding nodes to the graph might change the graph's open
370 * branches state. Create a local copy of the current state.
372 branches = calloc(arg.nbranches, sizeof(*branches));
373 if (branches == NULL)
374 return got_error_from_errno();
375 arg.nbranches = 0; /* reset; gather_branches() will increment */
376 arg.branches = branches;
377 got_object_idset_for_each(graph->open_branches, gather_branches, &arg);
379 for (i = 0; i < arg.nbranches; i++) {
380 struct got_object_id *commit_id;
381 struct got_commit_graph_node *child_node, *new_node;
382 struct got_commit_object *commit;
384 commit_id = &branches[i].parent_id;
385 child_node = branches[i].node;
387 err = got_object_open_as_commit(&commit, repo, commit_id);
391 err = add_node(&new_node, graph, commit_id, commit, child_node);
392 got_object_commit_close(commit);
394 if (err->code != GOT_ERR_OBJ_EXISTS)
400 if (wanted_id && got_object_id_cmp(commit_id, wanted_id) == 0)
401 *wanted_id_added = 1;
408 const struct got_error *
409 got_commit_graph_fetch_commits(int *nfetched, struct got_commit_graph *graph,
410 int limit, struct got_repository *repo)
412 const struct got_error *err;
417 while (*nfetched < limit) {
418 err = fetch_commits_from_open_branches(&ncommits, NULL,
424 *nfetched += ncommits;
430 const struct got_error *
431 got_commit_graph_fetch_commits_up_to(int *nfetched,
432 struct got_commit_graph *graph, struct got_object_id *wanted_id,
433 struct got_repository *repo)
435 const struct got_error *err;
436 int ncommits, wanted_id_added = 0;
440 if (got_object_idset_get(graph->node_ids, wanted_id) != NULL)
443 while (!wanted_id_added) {
444 err = fetch_commits_from_open_branches(&ncommits,
445 &wanted_id_added, graph, repo, wanted_id);
450 *nfetched += ncommits;
457 free_graph_node(struct got_object_id *id, void *data, void *arg)
459 struct got_commit_graph_node *node = data;
460 while (!SIMPLEQ_EMPTY(&node->child_ids)) {
461 struct got_object_qid *child = SIMPLEQ_FIRST(&node->child_ids);
462 SIMPLEQ_REMOVE_HEAD(&node->child_ids, entry);
469 got_commit_graph_close(struct got_commit_graph *graph)
471 got_object_idset_free(graph->open_branches);
472 got_object_idset_for_each(graph->node_ids, free_graph_node, NULL);
473 got_object_idset_free(graph->node_ids);
477 const struct got_error *
478 got_commit_graph_iter_start(struct got_commit_graph *graph,
479 struct got_object_id *id)
481 struct got_commit_graph_node *start_node;
483 start_node = got_object_idset_get(graph->node_ids, id);
484 if (start_node == NULL)
485 return got_error(GOT_ERR_NO_OBJ);
487 graph->iter_node = start_node;
491 const struct got_error *
492 got_commit_graph_iter_next(struct got_object_id **id,
493 struct got_commit_graph *graph)
497 if (graph->iter_node == NULL) {
498 /* We are done interating, or iteration was not started. */
499 return got_error(GOT_ERR_ITER_COMPLETED);
502 if (graph->iter_node ==
503 TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
504 got_object_idset_num_elements(graph->open_branches) == 0) {
505 *id = &graph->iter_node->id;
506 /* We are done interating. */
507 graph->iter_node = NULL;
511 if (TAILQ_NEXT(graph->iter_node, entry) == NULL)
512 return got_error(GOT_ERR_ITER_NEED_MORE);
514 *id = &graph->iter_node->id;
515 graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);