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_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;
41 /*
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.
46 */
47 int nparents;
48 struct got_object_id_queue parent_ids;
49 int nchildren;
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;
56 };
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;
65 /*
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.
71 *
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.
75 *
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.
79 */
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;
86 };
88 static struct got_commit_graph *
89 alloc_graph(void)
90 {
91 struct got_commit_graph *graph;
93 graph = calloc(1, sizeof(*graph));
94 if (graph == NULL)
95 return NULL;
97 graph->node_ids = got_object_idset_alloc();
98 if (graph->node_ids == NULL) {
99 free(graph);
100 return NULL;
103 graph->open_branches = got_object_idset_alloc();
104 if (graph->open_branches == NULL) {
105 got_object_idset_free(graph->node_ids);
106 free(graph);
107 return NULL;
110 TAILQ_INIT(&graph->iter_candidates);
111 return graph;
114 #if 0
115 static int
116 is_head_node(struct got_commit_graph_node *node)
118 return node->nchildren == 0;
121 static int
122 is_merge_point(struct got_commit_graph_node *node)
124 return node->nparents > 1;
127 int
128 is_branch_point(struct got_commit_graph_node *node)
130 return node->nchildren > 1;
132 #endif
134 static int
135 is_root_node(struct got_commit_graph_node *node)
137 return node->nparents == 0;
140 static void
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);
148 return;
151 TAILQ_FOREACH(n, &graph->iter_candidates, entry) {
152 if (node->commit_timestamp < n->commit_timestamp) {
153 next = TAILQ_NEXT(n, entry);
154 if (next == NULL) {
155 TAILQ_INSERT_AFTER(&graph->iter_candidates, n,
156 node, entry);
157 break;
159 if (node->commit_timestamp >= next->commit_timestamp) {
160 TAILQ_INSERT_BEFORE(next, node, entry);
161 break;
163 } else {
164 TAILQ_INSERT_BEFORE(n, node, entry);
165 break;
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));
176 if (qid == NULL)
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();
182 free(qid);
183 return err;
186 SIMPLEQ_INSERT_TAIL(ids, qid, entry);
187 return NULL;
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;
199 *new_node = NULL;
201 node = calloc(1, sizeof(*node));
202 if (node == NULL)
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);
210 if (err)
211 return err;
212 node->nparents++;
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);
218 if (err == NULL) {
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)
224 return err;
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,
229 qid->id, node);
230 if (err && err->code != GOT_ERR_OBJ_EXISTS)
231 return err;
233 *new_node = node;
234 } else if (err->code == GOT_ERR_OBJ_EXISTS) {
235 err = NULL;
236 free(node);
237 node = existing_node;
238 } else {
239 free(node);
240 return err;
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);
257 if (err)
258 return err;
259 node->nchildren++;
263 return err;
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;
273 *graph = NULL;
275 err = got_object_open_as_commit(&commit, repo, commit_id);
276 if (err)
277 return err;
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);
287 if (err) {
288 got_commit_graph_close(*graph);
289 *graph = NULL;
290 return err;
293 return NULL;
296 struct got_commit_graph_branch {
297 struct got_object_id parent_id;
298 struct got_commit_graph_node *node;
299 };
301 struct gather_branches_arg {
302 struct got_commit_graph_branch *branches;
303 int nbranches;
304 };
306 static void
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;
312 a->nbranches++;
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;
322 int i;
324 *ncommits = 0;
326 arg.nbranches = got_object_idset_num_elements(graph->open_branches);
327 if (arg.nbranches == 0)
328 return NULL;
330 /*
331 * Adding nodes to the graph might change the graph's open
332 * branches state. Create a local copy of the current state.
333 */
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);
350 if (err)
351 break;
353 err = add_node(&new_node, graph, commit_id, commit,
354 &child_node->id);
355 got_object_commit_close(commit);
356 if (err) {
357 if (err->code != GOT_ERR_OBJ_EXISTS)
358 break;
359 err = NULL;
361 if (new_node)
362 (*ncommits)++;
365 free(branches);
366 return err;
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;
376 *nfetched = 0;
378 while (total < limit) {
379 err = fetch_commits_from_open_branches(&ncommits, graph, repo);
380 if (err)
381 return err;
382 if (ncommits == 0)
383 break;
384 total += ncommits;
387 *nfetched = total;
388 return NULL;
391 static void
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);
398 free(child);
400 free(node);
403 void
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);
409 free(graph);
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);
433 if (node)
434 add_iteration_candidate(graph, node);
437 return NULL;
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. */
448 *id = NULL;
449 return NULL;
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;
458 return 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;
467 return NULL;