Git
Integrate codee format
with git
to format only the changes made to your
Fortran code.
Setup
codee format
ships with a git-codee-format
script, which formats only the
changes you have staged in git
.
Adding Codee's binaries to the system's PATH
will allow git
to recognize
git codee-format
as a valid sub-command. Please refer to the Codee
Installation Guide for platform-specific
instructions.
1. Apply format
Suppose a series of staged changes to your Fortran code:
diff --git a/tst.f90 b/tst.f90
index 000000000000..ffffffffffff 100644
--- a/tst.f90
+++ b/tst.f90
@@ -1,6 +1,7 @@
module points
type :: point
real :: x, y
+ integer::z
Run the git codee-format
command from a terminal inside your Git repository
to format only the staged changes.
git codee-format
After formatting, the changes will appear as new, unstaged modifications:
diff --git a/tst.f90 b/tst.f90
index f3059c04c2e4..d099353ca8fe 100644
--- a/tst.f90
+++ b/tst.f90
@@ -1,7 +1,7 @@
module points
type :: point
real :: x, y
- integer::z
+ integer :: z
end type point
2. Verify format with a pre-commit
hook
If you want to configure a pre-commit
requirement to make sure
your committed changes follow the desired formatting style please follow
this steps. This will block your commits if Codee Format was not
applied.
Optional tools
pre-commit
package manager installed in your system.
This is not an actual requirement but a simpler and more robust approach to git hooks.
Configuration
1. Codee Format pre-commit
configuration
Create a file named .pre-commit-config.yaml
. The following example
shows you how to configure the pre-commit
hook so it uses codee format
on the new code:
repos:
- repo: local
hooks:
- id: codee-format
name: Codee Format
entry: git codee-format
language: system
types: [file]
files: '\.(f|F|f90|F90|f95|F95)$'
Finally, run pre-commit install
in the root of the repository to set
up the git hook scripts.
pre-commit install
Now the pre-commit
hook is installed at .git/hooks/pre-commit
and it will
run automatically on git commit
.
2. Usage
Everytime we run git commit
, it will check if changes were made by
codee format
. If changes are required, the pre-commit
hook will aplly them
and reject the commit. You will need to stage (git add
) the modified
files and then reattempt the git commit
command.