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 GOT_OBJECT_IDSET_ITERATE_BY_OBJECT_ID);
105 if (graph->node_ids == NULL) {
106 free(graph);
107 return NULL;
110 graph->open_branches = got_object_idset_alloc(
111 GOT_OBJECT_IDSET_ITERATE_BY_OBJECT_ID);
112 if (graph->open_branches == NULL) {
113 got_object_idset_free(graph->node_ids);
114 free(graph);
115 return NULL;
118 TAILQ_INIT(&graph->iter_list);
119 return graph;
122 #if 0
123 static int
124 is_head_node(struct got_commit_graph_node *node)
126 return node->nchildren == 0;
129 static int
130 is_merge_point(struct got_commit_graph_node *node)
132 return node->nparents > 1;
135 int
136 is_branch_point(struct got_commit_graph_node *node)
138 return node->nchildren > 1;
141 static int
142 is_root_node(struct got_commit_graph_node *node)
144 return node->nparents == 0;
146 #endif
148 static void
149 add_node_to_iter_list(struct got_commit_graph *graph,
150 struct got_commit_graph_node *node,
151 struct got_commit_graph_node *child_node)
153 struct got_commit_graph_node *n, *next;
155 if (TAILQ_EMPTY(&graph->iter_list)) {
156 TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
157 return;
160 /* Ensure that an iteration in progress will see this new commit. */
161 if (graph->iter_node) {
162 n = graph->iter_node;
163 while (n) {
164 next = TAILQ_NEXT(n, entry);
165 if (next &&
166 node->commit_timestamp >= next->commit_timestamp) {
167 TAILQ_INSERT_BEFORE(next, node, entry);
168 return;
170 n = next;
172 TAILQ_INSERT_AFTER(&graph->iter_list, graph->iter_node,
173 node, entry);
174 return;
177 /*
178 * If a child node is known, begin looping over the list there
179 * instead of beginning from the list head.
180 */
181 n = child_node ? child_node : TAILQ_FIRST(&graph->iter_list);
183 /* Insert into list based on committer timestamp. */
184 do {
185 if (node->commit_timestamp == n->commit_timestamp) {
186 TAILQ_INSERT_AFTER(&graph->iter_list, n, node, entry);
187 break;
188 } else if (node->commit_timestamp < n->commit_timestamp) {
189 next = TAILQ_NEXT(n, entry);
190 if (next == NULL) {
191 TAILQ_INSERT_AFTER(&graph->iter_list, n,
192 node, entry);
193 break;
195 if (node->commit_timestamp >= next->commit_timestamp) {
196 TAILQ_INSERT_BEFORE(next, node, entry);
197 break;
199 } else {
200 TAILQ_INSERT_BEFORE(n, node, entry);
201 break;
203 n = TAILQ_NEXT(n, entry);
204 } while (n);
207 static const struct got_error *
208 add_vertex(struct got_object_id_queue *ids, struct got_object_id *id)
210 struct got_object_qid *qid;
212 qid = calloc(1, sizeof(*qid));
213 if (qid == NULL)
214 return got_error_from_errno();
216 qid->id = got_object_id_dup(id);
217 if (qid->id == NULL) {
218 const struct got_error *err = got_error_from_errno();
219 free(qid);
220 return err;
223 SIMPLEQ_INSERT_TAIL(ids, qid, entry);
224 return NULL;
227 static const struct got_error *
228 advance_open_branches(struct got_commit_graph *graph,
229 struct got_commit_graph_node *node,
230 struct got_object_id *commit_id, struct got_commit_object *commit)
232 const struct got_error *err;
233 struct got_object_qid *qid;
235 err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
236 if (err && err->code != GOT_ERR_NO_OBJ)
237 return err;
239 if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
240 qid = SIMPLEQ_FIRST(&commit->parent_ids);
241 if (qid == NULL)
242 return NULL;
243 err = got_object_idset_add(NULL, graph->open_branches, qid->id,
244 node);
245 if (err && err->code != GOT_ERR_OBJ_EXISTS)
246 return err;
247 return NULL;
250 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
251 if (got_object_idset_get(graph->node_ids, qid->id))
252 continue; /* parent already traversed */
253 err = got_object_idset_add(NULL, graph->open_branches,
254 qid->id, node);
255 if (err && err->code != GOT_ERR_OBJ_EXISTS)
256 return err;
259 return NULL;
262 static const struct got_error *
263 add_node(struct got_commit_graph_node **new_node,
264 struct got_commit_graph *graph, struct got_object_id *commit_id,
265 struct got_commit_object *commit, struct got_commit_graph_node *child_node)
267 const struct got_error *err = NULL;
268 struct got_commit_graph_node *node, *existing_node;
269 struct got_object_qid *qid;
271 *new_node = NULL;
273 node = calloc(1, sizeof(*node));
274 if (node == NULL)
275 return got_error_from_errno();
277 memcpy(&node->id, commit_id, sizeof(node->id));
278 SIMPLEQ_INIT(&node->parent_ids);
279 SIMPLEQ_INIT(&node->child_ids);
280 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
281 err = add_vertex(&node->parent_ids, qid->id);
282 if (err)
283 return err;
284 node->nparents++;
286 node->commit_timestamp = mktime(&commit->tm_committer);
287 if (node->commit_timestamp == -1)
288 return got_error_from_errno();
290 err = got_object_idset_add((void **)(&existing_node),
291 graph->node_ids, &node->id, node);
292 if (err == NULL) {
293 add_node_to_iter_list(graph, node, child_node);
294 err = advance_open_branches(graph, node, commit_id, commit);
295 *new_node = node;
296 } else if (err->code == GOT_ERR_OBJ_EXISTS) {
297 err = NULL;
298 free(node);
299 node = existing_node;
300 } else {
301 free(node);
302 return err;
305 if (child_node) {
306 struct got_object_qid *cid;
308 /* Prevent linking to self. */
309 if (got_object_id_cmp(commit_id, &child_node->id) == 0)
310 return got_error(GOT_ERR_BAD_OBJ_ID);
312 /* Prevent double-linking to the same child. */
313 SIMPLEQ_FOREACH(cid, &node->child_ids, entry) {
314 if (got_object_id_cmp(cid->id, &child_node->id) == 0)
315 return got_error(GOT_ERR_BAD_OBJ_ID);
318 err = add_vertex(&node->child_ids, &child_node->id);
319 if (err)
320 return err;
321 node->nchildren++;
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 got_commit_graph_branch {
362 struct got_object_id parent_id;
363 struct got_commit_graph_node *node;
364 };
366 struct gather_branches_arg {
367 struct got_commit_graph_branch *branches;
368 int nbranches;
369 };
371 static void
372 gather_branches(struct got_object_id *id, void *data, void *arg)
374 struct gather_branches_arg *a = arg;
375 memcpy(&a->branches[a->nbranches].parent_id, id, sizeof(*id));
376 a->branches[a->nbranches].node = data;
377 a->nbranches++;
380 static const struct got_error *
381 fetch_commits_from_open_branches(int *ncommits, int *wanted_id_added,
382 struct got_commit_graph *graph, struct got_repository *repo,
383 struct got_object_id *wanted_id)
385 const struct got_error *err;
386 struct got_commit_graph_branch *branches;
387 struct gather_branches_arg arg;
388 int i;
390 *ncommits = 0;
391 if (wanted_id_added)
392 *wanted_id_added = 0;
394 arg.nbranches = got_object_idset_num_elements(graph->open_branches);
395 if (arg.nbranches == 0)
396 return NULL;
398 /*
399 * Adding nodes to the graph might change the graph's open
400 * branches state. Create a local copy of the current state.
401 */
402 branches = calloc(arg.nbranches, sizeof(*branches));
403 if (branches == NULL)
404 return got_error_from_errno();
405 arg.nbranches = 0; /* reset; gather_branches() will increment */
406 arg.branches = branches;
407 got_object_idset_for_each(graph->open_branches, gather_branches, &arg);
409 for (i = 0; i < arg.nbranches; i++) {
410 struct got_object_id *commit_id;
411 struct got_commit_graph_node *child_node, *new_node;
412 struct got_commit_object *commit;
414 commit_id = &branches[i].parent_id;
415 child_node = branches[i].node;
417 err = got_object_open_as_commit(&commit, repo, commit_id);
418 if (err)
419 break;
421 err = add_node(&new_node, graph, commit_id, commit, child_node);
422 got_object_commit_close(commit);
423 if (err) {
424 if (err->code != GOT_ERR_OBJ_EXISTS)
425 break;
426 err = NULL;
428 if (new_node)
429 (*ncommits)++;
430 if (wanted_id && got_object_id_cmp(commit_id, wanted_id) == 0)
431 *wanted_id_added = 1;
434 free(branches);
435 return err;
438 const struct got_error *
439 got_commit_graph_fetch_commits(int *nfetched, struct got_commit_graph *graph,
440 int limit, struct got_repository *repo)
442 const struct got_error *err;
443 int ncommits;
445 *nfetched = 0;
447 while (*nfetched < limit) {
448 err = fetch_commits_from_open_branches(&ncommits, NULL,
449 graph, repo, NULL);
450 if (err)
451 return err;
452 if (ncommits == 0)
453 break;
454 *nfetched += ncommits;
457 return NULL;
460 const struct got_error *
461 got_commit_graph_fetch_commits_up_to(int *nfetched,
462 struct got_commit_graph *graph, struct got_object_id *wanted_id,
463 struct got_repository *repo)
465 const struct got_error *err;
466 int ncommits, wanted_id_added = 0;
468 *nfetched = 0;
470 if (got_object_idset_get(graph->node_ids, wanted_id) != NULL)
471 return NULL;
473 while (!wanted_id_added) {
474 err = fetch_commits_from_open_branches(&ncommits,
475 &wanted_id_added, graph, repo, wanted_id);
476 if (err)
477 return err;
478 if (ncommits == 0)
479 return NULL;
480 *nfetched += ncommits;
483 return NULL;
486 static void
487 free_graph_node(struct got_object_id *id, void *data, void *arg)
489 struct got_commit_graph_node *node = data;
490 while (!SIMPLEQ_EMPTY(&node->child_ids)) {
491 struct got_object_qid *child = SIMPLEQ_FIRST(&node->child_ids);
492 SIMPLEQ_REMOVE_HEAD(&node->child_ids, entry);
493 free(child);
495 free(node);
498 void
499 got_commit_graph_close(struct got_commit_graph *graph)
501 got_object_idset_free(graph->open_branches);
502 got_object_idset_for_each(graph->node_ids, free_graph_node, NULL);
503 got_object_idset_free(graph->node_ids);
504 free(graph);
507 const struct got_error *
508 got_commit_graph_iter_start(struct got_commit_graph *graph,
509 struct got_object_id *id)
511 struct got_commit_graph_node *start_node;
513 start_node = got_object_idset_get(graph->node_ids, id);
514 if (start_node == NULL)
515 return got_error(GOT_ERR_NO_OBJ);
517 graph->iter_node = start_node;
518 return NULL;
521 const struct got_error *
522 got_commit_graph_iter_next(struct got_object_id **id,
523 struct got_commit_graph *graph)
525 *id = NULL;
527 if (graph->iter_node == NULL) {
528 /* We are done interating, or iteration was not started. */
529 return got_error(GOT_ERR_ITER_COMPLETED);
532 if (graph->iter_node ==
533 TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
534 got_object_idset_num_elements(graph->open_branches) == 0) {
535 *id = &graph->iter_node->id;
536 /* We are done interating. */
537 graph->iter_node = NULL;
538 return NULL;
541 if (TAILQ_NEXT(graph->iter_node, entry) == NULL)
542 return got_error(GOT_ERR_ITER_NEED_MORE);
544 *id = &graph->iter_node->id;
545 graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);
546 return NULL;