- A hook that's run before (or after) parts of Git commit, push, etc.
- Can reject or modify commits.
- "But I don't use the command line for Git, I use an app."
- Doesn't matter - your app just uses them anyway!
- Client-side
- pre-commit
- prepare-commit-msg, ...
- Server-side
- pre-receive, ...
- Create shell scripts in your_repo/.git/hooks
- Git will launch them at the right time
- It's code. So keep it under git control!
- (Yes, keeping git hooks in git made my head hurt, too. Work through it, you’ll thank me!)
How about a shell script that invokes Ruby?
- Invoke Ruby at a shell script's hashbang
- Run your code
puts
the reasons (if any) for failureexit 1
to fail, otherwise pass
- "Bad content" in lines I'm changing
- Ensuring relationships between files
- If adding one of x, make sure that y is touched
- Bad Syntax (almost, I'll get back to that)
- For me, that’s code that I probably don't want to check in.
- Again: not really bad, just code that I probably don't want to check in.
- Pre-commit is an easily-overridable, local check
- Doesn’t have to be exact
- The whole point is to save me time and embarrassment.
- That's OK, it's super-easy to override.
- Do everything in your app, then
- If app fails to commit,
git commit -m "My Message" --no-verify
- Resume in your app.
- We all eventually check in git merge conflicts.
- So why not look for strings like
<<<<<<<<<
or>>>>>>>>>
or=========
- If this was server-side, I'd be pedantic about line endings, etc. But it's not.
console.debug
,console.log
binding.pry
,binding.remote-pry
- I never type in logger lines "by hand," I use TextExpander macros for that.
boblogd
expands into:Rails.logger.debug "\n\nTMP_DEBUG <insertion_point>\n\n"
- Thanks for making that generic, Jonathan!
- So, my pre-commit searches for
TMP_DEBUG
PRIVATE_KEY
ssh-rsa
- God No!
alert
- Sometimes I do mean to commit a call to alert.
- But for me this catches more mistakes than it flags "valid" calls. So, awesome!
- "Main" work project requires changes to production.rb when a JavaScript or CSS asset is added.
- Ensure that if .js, .coffee, .css, .scss, etc. is added, that production.rb is committed.
- Again, not foolproof, but who cares?
- Tools that use git (GitBox, Github, etc.) call client-side hooks "for free."
- Unfortunate idiosyncrasies. Example:
- If a commit from GitBox fails because of a pre-commit hook, the commit message is deleted from the prompt :-(
- So, I'm in the habit of Command-A, Command-C before I commit.
- Mac OS X Apps don't know rbenv (or rvm) from Adam.
- So, which version of Ruby does you favorite App run?
- Yeah. So...
- Ruby version used to run the shell script?
- Ruby grammar used to validate files?
- There's probably some .plist-based solution.
- Keep a separate "master" repo for your git hooks.
- symlink them into your working repos.
- See the setup.sh file
- Keep your symlinking script in the git hook repo, too.
- Oww, my head!
- There is no way that you can use these git hook on the git-hooks repo.
- Think about it. Every violation will appear in that file!
- Some repo's (like my main work repo) have weird requirements.
- Q: How do I reconcile that with the "one master copy" philosophy?
- A: Define constants in per-project /git/config files.
- See the git config doc, then look at my script to see it in use.
- [email protected]
- @bob_gilmore
- Bob on GitHub