Skip to content

Instantly share code, notes, and snippets.

@bobjackman
Last active February 22, 2017 13:55
Show Gist options
  • Save bobjackman/7779863 to your computer and use it in GitHub Desktop.
Save bobjackman/7779863 to your computer and use it in GitHub Desktop.
Find all branches that have unmerged commits authored by me. This is my first attempt at a bash script, so if you have suggestions for better/clearer/faster code, please let me know!
#!/usr/bin/env bash
# usage:
# local branches only: `myUnmerged`
# remote branches only: `myUnmerged -r`
# all branches: `myUnmerged -a`
#
# example output:
#
# BRANCH TOTAL COMMITS KOGI KOGI UNMERGED
# feature/branchA 168 50 40
# feature/branchB 169 49 40
# feature/branchC 206 70 0
switch=$1;
user=`whoami`;
branches=`git branch $switch --no-merged develop | grep -v "\->" | sed 's/^*/ /' | awk '{print $1}' | grep -v -E "\/release|rc|tag\/" | grep -v -E "\bdevelop|master\b"`;
longest=$((`wc -L <<< "$branches"` + 3));
userCaps=$(echo $user | tr [a-z] [A-Z]);
branchHeader="BRANCH";
totalHeader="TOTAL";
userHeader=" COMMITS $userCaps";
unmergedHeader=" $userCaps UNMERGED";
printf "%-${longest}s %s %s %s\n" "$branchHeader" "$totalHeader" "$userHeader" "$unmergedHeader";
for branch in $branches; do
printf "Scanning branch $branch... ";
base=`git merge-base HEAD $branch`;
all=(`git log $base..$branch | grep "^commit" -w | awk '{print $2}'`);
mine=(`git log --author="$user" $base..$branch | grep "^commit" -w | awk '{print $2}'`);
myUnmerged=();
i=0;
for commit in ${mine[@]}; do
# printf "\r%f" $(($i*100/${#mine[@]}));
printf "\rScanning branch %s... %d%%" $branch $(($i*100/${#mine[@]}));
containedIn=`git branch $switch --contains=$commit`;
inRc=`echo $containedIn | grep -c -w "feature/rc"`;
inRelease=`echo $containedIn | grep -c -w "release"`;
inMaster=`echo $containedIn | grep -c -w "master"`;
if [ $inRc -lt 1 ] && [ $inRelease -lt 1 ] && [ $inMaster -lt 1 ]; then # not in rc, release or master
myUnmerged+=($commit);
fi;
i=$(($i+1));
done;
printf "\r\033[K";
if [ ${#myUnmerged[@]} -gt 0 ]; then
printf "%-${longest}s %${#totalHeader}s %${#userHeader}s %${#unmergedHeader}s\n" $branch ${#all[@]} ${#mine[@]} ${#myUnmerged[@]};
fi;
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment