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 struct got_commit_graph {
59 /* The set of all commits we have traversed. */
60 struct got_object_idset *node_ids;
62 /* The commit at which traversal began (youngest commit in node_ids). */
63 struct got_commit_graph_node *head_node;
66 * A set of object IDs of known parent commits which we have not yet
67 * traversed. Each commit ID in this set represents a branch in commit
68 * history: Either the first-parent branch of the head node, or another
69 * branch corresponding to a traversed merge commit for which we have
70 * not traversed a branch point commit yet.
72 * Whenever we add a commit with a matching ID to the graph, we remove
73 * its corresponding element from this set, and add new elements for
74 * each of that commit's parent commits which were not traversed yet.
76 * When API users ask us to fetch more commits, we fetch commits from
77 * all currently open branches. This allows API users to process
78 * commits in linear order even though the history contains branches.
80 struct got_object_idset *open_branches;
82 /* The next commit to return when the API user asks for one. */
83 struct got_commit_graph_node *iter_node;
85 TAILQ_HEAD(, got_commit_graph_node) iter_candidates;
88 static struct got_commit_graph *
91 struct got_commit_graph *graph;
93 graph = calloc(1, sizeof(*graph));
97 graph->node_ids = got_object_idset_alloc();
98 if (graph->node_ids == NULL) {
103 graph->open_branches = got_object_idset_alloc();
104 if (graph->open_branches == NULL) {
105 got_object_idset_free(graph->node_ids);
110 TAILQ_INIT(&graph->iter_candidates);
116 is_head_node(struct got_commit_graph_node *node)
118 return node->nchildren == 0;
122 is_merge_point(struct got_commit_graph_node *node)
124 return node->nparents > 1;
128 is_branch_point(struct got_commit_graph_node *node)
130 return node->nchildren > 1;
135 is_root_node(struct got_commit_graph_node *node)
137 return node->nparents == 0;
141 add_iteration_candidate(struct got_commit_graph *graph,
142 struct got_commit_graph_node *node)
144 struct got_commit_graph_node *n, *next;
146 if (TAILQ_EMPTY(&graph->iter_candidates)) {
147 TAILQ_INSERT_TAIL(&graph->iter_candidates, node, entry);
151 TAILQ_FOREACH(n, &graph->iter_candidates, entry) {
152 if (node->commit_timestamp < n->commit_timestamp) {
153 next = TAILQ_NEXT(n, entry);
155 TAILQ_INSERT_AFTER(&graph->iter_candidates, n,
159 if (node->commit_timestamp >= next->commit_timestamp) {
160 TAILQ_INSERT_BEFORE(next, node, entry);
164 TAILQ_INSERT_BEFORE(n, node, entry);
170 static const struct got_error *
171 add_vertex(struct got_object_id_queue *ids, struct got_object_id *id)
173 struct got_object_qid *qid;
175 qid = calloc(1, sizeof(*qid));
177 return got_error_from_errno();
179 qid->id = got_object_id_dup(id);
180 if (qid->id == NULL) {
181 const struct got_error *err = got_error_from_errno();
186 SIMPLEQ_INSERT_TAIL(ids, qid, entry);
190 static const struct got_error *
191 add_node(struct got_commit_graph_node **new_node,
192 struct got_commit_graph *graph, struct got_object_id *commit_id,
193 struct got_commit_object *commit, struct got_object_id *child_commit_id)
195 const struct got_error *err = NULL;
196 struct got_commit_graph_node *node, *existing_node;
197 struct got_object_qid *qid;
201 node = calloc(1, sizeof(*node));
203 return got_error_from_errno();
205 memcpy(&node->id, commit_id, sizeof(node->id));
206 SIMPLEQ_INIT(&node->parent_ids);
207 SIMPLEQ_INIT(&node->child_ids);
208 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
209 err = add_vertex(&node->parent_ids, qid->id);
214 node->commit_timestamp = commit->committer_time; /* XXX not UTC! */
216 err = got_object_idset_add((void **)(&existing_node),
217 graph->node_ids, &node->id, node);
219 struct got_object_qid *qid;
221 add_iteration_candidate(graph, node);
222 err = got_object_idset_remove(graph->open_branches, commit_id);
223 if (err && err->code != GOT_ERR_NO_OBJ)
225 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
226 if (got_object_idset_get(graph->node_ids, qid->id))
227 continue; /* parent already traversed */
228 err = got_object_idset_add(NULL, graph->open_branches,
230 if (err && err->code != GOT_ERR_OBJ_EXISTS)
234 } else if (err->code == GOT_ERR_OBJ_EXISTS) {
237 node = existing_node;
243 if (child_commit_id) {
244 struct got_object_qid *cid;
246 /* Prevent linking to self. */
247 if (got_object_id_cmp(commit_id, child_commit_id) == 0)
248 return got_error(GOT_ERR_BAD_OBJ_ID);
250 /* Prevent double-linking to the same child. */
251 SIMPLEQ_FOREACH(cid, &node->child_ids, entry) {
252 if (got_object_id_cmp(cid->id, child_commit_id) == 0)
253 return got_error(GOT_ERR_BAD_OBJ_ID);
256 err = add_vertex(&node->child_ids, child_commit_id);
266 const struct got_error *
267 got_commit_graph_open(struct got_commit_graph **graph,
268 struct got_object_id *commit_id, struct got_repository *repo)
270 const struct got_error *err = NULL;
271 struct got_commit_object *commit;
275 err = got_object_open_as_commit(&commit, repo, commit_id);
279 *graph = alloc_graph();
280 if (*graph == NULL) {
281 got_object_commit_close(commit);
282 return got_error_from_errno();
285 err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL);
286 got_object_commit_close(commit);
288 got_commit_graph_close(*graph);
296 struct got_commit_graph_branch {
297 struct got_object_id parent_id;
298 struct got_commit_graph_node *node;
301 struct gather_branches_arg {
302 struct got_commit_graph_branch *branches;
307 gather_branches(struct got_object_id *id, void *data, void *arg)
309 struct gather_branches_arg *a = arg;
310 memcpy(&a->branches[a->nbranches].parent_id, id, sizeof(*id));
311 a->branches[a->nbranches].node = data;
315 const struct got_error *
316 fetch_commits_from_open_branches(int *ncommits,
317 struct got_commit_graph *graph, struct got_repository *repo)
319 const struct got_error *err;
320 struct got_commit_graph_branch *branches;
321 struct gather_branches_arg arg;
326 arg.nbranches = got_object_idset_num_elements(graph->open_branches);
327 if (arg.nbranches == 0)
331 * Adding nodes to the graph might change the graph's open
332 * branches state. Create a local copy of the current state.
334 branches = calloc(arg.nbranches, sizeof(*branches));
335 if (branches == NULL)
336 return got_error_from_errno();
337 arg.nbranches = 0; /* reset; gather_branches() will increment */
338 arg.branches = branches;
339 got_object_idset_for_each(graph->open_branches, gather_branches, &arg);
341 for (i = 0; i < arg.nbranches; i++) {
342 struct got_object_id *commit_id;
343 struct got_commit_graph_node *child_node, *new_node;
344 struct got_commit_object *commit;
346 commit_id = &branches[i].parent_id;
347 child_node = branches[i].node;
349 err = got_object_open_as_commit(&commit, repo, commit_id);
353 err = add_node(&new_node, graph, commit_id, commit,
355 got_object_commit_close(commit);
357 if (err->code != GOT_ERR_OBJ_EXISTS)
369 const struct got_error *
370 got_commit_graph_fetch_commits(int *nfetched, struct got_commit_graph *graph,
371 int limit, struct got_repository *repo)
373 const struct got_error *err;
374 int total = 0, ncommits;
378 while (total < limit) {
379 err = fetch_commits_from_open_branches(&ncommits, graph, repo);
392 free_graph_node(struct got_object_id *id, void *data, void *arg)
394 struct got_commit_graph_node *node = data;
395 while (!SIMPLEQ_EMPTY(&node->child_ids)) {
396 struct got_object_qid *child = SIMPLEQ_FIRST(&node->child_ids);
397 SIMPLEQ_REMOVE_HEAD(&node->child_ids, entry);
404 got_commit_graph_close(struct got_commit_graph *graph)
406 got_object_idset_free(graph->open_branches);
407 got_object_idset_for_each(graph->node_ids, free_graph_node, NULL);
408 got_object_idset_free(graph->node_ids);
412 const struct got_error *
413 got_commit_graph_iter_start(struct got_commit_graph *graph,
414 struct got_object_id *id)
416 struct got_commit_graph_node *start_node, *node;
417 struct got_object_qid *qid;
419 start_node = got_object_idset_get(graph->node_ids, id);
420 if (start_node == NULL)
421 return got_error(GOT_ERR_NO_OBJ);
423 graph->iter_node = start_node;
425 while (!TAILQ_EMPTY(&graph->iter_candidates)) {
426 node = TAILQ_FIRST(&graph->iter_candidates);
427 TAILQ_REMOVE(&graph->iter_candidates, node, entry);
430 /* Put all known parents of this commit on the candidate list. */
431 SIMPLEQ_FOREACH(qid, &start_node->parent_ids, entry) {
432 node = got_object_idset_get(graph->node_ids, qid->id);
434 add_iteration_candidate(graph, node);
440 const struct got_error *
441 got_commit_graph_iter_next(struct got_object_id **id,
442 struct got_commit_graph *graph)
444 struct got_commit_graph_node *node;
446 if (graph->iter_node == NULL) {
447 /* We are done interating, or iteration was not started. */
452 if (TAILQ_EMPTY(&graph->iter_candidates)) {
453 if (is_root_node(graph->iter_node) &&
454 got_object_idset_num_elements(graph->open_branches) == 0) {
455 *id = &graph->iter_node->id;
456 /* We are done interating. */
457 graph->iter_node = NULL;
460 return got_error(GOT_ERR_ITER_NEED_MORE);
463 *id = &graph->iter_node->id;
464 node = TAILQ_FIRST(&graph->iter_candidates);
465 TAILQ_REMOVE(&graph->iter_candidates, node, entry);
466 graph->iter_node = node;