File: new-branch

package info (click to toggle)
pcp 5.2.6-1
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 185,908 kB
  • sloc: ansic: 307,504; sh: 138,932; xml: 90,613; cpp: 65,924; python: 18,517; perl: 16,958; yacc: 8,925; lex: 2,843; makefile: 1,502; fortran: 60; java: 52
file content (98 lines) | stat: -rwxr-xr-x 1,616 bytes parent folder | download
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 master 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 master branch at this point
#
branch=`git branch --list | sed -n -e 's/^\* //p'`
if [ "$branch" != master ]
then
    echo "Error: should be on master 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 master

if $dryrun
then
    echo "+ echo $sha >$branch.sha"
else
    echo "$sha" >$branch.sha
fi

# all done
#
sts=0
exit