Repository navigation
chore: format Markdown with a repo-wide oxfmt config and pre-commit hook #350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+3,222
−1,285
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // Formats staged files with oxfmt and stages the result. Run by pre-commit, | ||
| // which picks the runtime (Node or bun) and passes the oxfmt launcher path. | ||
| // | ||
| // Paths come from git NUL-delimited and are passed on as argument arrays, | ||
| // never through a shell, so names with spaces, glob characters or non-ASCII | ||
| // characters reach oxfmt and git exactly as staged. | ||
| import { spawnSync } from "node:child_process"; | ||
|
|
||
| const oxfmt = process.argv[2]; | ||
|
|
||
| function git(...args) { | ||
| const result = spawnSync("git", ["--literal-pathspecs", ...args], { | ||
| encoding: "utf8", | ||
| }); | ||
| if (result.status !== 0) { | ||
| process.stderr.write(result.stderr); | ||
| process.exit(result.status ?? 1); | ||
| } | ||
| return result.stdout; | ||
| } | ||
|
|
||
| function paths(output) { | ||
| return output.split("\0").filter(Boolean); | ||
| } | ||
|
|
||
| // `./` keeps oxfmt from reading a name that starts with `-` as an option, or | ||
| // one that starts with `!` as an exclude pattern. | ||
| function runOxfmt(args, files, stdout) { | ||
| const result = spawnSync( | ||
| process.execPath, | ||
| [ | ||
| oxfmt, | ||
| "--no-error-on-unmatched-pattern", | ||
| ...args, | ||
| ...files.map((file) => `./${file}`), | ||
| ], | ||
| { stdio: ["ignore", stdout, "inherit"] }, | ||
| ); | ||
| return result.status ?? 1; | ||
| } | ||
|
|
||
| const staged = paths( | ||
| git("diff", "--cached", "--name-only", "-z", "--diff-filter=ACMR"), | ||
| ); | ||
|
|
||
| // A file with unstaged changes is only checked: formatting it and adding it | ||
| // again would also stage the changes that were deliberately left out. | ||
| const partial = new Set(paths(git("diff", "--name-only", "-z"))); | ||
| const toFormat = staged.filter((file) => !partial.has(file)); | ||
| const toCheck = staged.filter((file) => partial.has(file)); | ||
|
|
||
| // Never call oxfmt without files: with no paths it formats the whole directory. | ||
| if (toFormat.length > 0) { | ||
| if (runOxfmt([], toFormat, "ignore") !== 0) { | ||
| process.exit(1); | ||
| } | ||
| git("add", "--", ...toFormat); | ||
| } | ||
|
|
||
| if (toCheck.length > 0 && runOxfmt(["--check"], toCheck, "inherit") !== 0) { | ||
| console.error( | ||
| "pre-commit: the files above are partly staged and not formatted.", | ||
| ); | ||
| console.error( | ||
| "Run `bun run format` at the repo root, stage the result, and commit again.", | ||
| ); | ||
| process.exit(1); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| #!/bin/sh | ||
| # Formats staged files with oxfmt, using the repo-wide .oxfmtrc.json. | ||
| # Turned on by `bun install` or `npm install` at the repo root (the `prepare` | ||
| # script sets core.hooksPath), or by hand with `git config core.hooksPath .githooks`. | ||
| # Skip it once with `git commit --no-verify`. | ||
| set -eu | ||
|
|
||
| root=$(git rev-parse --show-toplevel) | ||
| oxfmt="$root/node_modules/.bin/oxfmt" | ||
| cd "$root" | ||
|
|
||
| # Nothing to format when the commit only deletes files. | ||
| if git diff --cached --quiet --diff-filter=ACMR; then | ||
| exit 0 | ||
| fi | ||
|
|
||
| # Formatting is a convenience here: the Format workflow enforces it on every | ||
| # PR. So a missing install never blocks a commit, which matters for worktrees: | ||
| # core.hooksPath is shared by all of them, but each needs its own install. | ||
| skip() { | ||
| echo "pre-commit: $1" >&2 | ||
| echo "pre-commit: committing without formatting; the Format check on the PR will catch it." >&2 | ||
| exit 0 | ||
| } | ||
|
|
||
| if [ ! -e "$oxfmt" ]; then | ||
| skip "oxfmt is not installed. Run \`bun install\` (or \`npm install\`) at the repo root." | ||
| fi | ||
|
|
||
| # The oxfmt launcher is a Node script; bun can run it too. | ||
| if command -v node >/dev/null 2>&1; then | ||
| runtime=node | ||
| elif command -v bun >/dev/null 2>&1; then | ||
| runtime=bun | ||
| else | ||
| skip "neither Node nor bun is on PATH, so oxfmt cannot run." | ||
| fi | ||
|
|
||
| # The file handling lives in JavaScript, which gets paths from git exactly | ||
| # and never passes them through a shell. | ||
| exec "$runtime" "$root/.githooks/format-staged.mjs" "$oxfmt" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| name: Format | ||
|
|
||
| on: | ||
| push: | ||
| branches: [main] | ||
| pull_request: | ||
|
|
||
| jobs: | ||
| check: | ||
| name: oxfmt | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 | ||
| - name: Setup Bun | ||
| uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0 | ||
| with: | ||
| bun-version: latest | ||
| - run: bun install --frozen-lockfile | ||
| - run: bun run format:check |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.