3 # Licensed to the Apache Software Foundation (ASF) under one
4 # or more contributor license agreements. See the NOTICE file
5 # distributed with this work for additional information
6 # regarding copyright ownership. The ASF licenses this file
7 # to you under the Apache License, Version 2.0 (the
8 # "License"); you may not use this file except in compliance
9 # with the License. You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing,
14 # software distributed under the License is distributed on an
15 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 # KIND, either express or implied. See the License for the
17 # specific language governing permissions and limitations
20 # This script assumes that your remote is called "origin"
21 # and that your local master branch is called "master".
22 # I am sure it could be made more abstract but these are the defaults.
24 # Edit this line to point to your default directory,
25 # or always pass a directory to the script.
29 function print_usage
{
32 $0: A script to manage your Apache HBase Git repository.
34 If run with no arguments, it reads the DEFAULT_DIR variable, which you
35 can specify by editing the script.
41 -d <dir> The absolute or relative directory of your Git repository.
45 function get_all_branches
{
46 # Gets all git branches present locally
48 for i
in `git branch --list | sed -e "s/\*//g"`; do
49 all_branches
+=("$(echo $i | awk '{print($1)}')")
53 function get_tracking_branches
{
54 # Gets all branches with a remote tracking branch
56 for i
in `git branch -lvv | grep "\[origin/" | sed -e 's/\*//g' | awk {'print $1'}`; do
57 tracking_branches
+=("$(echo $i | awk '{print($1)}')")
61 function check_git_branch_status
{
62 # Checks the current Git branch to see if it's dirty
63 # Returns 1 if the branch is dirty
64 git_dirty
=$
(git
diff --shortstat 2> /dev
/null |
wc -l|
awk {'print $1'})
65 if [ "$git_dirty" -ne 0 ]; then
66 echo "Git status is dirty. Commit locally first." >&2
71 function get_jira_status
{
72 # This function expects as an argument the JIRA ID,
73 # and returns 99 if resolved and 1 if it couldn't
76 # The JIRA status looks like this in the HTML:
77 # span id="resolution-val" class="value resolved" >
78 # The following is a bit brittle, but filters for lines with
79 # resolution-val returns 99 if it's resolved
80 jira_url
='https://issues.apache.org/jira/rest/api/2/issue'
82 curl
-s "$jira_url/$jira_id?fields=resolution" |
grep -q '{"resolution":null}'
84 if [ $status -ne 0 -a $status -ne 1 ]; then
85 echo "Could not get JIRA status. Check your network." >&2
88 if [ $status -ne 0 ]; then
93 # Process the arguments
94 while getopts ":hd:" opt
; do
97 # A directory was passed in
99 if [ ! -d "$dir/.git/" ]; then
100 echo "$dir does not exist or is not a Git repository." >&2
105 # Print usage instructions
110 echo "Invalid argument: $OPTARG" >&2
117 if [ -z "$dir" ]; then
118 # No directory was passed in
120 if [ "$dir" = "EDIT_ME" ]; then
121 echo "You need to edit the DEFAULT_DIR in $0." >&2
124 elif [ ! -d "$DEFAULT_DIR/.git/" ]; then
125 echo "Default directory $DEFAULT_DIR is not a Git repository." >&2
132 # For each tracking branch, check it out and make sure it's fresh
133 # This function creates tracking_branches array and stores the tracking branches in it
134 get_tracking_branches
135 for i
in "${tracking_branches[@]}"; do
137 # Exit if git status is dirty
138 check_git_branch_status
141 if [ "$status" -ne 0 ]; then
142 echo "Unable to pull changes in $i: $status Exiting." >&2
145 echo "Refreshed $i from remote."
148 # Run the function to get the list of all branches
149 # The function creates array all_branches and stores the branches in it
152 # Declare array to hold deleted branch info
155 for i
in "${all_branches[@]}"; do
156 # Check JIRA status to see if we still need this branch
157 # JIRA expects uppercase
158 jira_id
="$(echo $i | awk '{print toupper($0)'})"
159 if [[ "$jira_id" == HBASE-
* ]]; then
160 # Returns 1 if the JIRA is closed, 0 otherwise
161 get_jira_status
"$jira_id"
163 if [ $jira_status -eq 99 ]; then
164 # the JIRA seems to be resolved or is at least not unresolved
165 deleted_branches
+=("$i")
171 # Exit if git status is dirty
172 check_git_branch_status
174 # If this branch has a remote, don't rebase it
175 # If it has a remote, it has a log with at least one entry
176 git log
-n 1 origin
/"$i" > /dev
/null
2>&1
178 if [ $status -eq 128 ]; then
179 # Status 128 means there is no remote branch
180 # Try to rebase against master
181 echo "Rebasing $i on origin/master"
182 git rebase
-q origin
/master
> /dev
/null
2>&1
183 if [ $?
-ne 0 ]; then
184 echo "Failed. Rolling back. Rebase $i manually."
187 elif [ $status -ne 0 ]; then
188 # If status is 0 it means there is a remote branch, we already took care of it
189 echo "Unknown error: $?" >&2
194 # Offer to clean up all deleted branches
195 for i
in "${deleted_branches[@]}"; do
196 read -p "$i's JIRA is resolved. Delete? " yn
202 echo "To delete it manually, run git branch -D $deleted_branches"
206 git checkout
-q master