From: Simon Horman <horms@kernel.org>
To: Sasha Levin <sashal@kernel.org>
Cc: workflows@vger.kernel.org, apw@canonical.com, conor@kernel.org,
corbet@lwn.net, dwaipayanray1@gmail.com, geert+renesas@glider.be,
gitster@pobox.com, joe@perches.com, kees@kernel.org,
linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org,
linux@leemhuis.info, lukas.bulwahn@gmail.com,
miguel.ojeda.sandonis@gmail.com, niklas.soderlund@corigine.com,
torvalds@linux-foundation.org, willy@infradead.org
Subject: Re: [PATCH] scripts: Add git-resolve tool for full SHA-1 resolution
Date: Sun, 16 Mar 2025 13:24:17 +0000 [thread overview]
Message-ID: <20250316132417.GC4159220@kernel.org> (raw)
In-Reply-To: <20250311165336.248120-1-sashal@kernel.org>
On Tue, Mar 11, 2025 at 12:53:36PM -0400, Sasha Levin wrote:
> Introduce git-resolve.sh, a tool that resolves short git commit IDs to their
> full SHA-1 hash. This is particularly useful for navigating references in commit
> messages and verifying Fixes tags.
>
> When faced with ambiguous commit IDs or imprecise references in messages,
> this tool can help by resolving commit hashes based on not just the ID
> itself but also the commit subject, making it more robust than standard
> git rev-parse.
>
> This is especially valuable for maintainers who need to verify Fixes tags
> or cross-reference commits. Unlike proposals to add dates to Fixes tags
> (which would break existing tooling), this script provides a way to
> disambiguate commits without changing the established tag format.
>
> The script includes several features:
> - Resolves short commit IDs to full SHA-1 hashes
> - Uses commit subjects to disambiguate between multiple potential matches
> - Supports wildcard patterns in subjects with ellipsis (...)
> - Provides a force mode to attempt resolution by subject when ID lookup fails
> - Includes comprehensive self-tests
>
> Signed-off-by: Sasha Levin <sashal@kernel.org>
> ---
> scripts/git-resolve.sh | 199 +++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 199 insertions(+)
> create mode 100755 scripts/git-resolve.sh
>
> diff --git a/scripts/git-resolve.sh b/scripts/git-resolve.sh
...
> +git_resolve_commit() {
> + local force=0
> + if [ "$1" = "--force" ]; then
> + force=1
> + shift
> + fi
> +
> + # Split input into commit ID and subject
> + local input="$*"
> + local commit_id="${input%% *}"
> + local subject=""
> +
> + # Extract subject if present (everything after the first space)
> + if [[ "$input" == *" "* ]]; then
> + subject="${input#* }"
> + # Strip the ("...") quotes if present
> + subject="${subject#*(\"}"
> + subject="${subject%\")*}"
> + fi
> +
> + # Get all possible matching commit IDs
> + local matches
> + readarray -t matches < <(git rev-parse --disambiguate="$commit_id" 2>/dev/null)
> +
> + # Return immediately if we have exactly one match
> + if [ ${#matches[@]} -eq 1 ]; then
> + echo "${matches[0]}"
> + return 0
> + fi
> +
> + # If no matches and not in force mode, return failure
> + if [ ${#matches[@]} -eq 0 ] && [ $force -eq 0 ]; then
> + return 1
> + fi
> +
> + # If we have a subject, try to find a match with that subject
> + if [ -n "$subject" ]; then
> + # Convert subject with possible ellipsis to grep pattern
> + local grep_pattern
> + grep_pattern=$(convert_to_grep_pattern "$subject")
> +
> + # In force mode with no ID matches, use git log --grep directly
> + if [ ${#matches[@]} -eq 0 ] && [ $force -eq 1 ]; then
> + # Use git log to search, but filter to ensure subject matches exactly
> + local match
I suppose it doesn't matter in practice, but it seems somewhat inconsistent
to declare match as local here...
> + match=$(git log --format="%H %s" --grep="$grep_pattern" --perl-regexp -10 | \
> + while read -r hash subject; do
> + if echo "$subject" | grep -qP "$grep_pattern"; then
> + echo "$hash"
> + break
> + fi
> + done)
> + if [ -n "$match" ]; then
> + echo "$match"
> + return 0
> + fi
> + else
> + # Normal subject matching for existing matches
... but not here.
> + for match in "${matches[@]}"; do
> + if git log -1 --format="%s" "$match" | grep -qP "$grep_pattern"; then
> + echo "$match"
> + return 0
> + fi
> + done
> + fi
> + fi
> +
> + # No match found
> + return 1
> +}
...
prev parent reply other threads:[~2025-03-16 13:24 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-11 16:53 [PATCH] scripts: Add git-resolve tool for full SHA-1 resolution Sasha Levin
2025-03-11 22:48 ` Kees Cook
2025-03-16 13:24 ` Simon Horman [this message]
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=20250316132417.GC4159220@kernel.org \
--to=horms@kernel.org \
--cc=apw@canonical.com \
--cc=conor@kernel.org \
--cc=corbet@lwn.net \
--cc=dwaipayanray1@gmail.com \
--cc=geert+renesas@glider.be \
--cc=gitster@pobox.com \
--cc=joe@perches.com \
--cc=kees@kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@leemhuis.info \
--cc=lukas.bulwahn@gmail.com \
--cc=miguel.ojeda.sandonis@gmail.com \
--cc=niklas.soderlund@corigine.com \
--cc=sashal@kernel.org \
--cc=torvalds@linux-foundation.org \
--cc=willy@infradead.org \
--cc=workflows@vger.kernel.org \
/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;
as well as URLs for NNTP newsgroup(s).