Git development
 help / color / mirror / Atom feed
From: Pablo Sabater <pabloosabaterr@gmail.com>
To: git@vger.kernel.org
Cc: pabloosabaterr@gmail.com, ayu.chandekar@gmail.com,
	chandrapratap3519@gmail.com, christian.couder@gmail.com,
	gitster@pobox.com, jltobler@gmail.com, karthik.188@gmail.com,
	krka@spotify.com, peff@peff.net, phillip.wood@dunelm.org.uk,
	siddharthasthana31@gmail.com
Subject: [PATCH v7 2/3] graph: add a 2 commit buffer for lookahead
Date: Sat,  4 Jul 2026 10:52:34 +0200	[thread overview]
Message-ID: <20260704-ps-pre-commit-indent-v7-2-a94706cc8376@gmail.com> (raw)
In-Reply-To: <20260704-ps-pre-commit-indent-v7-0-a94706cc8376@gmail.com>

In a subsequent commit the graph renderer needs to know if the next
commit is a visual root or if it is the last commit to be shown. This
requires peeking 2 commits ahead.

Commits are pre-fetched at get_revision_internal() where they are also
marked as SHOWN.

Update graph_is_interesting() so it considers commits inside the
lookahead as interesting as well.

Helped-by: Kristofer Karlsson <krka@spotify.com>
Signed-off-by: Pablo Sabater <pabloosabaterr@gmail.com>
---
 graph.c    | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 graph.h    | 17 +++++++++++++++++
 revision.c | 17 ++++++++++++++++-
 3 files changed, 80 insertions(+), 1 deletion(-)

diff --git a/graph.c b/graph.c
index 842282685f..300ae67669 100644
--- a/graph.c
+++ b/graph.c
@@ -315,6 +315,14 @@ struct git_graph {
 	 * diff_output_prefix_callback().
 	 */
 	struct strbuf prefix_buf;
+
+	/*
+	 * Lookahead buffer: up to 2 pre-fetched commits that will be shown.
+	 * Populated by get_revision() so graph_peek_next_visible() can use
+	 * actual walk results instead of peeking at rev_info internals.
+	 */
+	struct commit *lookahead[2];
+	int lookahead_nr;
 };
 
 static inline int graph_needs_truncation(struct git_graph *graph, int lane)
@@ -388,6 +396,9 @@ struct git_graph *graph_init(struct rev_info *opt)
 	graph->num_columns = 0;
 	graph->num_new_columns = 0;
 	graph->mapping_size = 0;
