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