-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbazelw
More file actions
executable file
·330 lines (306 loc) · 13.3 KB
/
Copy pathbazelw
File metadata and controls
executable file
·330 lines (306 loc) · 13.3 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/usr/bin/env bash
# Unless explicitly stated otherwise all files in this repository are licensed under
# the Apache 2.0 License.
#
# This product includes software developed at Datadog
# (https://www.datadoghq.com/) Copyright 2025-Present Datadog, Inc.
set -euo pipefail
# Compute a salt value so the repository rule re-executes when it changes.
# If FETCH_SALT_TTL is set to a positive integer, the salt will change
# every TTL seconds (time-bucketed). Otherwise it will change every run.
now_ts="$(date +%s)"
ttl_seconds="${FETCH_SALT_TTL:-0}"
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
bazelversion_file="${script_dir}/.bazelversion"
wanted_version=""
if [[ -f "${bazelversion_file}" ]]; then
wanted_version="$(tr -d '[:space:]' < "${bazelversion_file}")"
fi
if [[ -z "${wanted_version}" ]]; then
echo "error: unable to read expected Bazel version from ${bazelversion_file}" >&2
exit 127
fi
extract_semver() {
local raw="${1:-}"
if [[ "${raw}" =~ ([0-9]+\.[0-9]+\.[0-9]+) ]]; then
printf '%s' "${BASH_REMATCH[1]}"
return 0
fi
return 1
}
detect_system_bazel_version() {
local raw=""
local parsed=""
raw="$(bazel --version 2>/dev/null || true)"
parsed="$(extract_semver "${raw}" || true)"
if [[ -z "${parsed}" ]]; then
raw="$(bazel version 2>/dev/null | awk -F': ' '/Build label:/ { print $2; exit }')"
parsed="$(extract_semver "${raw}" || true)"
fi
printf '%s' "${parsed}"
}
if [[ "${ttl_seconds}" =~ ^[0-9]+$ ]] && [[ "${ttl_seconds}" -gt 0 ]]; then
salt=$(( now_ts / ttl_seconds ))
else
salt="${now_ts}"
fi
if command -v bazelisk >/dev/null 2>&1; then
bazel_cmd="bazelisk"
elif command -v bazel >/dev/null 2>&1; then
system_bazel_version="$(detect_system_bazel_version)"
if [[ "${BAZELW_ALLOW_SYSTEM_BAZEL:-0}" == "1" ]]; then
echo "warning: bazelisk not found; using system bazel due to BAZELW_ALLOW_SYSTEM_BAZEL=1" >&2
bazel_cmd="bazel"
elif [[ -z "${system_bazel_version}" ]]; then
echo "error: bazelisk is required to enforce .bazelversion (unable to detect system bazel version)." >&2
exit 127
elif [[ "$system_bazel_version" == "$wanted_version" ]]; then
echo "warning: bazelisk not found; using system bazel version $system_bazel_version (matches .bazelversion)" >&2
bazel_cmd="bazel"
else
echo "error: bazelisk is required to enforce .bazelversion (wanted $wanted_version, found system bazel $system_bazel_version)." >&2
exit 127
fi
else
echo "error: bazelisk is required to enforce .bazelversion (set BAZELW_ALLOW_SYSTEM_BAZEL=1 to override)." >&2
exit 127
fi
sanitize_repository_url() {
local raw="${1:-}"
if [[ -z "${raw}" ]]; then
printf '%s' ""
return 0
fi
# Strip URL userinfo when present (for example: https://token@host/repo.git).
if [[ "${raw}" =~ ^([A-Za-z][A-Za-z0-9+.-]*://)[^/@]+@(.*)$ ]]; then
printf '%s%s' "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}"
return 0
fi
printf '%s' "${raw}"
}
# Return success when a supported CI provider is already exposing metadata.
# In that mode the wrapper only fills gaps that provider env does not cover well.
has_supported_ci_provider() {
[[ -n "${APPVEYOR:-}" ]] || \
[[ -n "${TF_BUILD:-}" ]] || \
[[ -n "${BITBUCKET_COMMIT:-}" ]] || \
[[ -n "${BUDDY:-}" ]] || \
[[ -n "${BUILDKITE:-}" ]] || \
[[ -n "${CIRCLECI:-}" ]] || \
[[ -n "${GITHUB_SHA:-}" ]] || \
[[ -n "${GITLAB_CI:-}" ]] || \
[[ -n "${JENKINS_URL:-}" ]] || \
[[ -n "${TEAMCITY_VERSION:-}" ]] || \
[[ -n "${TRAVIS:-}" ]] || \
[[ -n "${BITRISE_BUILD_SLUG:-}" ]] || \
[[ -n "${CF_BUILD_ID:-}" ]] || \
[[ -n "${CODEBUILD_INITIATOR:-}" ]] || \
[[ -n "${DRONE:-}" ]]
}
# Read a single git pretty-format field and keep it on one line for --repo_env.
git_pretty_value() {
local format="${1:-}"
git log -1 --date=iso-strict --pretty="${format}" 2>/dev/null | tr -d '\r\n' || true
}
# Return the first exact tag pointing at HEAD, or empty when HEAD is untagged.
git_head_tag() {
git tag --points-at HEAD 2>/dev/null | sed -n '1p' | tr -d '\r\n' || true
}
# Split args into startup args (leading dashes) and the command
startup_args=()
cmd=""
rest_args=()
for arg in "$@"; do
if [[ -z "${cmd}" ]]; then
if [[ "${arg}" == --* ]]; then
startup_args+=("${arg}")
continue
else
cmd="${arg}"
continue
fi
else
rest_args+=("${arg}")
fi
done
# Default to help if no command provided
if [[ -z "${cmd}" ]]; then
cmd="help"
fi
# Build final command line safely without expanding empty arrays
cmdline=("${bazel_cmd}")
if (( ${#startup_args[@]} )); then
for a in "${startup_args[@]}"; do cmdline+=("${a}"); done
fi
cmdline+=("${cmd}")
if [[ "${cmd}" =~ ^(build|test|run|sync|fetch|query|cquery|aquery|coverage|info)$ ]]; then
# The wrapper has two modes:
# - CI-provider mode preserves richer provider-derived branch/PR metadata.
# - Local-git mode preserves the existing local branch/SHA autodetection behavior.
provider_mode=0
if has_supported_ci_provider; then
provider_mode=1
fi
# Compute git metadata best-effort; wrapper execution must not fail on git issues.
git_dirty=""
local_git_repo_url=""
local_git_branch=""
local_git_tag=""
local_git_commit=""
local_git_commit_message=""
local_git_commit_author_name=""
local_git_commit_author_email=""
local_git_commit_author_date=""
local_git_commit_committer_name=""
local_git_commit_committer_email=""
local_git_commit_committer_date=""
if command -v git >/dev/null 2>&1; then
if git rev-parse --git-dir >/dev/null 2>&1; then
local_git_commit=$(git rev-parse HEAD 2>/dev/null || true)
local_git_commit_message="$(git_pretty_value '%s')"
local_git_commit_author_name="$(git_pretty_value '%an')"
local_git_commit_author_email="$(git_pretty_value '%ae')"
local_git_commit_author_date="$(git_pretty_value '%aI')"
local_git_commit_committer_name="$(git_pretty_value '%cn')"
local_git_commit_committer_email="$(git_pretty_value '%ce')"
local_git_commit_committer_date="$(git_pretty_value '%cI')"
local_git_tag="$(git_head_tag)"
if git diff-index --quiet HEAD -- 2>/dev/null; then
git_dirty="clean"
else
git_dirty="dirty"
fi
local_git_repo_url=$(git config --get remote.origin.url 2>/dev/null || true)
local_git_repo_url="$(sanitize_repository_url "${local_git_repo_url}")"
local_git_branch=$(git symbolic-ref --short -q HEAD 2>/dev/null || git rev-parse --abbrev-ref HEAD 2>/dev/null || true)
if [[ -z "${local_git_branch}" || "${local_git_branch}" == "HEAD" ]]; then
local_git_branch="auto:git-detached-head"
fi
fi
fi
cmdline+=("--repo_env=FETCH_SALT=${salt}")
# Explicit DD_GIT_* values always win over wrapper synthesis.
dd_git_repo_url="${DD_GIT_REPOSITORY_URL:-}"
dd_git_branch="${DD_GIT_BRANCH:-}"
dd_git_tag="${DD_GIT_TAG:-}"
dd_git_commit="${DD_GIT_COMMIT_SHA:-}"
dd_git_head_commit="${DD_GIT_HEAD_COMMIT:-}"
dd_git_commit_message="${DD_GIT_COMMIT_MESSAGE:-}"
dd_git_head_message="${DD_GIT_HEAD_MESSAGE:-}"
dd_git_commit_author_name="${DD_GIT_COMMIT_AUTHOR_NAME:-}"
dd_git_commit_author_email="${DD_GIT_COMMIT_AUTHOR_EMAIL:-}"
dd_git_commit_author_date="${DD_GIT_COMMIT_AUTHOR_DATE:-}"
dd_git_commit_committer_name="${DD_GIT_COMMIT_COMMITTER_NAME:-}"
dd_git_commit_committer_email="${DD_GIT_COMMIT_COMMITTER_EMAIL:-}"
dd_git_commit_committer_date="${DD_GIT_COMMIT_COMMITTER_DATE:-}"
dd_git_head_author_name="${DD_GIT_HEAD_AUTHOR_NAME:-}"
dd_git_head_author_email="${DD_GIT_HEAD_AUTHOR_EMAIL:-}"
dd_git_head_author_date="${DD_GIT_HEAD_AUTHOR_DATE:-}"
dd_git_head_committer_name="${DD_GIT_HEAD_COMMITTER_NAME:-}"
dd_git_head_committer_email="${DD_GIT_HEAD_COMMITTER_EMAIL:-}"
dd_git_head_committer_date="${DD_GIT_HEAD_COMMITTER_DATE:-}"
dd_git_pr_base_branch="${DD_GIT_PR_BASE_BRANCH:-}"
dd_git_pr_base_branch_sha="${DD_GIT_PR_BASE_BRANCH_SHA:-}"
dd_git_pr_base_branch_head_sha="${DD_GIT_PR_BASE_BRANCH_HEAD_SHA:-}"
dd_pr_number="${DD_PR_NUMBER:-}"
if [[ "${provider_mode}" -eq 1 ]]; then
if [[ -z "${dd_git_commit_message}" ]]; then dd_git_commit_message="${local_git_commit_message}"; fi
if [[ -z "${dd_git_commit_author_name}" ]]; then dd_git_commit_author_name="${local_git_commit_author_name}"; fi
if [[ -z "${dd_git_commit_author_email}" ]]; then dd_git_commit_author_email="${local_git_commit_author_email}"; fi
if [[ -z "${dd_git_commit_author_date}" ]]; then dd_git_commit_author_date="${local_git_commit_author_date}"; fi
if [[ -z "${dd_git_commit_committer_name}" ]]; then dd_git_commit_committer_name="${local_git_commit_committer_name}"; fi
if [[ -z "${dd_git_commit_committer_email}" ]]; then dd_git_commit_committer_email="${local_git_commit_committer_email}"; fi
if [[ -z "${dd_git_commit_committer_date}" ]]; then dd_git_commit_committer_date="${local_git_commit_committer_date}"; fi
else
if [[ -z "${dd_git_repo_url}" ]]; then dd_git_repo_url="${local_git_repo_url}"; fi
if [[ -z "${dd_git_branch}" ]]; then dd_git_branch="${local_git_branch}"; fi
if [[ -z "${dd_git_tag}" ]]; then dd_git_tag="${local_git_tag}"; fi
if [[ -z "${dd_git_commit}" ]]; then dd_git_commit="${local_git_commit}"; fi
if [[ -z "${dd_git_head_commit}" ]]; then dd_git_head_commit="${local_git_commit}"; fi
if [[ -z "${dd_git_commit_message}" ]]; then dd_git_commit_message="${local_git_commit_message}"; fi
if [[ -z "${dd_git_head_message}" ]]; then dd_git_head_message="${local_git_commit_message}"; fi
if [[ -z "${dd_git_commit_author_name}" ]]; then dd_git_commit_author_name="${local_git_commit_author_name}"; fi
if [[ -z "${dd_git_commit_author_email}" ]]; then dd_git_commit_author_email="${local_git_commit_author_email}"; fi
if [[ -z "${dd_git_commit_author_date}" ]]; then dd_git_commit_author_date="${local_git_commit_author_date}"; fi
if [[ -z "${dd_git_commit_committer_name}" ]]; then dd_git_commit_committer_name="${local_git_commit_committer_name}"; fi
if [[ -z "${dd_git_commit_committer_email}" ]]; then dd_git_commit_committer_email="${local_git_commit_committer_email}"; fi
if [[ -z "${dd_git_commit_committer_date}" ]]; then dd_git_commit_committer_date="${local_git_commit_committer_date}"; fi
fi
dd_git_repo_url="$(sanitize_repository_url "${dd_git_repo_url}")"
if [[ -n "${dd_git_commit}" ]]; then
cmdline+=("--repo_env=DD_GIT_COMMIT_SHA=${dd_git_commit}")
fi
if [[ -n "${git_dirty}" ]]; then
cmdline+=("--repo_env=GIT_DIRTY=${git_dirty}")
fi
if [[ -n "${dd_git_repo_url}" ]]; then
cmdline+=("--repo_env=DD_GIT_REPOSITORY_URL=${dd_git_repo_url}")
fi
if [[ -n "${dd_git_branch}" ]]; then
cmdline+=("--repo_env=DD_GIT_BRANCH=${dd_git_branch}")
fi
if [[ -n "${dd_git_tag}" ]]; then
cmdline+=("--repo_env=DD_GIT_TAG=${dd_git_tag}")
fi
if [[ -n "${dd_git_head_commit}" ]]; then
cmdline+=("--repo_env=DD_GIT_HEAD_COMMIT=${dd_git_head_commit}")
fi
if [[ -n "${dd_git_commit_message}" ]]; then
cmdline+=("--repo_env=DD_GIT_COMMIT_MESSAGE=${dd_git_commit_message}")
fi
if [[ -n "${dd_git_head_message}" ]]; then
cmdline+=("--repo_env=DD_GIT_HEAD_MESSAGE=${dd_git_head_message}")
fi
if [[ -n "${dd_git_commit_author_name}" ]]; then
cmdline+=("--repo_env=DD_GIT_COMMIT_AUTHOR_NAME=${dd_git_commit_author_name}")
fi
if [[ -n "${dd_git_commit_author_email}" ]]; then
cmdline+=("--repo_env=DD_GIT_COMMIT_AUTHOR_EMAIL=${dd_git_commit_author_email}")
fi
if [[ -n "${dd_git_commit_author_date}" ]]; then
cmdline+=("--repo_env=DD_GIT_COMMIT_AUTHOR_DATE=${dd_git_commit_author_date}")
fi
if [[ -n "${dd_git_commit_committer_name}" ]]; then
cmdline+=("--repo_env=DD_GIT_COMMIT_COMMITTER_NAME=${dd_git_commit_committer_name}")
fi
if [[ -n "${dd_git_commit_committer_email}" ]]; then
cmdline+=("--repo_env=DD_GIT_COMMIT_COMMITTER_EMAIL=${dd_git_commit_committer_email}")
fi
if [[ -n "${dd_git_commit_committer_date}" ]]; then
cmdline+=("--repo_env=DD_GIT_COMMIT_COMMITTER_DATE=${dd_git_commit_committer_date}")
fi
if [[ -n "${dd_git_head_author_name}" ]]; then
cmdline+=("--repo_env=DD_GIT_HEAD_AUTHOR_NAME=${dd_git_head_author_name}")
fi
if [[ -n "${dd_git_head_author_email}" ]]; then
cmdline+=("--repo_env=DD_GIT_HEAD_AUTHOR_EMAIL=${dd_git_head_author_email}")
fi
if [[ -n "${dd_git_head_author_date}" ]]; then
cmdline+=("--repo_env=DD_GIT_HEAD_AUTHOR_DATE=${dd_git_head_author_date}")
fi
if [[ -n "${dd_git_head_committer_name}" ]]; then
cmdline+=("--repo_env=DD_GIT_HEAD_COMMITTER_NAME=${dd_git_head_committer_name}")
fi
if [[ -n "${dd_git_head_committer_email}" ]]; then
cmdline+=("--repo_env=DD_GIT_HEAD_COMMITTER_EMAIL=${dd_git_head_committer_email}")
fi
if [[ -n "${dd_git_head_committer_date}" ]]; then
cmdline+=("--repo_env=DD_GIT_HEAD_COMMITTER_DATE=${dd_git_head_committer_date}")
fi
if [[ -n "${dd_git_pr_base_branch}" ]]; then
cmdline+=("--repo_env=DD_GIT_PR_BASE_BRANCH=${dd_git_pr_base_branch}")
fi
if [[ -n "${dd_git_pr_base_branch_sha}" ]]; then
cmdline+=("--repo_env=DD_GIT_PR_BASE_BRANCH_SHA=${dd_git_pr_base_branch_sha}")
fi
if [[ -n "${dd_git_pr_base_branch_head_sha}" ]]; then
cmdline+=("--repo_env=DD_GIT_PR_BASE_BRANCH_HEAD_SHA=${dd_git_pr_base_branch_head_sha}")
fi
if [[ -n "${dd_pr_number}" ]]; then
cmdline+=("--repo_env=DD_PR_NUMBER=${dd_pr_number}")
fi
fi
if (( ${#rest_args[@]} )); then
for a in "${rest_args[@]}"; do cmdline+=("${a}"); done
fi
exec "${cmdline[@]}"