Add support for submodules.
[git-scripts.git] / git-branch-status
blob09107c5149b15b92e54c6c7adbb77445b98f316c
1 #! /bin/bash
3 # This script uses git branch and rev-list commands to output changes between
4 # branches.
6 # $ git rev-list master..topic
7 # list all changes made in topic not yet in master
8 # This is used to list changes not yet merged in master
10 # $ git rev-list topic..master
11 # list all changes made on master not in topic
12 # This is used to check if a rebase is needed for a clean fast-forward
14 # The master branch is master by default
15 MASTER=
16 ORIGIN=
17 REBASE=0
18 VERBOSE=0
19 LIST=0
20 COLOR=1 # Colorize by default
21 SHOW_MERGE_BASE_DATE=0
23 function usage() {
24 echo "$0 [-r] [-v] [-l] [--help] [<origin>] [<branchname>]"
25 echo ""
26 echo "This command will compare all branches on a git repository against"
27 echo "the branch given in option (default is master). By default it just"
28 echo "output the number of commits not yet merged on <branchname>."
29 echo ""
30 echo " -r : also output the number of commits done"
31 echo " on <branchname> since last merge."
32 echo " -l : list commits to be merged."
33 echo " -sd : show split date (merge base)"
34 echo " --no-color : disable color"
35 echo " -v : verbose mode"
36 exit 1
39 function set_colors() {
40 ESC_SEQ="\x1b["
41 COL_RESET=$ESC_SEQ"39;49;00m"
42 COL_RED=$ESC_SEQ"31;01m"
43 COL_GREEN=$ESC_SEQ"32;01m"
44 COL_YELLOW=$ESC_SEQ"33;01m"
45 COL_BROWN=$ESC_SEQ"33;02m"
46 COL_BLUE=$ESC_SEQ"34;01m"
47 COL_MAGENTA=$ESC_SEQ"35;01m"
48 COL_CYAN=$ESC_SEQ"36;01m"
51 if [[ -z $(git-symbolic-ref HEAD 2>/dev/null) ]]; then
52 echo Must be run inside a Git repository
53 exit 1
54 fi;
56 while :
58 case "$1" in
59 -r)
60 REBASE=1
62 -v)
63 VERBOSE=1
65 -l)
66 LIST=1
68 -sd)
69 SHOW_MERGE_BASE_DATE=1
71 -h|--h|--he|--hel|--help)
72 usage
74 --no-color)
75 COLOR=
77 "") break
80 if [ "$MASTER" = "" ]; then
81 MASTER=$1
82 else
83 if [ "$ORIGIN" = "" ]; then
84 ORIGIN=$MASTER
85 MASTER=$1
89 esac
90 shift
91 done
93 if [ "$MASTER" = "" ]; then
94 MASTER=master
97 if [ "$COLOR" = "1" ]; then
98 set_colors;
101 # Check if $MASTER branch exists
103 if [[ -z $(git show-ref refs/heads/master) ]]; then
104 # No $MASTER branch. Use current branch
106 if [[ "$MASTER" != "master" ]]; then
107 # Do not warn if MASTER was automatically set
108 echo Warning. $MASTER branch does not exist
111 MASTER=$(git-symbolic-ref HEAD)
114 echo ""
115 echo Check all branches against $MASTER
117 function check_branch (){
118 left=0
119 right=0
120 branch=$1
122 if [ "$SHOW_MERGE_BASE_DATE" = "1" ]; then
123 refpoint=$(git merge-base $branch $MASTER)
124 else
125 refpoint=$(git rev-parse $branch)
128 BOUTPUT=$COL_BROWN${branch}$COL_BLUE
129 BOUTPUT=$BOUTPUT" "$(git log $refpoint -1 --pretty=format:"%ar, %s")
130 BOUTPUT=$BOUTPUT$COL_RESET
132 REVLIST=""
134 if [ "$REBASE" = "1" ]; then
135 REVLIST=$(git rev-list --pretty=oneline $branch..$MASTER)
136 if [ "$REVLIST" != "" ]; then
137 left=1
138 n=$(echo -e "$REVLIST" | wc -l)
139 BOUTPUT="$BOUTPUT\n\t$COL_RED$n$COL_RESET commit(s) away"
140 else
141 if [ "$VERBOSE" = "1" ]; then
142 BOUTPUT="$BOUTPUT\n\tno rebase required"
147 REVLIST=$(git rev-list --pretty=oneline $MASTER..$branch)
148 if [ "$REVLIST" != "" ]; then
149 right=1
150 n=$(echo -e "$REVLIST" | wc -l)
151 BOUTPUT="$BOUTPUT\n\t$COL_RED$n$COL_RESET merge(s) waiting"
152 if [ "$LIST" = "1" ]; then
153 BOUTPUT="$BOUTPUT\n$REVLIST"
155 else
156 if [ "$VERBOSE" = "1" ]; then
157 BOUTPUT="$BOUTPUT\n\tnothing to merge"
161 if [ "$BOUTPUT" != "$branch" ]; then
162 case "$left$right" in
163 "00") echo -n " "
165 "10") echo -n " -> "
167 "01") echo -n "<- "
169 "11") echo -n "<-> "
171 esac
172 echo -e "$BOUTPUT$COL_RESET"
173 echo
177 if [ "$ORIGIN" != "" ]; then
178 check_branch $ORIGIN
179 else
180 # List all branched, removes the star and skip the master branch
182 git branch | sed 's,*,,g' | grep -v $MASTER | while read entry
184 check_branch $entry
185 done