-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a pre-commit script and an adapted formatting file (#1534)
* Add a pre-commit script and an adapted formatting file * Formatting I am aware of the irony * Fix typo * Added documentation * Fix Typo
- Loading branch information
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,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[@]}" |
This file contains 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,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() |