Skip to contentSkip to conversation

scripts: git-resolve: ignore non-commit objects

PATCH Superseded Hamdy Abou El Anein 2 participants 4 messages Nov 3 – Nov 4

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scripts: git-resolve: ignore non-commit objects

From: Hamdy Abou El Anein <[email protected]>

git rev-parse --disambiguate lists objects of every type. A unique blob or tree prefix is therefore returned successfully as a commit ID. Such an ID cannot be used as a commit reference or Fixes tag.

Filter the candidates by object type before deciding whether the prefix is unique or looking up its subject.

Tested in a temporary Git repository: a real commit prefix resolves to its full ID, while a stored blob prefix is rejected with no output.

AI assistance: ChatGPT/Codex identified the issue and prepared the change, commit message and executable regression tests.

Fixes 7ae52a3d7f51 ("scripts: Add git-resolve tool for full SHA-1 resolution")
---
 scripts/git-resolve.sh | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
scripts/git-resolve.sh +3 -1
index e9b5940c0f2..0be8838cd13 100755
@@ -49,7 +49,9 @@ git_resolve_commit() {
 
 	# Get all possible matching commit IDs
 	local matches
-	readarray -t matches < <(git rev-parse --disambiguate="$commit_id" 2>/dev/null)
+	readarray -t matches < <(git rev-parse --disambiguate="$commit_id" 2>/dev/null |
+		git cat-file --batch-check='%(objectname) %(objecttype)' |
+		awk '$2 == "commit" { print $1 }')
 
 	# Return immediately if we have exactly one match
 	if [ ${#matches[@]} -eq 1 ]; then
Acked-by on v1 1/1
#1 lore raw
Quoted 6 lines · Hamdy Abou El Anein

git rev-parse --disambiguate lists objects of every type. A unique blob or tree prefix is therefore returned successfully as a commit ID. Such an ID cannot be used as a commit reference or Fixes tag.

Filter the candidates by object type before deciding whether the prefix is unique or looking up its subject.

This also fixes a more common case: an abbreviated ID that collides only with trees or blobs used to fail unless a subject was given. 117 of the last 2000 commits in linux.git have a 7 character abbreviation like that.

The argument check at the bottom of the script still counts every object type, so a prefix that matches only a blob now exits 1 without printing the "does not match any git commit" error. Could you send a follow-up that filters that check the same way?

Signature · 2 lines
Thanks,
Sasha
#2 lore raw

PATCH v2 1/1 scripts: git-resolve: ignore non-commit objects

In reply to Hamdy Abou El Anein

From: Hamdy Abou El Anein <[email protected]>

git rev-parse --disambiguate lists objects of every type. A unique blob or tree prefix is therefore returned successfully as a commit ID. Such an ID cannot be used as a commit reference or Fixes tag.

Filter the candidates by object type before deciding whether the prefix is unique or looking up its subject.

Tested in a temporary Git repository: a real commit prefix resolves to its full ID, while blob and tree prefixes produce the command-line error.

AI assistance: ChatGPT/Codex identified the issue and prepared the change, commit message and executable regression tests.

Fixes 7ae52a3d7f51 ("scripts: Add git-resolve tool for full SHA-1 resolution")
---
 scripts/git-resolve.sh | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
scripts/git-resolve.sh +6 -2
index e9b5940c0f28..7d14cf279e86 100755
@@ -49,7 +49,9 @@ git_resolve_commit() {
 
 	# Get all possible matching commit IDs
 	local matches
-	readarray -t matches < <(git rev-parse --disambiguate="$commit_id" 2>/dev/null)
+	readarray -t matches < <(git rev-parse --disambiguate="$commit_id" 2>/dev/null |
+		git cat-file --batch-check='%(objectname) %(objecttype)' |
+		awk '$2 == "commit" { print $1 }')
 
 	# Return immediately if we have exactly one match
 	if [ ${#matches[@]} -eq 1 ]; then
@@ -191,7 +193,9 @@ fi
 # Skip validation in force mode
 if [ -z "$force" ]; then
 	# Validate that the first argument matches at least one git commit
-	if [ "$(git rev-parse --disambiguate="$1" 2>/dev/null | wc -l)" -eq 0 ]; then
+	if [ "$(git rev-parse --disambiguate="$1" 2>/dev/null |
+		git cat-file --batch-check='%(objectname) %(objecttype)' |
+		awk '$2 == "commit" { count++ } END { print count + 0 }')" -eq 0 ]; then
 		echo "Error: '$1' does not match any git commit"
 		exit 1
 	fi
Signature · 1 line
2.56.0
Acked-by on v2 1/1
#3 lore raw
Quoted 6 lines · Hamdy Abou El Anein

git rev-parse --disambiguate lists objects of every type. A unique blob or tree prefix is therefore returned successfully as a commit ID. Such an ID cannot be used as a commit reference or Fixes tag.

Filter the candidates by object type before deciding whether the prefix is unique or looking up its subject.

Thanks for folding the argument check in.

--
Thanks,
Sasha