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_list parent_ids;
50 struct got_object_id_list 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_list *id_list, struct got_object_id *id)
173 struct got_parent_id *pid;
175 pid = calloc(1, sizeof(*pid));
177 return got_error_from_errno();
179 pid->id = got_object_id_dup(id);
180 if (pid->id == NULL) {
181 const struct got_error *err = got_error_from_errno();
186 SIMPLEQ_INSERT_TAIL(id_list, pid, 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_parent_id *pid;
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(pid, &commit->parent_ids, entry) {
209 err = add_vertex(&node->parent_ids, pid->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_parent_id *pid;
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(pid, &commit->parent_ids, entry) {
226 if (got_object_idset_get(graph->node_ids, pid->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_parent_id *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_object *obj;
272 struct got_commit_object *commit;
276 err = got_object_open(&obj, repo, commit_id);
279 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
280 err = got_error(GOT_ERR_OBJ_TYPE);
281 got_object_close(obj);
285 err = got_object_commit_open(&commit, repo, obj);
286 got_object_close(obj);
290 *graph = alloc_graph();
291 if (*graph == NULL) {
292 got_object_commit_close(commit);
293 return got_error_from_errno();
296 err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL);
297 got_object_commit_close(commit);
299 got_commit_graph_close(*graph);
307 static const struct got_error *
308 open_commit(struct got_commit_object **commit, struct got_object_id *id,
309 struct got_repository *repo)
311 const struct got_error *err;
312 struct got_object *obj;
314 err = got_object_open(&obj, repo, id);
317 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
318 err = got_error(GOT_ERR_OBJ_TYPE);
322 err = got_object_commit_open(commit, repo, obj);
324 got_object_close(obj);
328 struct got_commit_graph_branch {
329 struct got_object_id parent_id;
330 struct got_commit_graph_node *node;
333 struct gather_branches_arg {
334 struct got_commit_graph_branch *branches;
339 gather_branches(struct got_object_id *id, void *data, void *arg)
341 struct gather_branches_arg *a = arg;
342 memcpy(&a->branches[a->nbranches].parent_id, id, sizeof(*id));
343 a->branches[a->nbranches].node = data;
347 const struct got_error *
348 fetch_commits_from_open_branches(int *ncommits,
349 struct got_commit_graph *graph, struct got_repository *repo)
351 const struct got_error *err;
352 struct got_commit_graph_branch *branches;
353 struct gather_branches_arg arg;
358 arg.nbranches = got_object_idset_num_elements(graph->open_branches);
359 if (arg.nbranches == 0)
363 * Adding nodes to the graph might change the graph's open
364 * branches state. Create a local copy of the current state.
366 branches = calloc(arg.nbranches, sizeof(*branches));
367 if (branches == NULL)
368 return got_error_from_errno();
369 arg.nbranches = 0; /* reset; gather_branches() will increment */
370 arg.branches = branches;
371 got_object_idset_for_each(graph->open_branches, gather_branches, &arg);
373 for (i = 0; i < arg.nbranches; i++) {
374 struct got_object_id *commit_id;
375 struct got_commit_graph_node *child_node, *new_node;
376 struct got_commit_object *commit;
378 commit_id = &branches[i].parent_id;
379 child_node = branches[i].node;
381 err = open_commit(&commit, commit_id, repo);
385 err = add_node(&new_node, graph, commit_id, commit,
387 got_object_commit_close(commit);
389 if (err->code != GOT_ERR_OBJ_EXISTS)
401 const struct got_error *
402 got_commit_graph_fetch_commits(int *nfetched, struct got_commit_graph *graph,
403 int limit, struct got_repository *repo)
405 const struct got_error *err;
406 int total = 0, ncommits;
410 while (total < limit) {
411 err = fetch_commits_from_open_branches(&ncommits, graph, repo);
424 free_graph_node(struct got_object_id *id, void *data, void *arg)
426 struct got_commit_graph_node *node = data;
427 while (!SIMPLEQ_EMPTY(&node->child_ids)) {
428 struct got_parent_id *child = SIMPLEQ_FIRST(&node->child_ids);
429 SIMPLEQ_REMOVE_HEAD(&node->child_ids, entry);
436 got_commit_graph_close(struct got_commit_graph *graph)
438 got_object_idset_free(graph->open_branches);
439 got_object_idset_for_each(graph->node_ids, free_graph_node, NULL);
440 got_object_idset_free(graph->node_ids);
444 const struct got_error *
445 got_commit_graph_iter_start(struct got_commit_graph *graph,
446 struct got_object_id *id)
448 struct got_commit_graph_node *start_node, *node;
449 struct got_parent_id *pid;
451 start_node = got_object_idset_get(graph->node_ids, id);
452 if (start_node == NULL)
453 return got_error(GOT_ERR_NO_OBJ);
455 graph->iter_node = start_node;
457 while (!TAILQ_EMPTY(&graph->iter_candidates)) {
458 node = TAILQ_FIRST(&graph->iter_candidates);
459 TAILQ_REMOVE(&graph->iter_candidates, node, entry);
462 /* Put all known parents of this commit on the candidate list. */
463 SIMPLEQ_FOREACH(pid, &start_node->parent_ids, entry) {
464 node = got_object_idset_get(graph->node_ids, pid->id);
466 add_iteration_candidate(graph, node);
472 const struct got_error *
473 got_commit_graph_iter_next(struct got_object_id **id,
474 struct got_commit_graph *graph)
476 struct got_commit_graph_node *node;
478 if (graph->iter_node == NULL) {
479 /* We are done interating, or iteration was not started. */
484 if (TAILQ_EMPTY(&graph->iter_candidates)) {
485 if (is_root_node(graph->iter_node) &&
486 got_object_idset_num_elements(graph->open_branches) == 0) {
487 *id = &graph->iter_node->id;
488 /* We are done interating. */
489 graph->iter_node = NULL;
492 return got_error(GOT_ERR_ITER_NEED_MORE);
495 *id = &graph->iter_node->id;
496 node = TAILQ_FIRST(&graph->iter_candidates);
497 TAILQ_REMOVE(&graph->iter_candidates, node, entry);
498 graph->iter_node = node;