-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·97 lines (84 loc) · 3.06 KB
/
Copy pathpre-commit
File metadata and controls
executable file
·97 lines (84 loc) · 3.06 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
#!/usr/bin/env bash
set -euo pipefail
FORMATTER_VERSION="2.98.0.4"
CACHE_DIR="${HOME}/.cache/open-java-format"
BASE_URL="https://repo1.maven.org/maven2/dev/openjavaformat/open-java-format-native/${FORMATTER_VERSION}"
# The Windows binary is an .exe, on Maven Central and on disk.
case "$(uname -s)" in
MINGW* | MSYS* | CYGWIN*) EXTENSION="exe"; EXE=".exe" ;;
*) EXTENSION="bin"; EXE="" ;;
esac
BINARY="${CACHE_DIR}/open-java-format-${FORMATTER_VERSION}${EXE}"
# Read exclude patterns from .open-java-format-exclude (repo root), if present.
# Each non-empty, non-comment line is a git pathspec whose matches are skipped.
# This file is the single source of exclusions, shared with the GitHub Action.
EXCLUDES=()
EXCLUDE_FILE=".open-java-format-exclude"
if [ -f "$EXCLUDE_FILE" ]; then
while IFS= read -r line || [ -n "$line" ]; do
line="${line#"${line%%[![:space:]]*}"}"
line="${line%"${line##*[![:space:]]}"}"
case "$line" in ''|\#*) continue ;; esac
EXCLUDES+=(":(exclude)$line")
done < "$EXCLUDE_FILE"
fi
# Collect staged Java files (respecting excludes)
if [ ${#EXCLUDES[@]} -gt 0 ]; then
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACMR -- '*.java' "${EXCLUDES[@]}" || true)
else
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACMR -- '*.java' || true)
fi
if [ -z "$STAGED_FILES" ]; then
exit 0
fi
# Download native binary if not cached
if [ ! -x "$BINARY" ]; then
OS=$(uname -s)
ARCH=$(uname -m)
case "${OS}-${ARCH}" in
Linux-x86_64) SUFFIX="linux-glibc_x86-64" ;;
Linux-aarch64) SUFFIX="linux-glibc_aarch64" ;;
Darwin-arm64) SUFFIX="macos_aarch64" ;;
Darwin-x86_64) SUFFIX="macos_x86-64" ;;
MINGW*-x86_64 | MSYS*-x86_64 | CYGWIN*-x86_64) SUFFIX="windows_x86-64" ;;
*)
echo "ERROR: No open-java-format native binary for ${OS}-${ARCH}."
echo " Supported: Linux x86_64, Linux aarch64, macOS aarch64, macOS x86_64, Windows x86_64."
exit 1
;;
esac
ARTIFACT="open-java-format-native-${FORMATTER_VERSION}-nativeImage-${SUFFIX}.${EXTENSION}"
DOWNLOAD_URL="${BASE_URL}/${ARTIFACT}"
echo "Downloading open-java-format ${FORMATTER_VERSION} (${SUFFIX})..."
mkdir -p "$CACHE_DIR"
if ! curl -sSfL -o "$BINARY" "$DOWNLOAD_URL"; then
rm -f "$BINARY"
echo "ERROR: Failed to download open-java-format from:"
echo " $DOWNLOAD_URL"
exit 1
fi
chmod +x "$BINARY"
fi
# Check formatting
UNFORMATTED=()
for FILE in $STAGED_FILES; do
if [ -f "$FILE" ]; then
if ! "$BINARY" --dry-run --set-exit-if-changed "$FILE" > /dev/null 2>&1; then
UNFORMATTED+=("$FILE")
fi
fi
done
if [ ${#UNFORMATTED[@]} -gt 0 ]; then
echo ""
echo "ERROR: The following files are not formatted with open-java-format:"
echo ""
for FILE in "${UNFORMATTED[@]}"; do
echo " $FILE"
done
echo ""
echo "Run the following command to fix:"
echo ""
echo " $BINARY --replace ${UNFORMATTED[*]}"
echo ""
exit 1
fi