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(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 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 = mktime(&commit->tm_committer);
285 if (node->commit_timestamp == -1)
286 return got_error_from_errno();
288 err = got_object_idset_add((void **)(&existing_node),
289 graph->node_ids, &node->id, node);
290 if (err == NULL) {
291 add_node_to_iter_list(graph, node, child_node);
292 err = advance_open_branches(graph, node, commit_id, commit);
293 *new_node = node;
294 } else if (err->code == GOT_ERR_OBJ_EXISTS) {
295 err = NULL;
296 free(node);
297 node = existing_node;
298 } else {
299 free(node);
300 return err;
303 if (child_node) {
304 struct got_object_qid *cid;
306 /* Prevent linking to self. */
307 if (got_object_id_cmp(commit_id, &child_node->id) == 0)
308 return got_error(GOT_ERR_BAD_OBJ_ID);
310 /* Prevent double-linking to the same child. */
311 SIMPLEQ_FOREACH(cid, &node->child_ids, entry) {
312 if (got_object_id_cmp(cid->id, &child_node->id) == 0)
313 return got_error(GOT_ERR_BAD_OBJ_ID);
316 err = add_vertex(&node->child_ids, &child_node->id);
317 if (err)
318 return err;
319 node->nchildren++;
322 return err;
325 const struct got_error *
326 got_commit_graph_open(struct got_commit_graph **graph,
327 struct got_object_id *commit_id, int first_parent_traversal,
328 struct got_repository *repo)
330 const struct got_error *err = NULL;
331 struct got_commit_object *commit;
333 *graph = NULL;
335 err = got_object_open_as_commit(&commit, repo, commit_id);
336 if (err)
337 return err;
339 *graph = alloc_graph();
340 if (*graph == NULL) {
341 got_object_commit_close(commit);
342 return got_error_from_errno();
345 if (first_parent_traversal)
346 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
348 err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL);
349 got_object_commit_close(commit);
350 if (err) {
351 got_commit_graph_close(*graph);
352 *graph = NULL;
353 return err;
356 return NULL;
359 struct got_commit_graph_branch {
360 struct got_object_id parent_id;
361 struct got_commit_graph_node *node;
362 };
364 struct gather_branches_arg {
365 struct got_commit_graph_branch *branches;
366 int nbranches;
367 };
369 static void
370 gather_branches(struct got_object_id *id, void *data, void *arg)
372 struct gather_branches_arg *a = arg;
373 memcpy(&a->branches[a->nbranches].parent_id, id, sizeof(*id));
374 a->branches[a->nbranches].node = data;
375 a->nbranches++;
378 static const struct got_error *
379 fetch_commits_from_open_branches(int *ncommits, int *wanted_id_added,
380 struct got_commit_graph *graph, struct got_repository *repo,
381 struct got_object_id *wanted_id)
383 const struct got_error *err;
384 struct got_commit_graph_branch *branches;
385 struct gather_branches_arg arg;
386 int i;
388 *ncommits = 0;
389 if (wanted_id_added)
390 *wanted_id_added = 0;
392 arg.nbranches = got_object_idset_num_elements(graph->open_branches);
393 if (arg.nbranches == 0)
394 return NULL;
396 /*
397 * Adding nodes to the graph might change the graph's open
398 * branches state. Create a local copy of the current state.
399 */
400 branches = calloc(arg.nbranches, sizeof(*branches));
401 if (branches == NULL)
402 return got_error_from_errno();
403 arg.nbranches = 0; /* reset; gather_branches() will increment */
404 arg.branches = branches;
405 got_object_idset_for_each(graph->open_branches, gather_branches, &arg);
407 for (i = 0; i < arg.nbranches; i++) {
408 struct got_object_id *commit_id;
409 struct got_commit_graph_node *child_node, *new_node;
410 struct got_commit_object *commit;
412 commit_id = &branches[i].parent_id;
413 child_node = branches[i].node;
415 err = got_object_open_as_commit(&commit, repo, commit_id);
416 if (err)
417 break;
419 err = add_node(&new_node, graph, commit_id, commit, child_node);
420 got_object_commit_close(commit);
421 if (err) {
422 if (err->code != GOT_ERR_OBJ_EXISTS)
423 break;
424 err = NULL;
426 if (new_node)
427 (*ncommits)++;
428 if (wanted_id && got_object_id_cmp(commit_id, wanted_id) == 0)
429 *wanted_id_added = 1;
432 free(branches);
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_graph_node(struct got_object_id *id, void *data, void *arg)
487 struct got_commit_graph_node *node = data;
488 while (!SIMPLEQ_EMPTY(&node->child_ids)) {
489 struct got_object_qid *child = SIMPLEQ_FIRST(&node->child_ids);
490 SIMPLEQ_REMOVE_HEAD(&node->child_ids, entry);
491 free(child);
493 free(node);
496 void
497 got_commit_graph_close(struct got_commit_graph *graph)
499 got_object_idset_free(graph->open_branches);
500 got_object_idset_for_each(graph->node_ids, free_graph_node, NULL);
501 got_object_idset_free(graph->node_ids);
502 free(graph);
505 const struct got_error *
506 got_commit_graph_iter_start(struct got_commit_graph *graph,
507 struct got_object_id *id)
509 struct got_commit_graph_node *start_node;
511 start_node = got_object_idset_get(graph->node_ids, id);
512 if (start_node == NULL)
513 return got_error(GOT_ERR_NO_OBJ);
515 graph->iter_node = start_node;
516 return NULL;
519 const struct got_error *
520 got_commit_graph_iter_next(struct got_object_id **id,
521 struct got_commit_graph *graph)
523 *id = NULL;
525 if (graph->iter_node == NULL) {
526 /* We are done interating, or iteration was not started. */
527 return got_error(GOT_ERR_ITER_COMPLETED);
530 if (graph->iter_node ==
531 TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
532 got_object_idset_num_elements(graph->open_branches) == 0) {
533 *id = &graph->iter_node->id;
534 /* We are done interating. */
535 graph->iter_node = NULL;
536 return NULL;
539 if (TAILQ_NEXT(graph->iter_node, entry) == NULL)
540 return got_error(GOT_ERR_ITER_NEED_MORE);
542 *id = &graph->iter_node->id;
543 graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);
544 return NULL;