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 struct got_commit_object *commit; /* contains list of parents */
48 int nchildren;
49 SIMPLEQ_HEAD(, got_parent_id) child_ids;
51 /* Used during graph iteration. */
52 TAILQ_ENTRY(got_commit_graph_node) entry;
53 };
55 struct got_commit_graph {
56 /* The set of all commits we have traversed. */
57 struct got_object_idset *node_ids;
59 /* The commit at which traversal began (youngest commit in node_ids). */
60 struct got_commit_graph_node *head_node;
62 /*
63 * A set of object IDs of known parent commits which we have not yet
64 * traversed. Each commit ID in this set represents a branch in commit
65 * history: Either the first-parent branch of the head node, or another
66 * branch corresponding to a traversed merge commit for which we have
67 * not traversed a branch point commit yet.
68 *
69 * Whenever we add a commit with a matching ID to the graph, we remove
70 * its corresponding element from this set, and add new elements for
71 * each of that commit's parent commits which were not traversed yet.
72 *
73 * When API users ask us to fetch more commits, we fetch commits from
74 * all currently open branches. This allows API users to process
75 * commits in linear order even though the history contains branches.
76 */
77 struct got_object_idset *open_branches;
79 /* The next commit to return when the API user asks for one. */
80 struct got_commit_graph_node *iter_node;
82 TAILQ_HEAD(, got_commit_graph_node) iter_candidates;
83 };
85 static struct got_commit_graph *
86 alloc_graph(void)
87 {
88 struct got_commit_graph *graph;
90 graph = calloc(1, sizeof(*graph));
91 if (graph == NULL)
92 return NULL;
94 graph->node_ids = got_object_idset_alloc();
95 if (graph->node_ids == NULL) {
96 free(graph);
97 return NULL;
98 }
100 graph->open_branches = got_object_idset_alloc();
101 if (graph->open_branches == NULL) {
102 got_object_idset_free(graph->node_ids);
103 free(graph);
104 return NULL;
107 TAILQ_INIT(&graph->iter_candidates);
108 return graph;
111 #if 0
112 static int
113 is_head_node(struct got_commit_graph_node *node)
115 return node->nchildren == 0;
118 static int
119 is_merge_point(struct got_commit_graph_node *node)
121 return node->commit->nparents > 1;
124 int
125 is_branch_point(struct got_commit_graph_node *node)
127 return node->nchildren > 1;
129 #endif
131 static int
132 is_root_node(struct got_commit_graph_node *node)
134 return node->commit->nparents == 0;
137 static const struct got_error *
138 compare_commits(int *cmp, struct got_commit_object *c1,
139 struct got_commit_object *c2)
141 const struct got_error *err;
142 int64_t t1, t2;
144 err = got_object_commit_get_committer_time(&t1, c1);
145 if (err)
146 return err;
147 err = got_object_commit_get_committer_time(&t2, c2);
149 if (err)
150 return err;
152 if (t1 == t2)
153 *cmp = 0;
154 else if (t1 < t2)
155 *cmp = -1;
156 else
157 *cmp = 1;
159 return NULL;
162 static const struct got_error *
163 add_iteration_candidate(struct got_commit_graph *graph,
164 struct got_commit_graph_node *node)
166 struct got_commit_graph_node *n, *next;
168 if (TAILQ_EMPTY(&graph->iter_candidates)) {
169 TAILQ_INSERT_TAIL(&graph->iter_candidates, node, entry);
170 return NULL;
173 TAILQ_FOREACH(n, &graph->iter_candidates, entry) {
174 const struct got_error *err;
175 int cmp;
176 err = compare_commits(&cmp, node->commit, n->commit);
177 if (err)
178 return err;
179 if (cmp < 0) {
180 next = TAILQ_NEXT(n, entry);
181 if (next == NULL) {
182 TAILQ_INSERT_AFTER(&graph->iter_candidates, n,
183 node, entry);
184 break;
186 err = compare_commits(&cmp, node->commit, next->commit);
187 if (err)
188 return err;
189 if (cmp >= 0) {
190 TAILQ_INSERT_BEFORE(next, node, entry);
191 break;
193 } else {
194 TAILQ_INSERT_BEFORE(n, node, entry);
195 break;
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_object_id *child_commit_id)
207 const struct got_error *err = NULL;
208 struct got_commit_graph_node *node, *existing_node;
210 *new_node = NULL;
212 node = calloc(1, sizeof(*node));
213 if (node == NULL)
214 return got_error_from_errno();
216 memcpy(&node->id, commit_id, sizeof(node->id));
217 node->commit = commit;
218 SIMPLEQ_INIT(&node->child_ids);
220 err = got_object_idset_add((void **)(&existing_node),
221 graph->node_ids, &node->id, node);
222 if (err == NULL) {
223 struct got_parent_id *pid;
225 err = add_iteration_candidate(graph, node);
226 if (err)
227 return err;
229 err = got_object_idset_remove(graph->open_branches, commit_id);
230 if (err && err->code != GOT_ERR_NO_OBJ)
231 return err;
232 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
233 if (got_object_idset_get(graph->node_ids, pid->id))
234 continue; /* parent already traversed */
235 err = got_object_idset_add(NULL, graph->open_branches,
236 pid->id, node);
237 if (err && err->code != GOT_ERR_OBJ_EXISTS)
238 return err;
240 *new_node = node;
241 } else if (err->code == GOT_ERR_OBJ_EXISTS) {
242 err = NULL;
243 free(node);
244 node = existing_node;
245 } else {
246 free(node);
247 return err;
250 if (child_commit_id) {
251 struct got_parent_id *child, *cid;
253 /* Prevent linking to self. */
254 if (got_object_id_cmp(commit_id, child_commit_id) == 0)
255 return got_error(GOT_ERR_BAD_OBJ_ID);
257 /* Prevent double-linking to the same child. */
258 SIMPLEQ_FOREACH(cid, &node->child_ids, entry) {
259 if (got_object_id_cmp(cid->id, child_commit_id) == 0)
260 return got_error(GOT_ERR_BAD_OBJ_ID);
263 child = calloc(1, sizeof(*child));
264 if (child == NULL)
265 return got_error_from_errno();
266 child->id = got_object_id_dup(child_commit_id);
267 if (child->id == NULL) {
268 err = got_error_from_errno();
269 free(child);
270 return err;
272 SIMPLEQ_INSERT_TAIL(&node->child_ids, child, entry);
273 node->nchildren++;
276 return err;
279 const struct got_error *
280 got_commit_graph_open(struct got_commit_graph **graph,
281 struct got_object_id *commit_id, struct got_repository *repo)
283 const struct got_error *err = NULL;
284 struct got_object *obj;
285 struct got_commit_object *commit;
287 *graph = NULL;
289 err = got_object_open(&obj, repo, commit_id);
290 if (err)
291 return err;
292 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
293 err = got_error(GOT_ERR_OBJ_TYPE);
294 got_object_close(obj);
295 return err;
298 err = got_object_commit_open(&commit, repo, obj);
299 got_object_close(obj);
300 if (err)
301 return err;
303 *graph = alloc_graph();
304 if (*graph == NULL) {
305 got_object_commit_close(commit);
306 return got_error_from_errno();
309 err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL);
310 if (err) {
311 got_commit_graph_close(*graph);
312 *graph = NULL;
313 return err;
316 return NULL;
319 static const struct got_error *
320 open_commit(struct got_commit_object **commit, struct got_object_id *id,
321 struct got_repository *repo)
323 const struct got_error *err;
324 struct got_object *obj;
326 err = got_object_open(&obj, repo, id);
327 if (err)
328 return err;
329 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
330 err = got_error(GOT_ERR_OBJ_TYPE);
331 goto done;
334 err = got_object_commit_open(commit, repo, obj);
335 done:
336 got_object_close(obj);
337 return err;
340 struct got_commit_graph_branch {
341 struct got_object_id parent_id;
342 struct got_commit_graph_node *node;
343 };
345 struct gather_branches_arg {
346 struct got_commit_graph_branch *branches;
347 int nbranches;
348 };
350 static void
351 gather_branches(struct got_object_id *id, void *data, void *arg)
353 struct gather_branches_arg *a = arg;
354 memcpy(&a->branches[a->nbranches].parent_id, id, sizeof(*id));
355 a->branches[a->nbranches].node = data;
356 a->nbranches++;
359 const struct got_error *
360 fetch_commits_from_open_branches(int *ncommits,
361 struct got_commit_graph *graph, struct got_repository *repo)
363 const struct got_error *err;
364 struct got_commit_graph_branch *branches;
365 struct gather_branches_arg arg;
366 int i;
368 *ncommits = 0;
370 arg.nbranches = got_object_idset_num_elements(graph->open_branches);
371 if (arg.nbranches == 0)
372 return NULL;
374 /*
375 * Adding nodes to the graph might change the graph's open
376 * branches state. Create a local copy of the current state.
377 */
378 branches = calloc(arg.nbranches, sizeof(*branches));
379 if (branches == NULL)
380 return got_error_from_errno();
381 arg.nbranches = 0; /* reset; gather_branches() will increment */
382 arg.branches = branches;
383 got_object_idset_for_each(graph->open_branches, gather_branches, &arg);
385 for (i = 0; i < arg.nbranches; i++) {
386 struct got_object_id *commit_id;
387 struct got_commit_graph_node *child_node, *new_node;
388 struct got_commit_object *commit;
390 commit_id = &branches[i].parent_id;
391 child_node = branches[i].node;
393 err = open_commit(&commit, commit_id, repo);
394 if (err)
395 break;
397 err = add_node(&new_node, graph, commit_id, commit,
398 &child_node->id);
399 if (err) {
400 if (err->code != GOT_ERR_OBJ_EXISTS)
401 break;
402 err = NULL;
404 if (new_node)
405 (*ncommits)++;
406 else
407 got_object_commit_close(commit);
410 free(branches);
411 return err;
414 const struct got_error *
415 got_commit_graph_fetch_commits(int *nfetched, struct got_commit_graph *graph,
416 int limit, struct got_repository *repo)
418 const struct got_error *err;
419 int total = 0, ncommits;
421 *nfetched = 0;
423 while (total < limit) {
424 err = fetch_commits_from_open_branches(&ncommits, graph, repo);
425 if (err)
426 return err;
427 if (ncommits == 0)
428 break;
429 total += ncommits;
432 *nfetched = total;
433 return NULL;
436 static void
437 free_graph_node(struct got_object_id *id, void *data, void *arg)
439 struct got_commit_graph_node *node = data;
440 got_object_commit_close(node->commit);
441 while (!SIMPLEQ_EMPTY(&node->child_ids)) {
442 struct got_parent_id *child = SIMPLEQ_FIRST(&node->child_ids);
443 SIMPLEQ_REMOVE_HEAD(&node->child_ids, entry);
444 free(child);
446 free(node);
449 void
450 got_commit_graph_close(struct got_commit_graph *graph)
452 got_object_idset_free(graph->open_branches);
453 got_object_idset_for_each(graph->node_ids, free_graph_node, NULL);
454 got_object_idset_free(graph->node_ids);
455 free(graph);
458 const struct got_error *
459 got_commit_graph_iter_start(struct got_commit_graph *graph,
460 struct got_object_id *id)
462 struct got_commit_graph_node *start_node, *node;
463 struct got_parent_id *pid;
465 start_node = got_object_idset_get(graph->node_ids, id);
466 if (start_node == NULL)
467 return got_error(GOT_ERR_NO_OBJ);
469 graph->iter_node = start_node;
471 while (!TAILQ_EMPTY(&graph->iter_candidates)) {
472 node = TAILQ_FIRST(&graph->iter_candidates);
473 TAILQ_REMOVE(&graph->iter_candidates, node, entry);
476 /* Put all known parents of this commit on the candidate list. */
477 SIMPLEQ_FOREACH(pid, &start_node->commit->parent_ids, entry) {
478 node = got_object_idset_get(graph->node_ids, pid->id);
479 if (node) {
480 const struct got_error *err;
481 err = add_iteration_candidate(graph, node);
482 if (err)
483 return err;
487 return NULL;
490 const struct got_error *
491 got_commit_graph_iter_next(struct got_commit_object **commit,
492 struct got_object_id **id, struct got_commit_graph *graph)
494 struct got_commit_graph_node *node;
496 if (graph->iter_node == NULL) {
497 /* We are done interating, or iteration was not started. */
498 *commit = NULL;
499 *id = NULL;
500 return NULL;
503 if (TAILQ_EMPTY(&graph->iter_candidates)) {
504 if (is_root_node(graph->iter_node) &&
505 got_object_idset_num_elements(graph->open_branches) == 0) {
506 *commit = graph->iter_node->commit;
507 *id = &graph->iter_node->id;
508 /* We are done interating. */
509 graph->iter_node = NULL;
510 return NULL;
512 return got_error(GOT_ERR_ITER_NEED_MORE);
515 *commit = graph->iter_node->commit;
516 *id = &graph->iter_node->id;
517 node = TAILQ_FIRST(&graph->iter_candidates);
518 TAILQ_REMOVE(&graph->iter_candidates, node, entry);
519 graph->iter_node = node;
520 return NULL;
523 const struct got_commit_object *
524 got_commit_graph_get_commit(struct got_commit_graph *graph,
525 struct got_object_id *id)
527 struct got_commit_graph_node *node;
528 node = got_object_idset_get(graph->node_ids, id);
529 return node ? node->commit : NULL;