flake/generate-files: add --commit command + fixes

- Added `--commit` with a git-based message
- `nix fmt` only operates on files known to git
- ensure `generated` directory exists
This commit is contained in:
Matt Sturgeon 2024-07-09 20:14:15 +01:00
parent a5e9dbdef1
commit 7e3d629bb0
No known key found for this signature in database
GPG key ID: 4F91844CED1A8299

View file

@ -9,6 +9,21 @@
repo_root=$(git rev-parse --show-toplevel)
generated_dir=$repo_root/generated
commit=
while [ $# -gt 0 ]; do
case "$1" in
--commit) commit=1
;;
--*) echo "unknown option $1"
;;
*) echo "unexpected argument $1"
;;
esac
shift
done
mkdir -p "$generated_dir"
echo "Rust-Analyzer"
nix build .#rust-analyzer-options
cat ./result >"$generated_dir"/rust-analyzer.nix
@ -21,7 +36,40 @@
nix build .#none-ls-builtins
cat ./result >"$generated_dir"/none-ls.nix
git add --intent-to-add "$generated_dir"
nix fmt
if [ -n "$commit" ]; then
cd "$generated_dir"
git add .
# Construct a msg body from `git status`
body=$(
git status \
--short \
--ignored=no \
--untracked-files=no \
--no-ahead-behind \
| sed \
-e 's/^\s*\([A-Z]\)\s*/\1 /' \
-e 's/^A/Added/' \
-e 's/^M/Updated/' \
-e 's/^R/Renamed/' \
-e 's/^D/Removed/' \
-e 's/^/- /'
)
# Construct the commit message based on the body
count=$(echo "$body" | wc -l)
if [ "$count" -gt 1 ] || [ ''${#body} -gt 50 ]; then
msg=$(echo -e "generated: Update\n\n$body")
else
msg="generated:''${body:1}"
fi
# Commit if there are changes
[ "$count" -gt 0 ] && git commit -m "$msg" --no-verify
fi
'';
};