forked from Autodesk/enterprise-config-for-git
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hotfix-branch.sh
executable file
·90 lines (75 loc) · 2.9 KB
/
hotfix-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
#!/usr/bin/env bash
#/
#/ Create a branch to deliver a critical hotfix
#/
#/ Usage: git $KIT_ID hotfix-branch [--remote <owner>] --parent <base_branch> <branch>
#/
#/ The parent branch should follow the format "hotfix/release/name"
#/
#/ Example:
#/ git $KIT_ID hotfix-branch --parent hotfix/harrier/HF1 dev/corp_ID/name
#/
set -e
# Use git-sh-setup to initialize Git variables
SUBDIRECTORY_OK=Yes
. "$(git --exec-path)/git-sh-setup"
require_work_tree
cd_to_toplevel
# Source shared kit content
KIT_PATH=$(dirname "$0")
. "$KIT_PATH/enterprise.constants"
. "$KIT_PATH/lib/setup_helpers.sh"
: ${GIT_TRACE:=0}
function usage() {
echo "Usage: git $KIT_ID hotfix-branch [--remote <owner>] --parent <base_branch> <branch>" >&2
[[ $# -gt 0 ]] && die "$@"
exit 0
}
# Parse parameters
declare options
options=$(getopt --longoptions "remote:,parent:,help" --options "h" --name "git $KIT_ID hotfix-branch" -- "$@") || usage "Invalid parameters"
eval set -- "$options"
while :; do
case "$1" in
--remote) remote=$2; shift 2;;
--parent) parent=$2; shift 2;;
-h|--help) usage;;
--) shift; break;;
esac
done
[[ $# -eq 1 ]] || usage "Invalid parameters"
declare branch=$1
: ${remote:=$DEFAULT_OWNER}
# Calculate the branch
[[ $parent ]] || usage "Missing parent branch name"
[[ $branch ]] || usage "Missing branch name"
# Check the format of base branch, should be hotfix/$release/xxx
declare prefix=${parent%%/*}
declare branch_part=${parent#*/}
[[ $prefix == "hotfix" ]] || die "The parent branch name should start with 'hotfix' prefix"
[[ $branch_part == */* ]] || die "Invalid parent hotfix branch format"
if [[ $branch != dev/* ]]; then
declare gh_user=$(git config --global adsk.github.account)
[[ $gh_user ]] || die "Missing GitHub user account"
branch="dev/${gh_user,,}/$branch"
say "NOTE: Using $branch as branch name"
fi
status "Checking the origin remote"
declare origin_url=$(git remote get-url origin)
[[ $origin_url ]] || die "Failed to get URL for origin remote"
[[ $origin_url = https://* ]] || die "Please clone using HTTPS"
declare repo=${origin_url##*/}
declare server_url=${origin_url%/*/$repo}
declare remote_url="$server_url/$remote/$repo"
status "Checking for $remote remote"
if ! git remote get-url "$remote" &>/dev/null; then
status "Adding remote $remote"
git remote add "$remote" "$remote_url" || die "Failed to define $remote remote"
fi
status "Updating local information about $remote remote"
git remote update "$remote" || die "Failed to update information about $remote remote"
status "Creating local hotfix branch"
git checkout -b "$branch" --track remotes/"$remote"/"$parent" || die "Failed to create local $branch branch"
status "Configuring local hotfix branch to push to domain fork (origin)"
git config --local "branch.$branch.pushRemote" origin || die "Failed to set push destination of local $branch"
status "Your hotfix branch has been successfully created"