2

I'm trying to replicate Subversion's $Id: $ feature with git. I know that I can use .gitattributes to set the ident attribute, which will allow me to embed the blob ID in a source code comment. That's the basic requirement, and I'm covered.

But I'm struggling to work out how I can make the ID useful in a practical sense. git log and git blame require a filename, so I can't use the ID with them. git show just shows the blob contents, but doesn't give any link to the commit.

What I'd like is, given a blob ID, to get the commit that created that blob. (Ultimately, to get git log or git blame data for the file, or to be able to check out a revision that contained that file).

I appreciate that commit history in a distributed system like git is more complex than subversion, but if I can get anything as a starting point, that would be sufficient. All I really need is to be able to demonstrate that given the source code, I can track back into the version control history.

laser
  • 570
  • 4
  • 20
Paul Moore
  • 6,569
  • 6
  • 40
  • 47
  • https://stackoverflow.com/questions/39601215/finding-a-file-by-its-corresponding-blobs-hash-in-a-git-repository https://stackoverflow.com/questions/33211914/how-to-find-all-uses-of-a-blob-in-a-git-repo https://stackoverflow.com/questions/223678/which-commit-has-this-blob – Josh Lee Feb 14 '17 at 15:36

2 Answers2

0

What about git tag command? Check this link as starting point:

https://git-scm.com/book/en/v2/Git-Basics-Tagging

aicastell
  • 2,182
  • 2
  • 21
  • 33
0

In addition to the blob id, the gitattributes manual describes an export-subst filter, so you could turn that on and use $Format:%H$ to add the commit hash, or $Format:%d$ to include branch/tag names. You would have to release the files using git archive.

For example:

$ cat .gitattributes
* export-subst ident
$ cat foo.c
// Blob hash: $Id$
// Commit hash: $Format:%H%d$
$ git archive master | tar -xO
* export-subst ident
// Blob hash: $Id: 9e0569a55a4eaacdf8d100a2c3d3654cf767650b $
// Commit hash: 3802b7884faf182ce0994ac9d94925dad375be05 (HEAD -> master, tag: v2)
Josh Lee
  • 171,072
  • 38
  • 269
  • 275