HBASE-13911 update java/hadoop prereqs for 1.2
[hbase.git] / dev-support / rebase_all_git_branches.sh
blob261faa85de4765bd5e08abaf1e9369edf2b8331e
1 #!/bin/bash
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # This script assumes that your remote is called "origin"
16 # and that your local master branch is called "master".
17 # I am sure it could be made more abstract but these are the defaults.
19 # Edit this line to point to your default directory,
20 # or always pass a directory to the script.
22 DEFAULT_DIR="EDIT_ME"
24 function print_usage {
25 cat << __EOF
27 $0: A script to manage your Apache HBase Git repository.
29 If run with no arguments, it reads the DEFAULT_DIR variable, which you
30 can specify by editing the script.
32 Usage: $0 [-d <dir>]
33 $0 -h
35 -h Show this screen.
36 -d <dir> The absolute or relative directory of your Git repository.
37 __EOF
40 function get_all_branches {
41 # Gets all git branches present locally
42 all_branches=()
43 for i in `git branch --list | sed -e "s/\*//g"`; do
44 all_branches+=("$(echo $i | awk '{print($1)}')")
45 done
48 function get_tracking_branches {
49 # Gets all branches with a remote tracking branch
50 tracking_branches=()
51 for i in `git branch -lvv | grep "\[origin/" | sed -e 's/\*//g' | awk {'print $1'}`; do
52 tracking_branches+=("$(echo $i | awk '{print($1)}')")
53 done
56 function check_git_branch_status {
57 # Checks the current Git branch to see if it's dirty
58 # Returns 1 if the branch is dirty
59 git_dirty=$(git diff --shortstat 2> /dev/null | wc -l|awk {'print $1'})
60 if [ "$git_dirty" -ne 0 ]; then
61 echo "Git status is dirty. Commit locally first." >&2
62 exit 1
66 function get_jira_status {
67 # This function expects as an argument the JIRA ID,
68 # and returns 99 if resolved and 1 if it couldn't
69 # get the status.
71 # The JIRA status looks like this in the HTML:
72 # span id="resolution-val" class="value resolved" >
73 # The following is a bit brittle, but filters for lines with
74 # resolution-val returns 99 if it's resolved
75 jira_url='https://issues.apache.org/jira/rest/api/2/issue'
76 jira_id="$1"
77 curl -s "$jira_url/$jira_id?fields=resolution" |grep -q '{"resolution":null}'
78 status=$?
79 if [ $status -ne 0 -a $status -ne 1 ]; then
80 echo "Could not get JIRA status. Check your network." >&2
81 exit 1
83 if [ $status -ne 0 ]; then
84 return 99
88 # Process the arguments
89 while getopts ":hd:" opt; do
90 case $opt in
92 # A directory was passed in
93 dir="$OPTARG"
94 if [ ! -d "$dir/.git/" ]; then
95 echo "$dir does not exist or is not a Git repository." >&2
96 exit 1
100 # Print usage instructions
101 print_usage
102 exit 0
105 echo "Invalid argument: $OPTARG" >&2
106 print_usage >&2
107 exit 1
109 esac
110 done
112 if [ -z "$dir" ]; then
113 # No directory was passed in
114 dir="$DEFAULT_DIR"
115 if [ "$dir" = "EDIT_ME" ]; then
116 echo "You need to edit the DEFAULT_DIR in $0." >&2
117 $0 -h
118 exit 1
119 elif [ ! -d "$DEFAULT_DIR/.git/" ]; then
120 echo "Default directory $DEFAULT_DIR is not a Git repository." >&2
121 exit 1
125 cd "$dir"
127 # For each tracking branch, check it out and make sure it's fresh
128 # This function creates tracking_branches array and stores the tracking branches in it
129 get_tracking_branches
130 for i in "${tracking_branches[@]}"; do
131 git checkout -q "$i"
132 # Exit if git status is dirty
133 check_git_branch_status
134 git pull -q --rebase
135 status=$?
136 if [ "$status" -ne 0 ]; then
137 echo "Unable to pull changes in $i: $status Exiting." >&2
138 exit 1
140 echo "Refreshed $i from remote."
141 done
143 # Run the function to get the list of all branches
144 # The function creates array all_branches and stores the branches in it
145 get_all_branches
147 # Declare array to hold deleted branch info
148 deleted_branches=()
150 for i in "${all_branches[@]}"; do
151 # Check JIRA status to see if we still need this branch
152 # JIRA expects uppercase
153 jira_id="$(echo $i | awk '{print toupper($0)'})"
154 if [[ "$jira_id" == HBASE-* ]]; then
155 # Returns 1 if the JIRA is closed, 0 otherwise
156 get_jira_status "$jira_id"
157 jira_status=$?
158 if [ $jira_status -eq 99 ]; then
159 # the JIRA seems to be resolved or is at least not unresolved
160 deleted_branches+=("$i")
164 git checkout -q "$i"
166 # Exit if git status is dirty
167 check_git_branch_status
169 # If this branch has a remote, don't rebase it
170 # If it has a remote, it has a log with at least one entry
171 git log -n 1 origin/"$i" > /dev/null 2>&1
172 status=$?
173 if [ $status -eq 128 ]; then
174 # Status 128 means there is no remote branch
175 # Try to rebase against master
176 echo "Rebasing $i on origin/master"
177 git rebase -q origin/master > /dev/null 2>&1
178 if [ $? -ne 0 ]; then
179 echo "Failed. Rolling back. Rebase $i manually."
180 git rebase --abort
182 elif [ $status -ne 0 ]; then
183 # If status is 0 it means there is a remote branch, we already took care of it
184 echo "Unknown error: $?" >&2
185 exit 1
187 done
189 # Offer to clean up all deleted branches
190 for i in "${deleted_branches[@]}"; do
191 read -p "$i's JIRA is resolved. Delete? " yn
192 case $yn in
193 [Yy])
194 git branch -D $i
197 echo "To delete it manually, run git branch -D $deleted_branches"
199 esac
200 done
201 git checkout -q master
202 exit 0