HBASE-26312 Shell scan fails with timestamp (#3734)
[hbase.git] / dev-support / rebase_all_git_branches.sh
blobef213c8fb3db7799e889b43ac809fbb2bf83eaa9
1 #!/bin/bash
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
18 # under the License.
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.
27 DEFAULT_DIR="EDIT_ME"
29 function print_usage {
30 cat << __EOF
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.
37 Usage: $0 [-d <dir>]
38 $0 -h
40 -h Show this screen.
41 -d <dir> The absolute or relative directory of your Git repository.
42 __EOF
45 function get_all_branches {
46 # Gets all git branches present locally
47 all_branches=()
48 for i in `git branch --list | sed -e "s/\*//g"`; do
49 all_branches+=("$(echo $i | awk '{print($1)}')")
50 done
53 function get_tracking_branches {
54 # Gets all branches with a remote tracking branch
55 tracking_branches=()
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)}')")
58 done
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
67 exit 1
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
74 # get the status.
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'
81 jira_id="$1"
82 curl -s "$jira_url/$jira_id?fields=resolution" |grep -q '{"resolution":null}'
83 status=$?
84 if [ $status -ne 0 -a $status -ne 1 ]; then
85 echo "Could not get JIRA status. Check your network." >&2
86 exit 1
88 if [ $status -ne 0 ]; then
89 return 99
93 # Process the arguments
94 while getopts ":hd:" opt; do
95 case $opt in
97 # A directory was passed in
98 dir="$OPTARG"
99 if [ ! -d "$dir/.git/" ]; then
100 echo "$dir does not exist or is not a Git repository." >&2
101 exit 1
105 # Print usage instructions
106 print_usage
107 exit 0
110 echo "Invalid argument: $OPTARG" >&2
111 print_usage >&2
112 exit 1
114 esac
115 done
117 if [ -z "$dir" ]; then
118 # No directory was passed in
119 dir="$DEFAULT_DIR"
120 if [ "$dir" = "EDIT_ME" ]; then
121 echo "You need to edit the DEFAULT_DIR in $0." >&2
122 $0 -h
123 exit 1
124 elif [ ! -d "$DEFAULT_DIR/.git/" ]; then
125 echo "Default directory $DEFAULT_DIR is not a Git repository." >&2
126 exit 1
130 cd "$dir"
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
136 git checkout -q "$i"
137 # Exit if git status is dirty
138 check_git_branch_status
139 git pull -q --rebase
140 status=$?
141 if [ "$status" -ne 0 ]; then
142 echo "Unable to pull changes in $i: $status Exiting." >&2
143 exit 1
145 echo "Refreshed $i from remote."
146 done
148 # Run the function to get the list of all branches
149 # The function creates array all_branches and stores the branches in it
150 get_all_branches
152 # Declare array to hold deleted branch info
153 deleted_branches=()
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"
162 jira_status=$?
163 if [ $jira_status -eq 99 ]; then
164 # the JIRA seems to be resolved or is at least not unresolved
165 deleted_branches+=("$i")
169 git checkout -q "$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
177 status=$?
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."
185 git rebase --abort
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
190 exit 1
192 done
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
197 case $yn in
198 [Yy])
199 git branch -D $i
202 echo "To delete it manually, run git branch -D $deleted_branches"
204 esac
205 done
206 git checkout -q master
207 exit 0