Skip to content

Commit 0c83b88

Browse files
committed
Check with open-java-format 2.98.0.2, and on Windows too
open-java-format 2.98.0.2 has a single style, so the action and the pre-commit hook stop passing --ojf, which that version only warns about, and both default to 2.98.0.2. A workflow that still pins 2.98.0.1 would now check in Google style, so this goes out as v2, and such a workflow stays on v1. 2.98.0.2 also ships a native binary for Windows on x86-64. The action and the hook recognise a Windows runner or Git Bash by uname, download the binary with its .exe extension, and keep that extension on disk. The CI used to run the action on a repository without Java files, so it checked nothing. It now writes a formatted file and one that is not, and expects the action to pass the first and fail the second, on Linux and macOS on both architectures and on Windows; the hook runs through the same two cases in a scratch repository.
1 parent c92d916 commit 0c83b88

5 files changed

Lines changed: 95 additions & 31 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,57 @@ jobs:
1414
fetch-depth: 0
1515
- uses: ./
1616
with:
17-
version: '2.98.0.1'
17+
version: '2.98.0.2'
1818
mode: changed
1919

20-
test-all:
21-
name: Test mode=all
22-
runs-on: ubuntu-latest
20+
# The action and the hook on every platform they support, with a file that is formatted and one that
21+
# is not. The repository itself has no Java files, so the tests write their own.
22+
test-platforms:
23+
name: Test on ${{ matrix.os }}
24+
runs-on: ${{ matrix.os }}
25+
strategy:
26+
fail-fast: false
27+
matrix:
28+
os: [ubuntu-latest, ubuntu-24.04-arm, macos-latest, macos-15-intel, windows-latest]
29+
defaults:
30+
run:
31+
shell: bash
2332
steps:
2433
- uses: actions/checkout@v7
25-
- uses: ./
34+
35+
- name: Write a formatted file
36+
run: printf 'class Formatted {\n int x;\n}\n' > Formatted.java
37+
38+
- name: Check it
39+
uses: ./
40+
with:
41+
version: '2.98.0.2'
42+
mode: all
43+
44+
- name: Write a file that is not formatted
45+
run: printf 'class Unformatted{int x;}\n' > Unformatted.java
46+
47+
- name: Check it, expecting a failure
48+
id: unformatted
49+
continue-on-error: true
50+
uses: ./
2651
with:
27-
version: '2.98.0.1'
52+
version: '2.98.0.2'
2853
mode: all
54+
55+
- name: Make sure the check failed
56+
run: test "${{ steps.unformatted.outcome }}" = failure
57+
58+
- name: Run the pre-commit hook on a file that is not formatted, then on a formatted one
59+
run: |
60+
git init -q hook-test
61+
cd hook-test
62+
printf 'class Hook{int x;}\n' > Hook.java
63+
git add Hook.java
64+
if bash ../pre-commit; then
65+
echo "::error::The hook accepted a file that is not formatted"
66+
exit 1
67+
fi
68+
printf 'class Hook {\n int x;\n}\n' > Hook.java
69+
git add Hook.java
70+
bash ../pre-commit

‎README.md‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@ No Java, Maven, or Gradle required — downloads a native binary from Maven Cent
99
```yaml
1010
- uses: actions/checkout@v7
1111

12-
- uses: openjavaformat/open-java-format-action@v1
12+
- uses: openjavaformat/open-java-format-action@v2
1313
with:
14-
version: '2.98.0.1'
14+
version: '2.98.0.2'
1515
```
1616
17+
v2 is for open-java-format 2.98.0.2 and later, which has a single style. A workflow that pins 2.98.0.1 stays on `@v1`, which passes that version the `--ojf` flag it needs.
18+
1719
## Inputs
1820

