Working pre-commit hook

2017-05-10

I have a pre-commit hook working for building my blog! Some things I found out:

  • Here also it's necessary to explicitly specify git-dir and work-tree in order for git commands to work. As I mentioned in a previous post, my build script uses git log, so I had to add those to the script as well.
  • Python virtual environments made with virtualenv work just fine inside a git hook! You just source the activation script as normal. I'm glad this worked so painlessly.

Here's the hook in entirety for reference:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/bash

proj_dir="/path/to/nathanmorrison.net"
blog_work_dir="$proj_dir/blog_works"
git_dir="$proj_dir/.git"
venv_script="/path/to/venv/bin/activate"

cd $blog_work_dir

## Check to see if script should be run
for f in $(ls); do
    if [ f -nt "$proj_dir/public_html/blog/home.html" ]; then
        runme=${f::1}$runme
    fi
done

if [ $runme ]; then
    echo "Building the blog."
    source $venv_script
    python build_blog.py
    git --git-dir=$git_dir --work-tree=$proj_dir add ../public_html/blog/*.html
fi