Commit ee780d5ce8c335e430804438c35eb48564ec0afb

Stefan Sperling 2020-01-04T15:44:13

remove GOT_ERR_ITER_NEED_MORE; the commit graph now fetches internally instead

diff --git a/got/got.c b/got/got.c
index e658e62..9f9906e 100644
--- a/got/got.c
+++ b/got/got.c
@@ -927,17 +927,12 @@ check_same_branch(struct got_object_id *commit_id,
 
 	for (;;) {
 		struct got_object_id *id;
-		err = got_commit_graph_iter_next(&id, graph);
+		err = got_commit_graph_iter_next(&id, graph, repo,
+		    check_cancelled, NULL);
 		if (err) {
-			if (err->code == GOT_ERR_ITER_COMPLETED) {
+			if (err->code == GOT_ERR_ITER_COMPLETED)
 				err = NULL;
-				break;
-			} else if (err->code != GOT_ERR_ITER_NEED_MORE)
-				break;
-			err = got_commit_graph_fetch_commits(graph, 1,
-			    repo, check_cancelled, NULL);
-			if (err)
-				break;
+			break;
 		}
 
 		if (id) {
@@ -1787,20 +1782,12 @@ print_commits(struct got_object_id *root_id, struct got_repository *repo,
 		if (sigint_received || sigpipe_received)
 			break;
 
-		err = got_commit_graph_iter_next(&id, graph);
+		err = got_commit_graph_iter_next(&id, graph, repo,
+		    check_cancelled, NULL);
 		if (err) {
-			if (err->code == GOT_ERR_ITER_COMPLETED) {
+			if (err->code == GOT_ERR_ITER_COMPLETED)
 				err = NULL;
-				break;
-			}
-			if (err->code != GOT_ERR_ITER_NEED_MORE)
-				break;
-			err = got_commit_graph_fetch_commits(graph, 1, repo,
-			    check_cancelled, NULL);
-			if (err)
-				break;
-			else
-				continue;
+			break;
 		}
 		if (id == NULL)
 			break;
@@ -5375,20 +5362,16 @@ collect_commits(struct got_object_id_queue *commits,
 	if (err)
 		goto done;
 	while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
-		err = got_commit_graph_iter_next(&parent_id, graph);
+		err = got_commit_graph_iter_next(&parent_id, graph, repo,
+		    check_cancelled, NULL);
 		if (err) {
 			if (err->code == GOT_ERR_ITER_COMPLETED) {
 				err = got_error_msg(GOT_ERR_ANCESTRY,
 				    "ran out of commits to rebase before "
 				    "youngest common ancestor commit has "
 				    "been reached?!?");
-				goto done;
-			} else if (err->code != GOT_ERR_ITER_NEED_MORE)
-				goto done;
-			err = got_commit_graph_fetch_commits(graph, 1, repo,
-			    check_cancelled, NULL);
-			if (err)
-				goto done;
+			}
+			goto done;
 		} else {
 			err = check_path_prefix(parent_id, commit_id,
 			    path_prefix, path_prefix_errcode, repo);
diff --git a/include/got_commit_graph.h b/include/got_commit_graph.h
index 488763b..e21a530 100644
--- a/include/got_commit_graph.h
+++ b/include/got_commit_graph.h
@@ -21,14 +21,11 @@ const struct got_error *got_commit_graph_open(struct got_commit_graph **,
     struct got_repository *repo);
 void got_commit_graph_close(struct got_commit_graph *);
 
-const struct got_error *got_commit_graph_fetch_commits(
-    struct got_commit_graph *, int, struct got_repository *,
-    got_cancel_cb, void *);
 const struct got_error *got_commit_graph_iter_start(
     struct got_commit_graph *, struct got_object_id *, struct got_repository *,
     got_cancel_cb, void *);
 const struct got_error *got_commit_graph_iter_next(struct got_object_id **,
-    struct got_commit_graph *);
+    struct got_commit_graph *, struct got_repository *, got_cancel_cb, void *);
 const struct got_error *got_commit_graph_intersect(struct got_object_id **,
     struct got_commit_graph *, struct got_commit_graph *,
     struct got_repository *);
diff --git a/include/got_error.h b/include/got_error.h
index 577bc08..b8f22cf 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -58,7 +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
+/* 45 is currently unused */
 #define GOT_ERR_ITER_COMPLETED	46
 #define GOT_ERR_RANGE		47
 #define GOT_ERR_EXPECTED	48 /* for use in regress tests only */
@@ -176,7 +176,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" },
+	/* 45 is currently unused */
 	{ GOT_ERR_ITER_COMPLETED,"iteration completed" },
 	{ GOT_ERR_RANGE,	"value out of range" },
 	{ GOT_ERR_EXPECTED,	"expected an error but have no error" },
diff --git a/lib/blame.c b/lib/blame.c
index a21b3cf..00b8798 100644
--- a/lib/blame.c
+++ b/lib/blame.c
@@ -246,19 +246,12 @@ blame_open(struct got_blame **blamep, const char *path,
 		goto done;
 	id = start_commit_id;
 	for (;;) {
-		err = got_commit_graph_iter_next(&pid, graph);
+		err = got_commit_graph_iter_next(&pid, graph, repo,
+		    cancel_cb, cancel_arg);
 		if (err) {
-			if (err->code == GOT_ERR_ITER_COMPLETED) {
+			if (err->code == GOT_ERR_ITER_COMPLETED)
 				err = NULL;
-				break;
-			}
-			if (err->code != GOT_ERR_ITER_NEED_MORE)
-				break;
-			err = got_commit_graph_fetch_commits(graph, 1, repo,
-			    cancel_cb, cancel_arg);
-			if (err)
-				break;
-			continue;
+			break;
 		}
 		if (pid) {
 			err = blame_commit(blame, pid, id, path, repo, cb, arg);
diff --git a/lib/commit_graph.c b/lib/commit_graph.c
index 899ed14..c4649f5 100644
--- a/lib/commit_graph.c
+++ b/lib/commit_graph.c
@@ -537,33 +537,6 @@ done:
 	return err;
 }
 
-const struct got_error *
-got_commit_graph_fetch_commits(struct got_commit_graph *graph, int limit,
-    struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
-{
-	const struct got_error *err;
-	int nfetched = 0, ncommits;
-	struct got_object_id *changed_id = NULL;
-
-	while (nfetched < limit) {
-		if (cancel_cb) {
-			err = (*cancel_cb)(cancel_arg);
-			if (err)
-				return err;
-		}
-		err = fetch_commits_from_open_branches(&ncommits,
-		    &changed_id, graph, repo, cancel_cb, cancel_arg);
-		if (err)
-			return err;
-		if (ncommits == 0)
-			break;
-		if (changed_id)
-			nfetched += ncommits;
-	}
-
-	return NULL;
-}
-
 static const struct got_error *
 free_node_iter(struct got_object_id *id, void *data, void *arg)
 {
@@ -638,8 +611,11 @@ got_commit_graph_iter_start(struct got_commit_graph *graph,
 
 const struct got_error *
 got_commit_graph_iter_next(struct got_object_id **id,
-    struct got_commit_graph *graph)
+    struct got_commit_graph *graph, struct got_repository *repo,
+    got_cancel_cb cancel_cb, void *cancel_arg)
 {
+	const struct got_error *err = NULL;
+
 	*id = NULL;
 
 	if (graph->iter_node == NULL) {
@@ -656,8 +632,15 @@ got_commit_graph_iter_next(struct got_object_id **id,
 		return NULL;
 	}
 
-	if (TAILQ_NEXT(graph->iter_node, entry) == NULL)
-		return got_error(GOT_ERR_ITER_NEED_MORE);
+	while (TAILQ_NEXT(graph->iter_node, entry) == NULL &&
+	    got_object_idset_num_elements(graph->open_branches) > 0) {
+		int ncommits;
+		struct got_object_id *changed_id;
+		err = fetch_commits_from_open_branches(&ncommits,
+		    &changed_id, graph, repo, cancel_cb, cancel_arg);
+		if (err)
+			return err;
+	}
 
 	*id = &graph->iter_node->id;
 	graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);
@@ -699,7 +682,7 @@ got_commit_graph_find_youngest_common_ancestor(struct got_object_id **yca_id,
 		goto done;
 
 	for (;;) {
-		struct got_object_id *id, *id2;
+		struct got_object_id *id = NULL, *id2 = NULL;
 
 		if (cancel_cb) {
 			err = (*cancel_cb)(cancel_arg);
@@ -708,30 +691,24 @@ got_commit_graph_find_youngest_common_ancestor(struct got_object_id **yca_id,
 		}
 
 		if (!completed) {
-			err = got_commit_graph_iter_next(&id, graph);
+			err = got_commit_graph_iter_next(&id, graph, repo,
+			    cancel_cb, cancel_arg);
 			if (err) {
-				if (err->code == GOT_ERR_ITER_COMPLETED)
-					completed = 1;
-				else if (err->code != GOT_ERR_ITER_NEED_MORE)
-					break;
-				err = got_commit_graph_fetch_commits(graph, 1,
-				    repo, cancel_cb, cancel_arg);
-				if (err)
+				if (err->code != GOT_ERR_ITER_COMPLETED)
 					break;
+				err = NULL;
+				completed = 1;
 			}
 		}
 
 		if (!completed2) {
-			err = got_commit_graph_iter_next(&id2, graph2);
+			err = got_commit_graph_iter_next(&id2, graph2, repo,
+			    cancel_cb, cancel_arg);
 			if (err) {
-				if (err->code == GOT_ERR_ITER_COMPLETED)
-					completed2 = 1;
-				else if (err->code != GOT_ERR_ITER_NEED_MORE)
-					break;
-				err = got_commit_graph_fetch_commits(graph2, 1,
-				    repo, cancel_cb, cancel_arg);
-				if (err)
+				if (err->code != GOT_ERR_ITER_COMPLETED)
 					break;
+				err = NULL;
+				completed2 = 1;
 			}
 		}
 
diff --git a/tog/tog.c b/tog/tog.c
index db28ea8..837e0f9 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -1397,18 +1397,8 @@ queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
 		struct commit_queue_entry *entry;
 		int errcode;
 
-		err = got_commit_graph_iter_next(&id, graph);
-		if (err) {
-			if (err->code != GOT_ERR_ITER_NEED_MORE)
-				break;
-			err = got_commit_graph_fetch_commits(graph,
-			    minqueue, repo, NULL, NULL);
-			if (err)
-				return err;
-			continue;
-		}
-
-		if (id == NULL)
+		err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
+		if (err || id == NULL)
 			break;
 
 		err = got_object_open_as_commit(&commit, repo, id);