Chatumbabub
Chatumbabub

Reputation: 1637

How to get list of branches with number of commit Ahead from other branch

Is there a way to get list of branches with number of commit Ahead from other branch?

Consider this this branches:

master
feature/one
feature/two
feature/three

feature/* where created in same time from master. Than, in feature/one was created one new commit. In feature/two were created two new commits. In feature/three were created three new commits.

Than, feature/two was merged back to master.

I' looking for way to get this result: (number means how many commits is branch Ahead of master.

feature/two 0
feature/one 1
feature/three 3

Thanks

Upvotes: 2

Views: 222

Answers (2)

torek
torek

Reputation: 488243

The script in choroba's answer should work but there's a somewhat better way, also using scripting.

The first thing to realize is that there is no need to check out each branch. All we want is a count of commits that are "on" (contained within) the given branch that are not also on (contained within) master, and the master..$branch syntax will suffice to specify those.

Using git log --online piped to wc -l will work, but we can do this directly within Git using git rev-list --count.

Last, git branch is a so-called "porcelain" Git command, vs Git's "plumbing" commands: the plumbing commands are designed for scripting while the porcelain ones are not. Usually scripts work better with the plumbing commands. The way to get a list of branches with a plumbing command is slightly verbose:

git for-each-ref --format '%(refname:short)' refs/heads

Putting these together we get:

git for-each-ref --format '%(refname:short)' refs/head |
    while read branch; do
        printf "%s " $branch  # %s just in case $branch contains a %
        git rev-list --count master..$branch
    done

which is really basically the same thing, just using plumbing commands.

Upvotes: 2

choroba
choroba

Reputation: 241908

You can count the number of commits in the log:

#! /bin/bash
git branch \
| while read b ; do
    b=${b#\* }                          # Remove the current branch mark.
    git checkout "$b" &>/dev/null
    printf "$b "
    git log --oneline master..@ | wc -l
done

Upvotes: 1

Related Questions