Skip to content

Commit ca0449f

Browse files
committed
Fix install-hooks.bash script
I hadn’t realized there were Git hooks, but noticed them when I was adding the Weeder script. So, I documented that they exist, and then fixed the script since it wasn’t working for me. It now works - when run it from anywhere in the repo (not just from two directories down somewhere), - when `core.hooksPath` is set (i.e., `$GIT_DIR/hooks` isn’t the right place), - when run from a worktree (which doesn’t necessarily have its own hooks dir), - when the hooks directory doesn’t exist, and - when the hooks already exist (if you pass `-f`). This also replaces `ln -s` with `cp` so that in case the particular worktree it was run from is renamed or deleted, it doesn’t break the hooks for the repository.
1 parent 27c18a4 commit ca0449f

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

development.markdown

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ To get cracking with Unison:
2020

2121
On startup, Unison prints a url for the codebase UI. If you did step 3 above, then visiting that URL in a browser will give you a nice interface to your codebase.
2222

23+
## Git hooks
24+
25+
There are some Git hooks provided by Unison. If you want to use them, you can run
26+
27+
``` bash
28+
./scripts/install-hooks.bash
29+
```
30+
2331
## Autoformatting your code with Ormolu
2432

2533
We use Ormolu (see [the specific version](./nix/versions.nix)) and CI will add an extra commit, if needed, to autoformat your code.

scripts/install-hooks.bash

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,39 @@
11
#!/usr/bin/env bash
2+
set -euo pipefail
3+
SCRIPTNAME="$(readlink -f -- "${BASH_SOURCE[0]}")"
4+
SCRIPTDIR="$(dirname -- "$SCRIPTNAME")"
25

3-
GIT_DIR=$(git rev-parse --git-dir)
6+
function usage {
7+
echo "Usage: $SCRIPTNAME [-f] [-h]"
8+
echo
9+
echo " -f Overwrite hooks if they already exist"
10+
echo " -h Show this usage message"
11+
}
12+
13+
trap "echo; usage" ERR
14+
15+
force=("-n")
16+
while getopts ':fh' OPTION; do
17+
case "$OPTION" in
18+
f)
19+
force=("-f")
20+
;;
21+
h)
22+
usage
23+
exit 0
24+
;;
25+
\?)
26+
usage
27+
exit 1
28+
;;
29+
esac
30+
done
31+
32+
hooks_dir=$(git rev-parse --git-path hooks)
433

534
echo "Installing hooks..."
35+
mkdir -p "$hooks_dir"
636
# this command creates symlink to our pre-commit script
7-
ln -s ../../scripts/pre-commit.bash $GIT_DIR/hooks/pre-commit
8-
ln -s ../../scripts/pre-push.bash $GIT_DIR/hooks/pre-push
37+
cp "${force[@]}" "$SCRIPTDIR/pre-commit.bash" "$hooks_dir/pre-commit"
38+
cp "${force[@]}" "$SCRIPTDIR/pre-push.bash" "$hooks_dir/pre-push"
939
echo "Done!"

0 commit comments

Comments
 (0)