From 9ea37cce8f510dcc135ad568280fd770a693ba10 Mon Sep 17 00:00:00 2001 From: David Knapp Date: Mon, 19 Jun 2023 16:16:48 +0200 Subject: [PATCH] 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 --- docs/src/styleguide.md | 11 ++++++++++ utils/pre-commit | 40 ++++++++++++++++++++++++++++++++++++ utils/trixi-format-file.jl | 42 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100755 utils/pre-commit create mode 100755 utils/trixi-format-file.jl diff --git a/docs/src/styleguide.md b/docs/src/styleguide.md index de367c086cc..9e6b6a8c265 100644 --- a/docs/src/styleguide.md +++ b/docs/src/styleguide.md @@ -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 +``` \ No newline at end of file diff --git a/utils/pre-commit b/utils/pre-commit new file mode 100755 index 00000000000..2977b9a200b --- /dev/null +++ b/utils/pre-commit @@ -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[@]}" diff --git a/utils/trixi-format-file.jl b/utils/trixi-format-file.jl new file mode 100755 index 00000000000..c4d8e7c9032 --- /dev/null +++ b/utils/trixi-format-file.jl @@ -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()