Skip to content

Instantly share code, notes, and snippets.

@unarist
Last active September 11, 2018 11:53
Show Gist options
  • Save unarist/bd912e7c6024c9ade792 to your computer and use it in GitHub Desktop.
Save unarist/bd912e7c6024c9ade792 to your computer and use it in GitHub Desktop.
git-branch-stats.sh
#!/bin/bash
# git-branch-stats.sh (c)2014 unarist
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Compare branches with master and upstream.
# Requires Git >= 1.9.0
#
# Example output:
#
# feature/a merged =origin
# feature/b +5-2 commits >origin
# my_local_branch +1-0 commits =???
# master =origin
base=$1
test "$base" = "" && base='master'
git for-each-ref --format '%(refname:short) %(upstream:trackshort)%(upstream:short)' refs/heads | \
sed -re 's|(([^ ]+).*[^/]\w+)/\2$|\1|g' | \
while read line; do
line=($line); branch=${line[0]}; uptrack=${line[1]}
test "$uptrack" = "" && uptrack='=???'
if [ "$branch" = "$base" ]; then
brstat=''
elif git merge-base --is-ancestor $branch $base; then
brstat='merged'
else
# git diff --numstat master staging/master | awk '{plus+=$1;minus+=$2}END{print "+" plus "-" minus}'
revdiff=($(git rev-list --ancestry-path --left-right --count $base...$branch))
brstat="+${revdiff[1]}-${revdiff[0]} commits"
fi
echo " $branch $brstat $uptrack"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment