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 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;
67 int flags;
68 #define GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL 0x01
70 /*
71 * A set of object IDs of known parent commits which we have not yet
72 * traversed. Each commit ID in this set represents a branch in commit
73 * history: Either the first-parent branch of the head node, or another
74 * branch corresponding to a traversed merge commit for which we have
75 * not traversed a branch point commit yet.
76 *
77 * Whenever we add a commit with a matching ID to the graph, we remove
78 * its corresponding element from this set, and add new elements for
79 * each of that commit's parent commits which were not traversed yet.
80 *
81 * When API users ask us to fetch more commits, we fetch commits from
82 * all currently open branches. This allows API users to process
83 * commits in linear order even though the history contains branches.
84 */
85 struct got_object_idset *open_branches;
87 /* The next commit to return when the API user asks for one. */
88 struct got_commit_graph_node *iter_node;
90 /* The graph iteration list contains all nodes in sorted order. */
91 struct got_commit_graph_iter_list iter_list;
92 };
94 static struct got_commit_graph *
95 alloc_graph(void)
96 {
97 struct got_commit_graph *graph;
99 graph = calloc(1, sizeof(*graph));
100 if (graph == NULL)
101 return NULL;
103 graph->node_ids = got_object_idset_alloc();
104 if (graph->node_ids == NULL) {
105 free(graph);
106 return NULL;
109 graph->open_branches = got_object_idset_alloc();
110 if (graph->open_branches == NULL) {
111 got_object_idset_free(graph->node_ids);
112 free(graph);
113 return NULL;
116 TAILQ_INIT(&graph->iter_list);
117 return graph;
120 #if 0
121 static int
122 is_head_node(struct got_commit_graph_node *node)
124 return node->nchildren == 0;
127 static int
128 is_merge_point(struct got_commit_graph_node *node)
130 return node->nparents > 1;
133 int
134 is_branch_point(struct got_commit_graph_node *node)
136 return node->nchildren > 1;
139 static int
140 is_root_node(struct got_commit_graph_node *node)
142 return node->nparents == 0;
144 #endif
146 static void
147 add_node_to_iter_list(struct got_commit_graph *graph,
148 struct got_commit_graph_node *node,
149 struct got_commit_graph_node *child_node)
151 struct got_commit_graph_node *n, *next;
153 if (TAILQ_EMPTY(&graph->iter_list)) {
154 TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
155 return;
158 /* Ensure that an iteration in progress will see this new commit. */
159 if (graph->iter_node) {
160 n = graph->iter_node;
161 while (n) {
162 next = TAILQ_NEXT(n, entry);
163 if (next &&
164 node->commit_timestamp >= next->commit_timestamp) {
165 TAILQ_INSERT_BEFORE(next, node, entry);
166 return;
168 n = next;
170 TAILQ_INSERT_AFTER(&graph->iter_list, graph->iter_node,
171 node, entry);
172 return;
175 /*
176 * If a child node is known, begin looping over the list there
177 * instead of beginning from the list head.
178 */
179 n = child_node ? child_node : TAILQ_FIRST(&graph->iter_list);
181 /* Insert into list based on committer timestamp. */
182 do {
183 if (node->commit_timestamp == n->commit_timestamp) {
184 TAILQ_INSERT_AFTER(&graph->iter_list, n, node, entry);
185 break;
186 } else if (node->commit_timestamp < n->commit_timestamp) {
187 next = TAILQ_NEXT(n, entry);
188 if (next == NULL) {
189 TAILQ_INSERT_AFTER(&graph->iter_list, n,
190 node, entry);
191 break;
193 if (node->commit_timestamp >= next->commit_timestamp) {
194 TAILQ_INSERT_BEFORE(next, node, entry);
195 break;
197 } else {
198 TAILQ_INSERT_BEFORE(n, node, entry);
199 break;
201 n = TAILQ_NEXT(n, entry);
202 } while (n);
205 static const struct got_error *
206 add_vertex(struct got_object_id_queue *ids, struct got_object_id *id)
208 struct got_object_qid *qid;
210 qid = calloc(1, sizeof(*qid));
211 if (qid == NULL)
212 return got_error_from_errno();
214 qid->id = got_object_id_dup(id);
215 if (qid->id == NULL) {
216 const struct got_error *err = got_error_from_errno();
217 free(qid);
218 return err;
221 SIMPLEQ_INSERT_TAIL(ids, qid, entry);
222 return NULL;
225 static const struct got_error *
226 advance_open_branches(struct got_commit_graph *graph,
227 struct got_commit_graph_node *node,
228 struct got_object_id *commit_id, struct got_commit_object *commit)
230 const struct got_error *err;
231 struct got_object_qid *qid;
233 err = got_object_idset_remove(graph->open_branches, commit_id);
234 if (err && err->code != GOT_ERR_NO_OBJ)
235 return err;
237 if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
238 qid = SIMPLEQ_FIRST(&commit->parent_ids);
239 if (qid == NULL)
240 return NULL;
241 err = got_object_idset_add(NULL, graph->open_branches, qid->id,
242 node);
243 if (err && err->code != GOT_ERR_OBJ_EXISTS)
244 return err;
245 return NULL;
248 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
249 if (got_object_idset_get(graph->node_ids, qid->id))
250 continue; /* parent already traversed */
251 err = got_object_idset_add(NULL, graph->open_branches,
252 qid->id, node);
253 if (err && err->code != GOT_ERR_OBJ_EXISTS)
254 return err;
257 return NULL;
260 static const struct got_error *
261 add_node(struct got_commit_graph_node **new_node,
262 struct got_commit_graph *graph, struct got_object_id *commit_id,
263 struct got_commit_object *commit, struct got_commit_graph_node *child_node)
265 const struct got_error *err = NULL;
266 struct got_commit_graph_node *node, *existing_node;
267 struct got_object_qid *qid;
269 *new_node = NULL;
271 node = calloc(1, sizeof(*node));
272 if (node == NULL)
273 return got_error_from_errno();
275 memcpy(&node->id, commit_id, sizeof(node->id));
276 SIMPLEQ_INIT(&node->parent_ids);
277 SIMPLEQ_INIT(&node->child_ids);
278 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
279 err = add_vertex(&node->parent_ids, qid->id);
280 if (err)
281 return err;
282 node->nparents++;
284 node->commit_timestamp = commit->committer_time; /* XXX not UTC! */
286 err = got_object_idset_add((void **)(&existing_node),
287 graph->node_ids, &node->id, node);
288 if (err == NULL) {
289 add_node_to_iter_list(graph, node, child_node);
290 err = advance_open_branches(graph, node, commit_id, commit);
291 *new_node = node;
292 } else if (err->code == GOT_ERR_OBJ_EXISTS) {
293 err = NULL;
294 free(node);
295 node = existing_node;
296 } else {
297 free(node);
298 return err;
301 if (child_node) {
302 struct got_object_qid *cid;
304 /* Prevent linking to self. */
305 if (got_object_id_cmp(commit_id, &child_node->id) == 0)
306 return got_error(GOT_ERR_BAD_OBJ_ID);
308 /* Prevent double-linking to the same child. */
309 SIMPLEQ_FOREACH(cid, &node->child_ids, entry) {
310 if (got_object_id_cmp(cid->id, &child_node->id) == 0)
311 return got_error(GOT_ERR_BAD_OBJ_ID);
314 err = add_vertex(&node->child_ids, &child_node->id);
315 if (err)
316 return err;
317 node->nchildren++;
320 return err;
323 const struct got_error *
324 got_commit_graph_open(struct got_commit_graph **graph,
325 struct got_object_id *commit_id, int first_parent_traversal,
326 struct got_repository *repo)
328 const struct got_error *err = NULL;
329 struct got_commit_object *commit;
331 *graph = NULL;
333 err = got_object_open_as_commit(&commit, repo, commit_id);
334 if (err)
335 return err;
337 *graph = alloc_graph();
338 if (*graph == NULL) {
339 got_object_commit_close(commit);
340 return got_error_from_errno();
343 if (first_parent_traversal)
344 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
346 err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL);
347 got_object_commit_close(commit);
348 if (err) {
349 got_commit_graph_close(*graph);
350 *graph = NULL;
351 return err;
354 return NULL;
357 struct got_commit_graph_branch {
358 struct got_object_id parent_id;
359 struct got_commit_graph_node *node;
360 };
362 struct gather_branches_arg {
363 struct got_commit_graph_branch *branches;
364 int nbranches;
365 };
367 static void
368 gather_branches(struct got_object_id *id, void *data, void *arg)
370 struct gather_branches_arg *a = arg;
371 memcpy(&a->branches[a->nbranches].parent_id, id, sizeof(*id));
372 a->branches[a->nbranches].node = data;
373 a->nbranches++;
376 static const struct got_error *
377 fetch_commits_from_open_branches(int *ncommits, int *wanted_id_added,
378 struct got_commit_graph *graph, struct got_repository *repo,
379 struct got_object_id *wanted_id)
381 const struct got_error *err;
382 struct got_commit_graph_branch *branches;
383 struct gather_branches_arg arg;
384 int i;
386 *ncommits = 0;
387 if (wanted_id_added)
388 *wanted_id_added = 0;
390 arg.nbranches = got_object_idset_num_elements(graph->open_branches);
391 if (arg.nbranches == 0)
392 return NULL;
394 /*
395 * Adding nodes to the graph might change the graph's open
396 * branches state. Create a local copy of the current state.
397 */
398 branches = calloc(arg.nbranches, sizeof(*branches));
399 if (branches == NULL)
400 return got_error_from_errno();
401 arg.nbranches = 0; /* reset; gather_branches() will increment */
402 arg.branches = branches;
403 got_object_idset_for_each(graph->open_branches, gather_branches, &arg);
405 for (i = 0; i < arg.nbranches; i++) {
406 struct got_object_id *commit_id;
407 struct got_commit_graph_node *child_node, *new_node;
408 struct got_commit_object *commit;
410 commit_id = &branches[i].parent_id;
411 child_node = branches[i].node;
413 err = got_object_open_as_commit(&commit, repo, commit_id);
414 if (err)
415 break;
417 err = add_node(&new_node, graph, commit_id, commit, child_node);
418 got_object_commit_close(commit);
419 if (err) {
420 if (err->code != GOT_ERR_OBJ_EXISTS)
421 break;
422 err = NULL;
424 if (new_node)
425 (*ncommits)++;
426 if (wanted_id && got_object_id_cmp(commit_id, wanted_id) == 0)
427 *wanted_id_added = 1;
430 free(branches);
431 return err;
434 const struct got_error *
435 got_commit_graph_fetch_commits(int *nfetched, struct got_commit_graph *graph,
436 int limit, struct got_repository *repo)
438 const struct got_error *err;
439 int ncommits;
441 *nfetched = 0;
443 while (*nfetched < limit) {
444 err = fetch_commits_from_open_branches(&ncommits, NULL,
445 graph, repo, NULL);
446 if (err)
447 return err;
448 if (ncommits == 0)
449 break;
450 *nfetched += ncommits;
453 return NULL;
456 const struct got_error *
457 got_commit_graph_fetch_commits_up_to(int *nfetched,
458 struct got_commit_graph *graph, struct got_object_id *wanted_id,
459 struct got_repository *repo)
461 const struct got_error *err;
462 int ncommits, wanted_id_added = 0;
464 *nfetched = 0;
466 if (got_object_idset_get(graph->node_ids, wanted_id) != NULL)
467 return NULL;
469 while (!wanted_id_added) {
470 err = fetch_commits_from_open_branches(&ncommits,
471 &wanted_id_added, graph, repo, wanted_id);
472 if (err)
473 return err;
474 if (ncommits == 0)
475 return NULL;
476 *nfetched += ncommits;
479 return NULL;
482 static void
483 free_graph_node(struct got_object_id *id, void *data, void *arg)
485 struct got_commit_graph_node *node = data;
486 while (!SIMPLEQ_EMPTY(&node->child_ids)) {
487 struct got_object_qid *child = SIMPLEQ_FIRST(&node->child_ids);
488 SIMPLEQ_REMOVE_HEAD(&node->child_ids, entry);
489 free(child);
491 free(node);
494 void
495 got_commit_graph_close(struct got_commit_graph *graph)
497 got_object_idset_free(graph->open_branches);
498 got_object_idset_for_each(graph->node_ids, free_graph_node, NULL);
499 got_object_idset_free(graph->node_ids);
500 free(graph);
503 const struct got_error *
504 got_commit_graph_iter_start(struct got_commit_graph *graph,
505 struct got_object_id *id)
507 struct got_commit_graph_node *start_node;
509 start_node = got_object_idset_get(graph->node_ids, id);
510 if (start_node == NULL)
511 return got_error(GOT_ERR_NO_OBJ);
513 graph->iter_node = start_node;
514 return NULL;
517 const struct got_error *
518 got_commit_graph_iter_next(struct got_object_id **id,
519 struct got_commit_graph *graph)
521 *id = NULL;
523 if (graph->iter_node == NULL) {
524 /* We are done interating, or iteration was not started. */
525 return got_error(GOT_ERR_ITER_COMPLETED);
528 if (graph->iter_node ==
529 TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
530 got_object_idset_num_elements(graph->open_branches) == 0) {
531 *id = &graph->iter_node->id;
532 /* We are done interating. */
533 graph->iter_node = NULL;
534 return NULL;
537 if (TAILQ_NEXT(graph->iter_node, entry) == NULL)
538 return got_error(GOT_ERR_ITER_NEED_MORE);
540 *id = &graph->iter_node->id;
541 graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);
542 return NULL;