Git development
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Pablo Sabater <pabloosabaterr@gmail.com>
Cc: git@vger.kernel.org,  krka@spotify.com,  ayu.chandekar@gmail.com,
	chandrapratap3519@gmail.com,  christian.couder@gmail.com,
	jltobler@gmail.com,  karthik.188@gmail.com,  peff@peff.net,
	phillip.wood@dunelm.org.uk,  siddharthasthana31@gmail.com,
	 Kristofer Karlsson <stoansen@gmail.com>
Subject: Re: [PATCH v6 2/3] revision: add peek functions for lookahead
Date: Sat, 20 Jun 2026 07:56:42 -0700	[thread overview]
Message-ID: <xmqqzf0pfefp.fsf@gitster.g> (raw)
In-Reply-To: <20260620-ps-pre-commit-indent-v6-2-cdc6d8fd5fbc@gmail.com> (Pablo Sabater's message of "Sat, 20 Jun 2026 12:11:51 +0200")

Pablo Sabater <pabloosabaterr@gmail.com> writes:

> 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>
> ---
>  revision.c | 38 ++++++++++++++++++++++++++++++++++++++
>  revision.h | 10 ++++++++++
>  2 files changed, 48 insertions(+)
>
> diff --git a/revision.c b/revision.c
> index e91d7e1f11..a472a28853 100644
> --- a/revision.c
> +++ b/revision.c
> @@ -3708,6 +3708,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;
> +}

OK.  "If we are doing topo_walk, topo_queue is the priority queue to
peek into, otherwise revs->commits list is being used" is a bit too
intimate implementation detail I am not comfortable to depend on,
but as long as it is contained inside revision.c it should be OK.

Lose the space between the function name and its (parameter list)
from this and the next function.

> +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;
> +}

Regarding the use of get_commit_action() here, I wondered if this is
safe, because usually get_commit_action() is called only once per
commit during history traversal from simplify_commit(), but this
patch adds calls to it for all of the remaining commits being
processed without consuming them (so get_commit_action() will be
called on these commits again later as the history traversal
progresses).

If get_commit_action() a pure function without any side effects,
this may be safe, but line-log has something with side effect in the
function.

I _think_ this is OK, as "--graph" sets .rewrite_parents bit (as
well as .topo_order bit) on, which makes want_ancestry() to return
true.  Which in turn means even if -L is in effect, we will not call
line_log_process_ranges_arbitrary_commit() that is the only source
of side effect in this function.  Somebody needs to sanity check
this, but we may want to leave an in-code comment to warn future
developers not to call get_commit_action() on random commits outside
of the normal history traversal under what condition (namely, -L
without rewrite_parents).

Even better, perhaps add

	if (revs->line_level_traverse && !want_ancestry(revs))
		BUG("do not call this");

at the beginning of revision_has_commits_after() function, and
describe why in the header file comment for this function below?

> diff --git a/revision.h b/revision.h
> index 00c392be37..a10c6b0940 100644
> --- a/revision.h
> +++ b/revision.h
> @@ -572,4 +572,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

  parent reply	other threads:[~2026-06-20 14:56 UTC|newest]

Thread overview: 53+ 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-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 [this message]
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-20 10:11           ` [PATCH v6 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=xmqqzf0pfefp.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=ayu.chandekar@gmail.com \
    --cc=chandrapratap3519@gmail.com \
    --cc=christian.couder@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=jltobler@gmail.com \
    --cc=karthik.188@gmail.com \
    --cc=krka@spotify.com \
    --cc=pabloosabaterr@gmail.com \
    --cc=peff@peff.net \
    --cc=phillip.wood@dunelm.org.uk \
    --cc=siddharthasthana31@gmail.com \
    --cc=stoansen@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