Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/scripts/format.sh
4500 views
1
#!/usr/bin/env bash
2
# Code formatter - runs targeted formatters based on what changed from trunk.
3
# Usage: format.sh [--all] [--pre-commit] [--pre-push] [--lint]
4
# (default) Check all changes relative to trunk including uncommitted work
5
# --all Format everything, skip change detection (previous behavior)
6
# --pre-commit Only check staged changes
7
# --pre-push Only check committed changes relative to trunk
8
# --lint Also run linters before formatting
9
set -eufo pipefail
10
11
run_lint=false
12
format_all=false
13
mode="default"
14
for arg in "$@"; do
15
case "$arg" in
16
--lint) run_lint=true ;;
17
--all) format_all=true ;;
18
19
--pre-commit|--pre-push)
20
[[ "$mode" == "default" ]] || { echo "Cannot use both --pre-commit and --pre-push" >&2; exit 1; }
21
mode="${arg#--}"
22
;;
23
*)
24
echo "Unknown option: $arg" >&2
25
echo "Usage: $0 [--all] [--pre-commit] [--pre-push] [--lint]" >&2
26
exit 1
27
;;
28
esac
29
done
30
31
section() {
32
echo "- $*" >&2
33
}
34
35
# Find what's changed compared to trunk (skip if --all)
36
trunk_ref="$(git rev-parse --verify trunk 2>/dev/null || echo "")"
37
38
if [[ "$format_all" == "false" && -n "$trunk_ref" ]]; then
39
base="$(git merge-base HEAD "$trunk_ref" 2>/dev/null || echo "")"
40
if [[ -n "$base" ]]; then
41
case "$mode" in
42
pre-commit)
43
changed="$(git diff --name-only --cached)"
44
;;
45
pre-push)
46
changed="$(git diff --name-only "$base" HEAD)"
47
;;
48
default)
49
committed="$(git diff --name-only "$base" HEAD)"
50
staged="$(git diff --name-only --cached)"
51
unstaged="$(git diff --name-only)"
52
untracked="$(git ls-files --others --exclude-standard)"
53
changed="$(printf '%s\n%s\n%s\n%s' "$committed" "$staged" "$unstaged" "$untracked" | sort -u)"
54
;;
55
esac
56
else
57
format_all=true
58
fi
59
elif [[ "$format_all" == "false" ]]; then
60
# No trunk ref found, format everything
61
format_all=true
62
fi
63
64
# Helper to check if a pattern matches changed files
65
changed_matches() {
66
[[ "$format_all" == "true" ]] || echo "$changed" | grep -qE "$1"
67
}
68
69
WORKSPACE_ROOT="$(bazel info workspace)"
70
71
# Capture baseline to detect formatter-introduced changes (allows pre-existing uncommitted work)
72
baseline="$(git status --porcelain)"
73
74
# Always run buildifier and copyright
75
section "Buildifier"
76
echo " buildifier" >&2
77
bazel run //:buildifier
78
79
section "Copyright"
80
echo " update_copyright" >&2
81
bazel run //scripts:update_copyright
82
83
# Run language formatters only if those files changed
84
if changed_matches '^java/'; then
85
section "Java"
86
echo " google-java-format" >&2
87
GOOGLE_JAVA_FORMAT="$(bazel run --run_under=echo //scripts:google-java-format)"
88
find "${WORKSPACE_ROOT}/java" -type f -name '*.java' -exec "$GOOGLE_JAVA_FORMAT" --replace {} +
89
fi
90
91
if changed_matches '^javascript/selenium-webdriver/'; then
92
section "JavaScript"
93
echo " prettier" >&2
94
NODE_WEBDRIVER="${WORKSPACE_ROOT}/javascript/selenium-webdriver"
95
bazel run //javascript:prettier -- "${NODE_WEBDRIVER}" --write "${NODE_WEBDRIVER}/.prettierrc" --log-level=warn
96
fi
97
98
if changed_matches '^rb/|^rake_tasks/|^Rakefile'; then
99
section "Ruby"
100
echo " rubocop -a" >&2
101
if [[ "$run_lint" == "true" ]]; then
102
bazel run //rb:rubocop -- -a
103
else
104
bazel run //rb:rubocop -- -a --fail-level F
105
fi
106
fi
107
108
if changed_matches '^rust/'; then
109
section "Rust"
110
echo " rustfmt" >&2
111
bazel run @rules_rust//:rustfmt
112
fi
113
114
if changed_matches '^py/'; then
115
section "Python"
116
if [[ "$run_lint" == "true" ]]; then
117
echo " ruff check" >&2
118
bazel run //py:ruff-check
119
fi
120
echo " ruff format" >&2
121
bazel run //py:ruff-format
122
fi
123
124
if changed_matches '^dotnet/'; then
125
section ".NET"
126
echo " dotnet format" >&2
127
bazel run //dotnet:format -- style --severity warn
128
bazel run //dotnet:format -- whitespace
129
fi
130
131
# Run shellcheck and actionlint when --lint is passed
132
if [[ "$run_lint" == "true" ]]; then
133
section "Shell/Actions"
134
echo " actionlint (with shellcheck)" >&2
135
SHELLCHECK="$(bazel run --run_under=echo @multitool//tools/shellcheck)"
136
bazel run @multitool//tools/actionlint:cwd -- -shellcheck "$SHELLCHECK"
137
fi
138
139
# Check if formatting introduced new changes (comparing to baseline)
140
if [[ "$(git status --porcelain)" != "$baseline" ]]; then
141
echo "" >&2
142
echo "Formatters modified files:" >&2
143
git diff --name-only >&2
144
exit 1
145
fi
146
147
echo "Format check passed." >&2
148
149