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, being looping over the list there
157 * instead of from the list head.
158 * All parent commits *should* appear before their children unless
159 * commit timestamps are broken (in which case the ordering of
160 * commits will be broken in some way in any case).
161 */
162 n = child_node ? child_node : TAILQ_FIRST(&graph->iter_list);
164 /* Ensure that an iteration in progress will see this new commit. */
165 if (graph->iter_node) {
166 n = graph->iter_node;
167 do {
168 next = TAILQ_NEXT(n, entry);
169 if (next &&
170 node->commit_timestamp >= next->commit_timestamp) {
171 TAILQ_INSERT_BEFORE(next, node, entry);
172 return;
174 n = next;
175 } while (next);
177 TAILQ_INSERT_AFTER(&graph->iter_list, graph->iter_node,
178 node, entry);
179 return;
182 /* Insert into list based on committer timestamp. */
183 do {
184 if (node->commit_timestamp == n->commit_timestamp) {
185 TAILQ_INSERT_AFTER(&graph->iter_list, n, node, entry);
186 break;
187 } else if (node->commit_timestamp < n->commit_timestamp) {
188 next = TAILQ_NEXT(n, entry);
189 if (next == NULL) {
190 TAILQ_INSERT_AFTER(&graph->iter_list, n,
191 node, entry);
192 break;
194 if (node->commit_timestamp >= next->commit_timestamp) {
195 TAILQ_INSERT_BEFORE(next, node, entry);
196 break;
198 } else {
199 TAILQ_INSERT_BEFORE(n, node, entry);
200 break;
202 n = TAILQ_NEXT(n, entry);
203 } while (n);
206 static const struct got_error *
207 add_vertex(struct got_object_id_queue *ids, struct got_object_id *id)
209 struct got_object_qid *qid;
211 qid = calloc(1, sizeof(*qid));
212 if (qid == NULL)
213 return got_error_from_errno();
215 qid->id = got_object_id_dup(id);
216 if (qid->id == NULL) {
217 const struct got_error *err = got_error_from_errno();
218 free(qid);
219 return err;
222 SIMPLEQ_INSERT_TAIL(ids, qid, entry);
223 return NULL;
226 static const struct got_error *
227 add_node(struct got_commit_graph_node **new_node,
228 struct got_commit_graph *graph, struct got_object_id *commit_id,
229 struct got_commit_object *commit, struct got_commit_graph_node *child_node)
231 const struct got_error *err = NULL;
232 struct got_commit_graph_node *node, *existing_node;
233 struct got_object_qid *qid;
235 *new_node = NULL;
237 node = calloc(1, sizeof(*node));
238 if (node == NULL)
239 return got_error_from_errno();
241 memcpy(&node->id, commit_id, sizeof(node->id));
242 SIMPLEQ_INIT(&node->parent_ids);
243 SIMPLEQ_INIT(&node->child_ids);
244 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
245 err = add_vertex(&node->parent_ids, qid->id);
246 if (err)
247 return err;
248 node->nparents++;
250 node->commit_timestamp = commit->committer_time; /* XXX not UTC! */
252 err = got_object_idset_add((void **)(&existing_node),
253 graph->node_ids, &node->id, node);
254 if (err == NULL) {
255 struct got_object_qid *qid;
257 add_node_to_iter_list(graph, node, child_node);
258 err = got_object_idset_remove(graph->open_branches, commit_id);
259 if (err && err->code != GOT_ERR_NO_OBJ)
260 return err;
261 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
262 if (got_object_idset_get(graph->node_ids, qid->id))
263 continue; /* parent already traversed */
264 err = got_object_idset_add(NULL, graph->open_branches,
265 qid->id, node);
266 if (err && err->code != GOT_ERR_OBJ_EXISTS)
267 return err;
269 *new_node = node;
270 } else if (err->code == GOT_ERR_OBJ_EXISTS) {
271 err = NULL;
272 free(node);
273 node = existing_node;
274 } else {
275 free(node);
276 return err;
279 if (child_node) {
280 struct got_object_qid *cid;
282 /* Prevent linking to self. */
283 if (got_object_id_cmp(commit_id, &child_node->id) == 0)
284 return got_error(GOT_ERR_BAD_OBJ_ID);
286 /* Prevent double-linking to the same child. */
287 SIMPLEQ_FOREACH(cid, &node->child_ids, entry) {
288 if (got_object_id_cmp(cid->id, &child_node->id) == 0)
289 return got_error(GOT_ERR_BAD_OBJ_ID);
292 err = add_vertex(&node->child_ids, &child_node->id);
293 if (err)
294 return err;
295 node->nchildren++;
298 return err;
301 const struct got_error *
302 got_commit_graph_open(struct got_commit_graph **graph,
303 struct got_object_id *commit_id, struct got_repository *repo)
305 const struct got_error *err = NULL;
306 struct got_commit_object *commit;
308 *graph = NULL;
310 err = got_object_open_as_commit(&commit, repo, commit_id);
311 if (err)
312 return err;
314 *graph = alloc_graph();
315 if (*graph == NULL) {
316 got_object_commit_close(commit);
317 return got_error_from_errno();
320 err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL);
321 got_object_commit_close(commit);
322 if (err) {
323 got_commit_graph_close(*graph);
324 *graph = NULL;
325 return err;
328 return NULL;
331 struct got_commit_graph_branch {
332 struct got_object_id parent_id;
333 struct got_commit_graph_node *node;
334 };
336 struct gather_branches_arg {
337 struct got_commit_graph_branch *branches;
338 int nbranches;
339 };
341 static void
342 gather_branches(struct got_object_id *id, void *data, void *arg)
344 struct gather_branches_arg *a = arg;
345 memcpy(&a->branches[a->nbranches].parent_id, id, sizeof(*id));
346 a->branches[a->nbranches].node = data;
347 a->nbranches++;
350 static const struct got_error *
351 fetch_commits_from_open_branches(int *ncommits, int *wanted_id_added,
352 struct got_commit_graph *graph, struct got_repository *repo,
353 struct got_object_id *wanted_id)
355 const struct got_error *err;
356 struct got_commit_graph_branch *branches;
357 struct gather_branches_arg arg;
358 int i;
360 *ncommits = 0;
361 if (wanted_id_added)
362 *wanted_id_added = 0;
364 arg.nbranches = got_object_idset_num_elements(graph->open_branches);
365 if (arg.nbranches == 0)
366 return NULL;
368 /*
369 * Adding nodes to the graph might change the graph's open
370 * branches state. Create a local copy of the current state.
371 */
372 branches = calloc(arg.nbranches, sizeof(*branches));
373 if (branches == NULL)
374 return got_error_from_errno();
375 arg.nbranches = 0; /* reset; gather_branches() will increment */
376 arg.branches = branches;
377 got_object_idset_for_each(graph->open_branches, gather_branches, &arg);
379 for (i = 0; i < arg.nbranches; i++) {
380 struct got_object_id *commit_id;
381 struct got_commit_graph_node *child_node, *new_node;
382 struct got_commit_object *commit;
384 commit_id = &branches[i].parent_id;
385 child_node = branches[i].node;
387 err = got_object_open_as_commit(&commit, repo, commit_id);
388 if (err)
389 break;
391 err = add_node(&new_node, graph, commit_id, commit, child_node);
392 got_object_commit_close(commit);
393 if (err) {
394 if (err->code != GOT_ERR_OBJ_EXISTS)
395 break;
396 err = NULL;
398 if (new_node)
399 (*ncommits)++;
400 if (wanted_id && got_object_id_cmp(commit_id, wanted_id) == 0)
401 *wanted_id_added = 1;
404 free(branches);
405 return err;
408 const struct got_error *
409 got_commit_graph_fetch_commits(int *nfetched, struct got_commit_graph *graph,
410 int limit, struct got_repository *repo)
412 const struct got_error *err;
413 int ncommits;
415 *nfetched = 0;
417 while (*nfetched < limit) {
418 err = fetch_commits_from_open_branches(&ncommits, NULL,
419 graph, repo, NULL);
420 if (err)
421 return err;
422 if (ncommits == 0)
423 break;
424 *nfetched += ncommits;
427 return NULL;
430 const struct got_error *
431 got_commit_graph_fetch_commits_up_to(int *nfetched,
432 struct got_commit_graph *graph, struct got_object_id *wanted_id,
433 struct got_repository *repo)
435 const struct got_error *err;
436 int ncommits, wanted_id_added = 0;
438 *nfetched = 0;
440 if (got_object_idset_get(graph->node_ids, wanted_id) != NULL)
441 return NULL;
443 while (!wanted_id_added) {
444 err = fetch_commits_from_open_branches(&ncommits,
445 &wanted_id_added, graph, repo, wanted_id);
446 if (err)
447 return err;
448 if (ncommits == 0)
449 return NULL;
450 *nfetched += ncommits;
453 return NULL;
456 static void
457 free_graph_node(struct got_object_id *id, void *data, void *arg)
459 struct got_commit_graph_node *node = data;
460 while (!SIMPLEQ_EMPTY(&node->child_ids)) {
461 struct got_object_qid *child = SIMPLEQ_FIRST(&node->child_ids);
462 SIMPLEQ_REMOVE_HEAD(&node->child_ids, entry);
463 free(child);
465 free(node);
468 void
469 got_commit_graph_close(struct got_commit_graph *graph)
471 got_object_idset_free(graph->open_branches);
472 got_object_idset_for_each(graph->node_ids, free_graph_node, NULL);
473 got_object_idset_free(graph->node_ids);
474 free(graph);
477 const struct got_error *
478 got_commit_graph_iter_start(struct got_commit_graph *graph,
479 struct got_object_id *id)
481 struct got_commit_graph_node *start_node;
483 start_node = got_object_idset_get(graph->node_ids, id);
484 if (start_node == NULL)
485 return got_error(GOT_ERR_NO_OBJ);
487 graph->iter_node = start_node;
488 return NULL;
491 const struct got_error *
492 got_commit_graph_iter_next(struct got_object_id **id,
493 struct got_commit_graph *graph)
495 *id = NULL;
497 if (graph->iter_node == NULL) {
498 /* We are done interating, or iteration was not started. */
499 return got_error(GOT_ERR_ITER_COMPLETED);
502 if (graph->iter_node ==
503 TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
504 got_object_idset_num_elements(graph->open_branches) == 0) {
505 *id = &graph->iter_node->id;
506 /* We are done interating. */
507 graph->iter_node = NULL;
508 return NULL;
511 if (TAILQ_NEXT(graph->iter_node, entry) == NULL)
512 return got_error(GOT_ERR_ITER_NEED_MORE);
514 *id = &graph->iter_node->id;
515 graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);
516 return NULL;