Commit 3d5092374a96864fbc4c3d2d2e752f588c63ac39

Stefan Sperling 2020-01-04T18:25:28

simplify got_commit_graph_open()

diff --git a/got/got.c b/got/got.c
index 9f9906e..1600fc8 100644
--- a/got/got.c
+++ b/got/got.c
@@ -916,7 +916,7 @@ check_same_branch(struct got_object_id *commit_id,
 		goto done;
 	}
 
-	err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
+	err = got_commit_graph_open(&graph, "/", 1);
 	if (err)
 		goto done;
 
@@ -1767,8 +1767,7 @@ print_commits(struct got_object_id *root_id, struct got_repository *repo,
 	    regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
 		return got_error_msg(GOT_ERR_REGEX, search_pattern);
 
-	err = got_commit_graph_open(&graph, root_id, path,
-	    first_parent_traversal, repo);
+	err = got_commit_graph_open(&graph, path, first_parent_traversal);
 	if (err)
 		return err;
 	err = got_commit_graph_iter_start(graph, root_id, repo,
@@ -5353,7 +5352,7 @@ collect_commits(struct got_object_id_queue *commits,
 	struct got_object_qid *qid;
         struct got_object_id *commit_id = initial_commit_id;
 
-	err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
+	err = got_commit_graph_open(&graph, "/", 1);
 	if (err)
 		return err;
 
diff --git a/include/got_commit_graph.h b/include/got_commit_graph.h
index e21a530..2ad08d3 100644
--- a/include/got_commit_graph.h
+++ b/include/got_commit_graph.h
@@ -17,8 +17,7 @@
 struct got_commit_graph;
 
 const struct got_error *got_commit_graph_open(struct got_commit_graph **,
-    struct got_object_id *commit_id, const char *, int,
-    struct got_repository *repo);
+    const char *, int);
 void got_commit_graph_close(struct got_commit_graph *);
 
 const struct got_error *got_commit_graph_iter_start(
diff --git a/include/got_error.h b/include/got_error.h
index b8f22cf..e1ce795 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
-/* 45 is currently unused */
+#define GOT_ERR_ITER_BUSY	45
 #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" },
-	/* 45 is currently unused */
+	{ GOT_ERR_ITER_BUSY,	"iteration already in progress" },
 	{ 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 00b8798..004a114 100644
--- a/lib/blame.c
+++ b/lib/blame.c
@@ -237,7 +237,7 @@ blame_open(struct got_blame **blamep, const char *path,
 		goto done;
 	}
 
-	err = got_commit_graph_open(&graph, start_commit_id, path, 1, repo);
+	err = got_commit_graph_open(&graph, path, 1);
 	if (err)
 		return err;
 	err = got_commit_graph_iter_start(graph, start_commit_id, repo,
diff --git a/lib/commit_graph.c b/lib/commit_graph.c
index 3547dae..388504a 100644
--- a/lib/commit_graph.c
+++ b/lib/commit_graph.c
@@ -379,52 +379,15 @@ add_node(struct got_commit_graph_node **new_node, int *changed,
 
 const struct got_error *
 got_commit_graph_open(struct got_commit_graph **graph,
-    struct got_object_id *commit_id, const char *path,
-    int first_parent_traversal, struct got_repository *repo)
+    const char *path, int first_parent_traversal)
 {
-	const struct got_error *err = NULL;
-	struct got_commit_object *commit;
-	int changed, branch_done;
-	struct got_commit_graph_node *node;
-
-	*graph = NULL;
-
-	err = got_object_open_as_commit(&commit, repo, commit_id);
-	if (err)
-		return err;
-
-	/* The path must exist in our initial commit. */
-	if (!got_path_is_root_dir(path)) {
-		struct got_object_id *obj_id;
-		err = got_object_id_by_path(&obj_id, repo, commit_id, path);
-		if (err)
-			return err;
-		free(obj_id);
-	}
-
 	*graph = alloc_graph(path);
-	if (*graph == NULL) {
-		err = got_error_from_errno("alloc_graph");
-		got_object_commit_close(commit);
-		return err;
-	}
+	if (*graph == NULL)
+		return got_error_from_errno("alloc_graph");
 
 	if (first_parent_traversal)
 		(*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
 
-	err = add_node(&node, &changed, &branch_done, *graph,
-	    commit_id, commit, NULL, repo);
-	if (err == NULL) {
-		err = advance_branch(*graph, node, commit_id,
-		    commit, repo);
-	}
-	got_object_commit_close(commit);
-	if (err) {
-		got_commit_graph_close(*graph);
-		*graph = NULL;
-		return err;
-	}
-	
 	return NULL;
 }
 
@@ -562,24 +525,24 @@ got_commit_graph_iter_start(struct got_commit_graph *graph,
 	const struct got_error *err = NULL;
 	struct got_commit_graph_node *start_node;
 	struct got_commit_object *commit;
-	int changed;
+	int changed, branch_done;
 
-	start_node = got_object_idset_get(graph->node_ids, id);
-	while (start_node == NULL) {
-		int ncommits;
-		err = fetch_commits_from_open_branches(&ncommits, NULL, graph,
-		    repo, cancel_cb, cancel_arg);
-		if (err)
-			return err;
-		if (ncommits == 0)
-			return got_error_no_obj(id);
-		start_node = got_object_idset_get(graph->node_ids, id);
-	}
+	if (!TAILQ_EMPTY(&graph->iter_list))
+		return got_error(GOT_ERR_ITER_BUSY);
 
-	err = got_object_open_as_commit(&commit, repo, &start_node->id);
+	err = got_object_open_as_commit(&commit, repo, id);
 	if (err)
 		return err;
 
+	err = add_node(&start_node, &changed, &branch_done, graph, id,
+	    commit, NULL, repo);
+	if (err)
+		goto done;
+
+	err = advance_branch(graph, start_node, id, commit, repo);
+	if (err)
+		goto done;
+
 	err = detect_changed_path(&changed, commit, &start_node->id,
 	    graph->path, repo);
 	if (err) {
@@ -601,10 +564,11 @@ got_commit_graph_iter_start(struct got_commit_graph *graph,
 		}
 		start_node = got_object_idset_get(graph->node_ids, changed_id);
 	}
+done:
 	got_object_commit_close(commit);
-
-	graph->iter_node = start_node;
-	return NULL;
+	if (err == NULL)
+		graph->iter_node = start_node;
+	return err;
 }
 
 const struct got_error *
@@ -661,11 +625,11 @@ got_commit_graph_find_youngest_common_ancestor(struct got_object_id **yca_id,
 	if (commit_ids == NULL)
 		return got_error_from_errno("got_object_idset_alloc");
 
-	err = got_commit_graph_open(&graph, commit_id, "/", 1, repo);
+	err = got_commit_graph_open(&graph, "/", 1);
 	if (err)
 		goto done;
 
-	err = got_commit_graph_open(&graph2, commit_id2, "/", 1, repo);
+	err = got_commit_graph_open(&graph2, "/", 1);
 	if (err)
 		goto done;
 
diff --git a/tog/tog.c b/tog/tog.c
index 837e0f9..c74a02a 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -2153,8 +2153,7 @@ open_log_view(struct tog_view *view, struct got_object_id *start_id,
 	err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
 	if (err)
 		goto done;
-	err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
-	    0, thread_repo);
+	err = got_commit_graph_open(&thread_graph, s->in_repo_path, 0);
 	if (err)
 		goto done;