HBASE-19674: Improve make_patch.sh

Signed-off-by: Jan Hentschel <[email protected]>


Project: http://git-wip-us.apache.org/repos/asf/hbase/repo
Commit: http://git-wip-us.apache.org/repos/asf/hbase/commit/8ae2a215
Tree: http://git-wip-us.apache.org/repos/asf/hbase/tree/8ae2a215
Diff: http://git-wip-us.apache.org/repos/asf/hbase/diff/8ae2a215

Branch: refs/heads/HBASE-19397
Commit: 8ae2a2150b517123cde214bb5543557b562c4c01
Parents: 8a5b153
Author: Niels Basjes <[email protected]>
Authored: Sun Jan 7 14:30:46 2018 +0100
Committer: Jan Hentschel <[email protected]>
Committed: Sun Jan 7 14:41:40 2018 +0100

----------------------------------------------------------------------
 dev-support/make_patch.sh | 128 ++++++++++++++++++++++-------------------
 1 file changed, 68 insertions(+), 60 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hbase/blob/8ae2a215/dev-support/make_patch.sh
----------------------------------------------------------------------
diff --git a/dev-support/make_patch.sh b/dev-support/make_patch.sh
index 8179098..3737dc9 100755
--- a/dev-support/make_patch.sh
+++ b/dev-support/make_patch.sh
@@ -19,9 +19,11 @@
 
 # Make a patch for the current branch based on its tracking branch
 
+patch_version=''
+
 # Process args
-while getopts "ahd:b:" opt; do
-    case "$opt" in
+while getopts "ahtd:b:" opt; do
+    case "${opt}" in
         a)  addendum='-addendum'
             ;;
         d)
@@ -30,11 +32,15 @@ while getopts "ahd:b:" opt; do
         b)
             tracking_branch=$OPTARG
             ;;
+        t)
+            patch_version=".$(date -u +%Y%m%d-%H%M%S)"
+            ;;
         *)
-            echo -e "Usage: $0 [-h] [-a] [-d] <directory> \n\
+            echo -e "Usage: $0 [-h] [-a] [-t] [-d] <directory> \n\
         Must be run from within the git branch to make the patch against.\n\
         -h - display these instructions.\n\
         -a - Add an 'addendum' prefix to the patch name.\n\
+        -t - Use the current timestamp as the version indicator.\n\
         -b - Specify the base branch to diff from. (defaults to the tracking 
branch or origin master)\n\
         -d - specify a patch directory (defaults to ~/patches/)"
             exit 0
@@ -43,16 +49,16 @@ while getopts "ahd:b:" opt; do
 done
 
 # Find what branch we are on
-branch=$(git branch |grep '*' |awk '{print $2}')
-if [ ! "$branch" ]; then
+branch=$(git branch | grep '^\*' | cut -d' ' -f2- )
+if [ ! "${branch}" ]; then
     echo "Can't determine the git branch. Exiting." >&2
     exit 1
 fi
 
 # Exit if git status is dirty
-git_dirty=$(git diff --shortstat 2> /dev/null | wc -l|awk {'print $1'})
-echo "git_dirty is $git_dirty"
-if [ "$git_dirty" -ne 0 ]; then
+git_dirty=$(git diff --shortstat 2> /dev/null | wc -l)
+echo "git_dirty is ${git_dirty}"
+if [ "${git_dirty}" -ne 0 ]; then
     echo "Git status is dirty. Commit locally first.">&2
     exit 1
 fi
@@ -60,15 +66,15 @@ fi
 # Determine the tracking branch if needed.
 # If it was passed in from the command line
 # with -b then use dthat no matter what.
-if [ ! "$tracking_branch" ]; then
-  git log -n 1 origin/$branch > /dev/null 2>&1
+if [ ! "${tracking_branch}" ]; then
+  git log -n 1 "origin/${branch}" > /dev/null 2>&1
   status=$?
-  if [ "$status" -eq 128 ]; then
+  if [ "${status}" -eq 128 ]; then
       # Status 128 means there is no remote branch
       tracking_branch='origin/master'
-  elif [ "$status" -eq 0 ]; then
+  elif [ "${status}" -eq 0 ]; then
       # Status 0 means there is a remote branch
-      tracking_branch="origin/$branch"
+      tracking_branch="origin/${branch}"
   else
       echo "Unknown error: $?" >&2
       exit 1
@@ -76,81 +82,83 @@ if [ ! "$tracking_branch" ]; then
 fi
 
 
-# Deal with invalid or missing $patch_dir
-if [ ! "$patch_dir" ]; then
+# Deal with invalid or missing ${patch_dir}
+if [ ! "${patch_dir}" ]; then
     echo -e "Patch directory not specified. Falling back to ~/patches/."
     patch_dir=~/patches
 fi
 
-if [ ! -d "$patch_dir" ]; then
-    echo "$patch_dir does not exist. Creating it."
-    mkdir $patch_dir
+if [ ! -d "${patch_dir}" ]; then
+    echo "${patch_dir} does not exist. Creating it."
+    mkdir "${patch_dir}"
 fi
 
