I’m in a position where I’d like, when I export a subdirectory of my git repository, to have the full hash of the commit being archived inserted into a given file. The reason for this is that I ship this code (a subdirectory of a repository too large to conveniently regularly export) to my customer, who then builds it (outside of the git environment), and we want to be able to tell from the logs which version of the code generated it, to aid debugging.
The solution appears as if it should be to add the file to .gitattributes with the export-subst attribute, and have the %H token replaced with the has when git archive is run. I’m not seeing this behaviour, however.
I’ve been using the a page on the Pro Git book as a reference (see here), but trying to adapt it for use with a subdirectory rather than the root of the repository.
The directory I’m trying to export is called iDevice. I’ve created a file under that folder called hash_test, which contains the text Last commit date: $Format:%cd$.
When I run git archive --format=tar HEAD:iDevice/ | gzip > test.tar.gz, the file is not modified. When I run git archive --format=tar HEAD | gzip > test.tar.gz the file is modified.
I have tried keeping the .gitattributes file in the root of my git directory, and in both the root and subdirectories. I’ve tried referencing the file as both iDevice/hash_test and hash_test in the .gitattributes file (as I thought it may be something similar to a bug I saw listed on kerneltrap.org where using –prefix= confused the archive command – the URL ends /mailarchive/git/2008/4/8/1378004 which I’d link to properly but I don’t have the reputation to post more links…). None of this has helped, however.
Any suggestions would be appreciated!
Attributes for
git-archiveare taken from the tree which is being archived. This means that:For
git archive ... HEAD:iDevicethe attributes must be in iDevice, or they can’t possibly be seen. (I assume this is supported; I’ve never tried it.)You must have committed your gitattributes file. Whatever’s in the worktree is irrelevant, unless you use the
--worktree-attributesoption togit-archive.And a suggestion which may bypass all of this: use
git archive ... HEAD iDevice, notgit archive HEAD:iDevice. The command supports<tree-ish> <path>arguments; no need to specify a tree object directly.