+	graph->lookahead[0] = NULL;
+	graph->lookahead[1] = NULL;
+	graph->lookahead_nr = 0;
 	/*
 	 * Start the column color at the maximum value, since we'll
 	 * always increment it for the first commit we output.
@@ -456,6 +467,15 @@ static void graph_ensure_capacity(struct git_graph *graph, int num_columns)
  */
 static int graph_is_interesting(struct git_graph *graph, struct commit *commit)
 {
+	/*
+	 * Commits in the lookahead buffer have been pre-fetched by
+	 * get_revision() and will be shown in the future. They already
+	 * have the SHOWN flag set by get_revision_internal(), but the
+	 * graph still needs to treat them as interesting parents.
+	 */
+	for (int i = 0; i < graph->lookahead_nr; i++)
+		if (graph->lookahead[i] == commit)
+			return 1;
 	/*
 	 * If revs->boundary is set, commits whose children have
 	 * been shown are always interesting, even if they have the
@@ -763,6 +783,33 @@ static int graph_needs_pre_commit_line(struct git_graph *graph)
 	       graph->expansion_row < graph_num_expansion_rows(graph);
 }
 
+struct commit *graph_pop_lookahead(struct git_graph *graph)
+{
+	struct commit *c;
+
+	if (!graph->lookahead_nr)
+		return NULL;
+
+	c = graph->lookahead[0];
+	graph->lookahead[0] = graph->lookahead[1];
+	graph->lookahead[1] = NULL;
+	graph->lookahead_nr--;
+	return c;
+}
+
+int graph_get_lookahead_room(struct git_graph *graph)
+{
+	return 2 - graph->lookahead_nr;
+}
+
+void graph_push_lookahead(struct git_graph *graph, struct commit *c)
+{
+	if (!graph_get_lookahead_room(graph))
+		BUG("pushing into lookahead buffer when it is already full");
+
+	graph->lookahead[graph->lookahead_nr++] = c;
+}
+
 void graph_update(struct git_graph *graph, struct commit *commit)
 {
 	struct commit_list *parent;
diff --git a/graph.h b/graph.h
index 3fd1dcb2e9..281603b020 100644
--- a/graph.h
+++ b/graph.h
@@ -262,4 +262,21 @@ void graph_show_commit_msg(struct git_graph *graph,
 			   FILE *file,
 			   struct strbuf const *sb);
 
+/*
+ * Pop the first commit from the graph's lookahead buffer.
+ * Returns NULL if the buffer is empty.
+ */
+struct commit *graph_pop_lookahead(struct git_graph *graph);
+
+/*
+ * Returns how many more commits can be added to the lookahead buffer.
+ */
+int graph_get_lookahead_room(struct git_graph *graph);
+
+/*
+ * Push a commit into the lookahead buffer. Must only be called when
+ * graph_lookahead_room() returns > 0.
+ */
+void graph_push_lookahead(struct git_graph *graph, struct commit *c);
+
 #endif /* GRAPH_H */
diff --git a/revision.c b/revision.c
index e91d7e1f11..58351aeeff 100644
--- a/revision.c
+++ b/revision.c
@@ -4699,12 +4699,27 @@ struct commit *get_revision(struct rev_info *revs)
 				for (p = c->parents; p; p = p->next)
 					p->item->object.flags |= CHILD_SHOWN;
 		}
+	} else if (revs->graph) {
+		c = graph_pop_lookahead(revs->graph);
+		if (!c)
+			c = get_revision_internal(revs);
+
 	} else {
 		c = get_revision_internal(revs);
 	}
 
