This repository has been archived by the owner on Jul 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 81
/
remote-branch.sh
executable file
·134 lines (107 loc) · 3.69 KB
/
remote-branch.sh
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# See usage function.
#
# Description:
# Script to create new remote branches in Git.
#
# Options:
# See usage function.
#========================================================================================
#========================================================================================
# Description: Display usage information then abort the script.
#========================================================================================
#include the utility library
source `dirname $0`/rhq_bash.lib
usage()
{
USAGE=$(
cat << EOF
USAGE: remote-branch.sh OPTIONS
--source-branch=git_branch [REQUIRED]
The branch to base the new remote branch on. Ex. release/jon3.1.x, or master. Script assumes the
most recent commits from the 'source-branch' are to be replicated to the new 'release-branch'.
--new-remote-branch=git_branch_name [REQUIRED]
Git branch name to be created. Assumes no such local or remote branch by this name already exists.
EOF
)
EXAMPLE="remote-branch.sh --source-branch=\"release/jon3.1.x\" --new-remote-branch=\"rc/jon3.1.2.GA\""
abort "$@" "$USAGE" "$EXAMPLE"
}
#========================================================================================
# Description: Validate and parse input arguments
#========================================================================================
parse_and_validate_options()
{
print_function_information $FUNCNAME
SOURCE_BRANCH=
NEW_REMOTE_BRANCH=
short_options="h"
long_options="help,source-branch:,new-remote-branch:"
PROGNAME=${0##*/}
ARGS=$(getopt -s bash --options $short_options --longoptions $long_options --name $PROGNAME -- "$@" )
eval set -- "$ARGS"
while true; do
case $1 in
-h|--help)
usage
;;
--source-branch)
shift
SOURCE_BRANCH="$1"
shift
;;
--new-remote-branch)
shift
NEW_REMOTE_BRANCH="$1"
shift
;;
--)
shift
break
;;
*)
usage
;;
esac
done
if [ -z "$SOURCE_BRANCH" ];
then
usage "Git source branch not specified!"
fi
if [ -z "$NEW_REMOTE_BRANCH" ];
then
usage "New remote branch name not specified!"
fi
print_centered "Script Options"
script_options=( "SOURCE_BRANCH" "NEW_REMOTE_BRANCH")
print_variables "${script_options[@]}"
}
#========================================================================================
# Description: Set all the local and environment variables required by the script.
#========================================================================================
synch_build_branch_and_push()
{
# checkout the source branch
echo "git checkout $GIT_SOURCE_BRANCH"
git checkout $GIT_SOURCE_BRANCH
# fetch all changes in remotes since last fetch
echo "git fetch --all"
git fetch --all
# synch the current branch with the latest on source branch
echo "git pull --rebase"
git pull --rebase
# echo current local branches
echo "git branch"
git branch
# create new git branch with last commit to source branch
echo "git checkout -b $NEW_REMOTE_BRANCH"
git checkout -b $NEW_REMOTE_BRANCH
# push new local branch remote. Local and remote branch has same name.
echo "git push origin $NEW_REMOTE_BRANCH:$NEW_REMOTE_BRANCH"
git push origin $NEW_REMOTE_BRANCH:$NEW_REMOTE_BRANCH
# set local branch to track remote git branch of the same name
echo "branch --set-upstream $NEW_REMOTE_BRANCH origin/$NEW_REMOTE_BRANCH"
git branch --set-upstream $NEW_REMOTE_BRANCH origin/$NEW_REMOTE_BRANCH
}
############ MAIN SCRIPT ############
parse_and_validate_options $@
synch_build_branch_and_push