16

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?

janesconference
  • 6,333
  • 8
  • 55
  • 73

3 Answers3

19

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).

Cascabel
  • 479,068
  • 72
  • 370
  • 318
  • Thanks, this does the trick. Why it isn't useful? I don't copy my .git directory in my remote server. What is copied are some selected html and javascript file I want to "publish" in my webserver. – janesconference Mar 03 '11 at 16:14
  • If the server doesn't have a repository, what good does a commit hash do you? And even the date seems iffy - so this file was changed three weeks ago. How? You rewrote the whole thing, or fixed a typo in a comment? Seems like in most practical cases, you'll immediately want to go back to the real repository and see what really changed. – Cascabel Mar 03 '11 at 16:54
  • I have this html/javascript demo. I publish it on my site to make people play with it, meanwhile I keep on developing on it. Since it's an early demo, I don't want to use tags; as I publish every two days, I would have a massive amount of tags, and it would get confusing. I just update the demo everytime I feel it's a bit stable. Everytime someone finds a bug or a problem, though, I want to know exactly what version are my "published" files, so I can compare them to my HEAD and see if it's a bug I already fixed or not. – janesconference Mar 03 '11 at 17:39
  • 1
    @janesconference: So all you really care about is the commit you published. That one hash is completely sufficient to show you what version of every file is there. `git checkout ` to get your whole working tree back to that state, or `git show :` to see the contents of one file. – Cascabel Mar 03 '11 at 17:43
  • 1
    @janesconference: Or, to compare, `git diff HEAD `. There's really no need to know the last time it changed. – Cascabel Mar 03 '11 at 18:24
  • mmh.. you're right. I was erroneously thinking that if I commit, say, file1, then I commit file2 two days later, and then push everything to the remote repository, the commit hash of the last commit alone (in which I committed file2) would refer only to the changes I made in file2 (and not in file1). I was wrong, wasn't I? – janesconference Mar 03 '11 at 18:52
  • @janesconference: Yeah, git isn't like cvs/svn with their revision numbers. A commit is the whole shebang: some metadata, and a reference to, essentially, a snapshot of the entire work tree. (There are internal details, but they're not important now.) – Cascabel Mar 03 '11 at 19:06
  • Thank you for the explanation. I come from svn and I still struggle on some basic git issues. – janesconference Mar 03 '11 at 19:51
7

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!

Community
  • 1
  • 1
Adam Spiers
  • 17,397
  • 5
  • 46
  • 65
0

This is faster and can be sorted by age:

find <dir> -exec git log -n 1 --pretty="%ai {}" "{}" \; | sort -r
Federico
  • 722
  • 12
  • 12
  • This does not answer the question as it does not produce any Git commit info. – henrebotha May 23 '19 at 13:57
  • And at no point is it showing the results of `git log`. Each line it prints looks like `2019-03-08 17:20:47 +0100 ./keymaps/henrebotha/keymap.c`, which doesn't contain a commit hash, a branch name, or anything else related to Git. – henrebotha Jun 01 '19 at 19:37