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, start iterating there.
157 * All parent commits *should* appear before their children unless
158 * commit timestamps are broken (in which case the ordering of
159 * commits will be broken either way).
160 */
161 n = child_node ? child_node : TAILQ_FIRST(&graph->iter_list);
162 do {
163 if (node->commit_timestamp < n->commit_timestamp) {
164 next = TAILQ_NEXT(n, entry);
165 if (next == NULL) {
166 TAILQ_INSERT_AFTER(&graph->iter_list, n,
167 node, entry);
168 break;
170 if (node->commit_timestamp >= next->commit_timestamp) {
171 TAILQ_INSERT_BEFORE(next, node, entry);
172 break;
174 } else {
175 TAILQ_INSERT_BEFORE(n, node, entry);
176 break;
178 n = TAILQ_NEXT(n, entry);
179 } while (n);
182 static const struct got_error *
183 add_vertex(struct got_object_id_queue *ids, struct got_object_id *id)
185 struct got_object_qid *qid;
187 qid = calloc(1, sizeof(*qid));
188 if (qid == NULL)
189 return got_error_from_errno();
191 qid->id = got_object_id_dup(id);
192 if (qid->id == NULL) {
193 const struct got_error *err = got_error_from_errno();
194 free(qid);
195 return err;
198 SIMPLEQ_INSERT_TAIL(ids, qid, entry);
199 return NULL;
202 static const struct got_error *
203 add_node(struct got_commit_graph_node **new_node,
204 struct got_commit_graph *graph, struct got_object_id *commit_id,
205 struct got_commit_object *commit, struct got_commit_graph_node *child_node)
207 const struct got_error *err = NULL;
208 struct got_commit_graph_node *node, *existing_node;
209 struct got_object_qid *qid;
211 *new_node = NULL;
213 node = calloc(1, sizeof(*node));
214 if (node == NULL)
215 return got_error_from_errno();
217 memcpy(&node->id, commit_id, sizeof(node->id));
218 SIMPLEQ_INIT(&node->parent_ids);
219 SIMPLEQ_INIT(&node->child_ids);
220 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
221 err = add_vertex(&node->parent_ids, qid->id);
222 if (err)
223 return err;
224 node->nparents++;
226 node->commit_timestamp = commit->committer_time; /* XXX not UTC! */
228 err = got_object_idset_add((void **)(&existing_node),
229 graph->node_ids, &node->id, node);
230 if (err == NULL) {
231 struct got_object_qid *qid;
233 add_node_to_iter_list(graph, node, child_node);
234 err = got_object_idset_remove(graph->open_branches, commit_id);
235 if (err && err->code != GOT_ERR_NO_OBJ)
236 return err;
237 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
238 if (got_object_idset_get(graph->node_ids, qid->id))
239 continue; /* parent already traversed */
240 err = got_object_idset_add(NULL, graph->open_branches,
241 qid->id, node);
242 if (err && err->code != GOT_ERR_OBJ_EXISTS)
243 return err;
245 *new_node = node;
246 } else if (err->code == GOT_ERR_OBJ_EXISTS) {
247 err = NULL;
248 free(node);
249 node = existing_node;
250 } else {
251 free(node);
252 return err;
255 if (child_node) {
256 struct got_object_qid *cid;
258 /* Prevent linking to self. */
259 if (got_object_id_cmp(commit_id, &child_node->id) == 0)
260 return got_error(GOT_ERR_BAD_OBJ_ID);
262 /* Prevent double-linking to the same child. */
263 SIMPLEQ_FOREACH(cid, &node->child_ids, entry) {
264 if (got_object_id_cmp(cid->id, &child_node->id) == 0)
265 return got_error(GOT_ERR_BAD_OBJ_ID);
268 err = add_vertex(&node->child_ids, &child_node->id);
269 if (err)
270 return err;
271 node->nchildren++;
274 return err;
277 const struct got_error *
278 got_commit_graph_open(struct got_commit_graph **graph,
279 struct got_object_id *commit_id, struct got_repository *repo)
281 const struct got_error *err = NULL;
282 struct got_commit_object *commit;
284 *graph = NULL;
286 err = got_object_open_as_commit(&commit, repo, commit_id);
287 if (err)
288 return err;
290 *graph = alloc_graph();
291 if (*graph == NULL) {
292 got_object_commit_close(commit);
293 return got_error_from_errno();
296 err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL);
297 got_object_commit_close(commit);
298 if (err) {
299 got_commit_graph_close(*graph);
300 *graph = NULL;
301 return err;
304 return NULL;
307 struct got_commit_graph_branch {
308 struct got_object_id parent_id;
309 struct got_commit_graph_node *node;
310 };
312 struct gather_branches_arg {
313 struct got_commit_graph_branch *branches;
314 int nbranches;
315 };
317 static void
318 gather_branches(struct got_object_id *id, void *data, void *arg)
320 struct gather_branches_arg *a = arg;
321 memcpy(&a->branches[a->nbranches].parent_id, id, sizeof(*id));
322 a->branches[a->nbranches].node = data;
323 a->nbranches++;
326 static const struct got_error *
327 fetch_commits_from_open_branches(int *ncommits, int *wanted_id_added,
328 struct got_commit_graph *graph, struct got_repository *repo,
329 struct got_object_id *wanted_id)
331 const struct got_error *err;
332 struct got_commit_graph_branch *branches;
333 struct gather_branches_arg arg;
334 int i;
336 *ncommits = 0;
337 if (wanted_id_added)
338 *wanted_id_added = 0;
340 arg.nbranches = got_object_idset_num_elements(graph->open_branches);
341 if (arg.nbranches == 0)
342 return NULL;
344 /*
345 * Adding nodes to the graph might change the graph's open
346 * branches state. Create a local copy of the current state.
347 */
348 branches = calloc(arg.nbranches, sizeof(*branches));
349 if (branches == NULL)
350 return got_error_from_errno();
351 arg.nbranches = 0; /* reset; gather_branches() will increment */
352 arg.branches = branches;
353 got_object_idset_for_each(graph->open_branches, gather_branches, &arg);
355 for (i = 0; i < arg.nbranches; i++) {
356 struct got_object_id *commit_id;
357 struct got_commit_graph_node *child_node, *new_node;
358 struct got_commit_object *commit;
360 commit_id = &branches[i].parent_id;
361 child_node = branches[i].node;
363 err = got_object_open_as_commit(&commit, repo, commit_id);
364 if (err)
365 break;
367 err = add_node(&new_node, graph, commit_id, commit, child_node);
368 got_object_commit_close(commit);
369 if (err) {
370 if (err->code != GOT_ERR_OBJ_EXISTS)
371 break;
372 err = NULL;
374 if (new_node)
375 (*ncommits)++;
376 if (wanted_id && got_object_id_cmp(commit_id, wanted_id) == 0)
377 *wanted_id_added = 1;
380 free(branches);
381 return err;
384 const struct got_error *
385 got_commit_graph_fetch_commits(int *nfetched, struct got_commit_graph *graph,
386 int limit, struct got_repository *repo)
388 const struct got_error *err;
389 int ncommits;
391 *nfetched = 0;
393 while (*nfetched < limit) {
394 err = fetch_commits_from_open_branches(&ncommits, NULL,
395 graph, repo, NULL);
396 if (err)
397 return err;
398 if (ncommits == 0)
399 break;
400 *nfetched += ncommits;
403 return NULL;
406 const struct got_error *
407 got_commit_graph_fetch_commits_up_to(int *nfetched,
408 struct got_commit_graph *graph, struct got_object_id *wanted_id,
409 struct got_repository *repo)
411 const struct got_error *err;
412 int ncommits, wanted_id_added = 0;
414 *nfetched = 0;
416 if (got_object_idset_get(graph->node_ids, wanted_id) != NULL)
417 return NULL;
419 while (!wanted_id_added) {
420 err = fetch_commits_from_open_branches(&ncommits,
421 &wanted_id_added, graph, repo, wanted_id);
422 if (err)
423 return err;
424 if (ncommits == 0)
425 return NULL;
426 *nfetched += ncommits;
429 return NULL;
432 static void
433 free_graph_node(struct got_object_id *id, void *data, void *arg)
435 struct got_commit_graph_node *node = data;
436 while (!SIMPLEQ_EMPTY(&node->child_ids)) {
437 struct got_object_qid *child = SIMPLEQ_FIRST(&node->child_ids);
438 SIMPLEQ_REMOVE_HEAD(&node->child_ids, entry);
439 free(child);
441 free(node);
444 void
445 got_commit_graph_close(struct got_commit_graph *graph)
447 got_object_idset_free(graph->open_branches);
448 got_object_idset_for_each(graph->node_ids, free_graph_node, NULL);
449 got_object_idset_free(graph->node_ids);
450 free(graph);
453 const struct got_error *
454 got_commit_graph_iter_start(struct got_commit_graph *graph,
455 struct got_object_id *id)
457 struct got_commit_graph_node *start_node;
459 start_node = got_object_idset_get(graph->node_ids, id);
460 if (start_node == NULL)
461 return got_error(GOT_ERR_NO_OBJ);
463 graph->iter_node = start_node;
464 return NULL;
467 const struct got_error *
468 got_commit_graph_iter_next(struct got_object_id **id,
469 struct got_commit_graph *graph)
471 *id = NULL;
473 if (graph->iter_node == NULL) {
474 /* We are done interating, or iteration was not started. */
475 return got_error(GOT_ERR_ITER_COMPLETED);
478 if (graph->iter_node ==
479 TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
480 got_object_idset_num_elements(graph->open_branches) == 0) {
481 *id = &graph->iter_node->id;
482 /* We are done interating. */
483 graph->iter_node = NULL;
484 return NULL;
487 if (TAILQ_NEXT(graph->iter_node, entry) == NULL)
488 return got_error(GOT_ERR_ITER_NEED_MORE);
490 *id = &graph->iter_node->id;
491 graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);
492 return NULL;