29

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.

Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
Jacko
  • 12,665
  • 18
  • 75
  • 126

2 Answers2

35

Something like:

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.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
15

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
...
Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175