Jacko
Jacko

Reputation: 13195

Git: want to iterate through all commits on branch, and list files in each commit

I need to write a script that will

  1. iterate through all of the commits on a branch, starting from the most recent
  2. for each commit, iterate through all of the files in the commit
  3. if it finds a file of type hbm.xml, store the commit to file and exit.

I have a script for step 2:

for i in `git show --pretty="format:" --name-only SHA1 | grep '.*\.hbm\.xml' `; do
    # call script here.....
    exit
done

Now, I need to figure out step 1.

Upvotes: 29

Views: 17643

Answers (2)

CB Bailey
CB Bailey

Reputation: 791421

Something like:

branch=master
for commit in $(git rev-list $branch)
do
    if git ls-tree --name-only -r $commit | grep -q '\.hbm\.xml$'; then
        echo $commit
        exit 0
    fi
done

Note that git show will only list files which have changed in that commit, if you want to know whether there is a path that matches a particular pattern in a commit you need to use something like git ls-tree.

Upvotes: 36

Michael Mrozek
Michael Mrozek

Reputation: 175325

git rev-list will list all revisions reachable from a given commit in reverse chronological order, so you can pass it a branch name to get the list for that branch head backwards:

$ git rev-list master                                                                                    
a6060477b9dca7021bc34f373360f75064a0b728
7146d679312ab5425fe531390c6bb389cd9c8910
53e3d0c1e1239d0e846b3947c4a6da585960e02d
a91b80b91e9890f522fe8e83feda32b8c6eab7b6
...

Upvotes: 15

Related Questions