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 /*
68 * A set of object IDs of known parent commits which we have not yet
69 * traversed. Each commit ID in this set represents a branch in commit
70 * history: Either the first-parent branch of the head node, or another
71 * branch corresponding to a traversed merge commit for which we have
72 * not traversed a branch point commit yet.
73 *
74 * Whenever we add a commit with a matching ID to the graph, we remove
75 * its corresponding element from this set, and add new elements for
76 * each of that commit's parent commits which were not traversed yet.
77 *
78 * When API users ask us to fetch more commits, we fetch commits from
79 * all currently open branches. This allows API users to process
80 * commits in linear order even though the history contains branches.
81 */
82 struct got_object_idset *open_branches;
84 /* The next commit to return when the API user asks for one. */
85 struct got_commit_graph_node *iter_node;
87 /* The graph iteration list contains all nodes in sorted order. */
88 struct got_commit_graph_iter_list iter_list;
89 };
91 static struct got_commit_graph *
92 alloc_graph(void)
93 {
94 struct got_commit_graph *graph;
96 graph = calloc(1, sizeof(*graph));
97 if (graph == NULL)
98 return NULL;
100 graph->node_ids = got_object_idset_alloc();
101 if (graph->node_ids == NULL) {
102 free(graph);
103 return NULL;
106 graph->open_branches = got_object_idset_alloc();
107 if (graph->open_branches == NULL) {
108 got_object_idset_free(graph->node_ids);
109 free(graph);
110 return NULL;
113 TAILQ_INIT(&graph->iter_list);
114 return graph;
117 #if 0
118 static int
119 is_head_node(struct got_commit_graph_node *node)
121 return node->nchildren == 0;
124 static int
125 is_merge_point(struct got_commit_graph_node *node)
127 return node->nparents > 1;
130 int
131 is_branch_point(struct got_commit_graph_node *node)
133 return node->nchildren > 1;
136 static int
137 is_root_node(struct got_commit_graph_node *node)
139 return node->nparents == 0;
141 #endif
143 static void
144 add_node_to_iter_list(struct got_commit_graph *graph,
145 struct got_commit_graph_node *node,
146 struct got_commit_graph_node *child_node)
148 struct got_commit_graph_node *n, *next;
150 if (TAILQ_EMPTY(&graph->iter_list)) {
151 TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
152 return;
155 /*
156 * If a child node is known, begin looping over the list there
157 * instead of beginning from the list head. Parent commits are
158 * traversed before their children.
159 */
160 n = child_node ? child_node : TAILQ_FIRST(&graph->iter_list);
162 /* Ensure that an iteration in progress will see this new commit. */
163 if (graph->iter_node) {
164 n = graph->iter_node;
165 do {
166 next = TAILQ_NEXT(n, entry);
167 if (next &&
168 node->commit_timestamp >= next->commit_timestamp) {
169 TAILQ_INSERT_BEFORE(next, node, entry);
170 return;
172 n = next;
173 } while (next);
175 TAILQ_INSERT_AFTER(&graph->iter_list, graph->iter_node,
176 node, entry);
177 return;
180 /* Insert into list based on committer timestamp. */
181 do {
182 if (node->commit_timestamp == n->commit_timestamp) {
183 TAILQ_INSERT_AFTER(&graph->iter_list, n, node, entry);
184 break;
185 } else if (node->commit_timestamp < n->commit_timestamp) {
186 next = TAILQ_NEXT(n, entry);
187 if (next == NULL) {
188 TAILQ_INSERT_AFTER(&graph->iter_list, n,
189 node, entry);
190 break;
192 if (node->commit_timestamp >= next->commit_timestamp) {
193 TAILQ_INSERT_BEFORE(next, node, entry);
194 break;
196 } else {
197 TAILQ_INSERT_BEFORE(n, node, entry);
198 break;
200 n = TAILQ_NEXT(n, entry);
201 } while (n);
204 static const struct got_error *
205 add_vertex(struct got_object_id_queue *ids, struct got_object_id *id)
207 struct got_object_qid *qid;
209 qid = calloc(1, sizeof(*qid));
210 if (qid == NULL)
211 return got_error_from_errno();
213 qid->id = got_object_id_dup(id);
214 if (qid->id == NULL) {
215 const struct got_error *err = got_error_from_errno();
216 free(qid);
217 return err;
220 SIMPLEQ_INSERT_TAIL(ids, qid, entry);
221 return NULL;
224 static const struct got_error *
225 add_node(struct got_commit_graph_node **new_node,
226 struct got_commit_graph *graph, struct got_object_id *commit_id,
227 struct got_commit_object *commit, struct got_commit_graph_node *child_node)
229 const struct got_error *err = NULL;
230 struct got_commit_graph_node *node, *existing_node;
231 struct got_object_qid *qid;
233 *new_node = NULL;
235 node = calloc(1, sizeof(*node));
236 if (node == NULL)
237 return got_error_from_errno();
239 memcpy(&node->id, commit_id, sizeof(node->id));
240 SIMPLEQ_INIT(&node->parent_ids);
241 SIMPLEQ_INIT(&node->child_ids);
242 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
243 err = add_vertex(&node->parent_ids, qid->id);
244 if (err)
245 return err;
246 node->nparents++;
248 node->commit_timestamp = commit->committer_time; /* XXX not UTC! */
250 err = got_object_idset_add((void **)(&existing_node),
251 graph->node_ids, &node->id, node);
252 if (err == NULL) {
253 struct got_object_qid *qid;
255 add_node_to_iter_list(graph, node, child_node);
256 err = got_object_idset_remove(graph->open_branches, commit_id);
257 if (err && err->code != GOT_ERR_NO_OBJ)
258 return err;
259 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
260 if (got_object_idset_get(graph->node_ids, qid->id))
261 continue; /* parent already traversed */
262 err = got_object_idset_add(NULL, graph->open_branches,
263 qid->id, node);
264 if (err && err->code != GOT_ERR_OBJ_EXISTS)
265 return err;
267 *new_node = node;
268 } else if (err->code == GOT_ERR_OBJ_EXISTS) {
269 err = NULL;
270 free(node);
271 node = existing_node;
272 } else {
273 free(node);
274 return err;
277 if (child_node) {
278 struct got_object_qid *cid;
280 /* Prevent linking to self. */
281 if (got_object_id_cmp(commit_id, &child_node->id) == 0)
282 return got_error(GOT_ERR_BAD_OBJ_ID);
284 /* Prevent double-linking to the same child. */
285 SIMPLEQ_FOREACH(cid, &node->child_ids, entry) {
286 if (got_object_id_cmp(cid->id, &child_node->id) == 0)
287 return got_error(GOT_ERR_BAD_OBJ_ID);
290 err = add_vertex(&node->child_ids, &child_node->id);
291 if (err)
292 return err;
293 node->nchildren++;
296 return err;
299 const struct got_error *
300 got_commit_graph_open(struct got_commit_graph **graph,
301 struct got_object_id *commit_id, struct got_repository *repo)
303 const struct got_error *err = NULL;
304 struct got_commit_object *commit;
306 *graph = NULL;
308 err = got_object_open_as_commit(&commit, repo, commit_id);
309 if (err)
310 return err;
312 *graph = alloc_graph();
313 if (*graph == NULL) {
314 got_object_commit_close(commit);
315 return got_error_from_errno();
318 err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL);
319 got_object_commit_close(commit);
320 if (err) {
321 got_commit_graph_close(*graph);
322 *graph = NULL;
323 return err;
326 return NULL;
329 struct got_commit_graph_branch {
330 struct got_object_id parent_id;
331 struct got_commit_graph_node *node;
332 };
334 struct gather_branches_arg {
335 struct got_commit_graph_branch *branches;
336 int nbranches;
337 };
339 static void
340 gather_branches(struct got_object_id *id, void *data, void *arg)
342 struct gather_branches_arg *a = arg;
343 memcpy(&a->branches[a->nbranches].parent_id, id, sizeof(*id));
344 a->branches[a->nbranches].node = data;
345 a->nbranches++;
348 static const struct got_error *
349 fetch_commits_from_open_branches(int *ncommits, int *wanted_id_added,
350 struct got_commit_graph *graph, struct got_repository *repo,
351 struct got_object_id *wanted_id)
353 const struct got_error *err;
354 struct got_commit_graph_branch *branches;
355 struct gather_branches_arg arg;
356 int i;
358 *ncommits = 0;
359 if (wanted_id_added)
360 *wanted_id_added = 0;
362 arg.nbranches = got_object_idset_num_elements(graph->open_branches);
363 if (arg.nbranches == 0)
364 return NULL;
366 /*
367 * Adding nodes to the graph might change the graph's open
368 * branches state. Create a local copy of the current state.
369 */
370 branches = calloc(arg.nbranches, sizeof(*branches));
371 if (branches == NULL)
372 return got_error_from_errno();
373 arg.nbranches = 0; /* reset; gather_branches() will increment */
374 arg.branches = branches;
375 got_object_idset_for_each(graph->open_branches, gather_branches, &arg);
377 for (i = 0; i < arg.nbranches; i++) {
378 struct got_object_id *commit_id;
379 struct got_commit_graph_node *child_node, *new_node;
380 struct got_commit_object *commit;
382 commit_id = &branches[i].parent_id;
383 child_node = branches[i].node;
385 err = got_object_open_as_commit(&commit, repo, commit_id);
386 if (err)
387 break;
389 err = add_node(&new_node, graph, commit_id, commit, child_node);
390 got_object_commit_close(commit);
391 if (err) {
392 if (err->code != GOT_ERR_OBJ_EXISTS)
393 break;
394 err = NULL;
396 if (new_node)
397 (*ncommits)++;
398 if (wanted_id && got_object_id_cmp(commit_id, wanted_id) == 0)
399 *wanted_id_added = 1;
402 free(branches);
403 return err;
406 const struct got_error *
407 got_commit_graph_fetch_commits(int *nfetched, struct got_commit_graph *graph,
408 int limit, struct got_repository *repo)
410 const struct got_error *err;
411 int ncommits;
413 *nfetched = 0;
415 while (*nfetched < limit) {
416 err = fetch_commits_from_open_branches(&ncommits, NULL,
417 graph, repo, NULL);
418 if (err)
419 return err;
420 if (ncommits == 0)
421 break;
422 *nfetched += ncommits;
425 return NULL;
428 const struct got_error *
429 got_commit_graph_fetch_commits_up_to(int *nfetched,
430 struct got_commit_graph *graph, struct got_object_id *wanted_id,
431 struct got_repository *repo)
433 const struct got_error *err;
434 int ncommits, wanted_id_added = 0;
436 *nfetched = 0;
438 if (got_object_idset_get(graph->node_ids, wanted_id) != NULL)
439 return NULL;
441 while (!wanted_id_added) {
442 err = fetch_commits_from_open_branches(&ncommits,
443 &wanted_id_added, graph, repo, wanted_id);
444 if (err)
445 return err;
446 if (ncommits == 0)
447 return NULL;
448 *nfetched += ncommits;
451 return NULL;
454 static void
455 free_graph_node(struct got_object_id *id, void *data, void *arg)
457 struct got_commit_graph_node *node = data;
458 while (!SIMPLEQ_EMPTY(&node->child_ids)) {
459 struct got_object_qid *child = SIMPLEQ_FIRST(&node->child_ids);
460 SIMPLEQ_REMOVE_HEAD(&node->child_ids, entry);
461 free(child);
463 free(node);
466 void
467 got_commit_graph_close(struct got_commit_graph *graph)
469 got_object_idset_free(graph->open_branches);
470 got_object_idset_for_each(graph->node_ids, free_graph_node, NULL);
471 got_object_idset_free(graph->node_ids);
472 free(graph);
475 const struct got_error *
476 got_commit_graph_iter_start(struct got_commit_graph *graph,
477 struct got_object_id *id)
479 struct got_commit_graph_node *start_node;
481 start_node = got_object_idset_get(graph->node_ids, id);
482 if (start_node == NULL)
483 return got_error(GOT_ERR_NO_OBJ);
485 graph->iter_node = start_node;
486 return NULL;
489 const struct got_error *
490 got_commit_graph_iter_next(struct got_object_id **id,
491 struct got_commit_graph *graph)
493 *id = NULL;
495 if (graph->iter_node == NULL) {
496 /* We are done interating, or iteration was not started. */
497 return got_error(GOT_ERR_ITER_COMPLETED);
500 if (graph->iter_node ==
501 TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
502 got_object_idset_num_elements(graph->open_branches) == 0) {
503 *id = &graph->iter_node->id;
504 /* We are done interating. */
505 graph->iter_node = NULL;
506 return NULL;
509 if (TAILQ_NEXT(graph->iter_node, entry) == NULL)
510 return got_error(GOT_ERR_ITER_NEED_MORE);
512 *id = &graph->iter_node->id;
513 graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);
514 return NULL;