donquixote
donquixote

Reputation: 5471

Compute git hash of file or directory outside of git repository

Question

Is it possible to compute a git hash of a file or directory outside of and independent of any git repository?

Motivation

I want to use this method to identify differences in generated artifacts (e.g. css generated from sass).

The benefit of doing this with git would be that the hashes can be compared against existing file hashes in a git history, to see if they look familiar.

Background

In How to compute the git hash-object of a directory? we learn how to compute the git hash of a directory. This method only works if the directory is within a git repository.

git ls-files -s somedirectory | git hash-object --stdin

From my understanding of git, the git hash of a file or directory depends only on the file or directory contents, perhaps file perms, but not on anything in the repository.

Known methods

Yes we could temporarily create a repo, but why that extra step?

Upvotes: 3

Views: 966

Answers (1)

torek
torek

Reputation: 487755

Files are easy; directories are hard. Read the Python code for directories; but files are just the checksum (SHA-1 for now, SHA-256 in the future) of the file's contents preceded by a blob header that includes, ASCII-fied in decimal, the size of the blob, plus a byte to separate the header from the data. That is, for a twelve-byte file, we have blob 12\0hello world\n as the input to sha1sum or whatever your local command or method of computing an SHA-1 checksum may be.

(You can also simply use git hash-object, for plain files. Directories remain hard.)

Upvotes: 3

Related Questions