-# Determine what to call the patch
-# Check to see if any patch exists that includes the branch name
-status=$(ls $patch_dir/*$branch* 2>/dev/null|grep -v addendum|wc -l|awk 
{'print $1'})
-if [ "$status" -eq 0 ]; then
-    # This is the first patch we are making for this release
-    prefix=''
-elif  [ "$status" -ge 1 ]; then
-    # At least one patch already exists -- add a version prefix
-    for i in {1..99}; do
-        # Check to see the maximum version of patch that exists
-        if [ ! -f "$patch_dir/$branch.v$i.patch" ]; then
-            version=$i
-            if [ -n "$addendum" ]; then
-                # Don't increment the patch # if it is an addendum
-                echo "Creating an addendum"
-                if [ "$version" -eq 1 ]; then
-                    # We are creating an addendum to the first version of the 
patch
-                    prefix=''
-                else
-                    # We are making an addendum to a different version of the 
patch
-                    let version=$version-1
-                    prefix=".v$version"
-                fi
-            else
-                prefix=".v$version"
-            fi
-            break
-        fi
-    done
-fi
 # If this is against a tracking branch other than master
 # include it in the patch name
 tracking_suffix=""
-if [[ $tracking_branch != "origin/master" \
-    &&  $tracking_branch != "master" ]]; then
+if [[ "${tracking_branch}" != "origin/master" \
+    &&  "${tracking_branch}" != "master" ]]; then
     tracking_suffix=".${tracking_branch#origin/}"
 fi
 
-patch_name="$branch$prefix$addendum$tracking_suffix.patch"
+# Determine what to call the patch
+if [[ "${patch_version}" == "" ]]; then
+    # If we do NOT have the timestamp we must find the version to use
+    version=0
+    # Check to see if any patch exists that includes the branch name
+    status=$(find "${patch_dir}" -maxdepth 1 -type f -name 
"${branch}${tracking_suffix}.v[0-9][0-9].patch" 2>/dev/null|grep -c -v addendum)
+    if [[ "${status}" -eq 0 ]]; then
+        # This is the first patch we are making for this release
+        version=1
+    elif  [[ "${status}" -ge 1 ]]; then
+        # At least one patch already exists -- find the last one
+        for i in {99..1}; do
+            # Check to see the maximum version of patch that exists
+            versionfiles=$(find "${patch_dir}" -maxdepth 1 -type f -name 
"${branch}${tracking_suffix}.v$(printf "%02d" "$i").patch" 2> /dev/null | wc -l)
+            if [ 0 -lt "${versionfiles}" ]; then
+                version=$((i+1))
+                break
+            fi
+        done
+    fi
+    if [ -n "${addendum}" ]; then
+        # Don't increment the patch # if it is an addendum
+        echo "Creating an addendum"
+        if [ "${version}" -ge 1 ]; then
+            # We are making an addendum to a different version of the patch
+            version=$((version-1))
+        fi
+    fi
+    patch_version=".v$(printf "%02d" "${version}")"
+fi
+
+
+patch_name="${branch}${tracking_suffix}${patch_version}${addendum}.patch"
 
 # Do we need to make a diff?
-git diff --quiet $tracking_branch
+git diff --quiet "${tracking_branch}"
 status=$?
-if [ "$status" -eq 0 ]; then
-    echo "There is no difference between $branch and $tracking_branch."
+if [ "${status}" -eq 0 ]; then
+    echo "There is no difference between ${branch} and ${tracking_branch}."
     echo "No patch created."
     exit 0
 fi
 
 # Check whether we need to squash or not
-local_commits=$(git log $tracking_branch..$branch|grep 'Author:'|wc -l|awk 
{'print $1'})
+local_commits=$(git log "${tracking_branch}..${branch}" | grep -c 'Author:')
 if [ "$local_commits" -gt 1 ]; then
     read -p "$local_commits commits exist only in your local branch. 
Interactive rebase?" yn
     case $yn in
         [Yy]* )
-            git rebase -i $tracking_branch
+            git rebase -i "${tracking_branch}"
                 ;;
         [Nn]* )
-          echo "Creating $patch_dir/$patch_name using git diff."
-          git diff $tracking_branch > $patch_dir/$patch_name
+          echo "Creating ${patch_dir}/${patch_name} using git diff."
+          git diff "${tracking_branch}" > "${patch_dir}/${patch_name}"
           exit 0
         ;;
     esac
 fi
 
-echo "Creating patch $patch_dir/$patch_name using git format-patch"
-git format-patch --stdout $tracking_branch > $patch_dir/$patch_name
+echo "Creating patch ${patch_dir}/${patch_name} using git format-patch"
+git format-patch --no-prefix --stdout "${tracking_branch}" > 
"${patch_dir}/${patch_name}"

Reply via email to