1921
| Input | Required | Default | Description |
2022
|-------|----------|---------|-------------|
21-
| `version` | yes | `2.98.0.1` | Version of the open-java-format native binary, one of those on [Maven Central](https://repo1.maven.org/maven2/dev/openjavaformat/open-java-format-native/) |
23+
| `version` | yes | `2.98.0.2` | Version of the open-java-format native binary, one of those on [Maven Central](https://repo1.maven.org/maven2/dev/openjavaformat/open-java-format-native/) |
2224
| `mode` | no | `changed` | `changed` — only files from PR or push; `all` — every `.java` file in repo |
2325

2426
## How `mode: changed` works
@@ -32,12 +34,12 @@ No Java, Maven, or Gradle required — downloads a native binary from Maven Cent
3234
- If all files are formatted correctly → passes ✅
3335
- If any file has formatting issues → lists unformatted files and fails ❌
3436

35-
Files are checked in the open-java-format style, `--ojf` (formerly `--palantir`). When the check fails, fix locally with a native binary or the runnable jar from the [open-java-format releases](https://github.com/openjavaformat/open-java-format/releases):
37+
Files are checked in the open-java-format style, the formatter's only style. When the check fails, fix locally with a native binary or the runnable jar from the [open-java-format releases](https://github.com/openjavaformat/open-java-format/releases):
3638

3739
```bash
38-
open-java-format --ojf --replace <files>
40+
open-java-format --replace <files>
3941
# or, with Java 21 or later
40-
java -jar open-java-format-2.98.0.1-all.jar --ojf --replace <files>
42+
java -jar open-java-format-2.98.0.2-all.jar --replace <files>
4143
```
4244

4345
## Excluding files
@@ -78,9 +80,9 @@ jobs:
7880
steps:
7981
- uses: actions/checkout@v7
8082
81-
- uses: openjavaformat/open-java-format-action@v1
83+
- uses: openjavaformat/open-java-format-action@v2
8284
with:
83-
version: '2.98.0.1'
85+
version: '2.98.0.2'
8486
mode: ${{ github.event_name == 'push' && 'all' || 'changed' }}
8587
```
8688

@@ -107,5 +109,6 @@ The hook pins its own formatter version in `FORMATTER_VERSION` at the top of the
107109
- Linux aarch64 (glibc)
108110
- macOS aarch64 (Apple Silicon)
109111
- macOS x86_64 (Intel)
112+
- Windows x86_64
110113

111-
There is no native binary for Windows or for musl-based Linux such as Alpine.
114+
There is no native binary for Windows on ARM or for musl-based Linux such as Alpine.

‎action.yml‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ inputs:
1010
version:
1111
description: 'Version of open-java-format to use'
1212
required: true
13-
default: '2.98.0.1'
13+
default: '2.98.0.2'
1414
mode:
1515
description: 'Which files to check: "changed" for files from PR/push, "all" for every .java file'
1616
required: false
@@ -31,13 +31,25 @@ runs:
3131
Linux-aarch64) SUFFIX="linux-glibc_aarch64" ;;
3232
Darwin-arm64) SUFFIX="macos_aarch64" ;;
3333
Darwin-x86_64) SUFFIX="macos_x86-64" ;;
34+
MINGW*-x86_64 | MSYS*-x86_64) SUFFIX="windows_x86-64" ;;
3435
*)
35-
echo "::error::No open-java-format native binary for ${OS}-${ARCH}. Supported: Linux x86_64, Linux aarch64, macOS aarch64, macOS x86_64."
36+
echo "::error::No open-java-format native binary for ${OS}-${ARCH}. Supported: Linux x86_64, Linux aarch64, macOS aarch64, macOS x86_64, Windows x86_64."
3637
exit 1
3738
;;
3839
esac
3940
41+
# The Windows binary is an .exe, on Maven Central and on disk. It exists from 2.98.0.2 on.
42+
if [ "$SUFFIX" = "windows_x86-64" ]; then
43+
EXTENSION="exe"
44+
BINARY="open-java-format.exe"
45+
else
46+
EXTENSION="bin"
47+
BINARY="open-java-format"
48+
fi
49+
4050
echo "suffix=$SUFFIX" >> "$GITHUB_OUTPUT"
51+
echo "extension=$EXTENSION" >> "$GITHUB_OUTPUT"
52+
echo "binary=$BINARY" >> "$GITHUB_OUTPUT"
4153
4254
- name: Check for newer open-java-format version
4355
shell: bash
@@ -68,7 +80,7 @@ runs:
6880
id: cache
6981
uses: actions/cache@v6
7082
with:
71-
path: ${{ github.action_path }}/open-java-format
83+
path: ${{ github.action_path }}/${{ steps.platform.outputs.binary }}
7284
key: open-java-format-${{ inputs.version }}-${{ steps.platform.outputs.suffix }}
7385

7486
- name: Download open-java-format native binary
@@ -78,9 +90,9 @@ runs:
7890
VERSION="${{ inputs.version }}"
7991
SUFFIX="${{ steps.platform.outputs.suffix }}"
8092
BASE_URL="https://repo1.maven.org/maven2/dev/openjavaformat/open-java-format-native/${VERSION}"
81-
ARTIFACT="open-java-format-native-${VERSION}-nativeImage-${SUFFIX}.bin"
93+
ARTIFACT="open-java-format-native-${VERSION}-nativeImage-${SUFFIX}.${{ steps.platform.outputs.extension }}"
8294
DOWNLOAD_URL="${BASE_URL}/${ARTIFACT}"
83-
BINARY="${{ github.action_path }}/open-java-format"
95+
BINARY="${{ github.action_path }}/${{ steps.platform.outputs.binary }}"
8496
8597
echo "Downloading open-java-format ${VERSION} (${SUFFIX})..."
8698
if ! curl -sSfL -o "$BINARY" "$DOWNLOAD_URL"; then
@@ -93,7 +105,7 @@ runs:
93105
94106
- name: Set binary path
95107
shell: bash
96-
run: echo "OJF_BINARY=${{ github.action_path }}/open-java-format" >> "$GITHUB_ENV"
108+
run: echo "OJF_BINARY=${{ github.action_path }}/${{ steps.platform.outputs.binary }}" >> "$GITHUB_ENV"
97109

98110
- name: Collect files to check
99111
shell: bash
@@ -159,7 +171,7 @@ runs:
159171
UNFORMATTED=()
160172
for FILE in $OJF_FILES; do
161173
if [ -f "$FILE" ]; then
162-
if ! "$OJF_BINARY" --ojf --dry-run --set-exit-if-changed "$FILE" > /dev/null 2>&1; then
174+
if ! "$OJF_BINARY" --dry-run --set-exit-if-changed "$FILE" > /dev/null 2>&1; then
163175
UNFORMATTED+=("$FILE")
164176
fi
165177
fi
@@ -174,7 +186,7 @@ runs:
174186
done
175187
echo ""
176188
echo "Run open-java-format locally to fix:"
177-
echo " open-java-format --ojf --replace ${UNFORMATTED[*]}"
189+
echo " open-java-format --replace ${UNFORMATTED[*]}"
178190
exit 1
179191
fi
180192

‎example-usage.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
- uses: actions/checkout@v7
1616

1717
- name: Check formatting
18-
uses: openjavaformat/open-java-format-action@v1
18+
uses: openjavaformat/open-java-format-action@v2
1919
with:
20-
version: '2.98.0.1'
20+
version: '2.98.0.2'
2121
mode: ${{ github.event_name == 'push' && 'all' || 'changed' }}

‎pre-commit‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
#!/usr/bin/env bash
22
set -euo pipefail
33

4-
FORMATTER_VERSION="2.98.0.1"
4+
FORMATTER_VERSION="2.98.0.2"
55
CACHE_DIR="${HOME}/.cache/open-java-format"
6-
BINARY="${CACHE_DIR}/open-java-format-${FORMATTER_VERSION}"
76
BASE_URL="https://repo1.maven.org/maven2/dev/openjavaformat/open-java-format-native/${FORMATTER_VERSION}"
87

8+
# The Windows binary is an .exe, on Maven Central and on disk.
9+
case "$(uname -s)" in
10+
MINGW* | MSYS* | CYGWIN*) EXTENSION="exe"; EXE=".exe" ;;
11+
*) EXTENSION="bin"; EXE="" ;;
12+
esac
13+
BINARY="${CACHE_DIR}/open-java-format-${FORMATTER_VERSION}${EXE}"
14+
915
# Read exclude patterns from .open-java-format-exclude (repo root), if present.
1016
# Each non-empty, non-comment line is a git pathspec whose matches are skipped.
1117
# This file is the single source of exclusions, shared with the GitHub Action.
@@ -41,14 +47,15 @@ if [ ! -x "$BINARY" ]; then
4147
Linux-aarch64) SUFFIX="linux-glibc_aarch64" ;;
4248
Darwin-arm64) SUFFIX="macos_aarch64" ;;
4349
Darwin-x86_64) SUFFIX="macos_x86-64" ;;
50+
MINGW*-x86_64 | MSYS*-x86_64 | CYGWIN*-x86_64) SUFFIX="windows_x86-64" ;;
4451
*)
4552
echo "ERROR: No open-java-format native binary for ${OS}-${ARCH}."
46-
echo " Supported: Linux x86_64, Linux aarch64, macOS aarch64, macOS x86_64."
53+
echo " Supported: Linux x86_64, Linux aarch64, macOS aarch64, macOS x86_64, Windows x86_64."
4754
exit 1
4855
;;
4956
esac
5057

51-
ARTIFACT="open-java-format-native-${FORMATTER_VERSION}-nativeImage-${SUFFIX}.bin"
58+
ARTIFACT="open-java-format-native-${FORMATTER_VERSION}-nativeImage-${SUFFIX}.${EXTENSION}"
5259
DOWNLOAD_URL="${BASE_URL}/${ARTIFACT}"
5360

5461
echo "Downloading open-java-format ${FORMATTER_VERSION} (${SUFFIX})..."
@@ -68,7 +75,7 @@ fi
6875
UNFORMATTED=()
6976
for FILE in $STAGED_FILES; do
7077
if [ -f "$FILE" ]; then
71-
if ! "$BINARY" --ojf --dry-run --set-exit-if-changed "$FILE" > /dev/null 2>&1; then
78+
if ! "$BINARY" --dry-run --set-exit-if-changed "$FILE" > /dev/null 2>&1; then
7279
UNFORMATTED+=("$FILE")
7380
fi
7481
fi
@@ -84,7 +91,7 @@ if [ ${#UNFORMATTED[@]} -gt 0 ]; then
8491
echo ""
8592
echo "Run the following command to fix:"
8693
echo ""
87-
echo " $BINARY --ojf --replace ${UNFORMATTED[*]}"
94+
echo " $BINARY --replace ${UNFORMATTED[*]}"
8895
echo ""
8996
exit 1
9097
fi

0 commit comments

Comments
 (0)