-
Notifications
You must be signed in to change notification settings - Fork 82
Add jlfmt pre-commit hook #992
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
Changes from all commits
03434db
1b85957
88a13c2
b1684d1
9fe7ee4
27a4240
3b196af
8fada19
b47f18a
fe95591
e1f89f5
acc056c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| # NOTE: This file should remain unformatted (as per JuliaFormatter's default style) as it is | ||
| # used to test the pre-commit hooks. | ||
| function f(x ) | ||
| return x+1 | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,19 @@ | ||
| - id: julia-formatter | ||
| name: "Julia Formatter" | ||
| entry: "julia -e 'import JuliaFormatter: format; format(ARGS)'" | ||
| entry: "julia --threads=auto -e 'import JuliaFormatter: format; format(ARGS)'" | ||
| pass_filenames: true | ||
| always_run: false | ||
| types: [file] | ||
| files: \.(jl|[jq]?md)$ | ||
| files: \.(jl|md|jmd|qmd)$ | ||
| language: "system" | ||
| description: "An opinionated code formatter for Julia. Plot twist - the opinion is your own." | ||
|
|
||
| - id: jlfmt | ||
| name: "JuliaFormatter (Pkg app)" | ||
| entry: bin/jlfmt-hook.sh | ||
| pass_filenames: true | ||
| always_run: false | ||
| types: [file] | ||
| files: \.(jl|md|jmd|qmd)$ | ||
| language: "script" | ||
| description: "An opinionated code formatter for Julia, but now via a Pkg app. Plot twist - the opinion is your own." |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| name = "JuliaFormatter" | ||
| uuid = "98e50ef6-434e-11e9-1051-2b60c6c9e899" | ||
| version = "2.3.3" | ||
| version = "2.4.0" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my comment about package versioning vs hook versioning. Here I think the bump might be justified because of a docs change, but in general it depends whether you consider the hook to be "part of" the Julia package, which is debatable. For example, it won't be installed when people do
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, thanks for the suggestions! There are indeed a few good options to decouple semver of the package from semver of the hook. But also, I don't really envision that there would be much need to update the hooks much in the future, so my inclination right now is that it's not hugely important to sort it out, and we can continue to live in a regime where hook behaviour is in practice guaranteed by package semver but not officially documented. I think if there is a need to make a breaking change in the future then we can revisit this design?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (I think if I were to change this design, I would probably use the tag prefixes, that seems like the most elegant way to solve it. I'd also be interested in finding out how that interacts with things like Dependabot, but that'd be a minor detail.) |
||
| authors = ["Dominique Luna <dluna132@gmail.com> and contributors"] | ||
|
|
||
| [deps] | ||
|
|
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| # Wrapper for the jlfmt executable, which is used in the jlfmt pre-commit hook. | ||
| # | ||
| # Resolution order for jlfmt: | ||
| # | ||
| # 1. --jlfmt-path=<path> (explicit override) | ||
| # 2. jlfmt on PATH | ||
| # 3. $JULIA_DEPOT_PATH/bin (each entry, colon-separated) | ||
| # 4. ~/.julia/bin (default depot) | ||
| # | ||
| # Gracefully errors if jlfmt can't be found. | ||
|
|
||
| jlfmt="" | ||
|
|
||
| # Parse --jlfmt-path argument and pass the rest through. | ||
| args=() | ||
| for arg in "$@"; do | ||
| case "$arg" in | ||
| --jlfmt-path=*) jlfmt="${arg#--jlfmt-path=}" ;; | ||
| *) args+=("$arg") ;; | ||
| esac | ||
| done | ||
|
|
||
| if [ -n "$jlfmt" ]; then | ||
| # Path was explicitly specified. Expand tildes and check if it exists | ||
| jlfmt="${jlfmt/#\~/$HOME}" | ||
| if ! command -v "$jlfmt" &>/dev/null; then | ||
| echo "Error: The executable '$jlfmt' (passed via --jlfmt-path) was not found." | ||
| exit 1 | ||
| fi | ||
| else | ||
| # Not specified, we'll have to search for it ourselves. | ||
| # Step 1: Check PATH | ||
| if command -v jlfmt &>/dev/null; then | ||
| jlfmt="jlfmt" | ||
| # Step 2: Check JULIA_DEPOT_PATH | ||
| elif [ -n "$JULIA_DEPOT_PATH" ]; then | ||
| IFS=: read -ra depots <<< "$JULIA_DEPOT_PATH" | ||
| for depot in "${depots[@]}"; do | ||
| depot="${depot/#\~/$HOME}" | ||
| if [ -x "$depot/bin/jlfmt" ]; then | ||
| jlfmt="$depot/bin/jlfmt" | ||
| break | ||
| fi | ||
| done | ||
| fi | ||
| # Step 3: Check ~/.julia/bin (default fallback: cf. Julia's | ||
| # `Base.init_depot_path()` implementation). In principle, we would like to | ||
| # run `julia -e 'println(DEPOT_PATH)'`, but that takes too long, so is not | ||
| # a viable option for pre-commit. | ||
| if [ -z "$jlfmt" ] && [ -x "${HOME}/.julia/bin/jlfmt" ]; then | ||
| jlfmt="${HOME}/.julia/bin/jlfmt" | ||
| fi | ||
| # Error | ||
| if [ -z "$jlfmt" ]; then | ||
| echo "ERROR: 'jlfmt' not found." | ||
| echo "Install it with: julia -e 'import Pkg; Pkg.Apps.add(\"JuliaFormatter\")'" | ||
| echo "Then make sure that jlfmt is on your PATH, or tell pre-commit its location with:" | ||
| echo "" | ||
| echo " args: [\"--jlfmt-path=/path/to/jlfmt\"]" | ||
| echo "" | ||
| echo "See https://juliaeditorsupport.github.io/JuliaFormatter.jl/stable/integrations/ for details." | ||
| exit 1 | ||
| fi | ||
| fi | ||
|
|
||
| exec "$jlfmt" --threads=auto -- --inplace "${args[@]}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we ask for a backport on the issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I had actually meant to investigate this in more depth because it seems like it has been backported: JuliaLang/Pkg.jl#4493 I'm not sure why the CI runner doesn't pick it up.