-
Notifications
You must be signed in to change notification settings - Fork 31
/
push-merge.sh
executable file
·103 lines (89 loc) · 3.38 KB
/
push-merge.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
#!/usr/bin/env bash
#
# Copyright 2020 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"
check_env DEFAULT_GIT_BRANCH
logmust check_running_system
logmust run_setup_if_needed
function usage() {
[[ $# != 0 ]] && echo "$(basename "$0"): $*"
echo "Usage: $(basename "$0") <package>"
echo ""
echo " Push code that was previously merged. sync-with-upstream.sh must"
echo " already have been run. Before pushing the merge, it will first"
echo " check that the target branch has not been modified since the merge"
echo " was performed, and fail if it did."
echo ""
echo " As a safety check, DRYRUN environment variable must be set to"
echo " 'false'."
echo ""
echo " -h display this message and exit."
echo ""
exit 2
}
while getopts ':h' c; do
case "$c" in
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 [[ "$DRYRUN" != 'false' ]]; then
die "DRYRUN environment variable must be set to 'false'."
fi
logmust check_package_exists "$PACKAGE"
DEFAULT_REVISION="${DEFAULT_REVISION:-$(delphix_revision)}"
logmust load_package_config "$PACKAGE"
if [[ ! -d "$WORKDIR/repo" ]]; then
die "$WORKDIR/repo doesn't exist, have you run sync-with-upstream for" \
"package $PACKAGE?"
fi
logmust cd "$WORKDIR/repo"
#
# Check that the target branch has not been modified in the meanwhile.
# This is especially important for repositories that have
# FORCE_PUSH_ON_UPDATE set to true, such as the linux kernel. The file
# WORKDIR/merge-commit-outdated will be created if that is the case to let
# the caller know this is the reason the push failed.
#
set -o pipefail
echo "Running: git rev-parse refs/heads/repo-HEAD-saved"
saved_ref=$(git rev-parse refs/heads/repo-HEAD-saved) ||
die "Failed to read local ref refs/heads/repo-HEAD-saved"
git_url="$DEFAULT_PACKAGE_GIT_URL"
if [[ -n "$FETCH_GIT_TOKEN" ]] && [[ "$git_url" == https://github.com/* ]]; then
git_url="${git_url/https:\/\//https:\/\/${FETCH_GIT_TOKEN}@}"
fi
echo "Running: git ls-remote $DEFAULT_PACKAGE_GIT_URL refs/heads/$DEFAULT_GIT_BRANCH"
remote_ref=$(git ls-remote "$git_url" "refs/heads/$DEFAULT_GIT_BRANCH" |
awk '{print $1}') ||
die "Failed to read remote ref refs/heads/$DEFAULT_GIT_BRANCH"
set +o pipefail
if [[ "$saved_ref" != "$remote_ref" ]]; then
touch "$WORKDIR/merge-commit-outdated"
die "Remote branch $DEFAULT_GIT_BRANCH was modified while merge" \
"testing was being performed. Previous hash: $saved_ref," \
"new hash: $remote_ref. Not pushing merge."
fi
force_push="${FORCE_PUSH_ON_UPDATE:-false}"
logmust push_to_remote "refs/heads/repo-HEAD" \
"refs/heads/$DEFAULT_GIT_BRANCH" "$force_push"
echo_success "Merge pushed successfully for package $PACKAGE to remote" \
"branch $DEFAULT_GIT_BRANCH"