Commit 1142eae973ea98f41f38c35b80b1ed921aebf56d

Stefan Sperling 2018-06-11T15:22:15

introduce got_commit_graph_fetch_commits_up_to()

diff --git a/include/.got_commit_graph.h.swp b/include/.got_commit_graph.h.swp
new file mode 100644
index 0000000..b410fea
Binary files /dev/null and b/include/.got_commit_graph.h.swp differ
diff --git a/include/got_commit_graph.h b/include/got_commit_graph.h
index c97734f..66ff219 100644
--- a/include/got_commit_graph.h
+++ b/include/got_commit_graph.h
@@ -23,6 +23,8 @@ 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_fetch_commits_up_to(int *,
+    struct got_commit_graph *, struct got_object_id *, 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_object_id **,
diff --git a/lib/.commit_graph.c.swp b/lib/.commit_graph.c.swp
new file mode 100644
index 0000000..d7431b4
Binary files /dev/null and b/lib/.commit_graph.c.swp differ
diff --git a/lib/commit_graph.c b/lib/commit_graph.c
index 50d6b35..cebac9e 100644
--- a/lib/commit_graph.c
+++ b/lib/commit_graph.c
@@ -312,9 +312,10 @@ gather_branches(struct got_object_id *id, void *data, void *arg)
 	a->nbranches++;
 }
 
-const struct got_error *
-fetch_commits_from_open_branches(int *ncommits,
-    struct got_commit_graph *graph, struct got_repository *repo)
+static const struct got_error *
+fetch_commits_from_open_branches(int *ncommits, int *wanted_id_added,
+    struct got_commit_graph *graph, struct got_repository *repo,
+    struct got_object_id *wanted_id)
 {
 	const struct got_error *err;
 	struct got_commit_graph_branch *branches;
@@ -322,6 +323,8 @@ fetch_commits_from_open_branches(int *ncommits,
 	int i;
 
 	*ncommits = 0;
+	if (wanted_id_added)
+		*wanted_id_added = 0;
 
 	arg.nbranches = got_object_idset_num_elements(graph->open_branches);
 	if (arg.nbranches == 0)
@@ -360,6 +363,8 @@ fetch_commits_from_open_branches(int *ncommits,
 		}
 		if (new_node)
 			(*ncommits)++;
+		if (wanted_id && got_object_id_cmp(commit_id, wanted_id) == 0)
+			*wanted_id_added = 1;
 	}
 
 	free(branches);
@@ -371,20 +376,42 @@ 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;
+	int ncommits;
 
 	*nfetched = 0;
 
-	while (total < limit) {
-		err = fetch_commits_from_open_branches(&ncommits, graph, repo);
+	while (*nfetched < limit) {
+		err = fetch_commits_from_open_branches(&ncommits, NULL,
+		    graph, repo, NULL);
 		if (err)
 			return err;
 		if (ncommits == 0)
 			break;
-		total += ncommits;
+		*nfetched += ncommits;
+	}
+
+	return NULL;
+}
+
+const struct got_error *
+got_commit_graph_fetch_commits_up_to(int *nfetched,
+    struct got_commit_graph *graph, struct got_object_id *wanted_id,
+    struct got_repository *repo)
+{
+	const struct got_error *err;
+	int ncommits, wanted_id_added = 0;
+
+	*nfetched = 0;
+	while (!wanted_id_added) {
+		err = fetch_commits_from_open_branches(&ncommits,
+		    &wanted_id_added, graph, repo, wanted_id);
+		if (err)
+			return err;
+		if (ncommits == 0)
+			return NULL;
+		*nfetched += ncommits;
 	}
 
-	*nfetched = total;
 	return NULL;
 }