forked from delphix/linux-pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
push-updates.sh
executable file
·139 lines (122 loc) · 4.12 KB
/
push-updates.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
#!/bin/bash
#
# Copyright 2018 Delphix
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
TOP="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
source "$TOP/lib/common.sh"
logmust check_running_system
function usage() {
[[ $# != 0 ]] && echo "$(basename "$0"): $*"
echo "Usage: $(basename "$0") -m|-u [-ny] [-g target_git_url]"
echo " [-b target_git_branch] package"
echo ""
echo " This script pushes updates generated by 'buildpkg.sh -u' to the"
echo " default remote for the package. The updates should be present"
echo " in 'packages/<package>/tmp/repo', in branch repo-HEAD for the merge"
echo " branch and upstream-HEAD for the upstream branch."
echo " Options:"
echo ""
echo " -u push updates for the upstream branch"
echo " -m push updates for the merge branch"
echo " -g repository to push to. Defaults to value of"
echo " DEFAULT_PACKAGE_GIT_URL for the package."
echo " -b branch to push to. Defaults to 'master' when passing -m"
echo " or to $REPO_UPSTREAM_BRANCH when passing -u."
echo " -n dry-run. Pass the dry-run flag to git push (git push -n)."
echo " -y do not prompt for confirmation before pushing."
echo " -h display this message and exit."
echo ""
exit 2
}
unset TARGET_GIT_URL
unset TARGET_GIT_BRANCH
do_push_merge=false
do_push_upstream=false
do_ask=true
dry_run=false
while getopts ':b:g:hmnuy' c; do
case "$c" in
b) TARGET_GIT_BRANCH="$OPTARG" ;;
g) TARGET_GIT_URL="$OPTARG" ;;
m) do_push_merge=true ;;
u) do_push_upstream=true ;;
n) dry_run=true ;;
y) do_ask=false ;;
h) usage >&2 ;;
*) usage "illegal option -- $OPTARG" >&2 ;;
esac
done
shift $((OPTIND - 1))
[[ $# -lt 1 ]] && usage "package argument missing" >&2
[[ $# -gt 1 ]] && usage "too many arguments" >&2
PACKAGE=$1
if $do_push_merge && $do_push_upstream; then
usage "-m and -u are exclusive." >&2
elif ! ($do_push_merge || $do_push_upstream); then
usage "must pass either -m or -u." >&2
fi
query_git_credentials
logmust check_package_exists "$PACKAGE"
PKGDIR="$TOP/packages/$PACKAGE"
WORKDIR="$PKGDIR/tmp"
if [[ -z "$TARGET_GIT_URL" ]]; then
#
# load the package's config just so that we can retrieve
# DEFAULT_PACKAGE_GIT_URL.
#
logmust load_package_config "$PACKAGE"
check_env DEFAULT_PACKAGE_GIT_URL
TARGET_GIT_URL="$DEFAULT_PACKAGE_GIT_URL"
echo "TARGET_GIT_URL set to DEFAULT_PACKAGE_GIT_URL"
fi
if [[ -z "$TARGET_GIT_BRANCH" ]]; then
if $do_push_merge; then
TARGET_GIT_BRANCH="master"
echo "TARGET_GIT_BRANCH defaults to 'master' for merge pushes."
else
TARGET_GIT_BRANCH="$REPO_UPSTREAM_BRANCH"
echo "TARGET_GIT_BRANCH defaults to '$REPO_UPSTREAM_BRANCH' for" \
"upstream pushes."
fi
fi
[[ "$TARGET_GIT_URL" == https://* ]] ||
die "Target git url must begin with https://"
git_url_with_creds="${TARGET_GIT_URL/https:\/\//https:\/\/${PUSH_GIT_USER}:${PUSH_GIT_PASSWORD}@}"
git_url_with_fake_creds="${TARGET_GIT_URL/https:\/\//https:\/\/<user>:<password>@}"
if $do_push_merge; then
ref="refs/heads/repo-HEAD"
check_updated="$WORKDIR/repo-updated"
else
ref="refs/heads/upstream-HEAD"
check_updated="$WORKDIR/upstream-updated"
fi
[[ -f "$check_updated" ]] ||
die "$check_updated is missing, aborting push."
logmust cd "$WORKDIR/repo"
check_git_ref "$ref"
if $do_ask; then
echo_bold "WARNING: You are about to push to branch" \
"$TARGET_GIT_BRANCH of $TARGET_GIT_URL."
read -rp "Do you want to proceed (y/[n])? "
if [[ "$REPLY" != "y" ]]; then
echo "Push aborted by user."
exit 1
fi
fi
flags=""
$dry_run && flags="-n"
echo "Running: git push $flags $git_url_with_fake_creds $ref:$TARGET_GIT_BRANCH"
git push $flags "$git_url_with_creds" "$ref:$TARGET_GIT_BRANCH" ||
die "Push failed."