fix: move pre-commit treefmt hook into CI

The logic inside the hook was rather broken, causing confusing merges,
stashes, and aborts. Unfortunately `treefmt` does not provide a pure
lint option that checks without writing the changes. So instead, we do
this in CI.
This commit is contained in:
Benjamin Bädorf 2023-07-13 16:59:22 +02:00
parent 8d23a7df43
commit 3c3abec238
No known key found for this signature in database
GPG key ID: 4406E80E13CD656C
4 changed files with 20 additions and 49 deletions

View file

@ -15,6 +15,8 @@ steps:
commands: commands:
- 'echo DEBUG: Using NIX_FLAGS: $NIX_FLAGS' - 'echo DEBUG: Using NIX_FLAGS: $NIX_FLAGS'
- nix $$NIX_FLAGS develop --command nix flake show - nix $$NIX_FLAGS develop --command nix flake show
- nix $$NIX_FLAGS develop --command treefmt --fail-on-change
- nix $$NIX_FLAGS develop --command editorconfig-checker
- nix $$NIX_FLAGS build ".#nixosConfigurations.PubSolarOS.config.system.build.toplevel" - nix $$NIX_FLAGS build ".#nixosConfigurations.PubSolarOS.config.system.build.toplevel"
--- ---
@ -147,6 +149,6 @@ volumes:
--- ---
kind: signature kind: signature
hmac: 6aee0ffe22111bb629c0a79940bfbc3fa75f68c5ed5c4bba68abf6797b87a7ab hmac: a116f78a0b22188052893bdb46aa40f8de66438826c10ced362ea183d7644d67
... ...

View file

@ -15,6 +15,9 @@ end_of_line = unset
insert_final_newline = unset insert_final_newline = unset
trim_trailing_whitespace = unset trim_trailing_whitespace = unset
indent_size = unset indent_size = unset
charset = unset
indent_style = unset
indent_size = unset
[{.*,secrets}/**] [{.*,secrets}/**]
end_of_line = unset end_of_line = unset

View file

@ -1,24 +1,29 @@
# Quick branch overview # Quick branch overview
We work with several branches in this repo. This document aims to explain how We work with several branches in this repo. This document aims to explain how
to contribute changes to the existing branches. to contribute changes to the existing branches.
### `main` branch ### `main` branch
- Changes to `modules` and `profiles` should go [the main branch](https://git.pub.solar/pub-solar/os/src/branch/main) - Changes to `modules` and `profiles` should go [the main branch](https://git.pub.solar/pub-solar/os/src/branch/main)
- Changes can get accepted via: Pull Request - Changes can get accepted via: Pull Request
- Branch protected from direct `git push` - Branch protected from direct `git push`
### `infra` branch ### `infra` branch
- Changes to the [pub.solar](https://pub.solar) infrastructure should be merged [into this branch](https://git.pub.solar/pub-solar/os/src/branch/infra) - Changes to the [pub.solar](https://pub.solar) infrastructure should be merged [into this branch](https://git.pub.solar/pub-solar/os/src/branch/infra)
- Changes can get accepted via: Pull Request - Changes can get accepted via: Pull Request
- Branch protected from direct `git push` - Branch protected from direct `git push`
### `momo/main` branch ### `momo/main` branch
- Changes to the [Momo](https://momo.koeln) infrastructure should be merged [into this branch](https://git.pub.solar/pub-solar/os/src/branch/momo/main) - Changes to the [Momo](https://momo.koeln) infrastructure should be merged [into this branch](https://git.pub.solar/pub-solar/os/src/branch/momo/main)
- Changes can get accepted via: Pull Request - Changes can get accepted via: Pull Request
- Deployment of changes is [automatic via CI pipeline](https://git.pub.solar/pub-solar/os/src/commit/43bd7421509f7cc9ba06d7c740f3f536a4a2af76/.drone.yml#L20-L38) - Deployment of changes is [automatic via CI pipeline](https://git.pub.solar/pub-solar/os/src/commit/43bd7421509f7cc9ba06d7c740f3f536a4a2af76/.drone.yml#L20-L38)
- Branch protected from direct `git push` - Branch protected from direct `git push`
### `$USER` branches ### `$USER` branches
- User's custom hosts and changes can be worked on in these branches - User's custom hosts and changes can be worked on in these branches
- Direct `git push` possible - Direct `git push` possible
- Examples: - Examples:

View file

@ -1,48 +1,9 @@
#!/usr/bin/env bash #!/usr/bin/env bash
if git rev-parse --verify HEAD >/dev/null 2>&1; then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=$(${git}/bin/git hash-object -t tree /dev/null)
fi
# Stash only unstaged changes, keeping staged changes
# We have to stash two times, because:
# --keep-index also stashes the staged changes.
# The staged changes end up in both the stage AND the stash.
# https://overflow.hostux.net/questions/7650797/how-to-stash-only-unstaged-changes-in-git#60875082
old_stash=$(git rev-parse --quiet --verify refs/stash)
git stash push --quiet --staged --message "Staged changes before pre-commit hook"
git stash push --quiet --message "Unstaged changes before pre-commit hook"
if git stash show "stash@{1}" 2>/dev/null; then
git stash pop --quiet --index "stash@{1}"
else
git stash pop --quiet --index "stash@{0}"
fi
new_stash=$(git rev-parse --quiet --verify refs/stash)
diff="git diff-index --name-only --cached $against --diff-filter d"
mapfile -t all_files < <($diff)
# Format staged files
if ((${#all_files[@]} != 0)); then
treefmt "${all_files[@]}" &&
git add "${all_files[@]}"
fi
# If unstaged changes were stashed re-apply to working tree
if [ "$old_stash" != "$new_stash" ]; then
git stash pop --quiet
fi
# Check editorconfig # Check editorconfig
if ((${#all_files[@]} != 0)); then if ! editorconfig-checker; then
if ! editorconfig-checker -- "${all_files[@]}"; then
printf "%b\n" \ printf "%b\n" \
"\nCode is not aligned with .editorconfig" \ "\nCode is not aligned with .editorconfig" \
"Review the output and commit your fixes" >&2 "Review the output and commit your fixes" >&2
exit 1 exit 1
fi
fi fi