Tag a file in Git

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Tag a file in Git



In my build process, as a first step, there is a security analysis which has to occur. Upon completion, I'd like to tag the file that has passed with some identifier so that when the build script runs it can simply grab the specified, tagged versions.



I can see how to tag a repo, not an individual file.




1 Answer
1



Background: In Git, a tag name can point to any object type. Typically, they will point to commit objects (the same way that branch names point to commit objects)—these are called lightweight tags—or to an annotated tag object, which is a repository object that in turn points to another repository object (typically a commit). A tag name that points to an annotated tag object is called an annotated tag.



Git does not have files, but Git does have blobs. So you can tag a blob object. You will not know what file name(s) it may have in any commit(s), if it even exists in any commit(s), but you will be able to extract the file's data:


git tag $name $hash



or:


git tag -a $name $hash



where $name and $hash stand for the tag name and the blob hash ID respectively.


$name


$hash



Tree objects mate file names (names without embedded slashes) to blob hash IDs (and also mode information), so if you need to store a <file name, hash ID> pair, you could create a tree object with a dummy mode (always 100644), use that to pair the name with the blob hash ID, and point a tag to that tree object. Tree objects are more difficult to create, though: you will need to create a temporary index using GIT_INDEX_FILE and git update-index. This method also allows you to store path names that include embedded slashes, by putting the final pair into a subtree of a higher level tree:


100644


GIT_INDEX_FILE


git update-index


GIT_INDEX_FILE=$(mktemp)
rm -f $GIT_INDEX_FILE
git update-index --cacheinfo 100644,$hash,$path || ... handle error ...
treehash=$(git write-tree) || ... handle error ...
git tag $annotated_opt $name $treehash || ... handle error ...
rm -f $GIT_INDEX_FILE



where $annotated_opt is either -a for an annotated tag, or empty for a lightweight tag.


$annotated_opt


-a



Note: to add some arbitrary content as a blob object and obtain its hash ID, use git hash-object -w:


git hash-object -w


blobhash=$(generate_data | git hash-object -w --stdin) || ... handle error ...



for instance.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS