]> git.ipfire.org Git - thirdparty/git.git/commitdiff
revision: add peek functions for lookahead
authorPablo Sabater <pabloosabaterr@gmail.com>
Sat, 20 Jun 2026 10:11:51 +0000 (12:11 +0200)
committerJunio C Hamano <gitster@pobox.com>
Sat, 20 Jun 2026 14:31:28 +0000 (07:31 -0700)
The graph code in a subsequent commit needs to be able to look ahead in
order to set indentation-related flags.

Using revs->commits is brittle and the data structure that holds the
pending commits might change in the future.

Add two functions that abstract this for the graph.

Helped-by: Kristofer Karlsson <stoansen@gmail.com>
Signed-off-by: Pablo Sabater <pabloosabaterr@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
revision.c
revision.h

index 6a8101e8b7ef5f1ab9bc0128d13734e282bcc604..b9f947b03e6439ac37abf530ce8150bb6e8f5a51 100644 (file)
@@ -3692,6 +3692,44 @@ static unsigned int count_explore_walked;
 static unsigned int count_indegree_walked;
 static unsigned int count_topo_walked;
 
+struct commit *revision_peek_next_commit (struct rev_info *revs)
+{
+       struct topo_walk_info *info = revs->topo_walk_info;
+
+       if (info)
+               return prio_queue_peek(&info->topo_queue);
+       if (revs->commits)
+               return revs->commits->item;
+
+       return NULL;
+}
+
+int revision_has_commits_after (struct rev_info *revs, int n)
+{
+       struct topo_walk_info *info = revs->topo_walk_info;
+
+       if (info) {
+               int visible = 0;
+               for (size_t i = 0; i < info->topo_queue.nr && visible < n; i++) {
+                       struct commit *c = info->topo_queue.array[i].data;
+                       if (get_commit_action(revs, c) == commit_show)
+                               visible++;
+               }
+               return visible > n-1;
+       }
+       if (revs->commits) {
+               struct commit_list *cl;
+               int visible = 0;
+               for (cl = revs->commits; cl && visible < n; cl = cl->next) {
+                       if (get_commit_action(revs, cl->item) == commit_show)
+                               visible++;
+               }
+               return visible > n-1;
+       }
+
+       return 0;
+}
+
 static void trace2_topo_walk_statistics_atexit(void)
 {
        struct json_writer jw = JSON_WRITER_INIT;
index c9a11827cc702e612f04d9a212efa5cd5a3ceb2c..fe297fdfa5fc589d5432dcb8c05c34a892960787 100644 (file)
@@ -560,4 +560,14 @@ int rewrite_parents(struct rev_info *revs,
  */
 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit);
 
+/*
+ * Peek into revision's next commit without consuming it.
+ */
+struct commit *revision_peek_next_commit(struct rev_info *revs);
+
+/*
+ * Check if there are n more commits to be shown yet.
+ */
+int revision_has_commits_after(struct rev_info *revs, int n);
+
 #endif