From ba011253d8e606135b7ee3c5ddb6dd519d744646 Mon Sep 17 00:00:00 2001 From: Joshua Potter Date: Wed, 29 Nov 2023 10:01:06 -0700 Subject: [PATCH] Add a formatting pre-commit hook. --- .envrc | 4 ++++ .githooks/pre-commit | 13 +++++++++++++ README.md | 21 ++++++++++++++++----- 3 files changed, 33 insertions(+), 5 deletions(-) create mode 100755 .githooks/pre-commit diff --git a/.envrc b/.envrc index b9238c3..817939c 100644 --- a/.envrc +++ b/.envrc @@ -1,3 +1,7 @@ #!/usr/bin/env bash +if command -v git > /dev/null && on_git_branch; then + git config --local core.hooksPath .githooks/ +fi + use flake diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 0000000..90300f5 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +set -e + +filesToFormat=$( + git --no-pager diff --name-status --no-color --cached | \ + awk '$1 != "D" && $2 ~ /\.exs?$/ {print $NF}' +) + +for path in $filesToFormat +do + mix format "$path" + git add "$path" +done diff --git a/README.md b/README.md index a71b5b7..55a91c3 100644 --- a/README.md +++ b/README.md @@ -34,11 +34,6 @@ and then start the local server: $> mix phx.server ``` -## Language Server - -The [elixir-ls](https://github.com/elixir-lsp/elixir-ls) LSP (version 0.17.10) -is included in this flake. - ## Dependencies This project pins Mix dependencies using [mix2nix](https://github.com/ydlr/mix2nix). @@ -49,3 +44,19 @@ mix2nix > deps.nix As of now, `mix2nix` cannot handle git dependencies found inside the `mix.lock` file. If you have git dependencies, add them manually or use [FODs](https://nixos.org/manual/nixpkgs/stable/#packaging-beam-applications). + +## Language Server + +The [elixir-ls](https://github.com/elixir-lsp/elixir-ls) LSP (version 0.17.10) +is included in this flake. + +## Formatting + +Formatting depends on the `mix format` task. A `pre-commit` hook is included in +`.githooks` that can be used to format all `*.exs?` files prior to commit. +Install via: +```bash +$> git config --local core.hooksPath .githooks/ +``` +If running [direnv](https://direnv.net/), this hook is installed automatically +when entering the directory.