-	if (c && revs->graph)
+	if (c && revs->graph) {
+		if (!revs->max_count_stage && !revs->reverse_output_stage) {
+			while (graph_get_lookahead_room(revs->graph)) {
+				struct commit *next = get_revision_internal(revs);
+				if (!next)
+					break;
+				graph_push_lookahead(revs->graph, next);
+			}
+		}
 		graph_update(revs->graph, c);
+	}
+
 	if (!c) {
 		free_saved_parents(revs);
 		commit_list_free(revs->previous_parents);

-- 
2.54.0

  parent reply	other threads:[~2026-07-04  8:54 UTC|newest]

Thread overview: 65+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-02 21:17 [GSoC RFC PATCH 0/1] graph: add indentation for commits preceded by a root Pablo Sabater
2026-04-02 21:17 ` [GSoC RFC PATCH 1/1] " Pablo Sabater
2026-04-03 17:55   ` Junio C Hamano
2026-04-03 18:07     ` Pablo
2026-04-03  5:04 ` [GSoC RFC PATCH 0/1] " Junio C Hamano
2026-04-03  8:25   ` Pablo
2026-04-04  9:24 ` [GSoC RFC PATCH v2 0/1] graph: add indentation for commits preceded by a parentless commit Pablo Sabater
2026-04-04  9:24   ` [GSoC RFC PATCH v2 1/1] " Pablo Sabater
2026-04-10 16:25   ` [GSoC RFC PATCH v2 0/1] " Pablo
2026-04-10 16:54     ` Junio C Hamano
2026-04-27 10:28   ` [GSoC PATCH v3 " Pablo Sabater
2026-04-27 10:28     ` [GSoC PATCH v3 1/1] " Pablo Sabater
2026-05-13 23:02       ` Jeff King
2026-05-14 10:19         ` Pablo Sabater
2026-04-27 10:35     ` [GSoC PATCH v3 0/1] " Pablo
2026-06-12 13:48     ` [PATCH v4 0/2] graph: indent visual roots in graph Pablo Sabater
2026-06-12 13:48       ` [PATCH v4 1/2] lib-log-graph: move check_graph function Pablo Sabater
2026-06-12 13:48       ` [PATCH v4 2/2] graph: indent visual root in graph Pablo Sabater
2026-06-13  3:01       ` [PATCH v4 0/2] graph: indent visual roots " Junio C Hamano
2026-06-13 19:09       ` [PATCH v5 " Pablo Sabater
2026-06-13 19:09         ` [PATCH v5 1/2] lib-log-graph: move check_graph function Pablo Sabater
2026-06-13 19:09         ` [PATCH v5 2/2] graph: indent visual root in graph Pablo Sabater
2026-06-14  4:05           ` Junio C Hamano
2026-06-14  5:28             ` Pablo Sabater
2026-06-15 15:42               ` Junio C Hamano
2026-06-16 13:06                 ` Pablo Sabater
2026-06-16 16:36                   ` Junio C Hamano
2026-06-17 20:27           ` Jeff King
2026-06-18 12:42             ` Pablo Sabater
2026-06-18 13:31               ` Junio C Hamano
2026-06-18 16:05               ` Jeff King
2026-06-18 16:07                 ` Jeff King
2026-06-19  7:34                 ` Kristofer Karlsson
2026-06-21 18:05                   ` Jeff King
2026-06-22  8:29                     ` Kristofer Karlsson
2026-06-17 10:33         ` [PATCH v5 0/2] graph: indent visual roots " Chandra Pratap
2026-06-20 10:11         ` [PATCH v6 0/3] " Pablo Sabater
2026-06-20 10:11           ` [PATCH v6 1/3] lib-log-graph: move check_graph function Pablo Sabater
2026-06-20 10:11           ` [PATCH v6 2/3] revision: add peek functions for lookahead Pablo Sabater
2026-06-20 10:18             ` Pablo Sabater
2026-06-20 14:56             ` Junio C Hamano
2026-06-20 15:15               ` Junio C Hamano
2026-06-21  1:48             ` Junio C Hamano
2026-06-21  6:42             ` Chandra Pratap
2026-06-22  8:28             ` Kristofer Karlsson
2026-06-29 21:56               ` Junio C Hamano
2026-06-29 23:29                 ` Pablo Sabater
2026-06-20 10:11           ` [PATCH v6 3/3] graph: indent visual root in graph Pablo Sabater
2026-07-04  8:52           ` [PATCH v7 0/3] graph: indent visual roots " Pablo Sabater
2026-07-04  8:52             ` [PATCH v7 1/3] lib-log-graph: move check_graph function Pablo Sabater
2026-07-04  8:52             ` Pablo Sabater [this message]
2026-07-06  9:49               ` [PATCH v7 2/3] graph: add a 2 commit buffer for lookahead Chandra Pratap
2026-07-06 13:44                 ` Kristofer Karlsson
2026-07-06 15:33                   ` Chandra Pratap
2026-07-04  8:52             ` [PATCH v7 3/3] graph: indent visual root in graph Pablo Sabater
2026-05-14 15:15 ` [GSoC RFC PATCH 0/1] graph: add indentation for commits preceded by a root Phillip Wood
2026-05-14 17:45   ` Pablo Sabater
2026-05-15  9:33     ` Phillip Wood
2026-05-17  6:31       ` Chandra Pratap
2026-05-18 13:26         ` Pablo Sabater
2026-05-19  0:03           ` Junio C Hamano
2026-05-19  5:59             ` Pablo Sabater
2026-06-10 15:21               ` Junio C Hamano
2026-06-10 15:28                 ` Pablo Sabater
2026-05-19 10:39           ` Chandra Pratap

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260704-ps-pre-commit-indent-v7-2-a94706cc8376@gmail.com \
    --to=pabloosabaterr@gmail.com \
    --cc=ayu.chandekar@gmail.com \
    --cc=chandrapratap3519@gmail.com \
    --cc=christian.couder@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=jltobler@gmail.com \
    --cc=karthik.188@gmail.com \
    --cc=krka@spotify.com \
    --cc=peff@peff.net \
    --cc=phillip.wood@dunelm.org.uk \
    --cc=siddharthasthana31@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox