1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
#!/bin/sh
#
# Usage: scripts/new-branch
#
# This script uses today's date to create a new branch, record the
# HEAD SHA in <branch>.sha, then switch back to the main branch
#
# All of this sets up for a new batch of commits to be pushed via
# github pull requests using the sister-scripts scripts/pcp-push
#
tmp=/var/tmp/$$
sts=1
trap "rm -f $tmp.*; exit \$sts" 0 1 2 3 15
_usage()
{
echo >&2 "Usage: new-branch [options] [suffix]"
echo >&2
echo >&2 "options:"
echo >&2 " -n dryrun"
exit
}
dryrun=false
GIT=git
while getopts "n?" c
do
case $c
in
n)
dryrun=true
GIT="echo + git"
;;
*)
_usage
# NOTREACHED
esac
done
shift `expr $OPTIND - 1`
if [ $# -eq 1 ]
then
suff="$1"
shift
else
suff=''
fi
[ $# -eq 0 ] || _usage
# expect to be on main branch at this point
#
branch=`git branch --list | sed -n -e 's/^\* //p'`
if [ "$branch" != main ]
then
echo "Error: should be on main branch (not $branch)"
exit
fi
branch=`date '+%Y%m%d'`$suff
if git branch --list | sed -e 's/\*//' -e 's/ //g' | grep "^$branch\$" >/dev/null
then
echo "Error: branch $branch already exists"
exit
fi
if $dryrun
then
$GIT checkout -b "$branch"
else
if $GIT checkout -b "$branch"
then
:
else
echo "Error: branch $branch not created"
exit
fi
fi
# remember HEAD commit that was pushed to <branch>...
#
sha=`git log | sed -e 's/commit //' -e 1q`
# back to square one
#
$GIT checkout main
if $dryrun
then
echo "+ echo $sha >$branch.sha"
else
echo "$sha" >$branch.sha
fi
# all done
#
sts=0
exit
|