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_inflate.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(NULL, 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 void
261 free_node(struct got_commit_graph_node *node)
263 while (!SIMPLEQ_EMPTY(&node->child_ids)) {
264 struct got_object_qid *child = SIMPLEQ_FIRST(&node->child_ids);
265 SIMPLEQ_REMOVE_HEAD(&node->child_ids, entry);
266 free(child);
268 while (!SIMPLEQ_EMPTY(&node->parent_ids)) {
269 struct got_object_qid *pid = SIMPLEQ_FIRST(&node->parent_ids);
270 SIMPLEQ_REMOVE_HEAD(&node->parent_ids, entry);
271 free(pid);
273 free(node);
276 static const struct got_error *
277 add_node(struct got_commit_graph_node **new_node,
278 struct got_commit_graph *graph, struct got_object_id *commit_id,
279 struct got_commit_object *commit, struct got_commit_graph_node *child_node)
281 const struct got_error *err = NULL;
282 struct got_commit_graph_node *node, *existing_node;
283 struct got_object_qid *qid;
285 *new_node = NULL;
287 node = calloc(1, sizeof(*node));
288 if (node == NULL)
289 return got_error_from_errno();
291 memcpy(&node->id, commit_id, sizeof(node->id));
292 SIMPLEQ_INIT(&node->parent_ids);
293 SIMPLEQ_INIT(&node->child_ids);
294 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
295 err = add_vertex(&node->parent_ids, qid->id);
296 if (err) {
297 free_node(node);
298 return err;
300 node->nparents++;
302 node->commit_timestamp = mktime(&commit->tm_committer);
303 if (node->commit_timestamp == -1) {
304 free_node(node);
305 return got_error_from_errno();
308 err = got_object_idset_add((void **)(&existing_node),
309 graph->node_ids, &node->id, node);
310 if (err == NULL) {
311 add_node_to_iter_list(graph, node, child_node);
312 err = advance_open_branches(graph, node, commit_id, commit);
313 *new_node = node;
314 } else if (err->code == GOT_ERR_OBJ_EXISTS) {
315 err = NULL;
316 free_node(node);
317 node = existing_node;
318 } else {
319 free_node(node);
320 return err;
323 if (child_node) {
324 struct got_object_qid *cid;
326 /* Prevent linking to self. */
327 if (got_object_id_cmp(commit_id, &child_node->id) == 0) {
328 err = got_error(GOT_ERR_BAD_OBJ_ID);
329 goto done;
332 /* Prevent double-linking to the same child (treat as no-op). */
333 SIMPLEQ_FOREACH(cid, &node->child_ids, entry) {
334 if (got_object_id_cmp(cid->id, &child_node->id) == 0)
335 goto free_node;
338 err = add_vertex(&node->child_ids, &child_node->id);
339 if (err)
340 goto done;
341 node->nchildren++;
343 done:
344 if (err) {
345 free_node:
346 if (node != existing_node)
347 free_node(node);
348 *new_node = NULL;
350 return err;
353 const struct got_error *
354 got_commit_graph_open(struct got_commit_graph **graph,
355 struct got_object_id *commit_id, int first_parent_traversal,
356 struct got_repository *repo)
358 const struct got_error *err = NULL;
359 struct got_commit_object *commit;
361 *graph = NULL;
363 err = got_object_open_as_commit(&commit, repo, commit_id);
364 if (err)
365 return err;
367 *graph = alloc_graph();
368 if (*graph == NULL) {
369 got_object_commit_close(commit);
370 return got_error_from_errno();
373 if (first_parent_traversal)
374 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
376 err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL);
377 got_object_commit_close(commit);
378 if (err) {
379 got_commit_graph_close(*graph);
380 *graph = NULL;
381 return err;
384 return NULL;
387 struct got_commit_graph_branch {
388 struct got_object_id parent_id;
389 struct got_commit_graph_node *node;
390 };
392 struct gather_branches_arg {
393 struct got_commit_graph_branch *branches;
394 int nbranches;
395 };
397 static void
398 gather_branches(struct got_object_id *id, void *data, void *arg)
400 struct gather_branches_arg *a = arg;
401 memcpy(&a->branches[a->nbranches].parent_id, id, sizeof(*id));
402 a->branches[a->nbranches].node = data;
403 a->nbranches++;
406 static const struct got_error *
407 fetch_commits_from_open_branches(int *ncommits, int *wanted_id_added,
408 struct got_commit_graph *graph, struct got_repository *repo,
409 struct got_object_id *wanted_id)
411 const struct got_error *err;
412 struct got_commit_graph_branch *branches;
413 struct gather_branches_arg arg;
414 int i;
416 *ncommits = 0;
417 if (wanted_id_added)
418 *wanted_id_added = 0;
420 arg.nbranches = got_object_idset_num_elements(graph->open_branches);
421 if (arg.nbranches == 0)
422 return NULL;
424 /*
425 * Adding nodes to the graph might change the graph's open
426 * branches state. Create a local copy of the current state.
427 */
428 branches = calloc(arg.nbranches, sizeof(*branches));
429 if (branches == NULL)
430 return got_error_from_errno();
431 arg.nbranches = 0; /* reset; gather_branches() will increment */
432 arg.branches = branches;
433 got_object_idset_for_each(graph->open_branches, gather_branches, &arg);
435 for (i = 0; i < arg.nbranches; i++) {
436 struct got_object_id *commit_id;
437 struct got_commit_graph_node *child_node, *new_node;
438 struct got_commit_object *commit;
440 commit_id = &branches[i].parent_id;
441 child_node = branches[i].node;
443 err = got_object_open_as_commit(&commit, repo, commit_id);
444 if (err)
445 break;
447 err = add_node(&new_node, graph, commit_id, commit, child_node);
448 got_object_commit_close(commit);
449 if (err)
450 break;
451 if (new_node)
452 (*ncommits)++;
453 if (wanted_id && got_object_id_cmp(commit_id, wanted_id) == 0)
454 *wanted_id_added = 1;
457 free(branches);
458 return err;
461 const struct got_error *
462 got_commit_graph_fetch_commits(int *nfetched, struct got_commit_graph *graph,
463 int limit, struct got_repository *repo)
465 const struct got_error *err;
466 int ncommits;
468 *nfetched = 0;
470 while (*nfetched < limit) {
471 err = fetch_commits_from_open_branches(&ncommits, NULL,
472 graph, repo, NULL);
473 if (err)
474 return err;
475 if (ncommits == 0)
476 break;
477 *nfetched += ncommits;
480 return NULL;
483 const struct got_error *
484 got_commit_graph_fetch_commits_up_to(int *nfetched,
485 struct got_commit_graph *graph, struct got_object_id *wanted_id,
486 struct got_repository *repo)
488 const struct got_error *err;
489 int ncommits, wanted_id_added = 0;
491 *nfetched = 0;
493 if (got_object_idset_get(graph->node_ids, wanted_id) != NULL)
494 return NULL;
496 while (!wanted_id_added) {
497 err = fetch_commits_from_open_branches(&ncommits,
498 &wanted_id_added, graph, repo, wanted_id);
499 if (err)
500 return err;
501 if (ncommits == 0)
502 return NULL;
503 *nfetched += ncommits;
506 return NULL;
509 static void
510 free_node_iter(struct got_object_id *id, void *data, void *arg)
512 struct got_commit_graph_node *node = data;
513 free_node(node);
516 void
517 got_commit_graph_close(struct got_commit_graph *graph)
519 got_object_idset_free(graph->open_branches);
520 got_object_idset_for_each(graph->node_ids, free_node_iter, NULL);
521 got_object_idset_free(graph->node_ids);
522 free(graph);
525 const struct got_error *
526 got_commit_graph_iter_start(struct got_commit_graph *graph,
527 struct got_object_id *id)
529 struct got_commit_graph_node *start_node;
531 start_node = got_object_idset_get(graph->node_ids, id);
532 if (start_node == NULL)
533 return got_error(GOT_ERR_NO_OBJ);
535 graph->iter_node = start_node;
536 return NULL;
539 const struct got_error *
540 got_commit_graph_iter_next(struct got_object_id **id,
541 struct got_commit_graph *graph)
543 *id = NULL;
545 if (graph->iter_node == NULL) {
546 /* We are done interating, or iteration was not started. */
547 return got_error(GOT_ERR_ITER_COMPLETED);
550 if (graph->iter_node ==
551 TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
552 got_object_idset_num_elements(graph->open_branches) == 0) {
553 *id = &graph->iter_node->id;
554 /* We are done interating. */
555 graph->iter_node = NULL;
556 return NULL;
559 if (TAILQ_NEXT(graph->iter_node, entry) == NULL)
560 return got_error(GOT_ERR_ITER_NEED_MORE);
562 *id = &graph->iter_node->id;
563 graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);
564 return NULL;