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 an adjacency list.
44 * Adjacencies of a graph node are parent (older) commits.
45 */
46 int nparents;
47 struct got_object_id_queue parent_ids;
49 time_t commit_timestamp;
51 /* Used during graph iteration. */
52 TAILQ_ENTRY(got_commit_graph_node) entry;
53 };
55 TAILQ_HEAD(got_commit_graph_iter_list, got_commit_graph_node);
57 struct got_commit_graph_branch_tip {
58 struct got_object_id id;
59 struct got_commit_graph_node *node;
60 };
62 struct got_commit_graph {
63 /* The set of all commits we have traversed. */
64 struct got_object_idset *node_ids;
66 /* The commit at which traversal began (youngest commit in node_ids). */
67 struct got_commit_graph_node *head_node;
69 int flags;
70 #define GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL 0x01
72 /*
73 * A set of object IDs of known parent commits which we have not yet
74 * traversed. Each commit ID in this set represents a branch in commit
75 * history: Either the first-parent branch of the head node, or another
76 * branch corresponding to a traversed merge commit for which we have
77 * not traversed a branch point commit yet.
78 *
79 * Whenever we add a commit with a matching ID to the graph, we remove
80 * its corresponding element from this set, and add new elements for
81 * each of that commit's parent commits which were not traversed yet.
82 *
83 * When API users ask us to fetch more commits, we fetch commits from
84 * all currently open branches. This allows API users to process
85 * commits in linear order even though the history contains branches.
86 */
87 struct got_object_idset *open_branches;
89 /* Copy of known branch tips for fetch_commits_from_open_branches(). */
90 struct got_commit_graph_branch_tip *tips;
91 size_t ntips;
92 #define GOT_COMMIT_GRAPH_MIN_TIPS 100 /* minimum amount of tips to allocate */
94 /* The next commit to return when the API user asks for one. */
95 struct got_commit_graph_node *iter_node;
97 /* The graph iteration list contains all nodes in sorted order. */
98 struct got_commit_graph_iter_list iter_list;
99 };
101 static struct got_commit_graph *
102 alloc_graph(void)
104 struct got_commit_graph *graph;
106 graph = calloc(1, sizeof(*graph));
107 if (graph == NULL)
108 return NULL;
110 graph->node_ids = got_object_idset_alloc();
111 if (graph->node_ids == NULL) {
112 free(graph);
113 return NULL;
116 graph->open_branches = got_object_idset_alloc();
117 if (graph->open_branches == NULL) {
118 got_object_idset_free(graph->node_ids);
119 free(graph);
120 return NULL;
123 TAILQ_INIT(&graph->iter_list);
124 return graph;
127 #if 0
128 static int
129 is_head_node(struct got_commit_graph_node *node)
131 return node->nchildren == 0;
134 static int
135 is_merge_point(struct got_commit_graph_node *node)
137 return node->nparents > 1;
140 int
141 is_branch_point(struct got_commit_graph_node *node)
143 return node->nchildren > 1;
146 static int
147 is_root_node(struct got_commit_graph_node *node)
149 return node->nparents == 0;
151 #endif
153 static void
154 add_node_to_iter_list(struct got_commit_graph *graph,
155 struct got_commit_graph_node *node,
156 struct got_commit_graph_node *child_node)
158 struct got_commit_graph_node *n, *next;
160 if (TAILQ_EMPTY(&graph->iter_list)) {
161 TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
162 return;
165 /* Ensure that an iteration in progress will see this new commit. */
166 if (graph->iter_node) {
167 n = graph->iter_node;
168 while (n) {
169 next = TAILQ_NEXT(n, entry);
170 if (next &&
171 node->commit_timestamp >= next->commit_timestamp) {
172 TAILQ_INSERT_BEFORE(next, node, entry);
173 return;
175 n = next;
177 TAILQ_INSERT_AFTER(&graph->iter_list, graph->iter_node,
178 node, entry);
179 return;
182 /*
183 * If a child node is known, begin looping over the list there
184 * instead of beginning from the list head.
185 */
186 n = child_node ? child_node : TAILQ_FIRST(&graph->iter_list);
188 /* Insert into list based on committer timestamp. */
189 do {
190 if (node->commit_timestamp == n->commit_timestamp) {
191 TAILQ_INSERT_AFTER(&graph->iter_list, n, node, entry);
192 break;
193 } else if (node->commit_timestamp < n->commit_timestamp) {
194 next = TAILQ_NEXT(n, entry);
195 if (next == NULL) {
196 TAILQ_INSERT_AFTER(&graph->iter_list, n,
197 node, entry);
198 break;
200 if (node->commit_timestamp >= next->commit_timestamp) {
201 TAILQ_INSERT_BEFORE(next, node, entry);
202 break;
204 } else {
205 TAILQ_INSERT_BEFORE(n, node, entry);
206 break;
208 n = TAILQ_NEXT(n, entry);
209 } while (n);
212 static const struct got_error *
213 add_vertex(struct got_object_id_queue *ids, struct got_object_id *id)
215 struct got_object_qid *qid;
217 qid = calloc(1, sizeof(*qid));
218 if (qid == NULL)
219 return got_error_from_errno();
221 qid->id = got_object_id_dup(id);
222 if (qid->id == NULL) {
223 const struct got_error *err = got_error_from_errno();
224 got_object_qid_free(qid);
225 return err;
228 SIMPLEQ_INSERT_TAIL(ids, qid, entry);
229 return NULL;
232 static const struct got_error *
233 advance_open_branches(struct got_commit_graph *graph,
234 struct got_commit_graph_node *node,
235 struct got_object_id *commit_id, struct got_commit_object *commit)
237 const struct got_error *err;
238 struct got_object_qid *qid;
240 err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
241 if (err && err->code != GOT_ERR_NO_OBJ)
242 return err;
244 if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
245 qid = SIMPLEQ_FIRST(&commit->parent_ids);
246 if (qid == NULL)
247 return NULL;
248 err = got_object_idset_add(NULL, graph->open_branches, qid->id,
249 node);
250 if (err && err->code != GOT_ERR_OBJ_EXISTS)
251 return err;
252 return NULL;
255 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
256 if (got_object_idset_get(graph->node_ids, qid->id))
257 continue; /* parent already traversed */
258 err = got_object_idset_add(NULL, graph->open_branches,
259 qid->id, node);
260 if (err && err->code != GOT_ERR_OBJ_EXISTS)
261 return err;
264 return NULL;
267 static void
268 free_node(struct got_commit_graph_node *node)
270 while (!SIMPLEQ_EMPTY(&node->parent_ids)) {
271 struct got_object_qid *pid = SIMPLEQ_FIRST(&node->parent_ids);
272 SIMPLEQ_REMOVE_HEAD(&node->parent_ids, entry);
273 got_object_qid_free(pid);
275 free(node);
278 static const struct got_error *
279 add_node(struct got_commit_graph_node **new_node,
280 struct got_commit_graph *graph, struct got_object_id *commit_id,
281 struct got_commit_object *commit, struct got_commit_graph_node *child_node)
283 const struct got_error *err = NULL;
284 struct got_commit_graph_node *node;
285 struct got_object_qid *pid;
287 *new_node = NULL;
289 node = calloc(1, sizeof(*node));
290 if (node == NULL)
291 return got_error_from_errno();
293 memcpy(&node->id, commit_id, sizeof(node->id));
294 SIMPLEQ_INIT(&node->parent_ids);
295 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
296 err = add_vertex(&node->parent_ids, pid->id);
297 if (err) {
298 free_node(node);
299 return err;
301 node->nparents++;
303 node->commit_timestamp = mktime(&commit->tm_committer);
304 if (node->commit_timestamp == -1) {
305 free_node(node);
306 return got_error_from_errno();
309 err = got_object_idset_add(NULL, graph->node_ids, &node->id, node);
310 if (err) {
311 if (err->code == GOT_ERR_OBJ_EXISTS)
312 err = NULL;
313 free_node(node);
314 return err;
317 add_node_to_iter_list(graph, node, child_node);
318 err = advance_open_branches(graph, node, commit_id, commit);
319 if (err)
320 free_node(node);
321 else
322 *new_node = node;
324 return err;
327 const struct got_error *
328 got_commit_graph_open(struct got_commit_graph **graph,
329 struct got_object_id *commit_id, int first_parent_traversal,
330 struct got_repository *repo)
332 const struct got_error *err = NULL;
333 struct got_commit_object *commit;
335 *graph = NULL;
337 err = got_object_open_as_commit(&commit, repo, commit_id);
338 if (err)
339 return err;
341 *graph = alloc_graph();
342 if (*graph == NULL) {
343 got_object_commit_close(commit);
344 return got_error_from_errno();
347 if (first_parent_traversal)
348 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
350 err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL);
351 got_object_commit_close(commit);
352 if (err) {
353 got_commit_graph_close(*graph);
354 *graph = NULL;
355 return err;
358 return NULL;
361 struct gather_branch_tips_arg {
362 struct got_commit_graph_branch_tip *tips;
363 int ntips;
364 };
366 static void
367 gather_branch_tips(struct got_object_id *id, void *data, void *arg)
369 struct gather_branch_tips_arg *a = arg;
370 memcpy(&a->tips[a->ntips].id, id, sizeof(*id));
371 a->tips[a->ntips].node = data;
372 a->ntips++;
375 static const struct got_error *
376 fetch_commits_from_open_branches(int *ncommits, int *wanted_id_added,
377 struct got_commit_graph *graph, struct got_repository *repo,
378 struct got_object_id *wanted_id)
380 const struct got_error *err;
381 struct gather_branch_tips_arg arg;
382 int i;
384 *ncommits = 0;
385 if (wanted_id_added)
386 *wanted_id_added = 0;
388 arg.ntips = got_object_idset_num_elements(graph->open_branches);
389 if (arg.ntips == 0)
390 return NULL;
392 /*
393 * Adding nodes to the graph might change the graph's open
394 * branches state. Create a local copy of the current state.
395 */
396 if (graph->ntips < arg.ntips) {
397 struct got_commit_graph_branch_tip *tips;
398 if (arg.ntips < GOT_COMMIT_GRAPH_MIN_TIPS)
399 arg.ntips = GOT_COMMIT_GRAPH_MIN_TIPS;
400 tips = reallocarray(graph->tips, arg.ntips, sizeof(*tips));
401 if (tips == NULL)
402 return got_error_from_errno();
403 graph->tips = tips;
404 graph->ntips = arg.ntips;
406 arg.ntips = 0; /* reset; gather_branch_tips() will increment */
407 arg.tips = graph->tips;
408 got_object_idset_for_each(graph->open_branches,
409 gather_branch_tips, &arg);
411 for (i = 0; i < arg.ntips; i++) {
412 struct got_object_id *commit_id;
413 struct got_commit_graph_node *child_node, *new_node;
414 struct got_commit_object *commit;
416 commit_id = &graph->tips[i].id;
417 child_node = graph->tips[i].node;
419 err = got_object_open_as_commit(&commit, repo, commit_id);
420 if (err)
421 break;
423 err = add_node(&new_node, graph, commit_id, commit, child_node);
424 got_object_commit_close(commit);
425 if (err)
426 break;
427 if (new_node)
428 (*ncommits)++;
429 if (wanted_id && got_object_id_cmp(commit_id, wanted_id) == 0)
430 *wanted_id_added = 1;
433 return err;
436 const struct got_error *
437 got_commit_graph_fetch_commits(int *nfetched, struct got_commit_graph *graph,
438 int limit, struct got_repository *repo)
440 const struct got_error *err;
441 int ncommits;
443 *nfetched = 0;
445 while (*nfetched < limit) {
446 err = fetch_commits_from_open_branches(&ncommits, NULL,
447 graph, repo, NULL);
448 if (err)
449 return err;
450 if (ncommits == 0)
451 break;
452 *nfetched += ncommits;
455 return NULL;
458 const struct got_error *
459 got_commit_graph_fetch_commits_up_to(int *nfetched,
460 struct got_commit_graph *graph, struct got_object_id *wanted_id,
461 struct got_repository *repo)
463 const struct got_error *err;
464 int ncommits, wanted_id_added = 0;
466 *nfetched = 0;
468 if (got_object_idset_get(graph->node_ids, wanted_id) != NULL)
469 return NULL;
471 while (!wanted_id_added) {
472 err = fetch_commits_from_open_branches(&ncommits,
473 &wanted_id_added, graph, repo, wanted_id);
474 if (err)
475 return err;
476 if (ncommits == 0)
477 return NULL;
478 *nfetched += ncommits;
481 return NULL;
484 static void
485 free_node_iter(struct got_object_id *id, void *data, void *arg)
487 struct got_commit_graph_node *node = data;
488 free_node(node);
491 void
492 got_commit_graph_close(struct got_commit_graph *graph)
494 got_object_idset_free(graph->open_branches);
495 got_object_idset_for_each(graph->node_ids, free_node_iter, NULL);
496 got_object_idset_free(graph->node_ids);
497 free(graph->tips);
498 free(graph);
501 const struct got_error *
502 got_commit_graph_iter_start(struct got_commit_graph *graph,
503 struct got_object_id *id)
505 struct got_commit_graph_node *start_node;
507 start_node = got_object_idset_get(graph->node_ids, id);
508 if (start_node == NULL)
509 return got_error(GOT_ERR_NO_OBJ);
511 graph->iter_node = start_node;
512 return NULL;
515 const struct got_error *
516 got_commit_graph_iter_next(struct got_object_id **id,
517 struct got_commit_graph *graph)
519 *id = NULL;
521 if (graph->iter_node == NULL) {
522 /* We are done interating, or iteration was not started. */
523 return got_error(GOT_ERR_ITER_COMPLETED);
526 if (graph->iter_node ==
527 TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
528 got_object_idset_num_elements(graph->open_branches) == 0) {
529 *id = &graph->iter_node->id;
530 /* We are done interating. */
531 graph->iter_node = NULL;
532 return NULL;
535 if (TAILQ_NEXT(graph->iter_node, entry) == NULL)
536 return got_error(GOT_ERR_ITER_NEED_MORE);
538 *id = &graph->iter_node->id;
539 graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);
540 return NULL;