2

Is there a way to determine the new git sha of a file (or ideally of a tree), BEFORE committing the changes?

Background:

I'm trying to automatically change a string in one file in my repo to a unique value, every time any files in another directory of the repo are changed.

I was thinking that an easy way to do this would be to write a git hook that just uses the sha of the subdirectory tree as the unique string. This seems like it would be straightforward to do as a post-commit hook with git ls-tree, but that would require an extra dummy commit to commit the string each time, generating extra noise.

So far I've determined that this might be possible by running git hash-object, but I'd have to write a mildly complex script to recursively build up the hashes of the files in the directory in order to eventually compute the hash of the directory (tree). I'm wondering if there is a simpler option.

2
  • 1
    Can you tell us why you're trying to do this? That might help us solve the problem. What you're trying to do sounds like the wrong thing. Commented Apr 20, 2012 at 18:18
  • Basically those files contain some data that needs to get uploaded to our database when we deploy our code (if any of the data has changed). But in order to upload it atomically, the data is versioned using a unique identifier (which the rest of the code needs to know about -- which is why it's stored in another file). I thought it would be convenient to automatically generate the unique identifier based on the contents of the data files, and a git sha seemed to fit that bill nicely. Commented Apr 20, 2012 at 20:27

2 Answers 2

3

This doesn't exactly cover your answer, but here's a method for computing what the index of a tree will be in a commit, without actually creating a new commit first.

if you run git write-tree --prefix <subdirectory>/, it will write out the tree, as it is in the index, to disk, and return the hash. This is exactly the object that would be created anyway when the commit is performed.

Sign up to request clarification or add additional context in comments.

Comments

0

See my comment above...but if you want to get some sort of unique identifier for a directory tree, you could do something like this:

find directory -type f -print0 | 
  xargs -0 git hash-object | 
  git hash-object --stdin

This will compute a hash for every file in your directory...and then compute the hash of the list of hashes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.