kayahr
kayahr

Reputation: 22050

How do I list a directory in a Git repository together with the latest commit info for each directory entry?

I want to list a directory from a Git repository together with the latest commit information of each directory entry. Similiar to how GitHub displays directories or how viewvc displays a directory in an SVN/CVS repository.

Currently I do it like this:

  1. Get the directory entries with git ls-tree master and parse the directory structure from the output.

  2. Then for each directory entry I do this: git log -n 1 master -- filename and parse the commit information from it (I specify a special format string to make this easier, but this isn't relevant to my problem).

It's pretty obvious that this is very slow, because I have to call Git for each file. Is there a faster way to do this? Maybe a single command I can execute to get all the data I need at once?

Upvotes: 12

Views: 6497

Answers (3)

Vi.
Vi.

Reputation: 38734

Oneliner to parse git whatchanged output and print tree annotated with recent commit ids:

git whatchanged -r --pretty=oneline | perl -ne 'our %q; if (/^([0-9a-f]{40})/) {$c = $1} if (/:.{38}(.*)/) { $q{$1} = $c unless exists $q{$1} }; END { print map {"$q{$_} $_\n"} (keys %q); }'  | sort -k 2

Example output:

b32f7f65f19e547712388d2b22ea8d5355702ce2 code/unfreezemodule/file.c
b32f7f65f19e547712388d2b22ea8d5355702ce2 code/unfreezemodule/Makefile
b32f7f65f19e547712388d2b22ea8d5355702ce2 code/unfreezemodule/unfreezemodule.c
efeda0a22e4d1d5f3d755fee5b72fa99c0046e38 code/unfreezer/.gitignore
b822361ffbc4548d918df5381ce94c296459598a code/unfreezer/unfreezer.c
18ed76836182eba702e89b929eff2c0ea78ffb3e code/whole/wh
1e111ce4e8c0813b397f34e3634309f7594cb864 code/xbmctest/.gitignore
cb67943c4c18e3461267a34935fc8efaca5e2166 .config/awesome/awesome.lua
d5e2b7c1d24bed1d0b53338f43747dc856c9cfe4 .config/awesome/rc.lua
13ca8dbcd7f1ec684a7eba494442ee743ed577c0 cr
566b54fb72e32947fcbf7d5c58fed78b5abe976d d
392ac1a2456e8a29b2ebb7af9499c3f69d24a559 Desktop/CityInfo.desktop
392ac1a2456e8a29b2ebb7af9499c3f69d24a559 Desktop/.directory

You can adjust git whatchanged parameters and/or filter the output as you want.

Upvotes: 4

Seth Robertson
Seth Robertson

Reputation: 31461

You can request git log -n 1 <file|directoryname>. Thus you can parse git-ls-tree in a non-recursive mode and only need to run it for every file/directory at the level you are at.

Upvotes: 0

Amber
Amber

Reputation: 527043

You could perhaps parse git whatchanged master, continuing to go backwards in history until you've "seen" all file paths?

Depending on how long your history is and how old some files are, this might be faster.

Upvotes: 0

Related Questions