]> git.draconx.ca Git - scripts.git/blob - nanoc-post-receive
Add script to convert "JWK"-format RSA keys to normal.
[scripts.git] / nanoc-post-receive
1 #!/bin/sh
2 #
3 # Copyright © 2018 Nick Bowler
4 #
5 # Simple git post-receive hook to deploy a nanoc site
6 #
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.
10
11 GIT=/usr/bin/git
12 worktreedir=/var/cache/git-worktrees
13
14 master_update=
15 while read old new ref x; do
16   case $ref in
17   refs/heads/master) master_update=$new ;;
18   esac
19 done
20
21 worktree_remove() {
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
24   # this manually.
25
26   for f in worktrees/*/gitdir; do
27     exec 3<"$f" || continue
28     read l <&3 || continue
29
30     if test x"$l" = x"$1/.git"; then
31       rm -f "$f"
32       $GIT worktree prune -v
33       break
34     fi
35   done
36
37   exec 3<&-
38 }
39
40 cd_to_worktree_ () {
41   tmpfile=`mktemp`
42   exec 3>"$tmpfile" 4<"$tmpfile"
43   rm -f "$tmpfile"
44
45   branch=${1##*/}
46
47   repodir=`pwd`
48   reponame=${repodir##*/}
49   worktree=/no/where
50
51   # Remove any stale worktrees...
52   $GIT worktree prune -v
53
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 ||
57     rmdir "$tmp_worktree"
58
59   $GIT worktree list --porcelain >&3
60   exec 3<&-
61
62   while read a b x <&4; do
63     case $a in
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; }
68         cd -
69       fi
70
71       # Clean up
72       worktree_remove "$worktree"
73     fi ;;
74     esac
75   done
76
77   # Failed?
78   exec 4<&-
79   return 1
80 }
81
82 # Usage: cd_to_worktree branch
83 #
84 # Change the current working directory to the worktree for the specified
85 # branch, creating it if necessary.
86 cd_to_worktree () {
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`"
90     unset GIT_DIR
91   fi
92 }
93
94 deploy_branch () {
95   printf 'Deploying %s\n' "$1"
96   ( cd_to_worktree $1
97     { flock -w 60 9 || exit
98       git reset --hard || exit
99       git clean -qfd -e gitlock || exit
100       nanoc || exit
101       nanoc deploy master || exit
102     } 9>gitlock
103   )
104 }
105
106 ret=true
107 if test ${master_update:+y}; then
108   deploy_branch master || ret=false
109 fi
110
111 $ret