forked from codeenigma/ce-contrib-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commit.sh
executable file
·159 lines (142 loc) · 4.37 KB
/
commit.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/bash
# Script to prepare feature branches for pull requests
set -e
usage(){
echo 'commit.sh [OPTIONS]'
echo 'Prepare feature branches for pull requests.'
echo ''
echo 'Available options:'
echo '--apply: Creates a feature branch for committing to the live branch, false by default'
echo '--default: The target default live branch, defaults to 2.x'
echo '--remote: Defaults to origin, allows you to set an alternative remote name'
echo '--skip-checks: Skip over any Git verification steps'
}
# Set defaults
APPLY=false
DEFAULT="2.x"
DEVEL_BRANCH="devel-2.x"
REMOTE="origin"
# Parse options arguments.
parse_options(){
while [ "${1:-}" ]; do
case "$1" in
"--apply")
APPLY=true
;;
"--default")
shift
DEFAULT="$1"
DEVEL_BRANCH="devel-$1"
;;
"--skip-checks")
SKIP_CHECKS=true
;;
"--remote")
shift
REMOTE="$1"
;;
*)
usage
exit 1
;;
esac
shift
done
}
# Parse options.
echo "Parsing provided options."
parse_options "$@"
# Catch 1.x version of devel.
if [ $DEVEL_BRANCH == 'devel-1.x' ]; then
DEVEL_BRANCH="devel"
fi
# Determine the current branch
branch_name="$(git symbolic-ref HEAD 2>/dev/null)" ||
branch_name="(unnamed branch)" # detached HEAD
branch_name=${branch_name##refs/heads/}
echo "Working on branch $branch_name."
# Get remote info
remote_check=`git remote -v show`
# Determine if this is GitHub or GitLab
if [[ $remote_check == *"github"* ]]; then
echo "GitHub repo, will name branches with PR."
merge_indicator="PR"
else
echo "Not a GitHub repo, assuming GitLab, will name branches with MR."
merge_indicator="MR"
fi
# Determine if we're on a feature branch
if [[ $branch_name == *"$merge_indicator"* ]]; then
echo "Looks like you're trying to run this script from your merge branch!"
echo "You need to run it from your feature branch. Exiting."
exit 1
fi
# Fetch branches
git fetch --all
git_branches=`git branch`
# Determine naming convention applied
if [[ $git_branches == *"$DEFAULT"* ]]; then
echo "Using $DEVEL_BRANCH/$DEFAULT naming convention."
main_branch="$DEFAULT"
test_branch="$DEVEL_BRANCH"
else
echo "Using apply/test naming convention."
main_branch="apply"
test_branch="test"
fi
echo "NOW EXECUTING GIT COMMANDS!"
echo "---------------------------"
echo "Making sure main and test branches are up to date."
# Checkout and pull main and test branches
git checkout $test_branch
git pull $REMOTE $test_branch
git checkout $main_branch
git pull $REMOTE $main_branch
echo "---------------------------"
echo "Preparing test branch for merge request."
# Determine if merge branch exists for test
if [[ `git branch --list $branch_name-$merge_indicator-$test_branch` ]]; then
echo "Branch name with $branch_name-$merge_indicator-$test_branch already exists."
git_checkout_flag_test=""
else
echo "No branch with name $branch_name-$merge_indicator-$test_branch, creating."
git_checkout_flag_test=" -b"
fi
# Checkout test merge branch
git checkout$git_checkout_flag_test $branch_name-$merge_indicator-$test_branch
git pull $REMOTE $test_branch
git merge $branch_name
if [ $SKIP_CHECKS = true ]; then
git push --no-verify $REMOTE $branch_name-$merge_indicator-$test_branch
else
git push $REMOTE $branch_name-$merge_indicator-$test_branch
fi
echo "---------------------------"
if [ $APPLY = true ]; then
echo "Also merging main branch."
echo "Returning to our feature branch."
git checkout $branch_name
# Determine if merge branch exists for main
if [[ `git branch --list $branch_name-$merge_indicator-$main_branch` ]]; then
echo "Branch name with $branch_name-$merge_indicator-$main_branch already exists."
git_checkout_flag_main=""
else
echo "No branch with name $branch_name-$merge_indicator-$main_branch, creating."
git_checkout_flag_main=" -b"
fi
# Checkout main merge branch
git checkout$git_checkout_flag_main $branch_name-$merge_indicator-$main_branch
git pull $REMOTE $main_branch
git merge $branch_name
if [ $SKIP_CHECKS = true ]; then
git push --no-verify $REMOTE $branch_name-$merge_indicator-$main_branch
else
git push $REMOTE $branch_name-$merge_indicator-$main_branch
fi
echo "---------------------------"
fi
echo "Returning to our feature branch."
git checkout $branch_name
git merge $main_branch
echo "---------------------------"
echo "ALL DONE!"