gitgit-rev-list

Getting all commits from HEAD up to a different author


In my case, I want to be able to get a list of commit hashes of all commits from HEAD where I am the author, i.e. retuning all commits until there is a parent commit where I am not the author. e.g. in the following commit list

HEAD   - abc - Me
HEAD~1 - efg - Me
HEAD~2 - hij - Me
HEAD~3 - klm - Someone else
HEAD~4 - nmo - Me

I only want abc, efg and hij to be returned. I am able to assume HEAD is authored by me in my use case.

I tried looking into git rev-parse, but it can only get all or a certain number of commits where I am the author, not all sequential commits until there is a commit where I am not the author.


Solution

  • There is no a simple way to stop git rev-list when an author changes. So the solution is to get author from every commit one be one until the author changes:

    #! /bin/sh
    
    author_email=`git show --format="%ae" -s HEAD`
    
    git rev-list HEAD |
        while read commit_ID; do
           _email=`git show --format="%ae" -s $commit_ID`
            if [ "$_email" = "$author_email" ]; then
                echo $commit_ID
            else
                exit
            fi
        done
    

    PS. Running git show on an every commit for hundreds of commits would be slow. But for a few dozens it should be fine.