scripts: git-resolve: ignore non-commit objects
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.
--- scripts/git-resolve.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
scripts/git-resolve.sh +3 -1
@@ -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
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
PATCH v2 1/1 scripts: git-resolve: ignore non-commit objects
In reply to Hamdy Abou El AneinFrom: 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.
--- scripts/git-resolve.sh | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
scripts/git-resolve.sh +6 -2
@@ -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
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