2014-10-13 22:48:27 -04:00
|
|
|
#!/bin/bash
|
2014-10-24 00:46:35 -04:00
|
|
|
#/**
|
|
|
|
# * Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
# * or more contributor license agreements. See the NOTICE file
|
|
|
|
# * distributed with this work for additional information
|
|
|
|
# * regarding copyright ownership. The ASF licenses this file
|
|
|
|
# * to you under the Apache License, Version 2.0 (the
|
|
|
|
# * "License"); you may not use this file except in compliance
|
|
|
|
# * with the License. You may obtain a copy of the License at
|
|
|
|
# *
|
|
|
|
# * http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
# *
|
|
|
|
# * Unless required by applicable law or agreed to in writing, software
|
|
|
|
# * distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# * See the License for the specific language governing permissions and
|
|
|
|
# * limitations under the License.
|
|
|
|
# */
|
2014-10-13 22:48:27 -04:00
|
|
|
|
|
|
|
# Make a patch for the current branch based on its tracking branch
|
|
|
|
|
|
|
|
# Process args
|
2015-11-23 19:48:45 -05:00
|
|
|
while getopts "ahd:b:" opt; do
|
2014-10-13 22:48:27 -04:00
|
|
|
case "$opt" in
|
|
|
|
a) addendum='-addendum'
|
|
|
|
;;
|
2016-05-12 14:15:57 -04:00
|
|
|
d)
|
2014-10-22 01:17:15 -04:00
|
|
|
patch_dir=$OPTARG
|
2014-10-13 22:48:27 -04:00
|
|
|
;;
|
2015-11-23 19:48:45 -05:00
|
|
|
b)
|
|
|
|
tracking_branch=$OPTARG
|
|
|
|
;;
|
2014-10-13 22:48:27 -04:00
|
|
|
*)
|
|
|
|
echo -e "Usage: $0 [-h] [-a] [-d] <directory> \n\
|
|
|
|
Must be run from within the git branch to make the patch against.\n\
|
|
|
|
-h - display these instructions.\n\
|
2014-10-22 01:17:15 -04:00
|
|
|
-a - Add an 'addendum' prefix to the patch name.\n\
|
2015-11-23 19:48:45 -05:00
|
|
|
-b - Specify the base branch to diff from. (defaults to the tracking branch or origin master)\n\
|
2014-10-22 01:17:15 -04:00
|
|
|
-d - specify a patch directory (defaults to ~/patches/)"
|
2014-10-13 22:48:27 -04:00
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
# Find what branch we are on
|
|
|
|
branch=$(git branch |grep '*' |awk '{print $2}')
|
|
|
|
if [ ! "$branch" ]; then
|
2014-10-22 01:17:15 -04:00
|
|
|
echo "Can't determine the git branch. Exiting." >&2
|
|
|
|
exit 1
|
2014-10-13 22:48:27 -04:00
|
|
|
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"
|
2016-05-12 14:15:57 -04:00
|
|
|
if [ "$git_dirty" -ne 0 ]; then
|
2014-10-13 22:48:27 -04:00
|
|
|
echo "Git status is dirty. Commit locally first.">&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2015-11-23 19:48:45 -05:00
|
|
|
# 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
|
|
|
|
status=$?
|
|
|
|
if [ "$status" -eq 128 ]; then
|
|
|
|
# Status 128 means there is no remote branch
|
|
|
|
tracking_branch='origin/master'
|
|
|
|
elif [ "$status" -eq 0 ]; then
|
|
|
|
# Status 0 means there is a remote branch
|
|
|
|
tracking_branch="origin/$branch"
|
|
|
|
else
|
|
|
|
echo "Unknown error: $?" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
2014-10-13 22:48:27 -04:00
|
|
|
fi
|
|
|
|
|
2015-11-23 19:48:45 -05:00
|
|
|
|
2014-10-13 22:48:27 -04:00
|
|
|
# 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
|
|
|
|
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
|
2014-10-22 01:17:15 -04:00
|
|
|
# This is the first patch we are making for this release
|
|
|
|
prefix=''
|
2014-10-13 22:48:27 -04:00
|
|
|
elif [ "$status" -ge 1 ]; then
|
2014-10-22 01:17:15 -04:00
|
|
|
# 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
|
2016-05-13 19:38:02 -04:00
|
|
|
if [ ! -f "$patch_dir/$branch.v$i.patch" ]; then
|
2014-10-22 01:17:15 -04:00
|
|
|
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
|
2016-05-13 19:38:02 -04:00
|
|
|
prefix=".v$version"
|
2014-10-22 01:17:15 -04:00
|
|
|
fi
|
|
|
|
else
|
2016-05-13 19:38:02 -04:00
|
|
|
prefix=".v$version"
|
2014-10-22 01:17:15 -04:00
|
|
|
fi
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
2014-10-13 22:48:27 -04:00
|
|
|
fi
|
2016-05-12 14:15:57 -04:00
|
|
|
# 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
|
2016-05-13 19:38:02 -04:00
|
|
|
tracking_suffix=".${tracking_branch#origin/}"
|
2016-05-12 14:15:57 -04:00
|
|
|
fi
|
2014-10-13 22:48:27 -04:00
|
|
|
|
2016-05-12 14:15:57 -04:00
|
|
|
patch_name="$branch$prefix$addendum$tracking_suffix.patch"
|
2014-10-13 22:48:27 -04:00
|
|
|
|
|
|
|
# Do we need to make a diff?
|
|
|
|
git diff --quiet $tracking_branch
|
|
|
|
status=$?
|
|
|
|
if [ "$status" -eq 0 ]; then
|
2014-10-22 01:17:15 -04:00
|
|
|
echo "There is no difference between $branch and $tracking_branch."
|
|
|
|
echo "No patch created."
|
|
|
|
exit 0
|
2014-10-13 22:48:27 -04:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Check whether we need to squash or not
|
|
|
|
local_commits=$(git log $tracking_branch..$branch|grep 'Author:'|wc -l|awk {'print $1'})
|
|
|
|
if [ "$local_commits" -gt 1 ]; then
|
2014-10-22 01:17:15 -04:00
|
|
|
read -p "$local_commits commits exist only in your local branch. Interactive rebase?" yn
|
2014-10-13 22:48:27 -04:00
|
|
|
case $yn in
|
2016-05-12 14:15:57 -04:00
|
|
|
[Yy]* )
|
2014-10-22 01:17:15 -04:00
|
|
|
git rebase -i $tracking_branch
|
|
|
|
;;
|
2016-05-12 14:15:57 -04:00
|
|
|
[Nn]* )
|
2014-10-13 22:48:27 -04:00
|
|
|
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
|