tags:

views:

77

answers:

3

Hello everyone,

I'm trying to do a fancy stuff here with Git hooks, but I don't really know how to do it (or if it's possible).

What I need to do is: in every commit I want to take its hash and then update a file in the commit with this hash.

Any ideias?

+2  A: 

This can be achieved by using the filter attribute in gitattributes. You'd need to provide a smudge command that inserts the commit id, and a clean command that removes it, such that the file it's inserted in wouldn't change just because of the commit id.

Thus, the commit id is never stored in the blob of the file; it's just expanded in your working copy. (Actually inserting the commit id into the blob would become an infinitely recursive task. ☺) Anyone who clones this tree would need to set up the attributes for herself.

legoscia
Hummm that sounds promising! Thanks a lot everyone! I'll try some stuff here!
Felipe Kamakura
**Impossible** task, not recursive task. Commit hash depends on tree hash which depends on file hash, which depends on file contents. You have to get self-consistency. Unless you will find a kind of *[generalized] fixed point* for SHA-1 hash.
Jakub Narębski
+2  A: 

I don't think you actually want to do that, because when a file in the commit is changed, the hash of the commit is also changed.

midtiby
+3  A: 

I would recommend doing something similar to what you have in mind: placing the SHA1 in an untracked file, generated as part of the build/installation/deployment process. It's obviously easy to do (git rev-parse HEAD > filename or perhaps git describe [--tags] > filename), and it avoids doing anything crazy like ending up with a file that's different from what git's tracking.

Your code can then reference this file when it needs the version number, or a build process could incorporate the information into the final product. The latter is actually how git itself gets its version numbers - the build process grabs the version number out of the repo, then builds it into the executable.

Jefromi
Good point. This is a cleaner solution. Thanks.
Felipe Kamakura