Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How print last commit info for every file in a git repository

Tags:

git

bash

I have a script that copies some files from a git repository of mine on a remote server. For every file that is copied, if it is under version control, I want to generate a line, like:

Filename: <filename>, commit: <last-commit-hash>, date: <date of last commit>

The idea is to store these lines in a file and copy it as well on the remote server. This way I can always know which file on the server belongs to which commit on my git repository. Is there a quick way to do that?

like image 600
janesconference Avatar asked Mar 03 '11 15:03

janesconference


2 Answers

I'm dubious about how useful this will be, since you can always get the information from a local repository, or through gitweb, but here you are:

git ls-files | while read file; do git log -n 1 --pretty="Filename: $file, commit: %h, date: %ad" -- $file; done

The %h gives you an abbreviated hash; if you want the full one, use %H. You can also fiddle with the format of the date using --date=local|iso|rfc|short (see the git-log manpage).

like image 100
Cascabel Avatar answered Oct 14 '22 16:10

Cascabel


I had a chat about this on #git with a few guys, and one of them (thanks Mikachu) found this Perl script which had the right algorithm but some serious implementation flaws.

So I fixed up the issues with that script, tidied it up a lot, and here is the result (download from here). Note that it currently requires Term::ANSIColor to run. here you can see a screenshot of it in action:

screenshot of git-ls-dir runs
(source: adamspiers.org)

Hope that helps!

like image 39
Adam Spiers Avatar answered Oct 14 '22 17:10

Adam Spiers