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.
24 function print_usage
{
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.
36 -d <dir> The absolute or relative directory of your Git repository.
40 function get_all_branches
{
41 # Gets all git branches present locally
43 for i
in `git branch --list | sed -e "s/\*//g"`; do
44 all_branches
+=("$(echo $i | awk '{print($1)}')")
48 function get_tracking_branches
{
49 # Gets all branches with a remote tracking branch
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)}')")
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
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
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'
77 curl
-s "$jira_url/$jira_id?fields=resolution" |
grep -q '{"resolution":null}'
79 if [ $status -ne 0 -a $status -ne 1 ]; then
80 echo "Could not get JIRA status. Check your network." >&2
83 if [ $status -ne 0 ]; then
88 # Process the arguments
89 while getopts ":hd:" opt
; do
92 # A directory was passed in
94 if [ ! -d "$dir/.git/" ]; then
95 echo "$dir does not exist or is not a Git repository." >&2
100 # Print usage instructions
105 echo "Invalid argument: $OPTARG" >&2
112 if [ -z "$dir" ]; then
113 # No directory was passed in
115 if [ "$dir" = "EDIT_ME" ]; then
116 echo "You need to edit the DEFAULT_DIR in $0." >&2
119 elif [ ! -d "$DEFAULT_DIR/.git/" ]; then
120 echo "Default directory $DEFAULT_DIR is not a Git repository." >&2
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
132 # Exit if git status is dirty
133 check_git_branch_status
136 if [ "$status" -ne 0 ]; then
137 echo "Unable to pull changes in $i: $status Exiting." >&2
140 echo "Refreshed $i from remote."
143 # Run the function to get the list of all branches
144 # The function creates array all_branches and stores the branches in it
147 # Declare array to hold deleted branch info
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"
158 if [ $jira_status -eq 99 ]; then
159 # the JIRA seems to be resolved or is at least not unresolved
160 deleted_branches
+=("$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
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."
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
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
197 echo "To delete it manually, run git branch -D $deleted_branches"
201 git checkout
-q master