basic commit graph; make 'got log' use it; WIP with known bugs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
diff --git a/got/Makefile b/got/Makefile
index 810f98c..f3bd403 100644
--- a/got/Makefile
+++ b/got/Makefile
@@ -1,9 +1,10 @@
.PATH:${.CURDIR}/../lib
PROG= got
-SRCS= got.c delta.c diff.c diffreg.c error.c fileindex.c object.c \
- opentemp.c path.c pack.c privsep.c reference.c repository.c \
- sha1.c worktree.c zbuf.c
+SRCS= got.c commit_graph.c delta.c diff.c diffreg.c error.c \
+ fileindex.c object.c object_idset.c opentemp.c path.c \
+ pack.c privsep.c reference.c repository.c sha1.c \
+ worktree.c zbuf.c
CPPFLAGS = -I${.CURDIR}/../include -I${.CURDIR}/../lib
LDADD = -lutil -lz
diff --git a/got/got.c b/got/got.c
index 4849992..aaa7c0a 100644
--- a/got/got.c
+++ b/got/got.c
@@ -33,6 +33,7 @@
#include "got_repository.h"
#include "got_worktree.h"
#include "got_diff.h"
+#include "got_commit_graph.h"
#ifndef nitems
#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
@@ -331,101 +332,43 @@ print_commit(struct got_commit_object *commit, struct got_object_id *id,
return err;
}
-struct commit_queue_entry {
- TAILQ_ENTRY(commit_queue_entry) entry;
- struct got_object_id *id;
- struct got_commit_object *commit;
-};
-
static const struct got_error *
print_commits(struct got_object *root_obj, struct got_object_id *root_id,
struct got_repository *repo, int show_patch, int limit)
{
const struct got_error *err;
- struct got_commit_object *root_commit;
- TAILQ_HEAD(, commit_queue_entry) commits;
- struct commit_queue_entry *entry;
-
- TAILQ_INIT(&commits);
+ struct got_commit_graph *graph;
+ int ncommits;
- err = got_object_commit_open(&root_commit, repo, root_obj);
+ err = got_commit_graph_open(&graph, root_id, repo);
if (err)
return err;
-
- entry = calloc(1, sizeof(*entry));
- if (entry == NULL)
- return got_error_from_errno();
- entry->id = got_object_id_dup(root_id);
- if (entry->id == NULL) {
- err = got_error_from_errno();
- free(entry);
+ err = got_commit_graph_iter_start(graph, root_id);
+ if (err)
return err;
- }
- entry->commit = root_commit;
- TAILQ_INSERT_HEAD(&commits, entry, entry);
-
- while (!TAILQ_EMPTY(&commits)) {
- struct got_parent_id *pid;
- struct got_object *obj;
- struct got_commit_object *pcommit;
- struct commit_queue_entry *pentry;
-
- entry = TAILQ_FIRST(&commits);
-
- err = print_commit(entry->commit, entry->id, repo, show_patch);
- if (err)
- break;
-
- if (limit && --limit == 0)
- break;
-
- if (entry->commit->nparents == 0)
- break;
-
- /* Follow the first parent (TODO: handle merge commits). */
- pid = SIMPLEQ_FIRST(&entry->commit->parent_ids);
- err = got_object_open(&obj, repo, pid->id);
- if (err)
- break;
- if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
- err = got_error(GOT_ERR_OBJ_TYPE);
- break;
+ do {
+ struct got_commit_object *commit;
+ struct got_object_id *id;
+
+ err = got_commit_graph_iter_next(&commit, &id, graph);
+ if (err) {
+ if (err->code != GOT_ERR_ITER_NEED_MORE)
+ break;
+ err = got_commit_graph_fetch_commits(&ncommits,
+ graph, 1, repo);
+ if (err)
+ break;
+ else
+ continue;
}
-
- err = got_object_commit_open(&pcommit, repo, obj);
- got_object_close(obj);
- if (err)
+ if (commit == NULL)
break;
-
- pentry = calloc(1, sizeof(*pentry));
- if (pentry == NULL) {
- err = got_error_from_errno();
- got_object_commit_close(pcommit);
- break;
- }
- pentry->id = got_object_id_dup(pid->id);
- if (pentry->id == NULL) {
- err = got_error_from_errno();
- got_object_commit_close(pcommit);
+ err = print_commit(commit, id, repo, show_patch);
+ if (err || (limit && --limit == 0))
break;
- }
- pentry->commit = pcommit;
- TAILQ_INSERT_TAIL(&commits, pentry, entry);
-
- TAILQ_REMOVE(&commits, entry, entry);
- got_object_commit_close(entry->commit);
- free(entry->id);
- free(entry);
- }
-
- while (!TAILQ_EMPTY(&commits)) {
- entry = TAILQ_FIRST(&commits);
- TAILQ_REMOVE(&commits, entry, entry);
- got_object_commit_close(entry->commit);
- free(entry->id);
- free(entry);
- }
+ } while (ncommits > 0);
+ got_commit_graph_close(graph);
return err;
}
diff --git a/include/got_commit_graph.h b/include/got_commit_graph.h
new file mode 100644
index 0000000..1e63200
--- /dev/null
+++ b/include/got_commit_graph.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+struct got_commit_graph;
+
+const struct got_error *got_commit_graph_open(struct got_commit_graph **,
+ struct got_object_id *commit_id, struct got_repository *repo);
+void got_commit_graph_close(struct got_commit_graph *);
+
+const struct got_error *
+got_commit_graph_fetch_commits(int *, struct got_commit_graph *, int,
+ struct got_repository *);
+const struct got_error *got_commit_graph_iter_start(
+ struct got_commit_graph *, struct got_object_id *);
+const struct got_error *got_commit_graph_iter_next(struct got_commit_object **,
+ struct got_object_id **, struct got_commit_graph *);
+
+const struct got_commit_object *got_commit_graph_get_commit(
+ struct got_commit_graph *, struct got_object_id *);
diff --git a/include/got_error.h b/include/got_error.h
index ccb4b12..c833782 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -58,6 +58,7 @@
#define GOT_ERR_PACK_OFFSET 42
#define GOT_ERR_OBJ_EXISTS 43
#define GOT_ERR_BAD_OBJ_ID 44
+#define GOT_ERR_ITER_NEED_MORE 45
static const struct got_error {
int code;
@@ -105,6 +106,7 @@ static const struct got_error {
{ GOT_ERR_PACK_OFFSET, "bad offset in pack file" },
{ GOT_ERR_OBJ_EXISTS, "object already exists" },
{ GOT_ERR_BAD_OBJ_ID, "bad object id" },
+ { GOT_ERR_ITER_NEED_MORE,"more items needed to continue iteration" },
};
/*
diff --git a/lib/commit_graph.c b/lib/commit_graph.c
new file mode 100644
index 0000000..609fa34
--- /dev/null
+++ b/lib/commit_graph.c
@@ -0,0 +1,552 @@
+/*
+ * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/queue.h>
+#include <sys/stdint.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sha1.h>
+#include <zlib.h>
+#include <ctype.h>
+
+#include "got_error.h"
+#include "got_object.h"
+#include "got_commit_graph.h"
+
+#include "got_lib_delta.h"
+#include "got_lib_zbuf.h"
+#include "got_lib_object.h"
+#include "got_lib_object_idset.h"
+
+struct got_commit_graph_node {
+ struct got_object_id id;
+
+ /*
+ * Each graph node corresponds to a commit object.
+ * Graph vertices are modelled with two separate adjacency lists:
+ * Adjacencies of a graph node are either parent (older) commits,
+ * and child (younger) commits.
+ */
+ struct got_commit_object *commit; /* contains list of parents */
+ int nchildren;
+ SIMPLEQ_HEAD(, got_parent_id) child_ids;
+
+ /* Used during graph iteration. */
+ TAILQ_ENTRY(got_commit_graph_node) entry;
+};
+
+struct got_commit_graph {
+ /* The set of all commits we have traversed. */
+ struct got_object_idset *node_ids;
+
+ /* The commit at which traversal began (youngest commit in node_ids). */
+ struct got_commit_graph_node *head_node;
+
+ /*
+ * A set of object IDs of known parent commits which we have not yet
+ * traversed. Each commit ID in this set represents a branch in commit
+ * history: Either the first-parent branch of the head node, or another
+ * branch corresponding to a traversed merge commit for which we have
+ * not traversed a branch point commit yet.
+ *
+ * Whenever we add a commit with a matching ID to the graph, we remove
+ * its corresponding element from this set, and add new elements for
+ * each of that commit's parent commits which were not traversed yet.
+ *
+ * When API users ask us to fetch more commits, we fetch commits from
+ * all currently open branches. This allows API users to process
+ * commits in linear order even though the history contains branches.
+ */
+ struct got_object_idset *open_branches;
+
+ /* The next commit to return when the API user asks for one. */
+ struct got_commit_graph_node *iter_node;
+
+ TAILQ_HEAD(, got_commit_graph_node) iter_candidates;
+};
+
+static struct got_commit_graph *
+alloc_graph(void)
+{
+ struct got_commit_graph *graph;
+
+ graph = calloc(1, sizeof(*graph));
+ if (graph == NULL)
+ return NULL;
+
+ graph->node_ids = got_object_idset_alloc();
+ if (graph->node_ids == NULL) {
+ free(graph);
+ return NULL;
+ }
+
+ graph->open_branches = got_object_idset_alloc();
+ if (graph->open_branches == NULL) {
+ got_object_idset_free(graph->node_ids);
+ free(graph);
+ return NULL;
+ }
+
+ TAILQ_INIT(&graph->iter_candidates);
+ return graph;
+}
+
+#if 0
+static int
+is_head_node(struct got_commit_graph_node *node)
+{
+ return node->nchildren == 0;
+}
+
+static int
+is_merge_point(struct got_commit_graph_node *node)
+{
+ return node->commit->nparents > 1;
+}
+
+int
+is_branch_point(struct got_commit_graph_node *node)
+{
+ return node->nchildren > 1;
+}
+#endif
+
+static int
+is_root_node(struct got_commit_graph_node *node)
+{
+ return node->commit->nparents == 0;
+}
+
+static const struct got_error *
+parse_commit_time(int64_t *time, struct got_commit_object *commit)
+{
+ const struct got_error *err = NULL;
+ const char *errstr;
+ char *committer, *space;
+
+ *time = 0;
+
+ committer = strdup(commit->committer);
+ if (committer == NULL)
+ return got_error_from_errno();
+
+ /* Strip off trailing timezone indicator. */
+ space = strrchr(committer, ' ');
+ if (space == NULL) {
+ err = got_error(GOT_ERR_BAD_OBJ_DATA);
+ goto done;
+ }
+ *space = '\0';
+
+ /* Timestamp is separated from committer name + email by space. */
+ space = strrchr(committer, ' ');
+ if (space == NULL) {
+ err = got_error(GOT_ERR_BAD_OBJ_DATA);
+ goto done;
+ }
+
+ *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
+ if (errstr)
+ err = got_error(GOT_ERR_BAD_OBJ_DATA);
+
+done:
+ free(committer);
+ return err;
+}
+
+static const struct got_error *
+compare_commits(int *cmp, struct got_commit_object *c1,
+ struct got_commit_object *c2)
+{
+ const struct got_error *err;
+ int64_t t1, t2;
+
+ err = parse_commit_time(&t1, c1);
+ if (err)
+ return err;
+ err = parse_commit_time(&t2, c2);
+ if (err)
+ return err;
+
+ if (t1 == t2)
+ *cmp = 0;
+ else if (t1 < t2)
+ *cmp = -1;
+ else
+ *cmp = 1;
+
+ return NULL;
+}
+
+static const struct got_error *
+add_iteration_candidate(struct got_commit_graph *graph,
+ struct got_commit_graph_node *node)
+{
+ struct got_commit_graph_node *n;
+
+ if (TAILQ_EMPTY(&graph->iter_candidates)) {
+ TAILQ_INSERT_TAIL(&graph->iter_candidates, node, entry);
+ return NULL;
+ }
+
+ TAILQ_FOREACH(n, &graph->iter_candidates, entry) {
+ const struct got_error *err;
+ int cmp;
+ err = compare_commits(&cmp, node->commit, n->commit);
+ if (err)
+ return err;
+ if (cmp <= 0)
+ continue;
+ TAILQ_INSERT_BEFORE(n, node, entry);
+ break;
+ }
+
+ return NULL;
+}
+
+static const struct got_error *
+add_node(struct got_commit_graph_node **new_node,
+ struct got_commit_graph *graph, struct got_object_id *commit_id,
+ struct got_commit_object *commit, struct got_object_id *child_commit_id)
+{
+ const struct got_error *err = NULL;
+ struct got_commit_graph_node *node, *existing_node;
+
+ *new_node = NULL;
+
+ node = calloc(1, sizeof(*node));
+ if (node == NULL)
+ return got_error_from_errno();
+
+ memcpy(&node->id, commit_id, sizeof(node->id));
+ node->commit = commit;
+ SIMPLEQ_INIT(&node->child_ids);
+
+ err = got_object_idset_add((void **)(&existing_node),
+ graph->node_ids, &node->id, node);
+ if (err == NULL) {
+ struct got_parent_id *pid;
+
+ err = add_iteration_candidate(graph, node);
+ if (err)
+ return err;
+
+ err = got_object_idset_remove(graph->open_branches, commit_id);
+ if (err && err->code != GOT_ERR_NO_OBJ)
+ return err;
+ SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
+ if (got_object_idset_get(graph->node_ids, pid->id))
+ continue; /* parent already traversed */
+ err = got_object_idset_add(NULL, graph->open_branches,
+ pid->id, node);
+ if (err && err->code != GOT_ERR_OBJ_EXISTS)
+ return err;
+ }
+ *new_node = node;
+ } else if (err->code == GOT_ERR_OBJ_EXISTS) {
+ err = NULL;
+ free(node);
+ node = existing_node;
+ } else {
+ free(node);
+ return err;
+ }
+
+ if (child_commit_id) {
+ struct got_parent_id *child, *cid;
+
+ /* Prevent linking to self. */
+ if (got_object_id_cmp(commit_id, child_commit_id) == 0)
+ return got_error(GOT_ERR_BAD_OBJ_ID);
+
+ /* Prevent double-linking to the same child. */
+ SIMPLEQ_FOREACH(cid, &node->child_ids, entry) {
+ if (got_object_id_cmp(cid->id, child_commit_id) == 0)
+ return got_error(GOT_ERR_BAD_OBJ_ID);
+ }
+
+ child = calloc(1, sizeof(*child));
+ if (child == NULL)
+ return got_error_from_errno();
+ child->id = got_object_id_dup(child_commit_id);
+ if (child->id == NULL) {
+ err = got_error_from_errno();
+ free(child);
+ return err;
+ }
+ SIMPLEQ_INSERT_TAIL(&node->child_ids, child, entry);
+ node->nchildren++;
+ }
+
+ return err;
+}
+
+const struct got_error *
+got_commit_graph_open(struct got_commit_graph **graph,
+ struct got_object_id *commit_id, struct got_repository *repo)
+{
+ const struct got_error *err = NULL;
+ struct got_object *obj;
+ struct got_commit_object *commit;
+
+ *graph = NULL;
+
+ err = got_object_open(&obj, repo, commit_id);
+ if (err)
+ return err;
+ if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
+ err = got_error(GOT_ERR_OBJ_TYPE);
+ got_object_close(obj);
+ return err;
+ }
+
+ err = got_object_commit_open(&commit, repo, obj);
+ got_object_close(obj);
+ if (err)
+ return err;
+
+ *graph = alloc_graph();
+ if (*graph == NULL) {
+ got_object_commit_close(commit);
+ return got_error_from_errno();
+ }
+
+ err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL);
+ if (err) {
+ got_commit_graph_close(*graph);
+ *graph = NULL;
+ return err;
+ }
+
+ return NULL;
+}
+
+static const struct got_error *
+open_commit(struct got_commit_object **commit, struct got_object_id *id,
+ struct got_repository *repo)
+{
+ const struct got_error *err;
+ struct got_object *obj;
+
+ err = got_object_open(&obj, repo, id);
+ if (err)
+ return err;
+ if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
+ err = got_error(GOT_ERR_OBJ_TYPE);
+ goto done;
+ }
+
+ err = got_object_commit_open(commit, repo, obj);
+done:
+ got_object_close(obj);
+ return err;
+}
+
+struct got_commit_graph_branch {
+ struct got_object_id parent_id;
+ struct got_commit_graph_node *node;
+};
+
+struct gather_branches_arg {
+ struct got_commit_graph_branch *branches;
+ int nbranches;
+};
+
+static void
+gather_branches(struct got_object_id *id, void *data, void *arg)
+{
+ struct gather_branches_arg *a = arg;
+ memcpy(&a->branches[a->nbranches].parent_id, id, sizeof(*id));
+ a->branches[a->nbranches].node = data;
+ a->nbranches++;
+}
+
+const struct got_error *
+fetch_commits_from_open_branches(int *ncommits,
+ struct got_commit_graph *graph, struct got_repository *repo)
+{
+ const struct got_error *err;
+ struct got_commit_graph_branch *branches;
+ struct gather_branches_arg arg;
+ int i;
+
+ *ncommits = 0;
+
+ arg.nbranches = got_object_idset_num_elements(graph->open_branches);
+ if (arg.nbranches == 0)
+ return NULL;
+
+ /*
+ * Adding nodes to the graph might change the graph's open
+ * branches state. Create a local copy of the current state.
+ */
+ branches = calloc(arg.nbranches, sizeof(*branches));
+ if (branches == NULL)
+ return got_error_from_errno();
+ arg.nbranches = 0; /* reset; gather_branches() will increment */
+ arg.branches = branches;
+ got_object_idset_for_each(graph->open_branches, gather_branches, &arg);
+
+ for (i = 0; i < arg.nbranches; i++) {
+ struct got_object_id *commit_id;
+ struct got_commit_graph_node *child_node, *new_node;
+ struct got_commit_object *commit;
+
+ commit_id = &branches[i].parent_id;
+ child_node = branches[i].node;
+
+ err = open_commit(&commit, commit_id, repo);
+ if (err)
+ break;
+
+ err = add_node(&new_node, graph, commit_id, commit,
+ &child_node->id);
+ if (err) {
+ if (err->code != GOT_ERR_OBJ_EXISTS)
+ break;
+ err = NULL;
+ }
+ if (new_node)
+ (*ncommits)++;
+ else
+ got_object_commit_close(commit);
+ }
+
+ free(branches);
+ return err;
+}
+
+const struct got_error *
+got_commit_graph_fetch_commits(int *nfetched, struct got_commit_graph *graph,
+ int limit, struct got_repository *repo)
+{
+ const struct got_error *err;
+ int total = 0, ncommits;
+
+ *nfetched = 0;
+
+ while (total < limit) {
+ err = fetch_commits_from_open_branches(&ncommits, graph, repo);
+ if (err)
+ return err;
+ if (ncommits == 0)
+ break;
+ total += ncommits;
+ }
+
+ *nfetched = total;
+ return NULL;
+}
+
+static void
+free_graph_node(struct got_object_id *id, void *data, void *arg)
+{
+ struct got_commit_graph_node *node = data;
+ got_object_commit_close(node->commit);
+ while (!SIMPLEQ_EMPTY(&node->child_ids)) {
+ struct got_parent_id *child = SIMPLEQ_FIRST(&node->child_ids);
+ SIMPLEQ_REMOVE_HEAD(&node->child_ids, entry);
+ free(child);
+ }
+ free(node);
+}
+
+void
+got_commit_graph_close(struct got_commit_graph *graph)
+{
+ got_object_idset_free(graph->open_branches);
+ got_object_idset_for_each(graph->node_ids, free_graph_node, NULL);
+ got_object_idset_free(graph->node_ids);
+ free(graph);
+}
+
+const struct got_error *
+got_commit_graph_iter_start(struct got_commit_graph *graph,
+ struct got_object_id *id)
+{
+ struct got_commit_graph_node *start_node, *node;
+ struct got_parent_id *pid;
+
+ start_node = got_object_idset_get(graph->node_ids, id);
+ if (start_node == NULL)
+ return got_error(GOT_ERR_NO_OBJ);
+
+ graph->iter_node = start_node;
+
+ while (!TAILQ_EMPTY(&graph->iter_candidates)) {
+ node = TAILQ_FIRST(&graph->iter_candidates);
+ TAILQ_REMOVE(&graph->iter_candidates, node, entry);
+ }
+
+ /* Put all known parents of this commit on the candidate list. */
+ SIMPLEQ_FOREACH(pid, &start_node->commit->parent_ids, entry) {
+ node = got_object_idset_get(graph->node_ids, pid->id);
+ if (node) {
+ const struct got_error *err;
+ err = add_iteration_candidate(graph, node);
+ if (err)
+ return err;
+ }
+ }
+
+ return NULL;
+}
+
+const struct got_error *
+got_commit_graph_iter_next(struct got_commit_object **commit,
+ struct got_object_id **id, struct got_commit_graph *graph)
+{
+ struct got_commit_graph_node *node;
+
+ if (graph->iter_node == NULL) {
+ /* We are done interating, or iteration was not started. */
+ *commit = NULL;
+ *id = NULL;
+ return NULL;
+ }
+
+ if (TAILQ_EMPTY(&graph->iter_candidates)) {
+ if (is_root_node(graph->iter_node) &&
+ got_object_idset_num_elements(graph->open_branches) == 0) {
+ *commit = graph->iter_node->commit;
+ *id = &graph->iter_node->id;
+ /* We are done interating. */
+ graph->iter_node = NULL;
+ return NULL;
+ }
+ return got_error(GOT_ERR_ITER_NEED_MORE);
+ }
+
+ *commit = graph->iter_node->commit;
+ *id = &graph->iter_node->id;
+ node = TAILQ_FIRST(&graph->iter_candidates);
+ TAILQ_REMOVE(&graph->iter_candidates, node, entry);
+ graph->iter_node = node;
+ return NULL;
+}
+
+const struct got_commit_object *
+got_commit_graph_get_commit(struct got_commit_graph *graph,
+ struct got_object_id *id)
+{
+ struct got_commit_graph_node *node;
+ node = got_object_idset_get(graph->node_ids, id);
+ return node ? node->commit : NULL;
+}