]> Sergey Matveev's repositories - public-inbox.git/blob - examples/grok-pull.post_update_hook.sh
examples: add grok-pull post_update_hook example
[public-inbox.git] / examples / grok-pull.post_update_hook.sh
1 #!/bin/sh
2 # post_update_hook for repos.conf as used by grok-pull, takes a full
3 # git repo path as it's first and only arg.
4 full_git_dir="$1"
5
6 # same default as other public-inbox-* tools
7 PI_CONFIG=${PI_CONFIG-~/.public-inbox/config}
8
9 # FreeBSD expr(1) only supports BRE, so no '+'
10 EPOCH2MAIN='\(..*\)/git/[0-9][0-9]*\.git'
11
12 # see if it's v2 or v1 based on tree contents, since somebody could
13 # theoretically name a v1 inbox with a path that looks like a v2 epoch
14 if git --git-dir="$full_git_dir" ls-tree --name-only HEAD | \
15         grep -E '^(m|d)$' >/dev/null
16 then
17         inbox_fmt=2
18         inbox_mainrepo=$(expr "$full_git_dir" : "$EPOCH2MAIN")
19         inbox_name=$(basename "$inbox_mainrepo")
20         msgmap="$inbox_mainrepo"/msgmap.sqlite3
21 else
22         inbox_fmt=1
23         inbox_mainrepo="$full_git_dir"
24         inbox_name=$(basename "$inbox_mainrepo" .git)
25         msgmap="$inbox_mainrepo"/public-inbox/msgmap.sqlite3
26 fi
27
28 # run public-inbox-init iff unconfigured
29 cfg_mainrepo=$(git config -f "$PI_CONFIG" publicinbox."$inbox_name".mainrepo)
30 case $cfg_mainrepo in
31 '')
32         remote_git_url=$(git --git-dir="$full_git_dir" config remote.origin.url)
33         case $remote_git_url in
34         '')
35                 echo >&2 "remote.origin.url unset in $full_git_dir/config"
36                 exit 1
37                 ;;
38         esac
39
40         case $inbox_fmt in
41         1)
42                 remote_inbox_url="$remote_git_url"
43                 ;;
44         2)
45                 remote_inbox_url=$(expr "$remote_git_url" : "$EPOCH2MAIN")
46                 ;;
47         esac
48
49         config_url="$remote_inbox_url"/_/text/config/raw
50         remote_config="$inbox_mainrepo"/remote.config.$$
51         trap 'rm -f "$remote_config"' EXIT
52         if curl --compressed -sSf -v "$config_url" >"$remote_config"
53         then
54                 # n.b. inbox_name on the remote may not match our local
55                 # inbox_name, so we match all addresses in the remote config
56                 addresses=$(git config -f "$remote_config" -l | \
57                         sed -ne 's/^publicinbox\..\+\.address=//p')
58                 case $addresses in
59                 '')
60                         echo >&2 'unable to extract address(es) from ' \
61                                 "$remote_config"
62                         exit 1
63                         ;;
64                 esac
65                 newsgroups=$(git config -f "$remote_config" -l | \
66                         sed -ne 's/^publicinbox\..\+\.newsgroup=//p')
67         else
68                 newsgroups=
69                 addresses="$inbox_name@$$.$(hostname).example.com"
70                 echo >&2 "E: curl $config_url failed"
71                 echo >&2 "E: using bogus <$addresses> for $inbox_mainrepo"
72         fi
73         local_url="http://127.0.0.1:8080/$inbox_name"
74         public-inbox-init -V$inbox_fmt "$inbox_name" \
75                 "$inbox_mainrepo" "$local_url" $addresses
76
77         if test $? -ne 0
78         then
79                 echo >&2 "E: public-inbox-init failed on $inbox_mainrepo"
80                 exit 1
81         fi
82
83         for ng in $newsgroups
84         do
85                 git config -f "$PI_CONFIG" \
86                         "publicinbox.$inbox_name.newsgroup" "$ng"
87                 # only one newsgroup per inbox
88                 break
89         done
90         echo "I: $inbox_name at $inbox_mainrepo ($addresses) $local_url"
91         ;;
92 esac
93
94 # only run public-inbox-index if an index exists and has messages,
95 # since epochs may be cloned out-of-order by grokmirror and we also
96 # don't know what indexlevel a user wants
97 if test -f "$msgmap"
98 then
99         n=$(echo 'SELECT COUNT(*) FROM msgmap' | sqlite3 -readonly "$msgmap")
100         case $n in
101         0|'')
102                 : v2 inboxes may be init-ed with an empty msgmap
103                 ;;
104         *)
105                 $EATMYDATA public-inbox-index -v "$inbox_mainrepo"
106                 ;;
107         esac
108 fi