You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p>auto-install hooks when developers run <code>npm install</code>:</p>
104
100
<p><strong>npm / pnpm / bun:</strong></p>
105
-
<pre>npm pkg set scripts.prepare="./.githook.sh install"</pre>
101
+
_CODE(`npm pkg set scripts.prepare="./.githook.sh install"')
106
102
<p><strong>yarn:</strong></p>
107
-
<pre>npm pkg set scripts.postinstall="./.githook.sh install"</pre>
103
+
_CODE(`npm pkg set scripts.postinstall="./.githook.sh install"')
108
104
<p>yarn uses <code>postinstall</code> instead of <code>prepare</code>.</p>
109
105
110
106
<h2 id="makefile">makefile integration</h2>
111
107
<p>add a target for installing hooks:</p>
112
-
<pre>.PHONY: prepare
108
+
_CODE(`.PHONY: prepare
113
109
prepare:
114
-
./.githook.sh install</pre>
110
+
./.githook.sh install')
115
111
<p>run after cloning:</p>
116
-
<pre>make prepare</pre>
112
+
_CODE(`make prepare')
117
113
118
114
<h2 id="husky">migrating from husky</h2>
119
115
<p>hooks are automatically migrated during setup if <code>.husky/</code> exists. to migrate manually:</p>
120
-
<pre>./.githook.sh migrate husky</pre>
116
+
_CODE(`./.githook.sh migrate husky')
121
117
<p>this copies hook scripts from <code>.husky/</code> to <code>.githook/</code>. after verifying:</p>
122
-
<pre>rm -rf .husky
123
-
npm uninstall husky</pre>
118
+
_CODE(`rm -rf .husky
119
+
npm uninstall husky')
124
120
125
121
<h2 id="languages">using other languages</h2>
126
122
<p>hooks can be written in any language. use a shell wrapper:</p>
127
-
<pre>#!/bin/sh
128
-
node .githook/pre-commit.js</pre>
123
+
_CODE(`#!/bin/sh
124
+
node .githook/pre-commit.js')
129
125
<p>or use a shebang directly (the file must still be executable):</p>
130
-
<pre>#!/usr/bin/env python3
126
+
_CODE(`#!/usr/bin/env python3
131
127
import subprocess
132
128
import sys
133
129
result = subprocess.run(["pytest", "-x"])
134
-
sys.exit(result.returncode)</pre>
135
-
<pre>#!/usr/bin/env ruby
136
-
system("bundle exec rubocop") || exit(1)</pre>
130
+
sys.exit(result.returncode)')
131
+
_CODE(`#!/usr/bin/env ruby
132
+
system("bundle exec rubocop") || exit(1)')
137
133
138
134
<h2 id="nvm">node version managers</h2>
139
135
<p>gui clients and ides may not load your shell profile, causing "command not found" errors. create <code>~/.config/githook/init.sh</code> to fix this globally:</p>
140
-
<pre>mkdir -p ~/.config/githook
136
+
changequote([,])dnl
137
+
_CODE([mkdir -p ~/.config/githook
141
138
cat > ~/.config/githook/init.sh << 'EOF'
142
139
export NVM_DIR="$HOME/.nvm"
143
140
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
144
-
EOF</pre>
141
+
EOF])
142
+
changequote(`,')dnl
145
143
<p>this is sourced before every hook runs. for fnm use <code>eval "$(fnm env)"</code>, for volta add <code>$VOLTA_HOME/bin</code> to PATH.</p>
146
144
147
145
<h2 id="debug">debugging</h2>
148
146
<p>enable debug output:</p>
149
-
<pre>GITHOOK_DEBUG=1 git commit -m "test"</pre>
147
+
_CODE(`GITHOOK_DEBUG=1 git commit -m "test"')
150
148
<p>common issues:</p>
151
149
<p>• <strong>"command not found"</strong> — see <a href="#nvm">nvm section</a></p>
<p>• <strong>wrong directory</strong> — hooks run from repo root</p>
154
152
155
153
<h2 id="uninstall">uninstalling</h2>
156
154
<p>remove git configuration (keeps your hook scripts):</p>
157
-
<pre>./.githook.sh uninstall</pre>
155
+
_CODE(`./.githook.sh uninstall')
158
156
<p>to fully remove:</p>
159
-
<pre>rm -rf .githook .githook.sh</pre>
157
+
_CODE(`rm -rf .githook .githook.sh')
160
158
161
159
<h2 id="how">how it works</h2>
162
160
<p>githook.sh sets <code>core.hooksPath</code> to <code>.githook/_</code> which contains wrapper scripts. when git runs a hook, the wrapper sources <code>~/.config/githook/init.sh</code>, checks <code>GITHOOK=0</code>, then runs your script from <code>.githook/</code>.</p>
163
161
<p>quick setup (<code>curl | sh</code>) runs install if already setup.</p>
164
162
<p>each developer runs <code>./.githook.sh install</code> once after cloning because git config is local.</p>
0 commit comments