-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmerge
More file actions
executable file
·162 lines (140 loc) · 4.34 KB
/
merge
File metadata and controls
executable file
·162 lines (140 loc) · 4.34 KB
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
159
160
161
162
#!/bin/bash
command_v() {
command -v "$1" >/dev/null 2>/dev/null
}
add_app() {
if ! command_v "$1"; then
apps="$apps $@"
fi
}
install_tools() {
if [ -n "$apps" ]; then
export DEBIAN_FRONTEND=noninteractive
$SUDO apt-get -qq update &&
$SUDO apt-get -qq install --no-install-recommends -y $apps >/dev/null 2>/dev/null
echo "Installed: $apps" >&2
apps=
fi
}
install_gh() {
if [ "$(id -u)" != 0 ]; then
SUDO=sudo
fi
add_app curl
install_tools
if command_v apt-get &&
! apt-cache policy gh |
grep -q Candidate:; then
curl -f -s -S -L https://cli.github.com/packages/githubcli-archive-keyring.gpg |
$SUDO dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg 2> /dev/null
$SUDO chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" |
$SUDO tee /etc/apt/sources.list.d/github-cli.list > /dev/null
fi
add_app gh
install_tools
}
group_begin() {
echo "::group ::$1"
}
group_end() {
echo '::endgroup::'
}
wrap_in_backticks() {
b=$(
perl -e 'my $longest = 2;
while (<>) {
while (/(`+)/g) {
my $length = length $1;
$longest = $length if $length > $longest;
}
}
my $q = q<`>x ($longest + 1);
print $q;
' "$1"
)
echo "$b"
cat "$1"
echo "$b"
}
report_failure() {
echo "MERGE_FAILED=1" >> "$GITHUB_ENV"
echo 'status=failed' >> "$GITHUB_OUTPUT"
echo "message=$1" >> "$GITHUB_OUTPUT"
if [ -z "$INPUT_DO_NOT_REPORT" ]; then
echo "::error ::$1"
if [ -f "$GITHUB_STEP_SUMMARY" ]; then
(
echo "# checkout-merge failed
:x: $1"
if [ -s "$log" ]; then
echo
wrap_in_backticks "$log"
fi
) >> "$GITHUB_STEP_SUMMARY"
fi
fi
exit 0
}
maybe_debug() {
if [ "$GITHUB_RUN_ATTEMPT" != 1 ] || [ -n "$ACTIONS_STEP_DEBUG" ]; then
set -x
fi
}
maybe_debug
command_v gh || install_gh
log=$(mktemp)
merge_log=$(mktemp)
cd "$INPUT_PATH" || report_failure "Could not change to input_path ($INPUT_PATH)"
SENDER=$(jq -r .sender.login "$GITHUB_EVENT_PATH")
USER_JSON=$(mktemp)
(
gh api "/users/$SENDER" ||
curl -s "$GITHUB_API_URL/users/$SENDER" ||
echo '{"id": 0, "login": "unknown"}'
) > "$USER_JSON"
export GIT_AUTHOR_NAME=$(jq -r .name "$USER_JSON")
export GIT_AUTHOR_EMAIL=$(jq -r '.email // empty' "$USER_JSON")
if [ -z "$GIT_AUTHOR_NAME" ]; then
GIT_AUTHOR_NAME="$GITHUB_ACTOR"
fi
if [ -z "$GIT_AUTHOR_EMAIL" ]; then
GIT_AUTHOR_EMAIL=$(jq -r '.pusher.email // empty' "$GITHUB_EVENT_PATH")
if [ -z "$GIT_AUTHOR_EMAIL" ]; then
GIT_AUTHOR_EMAIL=$(jq -r '((.id|tostring + "+") + .login + "@users.noreply.github.com")' "$USER_JSON")
fi
fi
export GIT_COMMITTER_NAME=GitHub
export [email protected]
if [ "$(git rev-parse --is-shallow-repository)" = 'true' ]; then
UNSHALLOW=--unshallow
fi
contact_support() {
echo "Please contact support for ${GITHUB_ACTION_REPOSITORY:-$GITHUB_REPOSITORY}"
}
group_begin Fetching
git fetch $UNSHALLOW origin "$INPUT_BASE_REF" ||
report_failure "Can't get history for base_ref ($INPUT_BASE_REF). $(contact_support)"
GITHUB_BASE_SHA=$(git rev-parse FETCH_HEAD)
git -c advice.detachedHead=false checkout "$GITHUB_BASE_SHA" || {
git status
report_failure "Couldn't check out base_ref ($INPUT_BASE_REF); repository is probably dirty."
}
git fetch origin "$INPUT_HEAD_REF" ||
report_failure "Can't get head_ref ($INPUT_HEAD_REF). $(contact_support)"
GITHUB_HEAD_SHA=$(git rev-parse FETCH_HEAD)
group_end
group_begin Merging
if !(git merge -m "Merge $GITHUB_HEAD_SHA into $GITHUB_BASE_SHA" FETCH_HEAD 2>&1) > "$merge_log"; then
cat "$merge_log"
group_end
if grep '^CONFLICT ' "$merge_log" > "$log"; then
report_failure "Can't generate merge; there's a conflict. Resolve it to get workflow feedback."
else
cp "$merge_log" "$log"
report_failure "Can't generate merge; there's might be a conflict. Resolve it to get workflow feedback."
fi
fi
cat "$merge_log"
group_end
echo 'status=success' >> "$GITHUB_OUTPUT"