Skip to content

Commit 2a1ad18

Browse files
Switch pre-commit hook to CLI (#959)
* Switch pre-commit to CLI * Exclude test folder * Format app.jl * Add pre-commit CI workflow * Fix path * Use explicit path inside CI * Test * More prints * Create environments folder * Create apps * CD * Read dir * Print * Remove CI temporarily * Absolute path * Alias * Export path * Instantiate * Update .github/workflows/PreCommit.yml Co-authored-by: Ian Butterworth <i.r.butterworth@gmail.com> * Update PreCommit.yml * Update PreCommit.yml * Update PreCommit.yml * Re-add ci * Add doc --------- Co-authored-by: Ian Butterworth <i.r.butterworth@gmail.com>
1 parent 7245e92 commit 2a1ad18

File tree

5 files changed

+84
-34
lines changed

5 files changed

+84
-34
lines changed

.github/workflows/PreCommit.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Pre-Commit
2+
3+
# needed to allow julia-actions/cache to delete old caches that it has created
4+
permissions:
5+
actions: write
6+
contents: read
7+
8+
on:
9+
push:
10+
branches:
11+
- master
12+
tags: ['*']
13+
pull_request:
14+
workflow_dispatch:
15+
16+
jobs:
17+
pre-commit:
18+
runs-on: ubuntu-latest
19+
steps:
20+
- uses: actions/checkout@v6
21+
- uses: julia-actions/setup-julia@v2
22+
- uses: julia-actions/cache@v2
23+
- run: mkdir ${HOME}/.julia/environments
24+
- run: mkdir ${HOME}/.julia/environments/apps
25+
- name: Install local JuliaFormatter as Pkg app
26+
run: julia --project -e 'using Pkg; Pkg.instantiate(); Pkg.Apps.develop(; path=pwd())'
27+
- uses: astral-sh/setup-uv@v7
28+
- run: uv tool install pre-commit
29+
- run: pre-commit install
30+
- run: |
31+
export PATH=$PATH:${HOME}/.julia/bin/
32+
pre-commit run --all-files --show-diff-on-failure --color always

.pre-commit-config.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,16 @@
33
repos:
44
- repo: local
55
hooks:
6-
- id: "julia-formatter"
6+
- id: julia-formatter
7+
name: "Julia Formatter"
8+
entry: "jlfmt --inplace"
9+
pass_filenames: true
10+
always_run: false
11+
types: [file]
12+
files: \.(jl|[jq]?md)$
13+
exclude: '^test/'
14+
language: "system"
15+
- repo: meta
16+
hooks:
17+
- id: check-hooks-apply
18+
- id: check-useless-excludes

.pre-commit-hooks.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
- id: julia-formatter
22
name: "Julia Formatter"
3-
entry: "julia -e 'import JuliaFormatter: format; format(ARGS)'"
3+
entry: "jlfmt --inplace"
44
pass_filenames: true
55
always_run: false
66
types: [file]

docs/src/integrations.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ repos:
1818
# ... other repos you may have
1919
```
2020

21+
Note that it requires the `jlfmt` Pkg app to be installed with
22+
23+
```julia
24+
pkg> app add JuliaFormatter
25+
```
26+
2127
You can find a list of releases [here](https://github.com/domluna/JuliaFormatter.jl/releases).
2228
**Be sure to use the entire version string!** (You can double-check this by opening the
2329
release and looking at the part of the URL that follows `.../releases/tag/VERSION`.)

src/app.jl

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ function scandir!(files, root)
3131
elseif (@tryx(isfile(jf), false) || @tryx(islink(jf), false))
3232
# Check for .jl, .md, .jmd, .qmd files
3333
if endswith(jf, ".jl") ||
34-
endswith(jf, ".md") ||
35-
endswith(jf, ".jmd") ||
36-
endswith(jf, ".qmd")
34+
endswith(jf, ".md") ||
35+
endswith(jf, ".jmd") ||
36+
endswith(jf, ".qmd")
3737
push!(files, jf)
3838
end
3939
end
@@ -45,10 +45,10 @@ function scandir!(files, root)
4545
end
4646

4747
function panic(
48-
msg::String,
49-
err::Union{Exception, Nothing} = nothing,
50-
bt::Union{Vector{Base.StackFrame}, Nothing} = nothing,
51-
)
48+
msg::String,
49+
err::Union{Exception,Nothing} = nothing,
50+
bt::Union{Vector{Base.StackFrame},Nothing} = nothing,
51+
)
5252
printstyled(stderr, "ERROR: "; color = :red, bold = true)
5353
print(stderr, msg)
5454
if err !== nothing
@@ -315,7 +315,7 @@ function main(argv::Vector{String})
315315
format_markdown = false
316316
config_priority = false
317317
style_name = "default"
318-
format_options = Dict{Symbol, Any}()
318+
format_options = Dict{Symbol,Any}()
319319
ignore_patterns = String[]
320320

321321
paths = String[]
@@ -355,7 +355,7 @@ function main(argv::Vector{String})
355355
if i >= length(argv)
356356
return panic("expected output file argument after `-o`")
357357
end
358-
outputfile = argv[i + 1]
358+
outputfile = argv[i+1]
359359
i += 2
360360
elseif startswith(x, "--output=")
361361
m = match(r"^--output=(.+)$", x)
@@ -562,21 +562,21 @@ function main(argv::Vector{String})
562562
nfiles_str = string(length(inputfiles))
563563
options_list = (
564564
ProcessFileArgs(
565-
inputfile,
566-
file_counter,
567-
nfiles_str,
568-
print_progress,
569-
check,
570-
inplace,
571-
outputfile,
572-
input_is_stdin,
573-
stdin_filename,
574-
config_dir,
575-
format_options,
576-
diff,
577-
format_markdown,
578-
config_priority,
579-
) for (file_counter, inputfile) in enumerate(inputfiles)
565+
inputfile,
566+
file_counter,
567+
nfiles_str,
568+
print_progress,
569+
check,
570+
inplace,
571+
outputfile,
572+
input_is_stdin,
573+
stdin_filename,
574+
config_dir,
575+
format_options,
576+
diff,
577+
format_markdown,
578+
config_priority,
579+
) for (file_counter, inputfile) in enumerate(inputfiles)
580580
)
581581

582582
# Use multithreading for multiple files (only if multiple threads available)
@@ -629,7 +629,7 @@ struct ProcessFileArgs
629629
input_is_stdin::Bool
630630
stdin_filename::String
631631
config_dir::String
632-
format_options::Dict{Symbol, Any}
632+
format_options::Dict{Symbol,Any}
633633
diff::Bool
634634
format_markdown::Bool
635635
config_priority::Bool
@@ -739,8 +739,8 @@ function process_file(args::ProcessFileArgs)
739739
if args.outputfile == "" || args.outputfile == "-"
740740
Output(:stdout, "", stdout, false, false)
741741
elseif isfile(args.outputfile) &&
742-
!args.input_is_stdin &&
743-
samefile(args.outputfile, args.inputfile)
742+
!args.input_is_stdin &&
743+
samefile(args.outputfile, args.inputfile)
744744
if args.print_progress
745745
@lock print_lock begin
746746
buf = IOBuffer()
@@ -764,7 +764,7 @@ function process_file(args::ProcessFileArgs)
764764
effective_options = if args.inputfile != "-"
765765
# Find and load .JuliaFormatter.toml config
766766
config_nt = find_config_file(args.inputfile)
767-
config_dict = Dict{Symbol, Any}(Symbol(k) => v for (k, v) in pairs(config_nt))
767+
config_dict = Dict{Symbol,Any}(Symbol(k) => v for (k, v) in pairs(config_nt))
768768
# Merge: by default, command line options override config file
769769
# If --prioritize-config-file is set, config file options override command line
770770
if args.config_priority
@@ -781,7 +781,7 @@ function process_file(args::ProcessFileArgs)
781781
# Try to find config file recursively from the directory
782782
find_config_file(args.config_dir)
783783
end
784-
config_dict = Dict{Symbol, Any}(Symbol(k) => v for (k, v) in pairs(config_nt))
784+
config_dict = Dict{Symbol,Any}(Symbol(k) => v for (k, v) in pairs(config_nt))
785785
if args.config_priority
786786
merge(args.format_options, config_dict)
787787
else
@@ -795,9 +795,9 @@ function process_file(args::ProcessFileArgs)
795795
if args.inputfile != "-"
796796
ignore_patterns = get(effective_options, :ignore, String[])
797797
if any(
798-
pattern -> occursin(Glob.FilenameMatch("*$pattern"), args.inputfile),
799-
ignore_patterns,
800-
)
798+
pattern -> occursin(Glob.FilenameMatch("*$pattern"), args.inputfile),
799+
ignore_patterns,
800+
)
801801
# Skip ignored files
802802
if args.print_progress
803803
@lock print_lock begin

0 commit comments

Comments
 (0)