Let's start a new Rust project. $ mkdir best-fizzbuzz-ever $ cd best-fizzbuzz-ever $ cat << EOF > main.rs fn main() { for i in 0.. { println ("fizzbuzz"); }} EOF $ git init Initialized empty Git repository in /home/jyn/src/third-website/best-fizzbuzz-ever/.git/ $ git add main.rs $ git commit --message fizzbuzz [main (root-commit) 661dc28] fizzbuzz 1 file changed, 4 insertions(+) create mode 100644 main.rs Neat. Now let's say I add this to some list of fizzbuzz projects in different languages. Maybe .... this one. They tell me I need to have "proper formatting" and "use consistent style". How rude. Maybe I can write a pre-commit hook that checks that for me? $ cat << 'EOF' > pre-commit #!/bin/sh set -eu for f in *.rs; do rustfmt --check "$f" done EOF $ chmod +x pre-commit $ ln -s ../../pre-commit .git/hooks/pre-commit $ git add pre-commit $ git commit --message "add pre-commit hook" Diff in /home/jyn/src/third-website/best-fizzbuzz-ever/src/main.rs:1: -fn main() { for i in 0.. { - println ("fizzbuzz"); -}} +fn main() { + for i in 0.. { + println("fizzbuzz"); + } +} Neat! Let's commit that change. $ rustfmt main.rs $ git commit --message "add pre-commit hook" [main 3be7b87] add pre-commit hook 1 file changed, 4 insertions(+) create mode 100755 pre-commit $ git status On branch main Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: main.rs Oh ... We fixed the formatting, but we didn't actually stage the changes. The pre-commit hook runs on the working tree, not on the index, so it didn't catch the issue. We can see that the version tracked by git still has the wrong formatting: $ git show HEAD:main.rs fn main() { for i in 0.. { println ("fizzbuzz"); }} Maybe we can make the script smarter? Let's checkout all the files in the index into a temporary directory and run our pre-commit hook there. $ cat << 'EOF' > pre-commit #!/bin/sh set -eu tmpdir=$(mktem...
First seen: 2025-12-27 07:54
Last seen: 2025-12-28 04:56