So how can I ask Git if it treats a file as text or binary?
Not only git check-attr --all is a good option, but with Git 2.40 (Q1 2023), "git check-attr"(man) learned to take an optional tree-ish to read the .gitattributes file from.
That means you can Git if it treats a file as text or binary, for any commit, not just the current HEAD!
git check-attr --all --source=@~2 -- myFile
git check-attr --all --source=anotherBranch -- myFile
See commit 47cfc9b, commit c847e8c (14 Jan 2023) by Karthik Nayak (KarthikNayak).
(Merged by Junio C Hamano -- gitster -- in commit 577bff3, 23 Jan 2023)
attr: add flag --source to work with tree-ish
Signed-off-by: Karthik Nayak
Signed-off-by: Toon Claes
Co-authored-by: toon@iotcl.com
The contents of the .gitattributes files may evolve over time, but "git check-attr"(man) always checks attributes against them in the working tree and/or in the index.
It may be beneficial to optionally allow the users to check attributes taken from a commit other than HEAD against paths.
Add a new flag --source which will allow users to check the attributes against a commit (actually any tree-ish would do).
When the user uses this flag, we go through the stack of .gitattributes files but instead of checking the current working tree and/or in the index, we check the blobs from the provided tree-ish object.
This allows the command to also be used in bare repositories.
Since we use a tree-ish object, the user can pass "--source HEAD:subdirectory" and all the attributes will be looked up as if subdirectory was the root directory of the repository.
We cannot simply use the <rev>:<path> syntax without the --source flag, similar to how it is used in git show(man) because any non-flag parameter before -- is treated as an attribute and any parameter after -- is treated as a pathname.
The change involves creating a new function read_attr_from_blob, which given the path reads the blob for the path against the provided source and parses the attributes line by line.
This function is plugged into read_attr() function wherein we go through the stack of attributes files.
git check-attr now includes in its man page:
'git check-attr' [--source <tree-ish>] [-a | --all | <attr>...] [--] <pathname>...
'git check-attr' --stdin [-z] [--source <tree-ish>] [-a | --all | <attr>...]
git check-attr now includes in its man page:
--source=<tree-ish>
Check attributes against the specified tree-ish.
It is common to
specify the source tree by naming a commit, branch or tag associated
with it.