Skip to content

Commit

Permalink
Merge branch 'main' into jc/p4est_parabolic_BCs
Browse files Browse the repository at this point in the history
  • Loading branch information
jlchan authored Jun 19, 2023
2 parents e35c77d + 9ea37cc commit 2f9d13e
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
11 changes: 11 additions & 0 deletions docs/src/styleguide.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,14 @@ utils/trixi-format.jl
```
You can get more information about using the convenience script by running it with the
`--help`/`-h` flag.
### Checking formatting before committing
It can be convenient to check the formatting of source code automatically before each commit.
We use git-hooks for it and provide a `pre-commit` script in the `utils` folder. The script uses
[JuliaFormatter.jl](https://github.com/domluna/JuliaFormatter.jl) just like formatting script that
runs over the whole Trixi.jl directory.
You can copy the `pre-commit`-script into `.git/hooks/pre-commit` and it will check your formatting
before each commit. If errors are found the commit is aborted and you can add the corrections via
```shell
git add -p
```
40 changes: 40 additions & 0 deletions utils/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/sh

# Copy this file into .git/hooks/pre-commit to execute before each commit.
# It checks and corrects the format for each file.
# If incorrect formatting is found you can add the correction via git add -p

echo "Checking format before committing"

if git ref-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=280fc57fade28e35046c3e884e587ffef05d3867
fi

# Redirect output to stderr.
exec 1>&2

# Create a list of files to format.
files=()

for file in `git diff --cached --name-only`
do
# only indent existing files, this is necessary since if we rename or delete
# a file it is added to the committed files and we thus would try to indent a
# nonexisting file.
if [ ! -e $file ]
then
continue
fi
# We only indent .jl files
FILE_ENDING="${file##*.}"
if [ $FILE_ENDING = "jl" ]
then
files+=($file)
fi
done

julia utils/trixi-format-file.jl "${files[@]}"
42 changes: 42 additions & 0 deletions utils/trixi-format-file.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env julia

using Pkg
Pkg.activate(; temp = true, io = devnull)
Pkg.add("JuliaFormatter"; preserve = PRESERVE_ALL, io = devnull)

using JuliaFormatter: format_file

function main()
# Show help
if "-h" in ARGS || "--help" in ARGS
println("usage: trixi-format.jl PATH [PATH...]")
println()
println("positional arguments:")
println()
println(" PATH One or more paths (directories or files) to format. Default: '.'")
return nothing
end

file_list = ARGS
if isempty(ARGS)
exit(0)
end
non_formatted_files = Vector{String}()
for file in file_list
println("Checking file " * file)
if !format_file(file)
push!(non_formatted_files, file)
end
end
if isempty(non_formatted_files)
exit(0)
else
@error "Some files have not been formatted! Formatting has been applied, run 'git add -p' to update changes."
for file in non_formatted_files
println(file)
end
exit(1)
end
end

main()

0 comments on commit 2f9d13e

Please sign in to comment.