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 struct got_commit_graph {
59 /* The set of all commits we have traversed. */
60 struct got_object_idset *node_ids;
62 /* The commit at which traversal began (youngest commit in node_ids). */
63 struct got_commit_graph_node *head_node;
65 /*
66 * A set of object IDs of known parent commits which we have not yet
67 * traversed. Each commit ID in this set represents a branch in commit
68 * history: Either the first-parent branch of the head node, or another
69 * branch corresponding to a traversed merge commit for which we have
70 * not traversed a branch point commit yet.
71 *
72 * Whenever we add a commit with a matching ID to the graph, we remove
73 * its corresponding element from this set, and add new elements for
74 * each of that commit's parent commits which were not traversed yet.
75 *
76 * When API users ask us to fetch more commits, we fetch commits from
77 * all currently open branches. This allows API users to process
78 * commits in linear order even though the history contains branches.
79 */
80 struct got_object_idset *open_branches;
82 /* The next commit to return when the API user asks for one. */
83 struct got_commit_graph_node *iter_node;
85 TAILQ_HEAD(, got_commit_graph_node) iter_candidates;
86 };
88 static struct got_commit_graph *
89 alloc_graph(void)
90 {
91 struct got_commit_graph *graph;
93 graph = calloc(1, sizeof(*graph));
94 if (graph == NULL)
95 return NULL;
97 graph->node_ids = got_object_idset_alloc();
98 if (graph->node_ids == NULL) {
99 free(graph);
100 return NULL;
103 graph->open_branches = got_object_idset_alloc();
104 if (graph->open_branches == NULL) {
105 got_object_idset_free(graph->node_ids);
106 free(graph);
107 return NULL;
110 TAILQ_INIT(&graph->iter_candidates);
111 return graph;
114 #if 0
115 static int
116 is_head_node(struct got_commit_graph_node *node)
118 return node->nchildren == 0;
121 static int
122 is_merge_point(struct got_commit_graph_node *node)
124 return node->nparents > 1;
127 int
128 is_branch_point(struct got_commit_graph_node *node)
130 return node->nchildren > 1;
132 #endif
134 static int
135 is_root_node(struct got_commit_graph_node *node)
137 return node->nparents == 0;
140 static void
141 add_iteration_candidate(struct got_commit_graph *graph,
142 struct got_commit_graph_node *node)
144 struct got_commit_graph_node *n, *next;
146 if (TAILQ_EMPTY(&graph->iter_candidates)) {
147 TAILQ_INSERT_TAIL(&graph->iter_candidates, node, entry);
148 return;
151 TAILQ_FOREACH(n, &graph->iter_candidates, entry) {
152 if (node->commit_timestamp < n->commit_timestamp) {
153 next = TAILQ_NEXT(n, entry);
154 if (next == NULL) {
155 TAILQ_INSERT_AFTER(&graph->iter_candidates, n,
156 node, entry);
157 break;
159 if (node->commit_timestamp >= next->commit_timestamp) {
160 TAILQ_INSERT_BEFORE(next, node, entry);
161 break;
163 } else {
164 TAILQ_INSERT_BEFORE(n, node, entry);
165 break;
170 static const struct got_error *
171 add_vertex(struct got_object_id_queue *ids, struct got_object_id *id)
173 struct got_object_qid *qid;
175 qid = calloc(1, sizeof(*qid));
176 if (qid == NULL)
177 return got_error_from_errno();
179 qid->id = got_object_id_dup(id);
180 if (qid->id == NULL) {
181 const struct got_error *err = got_error_from_errno();
182 free(qid);
183 return err;
186 SIMPLEQ_INSERT_TAIL(ids, qid, entry);
187 return NULL;
190 static const struct got_error *
191 add_node(struct got_commit_graph_node **new_node,
192 struct got_commit_graph *graph, struct got_object_id *commit_id,
193 struct got_commit_object *commit, struct got_object_id *child_commit_id)
195 const struct got_error *err = NULL;
196 struct got_commit_graph_node *node, *existing_node;
197 struct got_object_qid *qid;
199 *new_node = NULL;
201 node = calloc(1, sizeof(*node));
202 if (node == NULL)
203 return got_error_from_errno();
205 memcpy(&node->id, commit_id, sizeof(node->id));
206 SIMPLEQ_INIT(&node->parent_ids);
207 SIMPLEQ_INIT(&node->child_ids);
208 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
209 err = add_vertex(&node->parent_ids, qid->id);
210 if (err)
211 return err;
212 node->nparents++;
214 node->commit_timestamp = commit->committer_time; /* XXX not UTC! */
216 err = got_object_idset_add((void **)(&existing_node),
217 graph->node_ids, &node->id, node);
218 if (err == NULL) {
219 struct got_object_qid *qid;
221 add_iteration_candidate(graph, node);
222 err = got_object_idset_remove(graph->open_branches, commit_id);
223 if (err && err->code != GOT_ERR_NO_OBJ)
224 return err;
225 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
226 if (got_object_idset_get(graph->node_ids, qid->id))
227 continue; /* parent already traversed */
228 err = got_object_idset_add(NULL, graph->open_branches,
229 qid->id, node);
230 if (err && err->code != GOT_ERR_OBJ_EXISTS)
231 return err;
233 *new_node = node;
234 } else if (err->code == GOT_ERR_OBJ_EXISTS) {
235 err = NULL;
236 free(node);
237 node = existing_node;
238 } else {
239 free(node);
240 return err;
243 if (child_commit_id) {
244 struct got_object_qid *cid;
246 /* Prevent linking to self. */
247 if (got_object_id_cmp(commit_id, child_commit_id) == 0)
248 return got_error(GOT_ERR_BAD_OBJ_ID);
250 /* Prevent double-linking to the same child. */
251 SIMPLEQ_FOREACH(cid, &node->child_ids, entry) {
252 if (got_object_id_cmp(cid->id, child_commit_id) == 0)
253 return got_error(GOT_ERR_BAD_OBJ_ID);
256 err = add_vertex(&node->child_ids, child_commit_id);
257 if (err)
258 return err;
259 node->nchildren++;
263 return err;
266 const struct got_error *
267 got_commit_graph_open(struct got_commit_graph **graph,
268 struct got_object_id *commit_id, struct got_repository *repo)
270 const struct got_error *err = NULL;
271 struct got_commit_object *commit;
273 *graph = NULL;
275 err = got_object_open_as_commit(&commit, repo, commit_id);
276 if (err)
277 return err;
279 *graph = alloc_graph();
280 if (*graph == NULL) {
281 got_object_commit_close(commit);
282 return got_error_from_errno();
285 err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL);
286 got_object_commit_close(commit);
287 if (err) {
288 got_commit_graph_close(*graph);
289 *graph = NULL;
290 return err;
293 return NULL;
296 struct got_commit_graph_branch {
297 struct got_object_id parent_id;
298 struct got_commit_graph_node *node;
299 };
301 struct gather_branches_arg {
302 struct got_commit_graph_branch *branches;
303 int nbranches;
304 };
306 static void
307 gather_branches(struct got_object_id *id, void *data, void *arg)
309 struct gather_branches_arg *a = arg;
310 memcpy(&a->branches[a->nbranches].parent_id, id, sizeof(*id));
311 a->branches[a->nbranches].node = data;
312 a->nbranches++;
315 static const struct got_error *
316 fetch_commits_from_open_branches(int *ncommits, int *wanted_id_added,
317 struct got_commit_graph *graph, struct got_repository *repo,
318 struct got_object_id *wanted_id)
320 const struct got_error *err;
321 struct got_commit_graph_branch *branches;
322 struct gather_branches_arg arg;
323 int i;
325 *ncommits = 0;
326 if (wanted_id_added)
327 *wanted_id_added = 0;
329 arg.nbranches = got_object_idset_num_elements(graph->open_branches);
330 if (arg.nbranches == 0)
331 return NULL;
333 /*
334 * Adding nodes to the graph might change the graph's open
335 * branches state. Create a local copy of the current state.
336 */
337 branches = calloc(arg.nbranches, sizeof(*branches));
338 if (branches == NULL)
339 return got_error_from_errno();
340 arg.nbranches = 0; /* reset; gather_branches() will increment */
341 arg.branches = branches;
342 got_object_idset_for_each(graph->open_branches, gather_branches, &arg);
344 for (i = 0; i < arg.nbranches; i++) {
345 struct got_object_id *commit_id;
346 struct got_commit_graph_node *child_node, *new_node;
347 struct got_commit_object *commit;
349 commit_id = &branches[i].parent_id;
350 child_node = branches[i].node;
352 err = got_object_open_as_commit(&commit, repo, commit_id);
353 if (err)
354 break;
356 err = add_node(&new_node, graph, commit_id, commit,
357 &child_node->id);
358 got_object_commit_close(commit);
359 if (err) {
360 if (err->code != GOT_ERR_OBJ_EXISTS)
361 break;
362 err = NULL;
364 if (new_node)
365 (*ncommits)++;
366 if (wanted_id && got_object_id_cmp(commit_id, wanted_id) == 0)
367 *wanted_id_added = 1;
370 free(branches);
371 return err;
374 const struct got_error *
375 got_commit_graph_fetch_commits(int *nfetched, struct got_commit_graph *graph,
376 int limit, struct got_repository *repo)
378 const struct got_error *err;
379 int ncommits;
381 *nfetched = 0;
383 while (*nfetched < limit) {
384 err = fetch_commits_from_open_branches(&ncommits, NULL,
385 graph, repo, NULL);
386 if (err)
387 return err;
388 if (ncommits == 0)
389 break;
390 *nfetched += ncommits;
393 return NULL;
396 const struct got_error *
397 got_commit_graph_fetch_commits_up_to(int *nfetched,
398 struct got_commit_graph *graph, struct got_object_id *wanted_id,
399 struct got_repository *repo)
401 const struct got_error *err;
402 int ncommits, wanted_id_added = 0;
404 *nfetched = 0;
405 while (!wanted_id_added) {
406 err = fetch_commits_from_open_branches(&ncommits,
407 &wanted_id_added, graph, repo, wanted_id);
408 if (err)
409 return err;
410 if (ncommits == 0)
411 return NULL;
412 *nfetched += ncommits;
415 return NULL;
418 static void
419 free_graph_node(struct got_object_id *id, void *data, void *arg)
421 struct got_commit_graph_node *node = data;
422 while (!SIMPLEQ_EMPTY(&node->child_ids)) {
423 struct got_object_qid *child = SIMPLEQ_FIRST(&node->child_ids);
424 SIMPLEQ_REMOVE_HEAD(&node->child_ids, entry);
425 free(child);
427 free(node);
430 void
431 got_commit_graph_close(struct got_commit_graph *graph)
433 got_object_idset_free(graph->open_branches);
434 got_object_idset_for_each(graph->node_ids, free_graph_node, NULL);
435 got_object_idset_free(graph->node_ids);
436 free(graph);
439 const struct got_error *
440 got_commit_graph_iter_start(struct got_commit_graph *graph,
441 struct got_object_id *id)
443 struct got_commit_graph_node *start_node, *node;
444 struct got_object_qid *qid;
446 start_node = got_object_idset_get(graph->node_ids, id);
447 if (start_node == NULL)
448 return got_error(GOT_ERR_NO_OBJ);
450 graph->iter_node = start_node;
452 while (!TAILQ_EMPTY(&graph->iter_candidates)) {
453 node = TAILQ_FIRST(&graph->iter_candidates);
454 TAILQ_REMOVE(&graph->iter_candidates, node, entry);
457 /* Put all known parents of this commit on the candidate list. */
458 SIMPLEQ_FOREACH(qid, &start_node->parent_ids, entry) {
459 node = got_object_idset_get(graph->node_ids, qid->id);
460 if (node)
461 add_iteration_candidate(graph, 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 struct got_commit_graph_node *node;
473 if (graph->iter_node == NULL) {
474 /* We are done interating, or iteration was not started. */
475 *id = NULL;
476 return NULL;
479 if (TAILQ_EMPTY(&graph->iter_candidates)) {
480 if (is_root_node(graph->iter_node) &&
481 got_object_idset_num_elements(graph->open_branches) == 0) {
482 *id = &graph->iter_node->id;
483 /* We are done interating. */
484 graph->iter_node = NULL;
485 return NULL;
487 return got_error(GOT_ERR_ITER_NEED_MORE);
490 *id = &graph->iter_node->id;
491 node = TAILQ_FIRST(&graph->iter_candidates);
492 TAILQ_REMOVE(&graph->iter_candidates, node, entry);
493 graph->iter_node = node;
494 return NULL;
497 int
498 got_commit_graph_contains_object(struct got_commit_graph *graph,
499 struct got_object_id *id)
501 return (got_object_idset_get(graph->node_ids, id) != NULL);