3 # Copyright © 2018 Nick Bowler
5 # Simple git post-receive hook to deploy a nanoc site
7 # License WTFPL2: Do What The Fuck You Want To Public License, version 2.
8 # This is free software: you are free to do what the fuck you want to.
9 # There is NO WARRANTY, to the extent permitted by law.
12 worktreedir=/var/cache/git-worktrees
15 while read old new ref x; do
17 refs/heads/master) master_update=$new ;;
22 # welp, git doesn't appear to have a way to ONLY remove worktree tracking
23 # files, it removes the work tree itself which we don't want to do. So do
26 for f in worktrees/*/gitdir; do
27 exec 3<"$f" || continue
28 read l <&3 || continue
30 if test x"$l" = x"$1/.git"; then
32 $GIT worktree prune -v
42 exec 3>"$tmpfile" 4<"$tmpfile"
48 reponame=${repodir##*/}
51 # Remove any stale worktrees...
52 $GIT worktree prune -v
54 # First just try to add a new worktree
55 tmp_worktree=`mktemp -d "${worktreedir%/}/$reponame-$branch-XXXXXXXX"`
56 $GIT worktree add "$tmp_worktree" "$branch" 2>/dev/null ||
59 $GIT worktree list --porcelain >&3
62 while read a b x <&4; do
64 worktree) worktree=${b:-/no/where} ;;
65 branch) if test x"$b" = x"refs/heads/$branch"; then
66 if cd "$worktree"; then
67 test x"`pwd -P`" != x"$worktree" || { exec 4<&-; return 0; }
72 worktree_remove "$worktree"
82 # Usage: cd_to_worktree branch
84 # Change the current working directory to the worktree for the specified
85 # branch, creating it if necessary.
87 # Try twice: stale entries might be removed by first pass and 2nd will work
88 if cd_to_worktree_ "$@" || cd_to_worktree_ "$@"; then
89 printf 'using worktree %s\n' "`pwd -P`"
95 printf 'Deploying %s\n' "$1"
97 { flock -w 60 9 || exit
98 git reset --hard || exit
99 git clean -qfd -e gitlock || exit
101 nanoc deploy master || exit
107 if test ${master_update:+y}; then
108 deploy_branch master || ret=false