toolbox: anchor _repo pattern to start of line
[girocco.git] / shlib.sh
blobfcfe660f29019788e983873cb3788e1adc6bf216
1 #!/bin/sh
3 # This is generic shell library for all the scripts used by Girocco;
4 # most importantly, it introduces all the $cfg_* shell variables.
6 # SHA-1 pattern
7 octet='[0-9a-f][0-9a-f]'
8 octet4="$octet$octet$octet$octet"
9 octet19="$octet4$octet4$octet4$octet4$octet$octet$octet"
10 octet20="$octet4$octet4$octet4$octet4$octet4"
12 get_girocco_config_var_list() {
13 # Export all the variables from Girocco::Config to suitable var= lines
14 # prefixing them with 'cfg_'. E.g. $cfg_admin is admin's mail address now
15 # and also setting a 'defined_cfg_' prefix to 1 if they are not undef.
16 __girocco_conf="$GIROCCO_CONF"
17 [ -n "$__girocco_conf" ] || __girocco_conf="Girocco::Config"
18 [ -z "$basedir" ] || __girocco_extrainc="-I$basedir"
19 perl -I@basedir@ $__girocco_extrainc -M$__girocco_conf -le \
20 'foreach (sort {uc($a) cmp uc($b)} keys %Girocco::Config::) {
21 my $val = ${$Girocco::Config::{$_}}; defined($val) or $val="";
22 $val =~ s/([\\"\$\`])/\\$1/gos;
23 $val =~ s/(?:\r\n|\r|\n)$//os;
24 print "cfg_$_=\"$val\"";
25 print "defined_cfg_$_=",
26 (defined(${$Girocco::Config::{$_}})?"1":"");
30 # If basedir has been replaced, and shlib_vars.sh exists, get the config
31 # definitions from it rather than running Perl.
32 if [ "@basedir@" = '@'basedir'@' ] || ! [ -r "@basedir@/shlib_vars.sh" ]; then
33 # Import all the variables from Girocco::Config to the local environment,
34 eval "$(get_girocco_config_var_list)"
35 else
36 # Import the variables from shlib_vars.sh which avoids needlessly
37 # running another copy of Perl
38 . "@basedir@/shlib_vars.sh"
41 # Make sure we have a reproducible environment by using a controlled HOME dir
42 XDG_CONFIG_HOME="$cfg_chroot/var/empty"
43 HOME="$cfg_chroot/etc/girocco"
44 GIT_CONFIG_NOSYSTEM=1
45 GIT_ATTR_NOSYSTEM=1
46 GIT_TERMINAL_PROMPT=0
47 export XDG_CONFIG_HOME
48 export HOME
49 export GIT_CONFIG_NOSYSTEM
50 export GIT_ATTR_NOSYSTEM
51 export GIT_TERMINAL_PROMPT
53 # We cannot use a git() {} or nc_openbsd() {} function to redirect git
54 # and nc_openbsd to the desired executables because when using
55 # "ENV_VAR=xxx func" the various /bin/sh implementations behave in various
56 # different and unexpected ways:
57 # a) treat "ENV_VAR=xxx" like a separate, preceding "export ENV_VAR=xxx"
58 # b) treat "ENV_VAR=xxx" like a separate, prededing "ENV_VAR=xxx"
59 # c) treat "ENV_VAR=xxx" like a temporary setting only while running func
60 # None of these are good. We want a temporary "export ENV_VAR=xxx"
61 # setting only while running func which none of the /bin/sh's do.
63 # Instead we'd like to use an alias that provides the desired behavior without
64 # any of the bad (a), (b) or (c) effects.
66 # However, unfortunately, some of the crazy /bin/sh implementations do not
67 # recognize alias expansions when preceded by variable assignments!
69 # So we are left with git() {} and nc_openbsd() {} functions and in the
70 # case of git() {} we can compensate for (b) and (c) failing to export
71 # but not (a) and (b) persisting the values so the caller will simply
72 # have to beware and explicitly unset any variables that should not persist
73 # beyond the function call itself.
75 git() (
76 [ "${GIT_DIR+set}" = "set" ] && export GIT_DIR
77 [ "${GIT_SSL_NO_VERIFY+set}" = "set" ] && export GIT_SSL_NO_VERIFY
78 [ "${GIT_TRACE_PACKET+set}" = "set" ] && export GIT_TRACE_PACKET
79 [ "${GIT_USER_AGENT+set}" = "set" ] && export GIT_USER_AGENT
80 exec "$cfg_git_bin" "$@"
83 nc_openbsd() { "$cfg_nc_openbsd_bin" "$@"; }
85 _addrlist() {
86 _list=
87 for _addr in "$@"; do
88 [ -z "$_list" ] || _list="$_list, "
89 _list="$_list$_addr"
90 done
91 echo "$_list"
94 _sendmail() {
95 _mailer="${cfg_sendmail_bin:-/usr/sbin/sendmail}"
96 if [ -n "$cfg_sender" ]; then
97 "$_mailer" -i -f "$cfg_sender" "$@"
98 else
99 "$_mailer" -i "$@"
103 mail() {
104 _subject=
105 if [ "$1" = "-s" ]; then
106 shift
107 _subject="$1"
108 shift
111 echo "From: \"$cfg_name\" ($cfg_title) <$cfg_admin>"
112 echo "To: $(_addrlist "$@")"
113 [ -z "$_subject" ] || echo "Subject: $_subject"
114 echo "MIME-Version: 1.0"
115 echo "Content-Type: text/plain; charset=utf-8"
116 echo "Content-Transfer-Encoding: 8bit"
117 echo "Auto-Submitted: auto-generated"
118 echo ""
120 } | _sendmail "$@"
123 # bang CMD... will execute the command with well-defined failure mode;
124 # set bang_action to string of the failed action ('clone', 'update', ...);
125 # pre-set bang_once=1 to make sure jobs banging on a repo repeatedly will
126 # not spam the owner; re-define the bang_trap() function to do custom
127 # cleanup before bailing out
128 bang() {
129 if [ -n "$show_progress" ]; then
130 exec 3>&1
131 errcode=
132 read -r errcode <<-EOT || :
134 exec 4>&3 3>&1 1>&4 4>&-
135 { "$@" 3>&- || echo $? >&3; } 2>&1 | tee -a "$bang_log"
138 exec 3>&-
139 if [ -z "$errcode" ]; then
140 # All right. Cool.
141 return;
143 else
144 if "$@" >>"$bang_log" 2>&1; then
145 # All right. Cool.
146 return;
147 else
148 errcode="$?"
151 if ! [ -e .banged ] || [ -e .bangagain ]; then
152 rm -f .bangagain
153 bangmailok=true
154 ! [ -f HEAD -a -f config -a -d objects ] ||
155 bangmailok="$(GIT_DIR=. git config --bool gitweb.statusupdates 2>/dev/null || echo true)"
156 bangaddrs=''
157 [ "$bangmailok" = "false" -o -z "$mail" ] || bangaddrs="$mail"
158 [ -z "$cfg_admincc" -o "$cfg_admincc" = "0" -o -z "$cfg_admin" ] ||
159 if [ -z "$bangaddrs" ]; then bangaddrs="$cfg_admin"; else bangaddrs="$bangaddrs,$cfg_admin"; fi
160 [ -z "$bangaddrs" ] ||
162 echo "$* failed with error code $errcode"
163 echo ""
164 [ ! -n "$bang_once" ] || echo "you will not receive any more notifications until recovery"
165 echo "this status message may be disabled on the project admin page"
166 echo ""
167 echo "Log follows:"
168 echo ""
169 cat "$bang_log"
170 } | mail -s "[$cfg_name] $proj $bang_action failed" "$bangaddrs"
172 touch .banged
173 bang_trap
174 exit 1
177 # bang_eval CMD... will evaluate the command with well-defined failure mode;
178 # Identical to bang CMD... except the command is eval'd instead of executed.
179 bang_eval() {
180 bang eval "$*"
183 # Default bang settings:
184 bang_setup() {
185 bang_action="lame_programmer"
186 bang_once=
187 bang_trap() { :; }
188 bang_log="$(mktemp -t repomgr-XXXXXX)"
189 trap "rm \"\$bang_log\"" EXIT
193 # Progress report - if show_progress is set, shows the given message.
194 progress() {
195 [ ! -n "$show_progress" ] || echo "$@"
199 # Project config accessors; must be run in project directory
200 config_get() {
201 git config "gitweb.$1"
204 config_set() {
205 git config "gitweb.$1" "$2" && chgrp repo config && chmod g+w config
208 config_set_raw() {
209 git config "$1" "$2" && chgrp repo config && chmod g+w config
212 config_get_date_seconds() {
213 _dt="$(config_get "$1" || :)"
214 [ -n "$_dt" ] || return 1
215 _ds="$(perl -I@basedir@ -MGirocco::Util -e "print parse_rfc2822_date('$_dt')")"
216 [ -n "$_ds" ] || return 1
217 echo "$_ds"
220 # Tool for checking whether given number of seconds has not passed yet
221 check_interval() {
222 os="$(config_get_date_seconds "$1")" || return 1
223 ns="$(date +%s)"
224 [ $ns -lt $(($os+$2)) ]
228 # List all Git repositories, with given prefix if specified, one-per-line
229 get_repo_list() {
230 if [ -n "$1" ]; then
231 cut -d : -f 1,3 "$cfg_chroot"/etc/group | grep "^$1"
232 else
233 cut -d : -f 1,3 "$cfg_chroot"/etc/group
234 fi | while IFS=: read name id; do
235 [ $id -lt 65536 ] || echo "$name"
236 done
239 # returns true if the passed in git dir (defaults to ".") is a mirror using git fast-import
240 is_gfi_mirror() {
241 _gitdir="${1-.}"
242 # always return false for non-mirrors
243 [ ! -e "$_gitdir/.nofetch" ] || return 1
244 _url="$(GIT_DIR="$_gitdir" config_get baseurl 2>/dev/null || :)"
245 case "$_url" in
246 svn://* | svn+http://* | svn+https://*)
247 # Don't think git-svn currently uses git fast-import
248 return 1
250 darcs://*)
251 # darcs mirrors use git fast-import
252 return 0
254 bzr://*)
255 # bzr mirrors use git fast-import
256 return 0
258 hg+http://* | hg+https://*)
259 # hg mirrors use git fast-import
260 return 0
262 esac
263 # assume it does not use git fast-import
264 return 1
267 # returns true if the passed in git dir (defaults to ".") is a mirror using git-svn
268 is_svn_mirror() {
269 _gitdir="${1-.}"
270 # always return false for non-mirrors
271 [ ! -e "$_gitdir/.nofetch" ] || return 1
272 _url="$(GIT_DIR="$_gitdir" config_get baseurl 2>/dev/null || :)"
273 case "$_url" in
274 svn://* | svn+http://* | svn+https://*)
275 return 0
276 esac
277 return 1
280 # A well-known UTF-8 locale is required for some of the fast-import providers
281 # in order to avoid mangling characters. Ideally we could use "POSIX.UTF-8"
282 # but that is not reliably UTF-8 but rather usually US-ASCII.
283 # We parse the output of `locale -a` and select a suitable UTF-8 locale.
284 # If we cannot find one in the `locale -a` output then we just use a well-known
285 # UTF-8 locale and hope for the best. We set LC_ALL to our choice and export
286 # it. We only set this temporarily when running the fast-import providers.
287 set_utf8_locale() {
288 _guess_locale="$(locale -a | grep -viE '^(posix|c)(\..*)?$' | \
289 grep -iE '\.utf-?8$' | sed -e 's/\.[Uu][Tt][Ff]-*8$//' | \
290 sed -e '/en_US/ s/^/0 /; /en_US/ !s/^/1 /' | LC_ALL=C sort | \
291 head -n 1 | cut -d ' ' -f 2)"
292 LC_ALL="${_guess_locale:-en_US}.UTF-8"
293 export LC_ALL
296 # hg-fast-export | git fast-import with error handling in current directory GIT_DIR
297 git_hg_fetch() (
298 set_utf8_locale
299 _python="${PYTHON:-python}"
300 _err1=
301 _err2=
302 exec 3>&1
303 { read -r _err1 || :; read -r _err2 || :; } <<-EOT
305 exec 4>&3 3>&1 1>&4 4>&-
307 _e1=0
308 [ -f hg2git-marks ] || touch hg2git-marks
309 _af="$(git config hg.authorsfile || :)"
310 _cmd='GIT_DIR="$(pwd)" "$_python" "$cfg_basedir/bin/hg-fast-export.py" \
311 --repo "$(pwd)/repo.hg" \
312 --marks "$(pwd)/hg2git-marks" \
313 --mapping "$(pwd)/hg2git-mapping" \
314 --heads "$(pwd)/hg2git-heads" \
315 --status "$(pwd)/hg2git-state" \
316 -U unknown --force --flatten --hg-hash'
317 [ -z "$_af" ] || _cmd="$_cmd"' --authors "$_af"'
318 eval "$_cmd" 3>&- || _e1=$?
319 echo $_e1 >&3
320 } | \
322 _e2=0
323 rm -f hg2git-marks.new
324 git fast-import \
325 --export-marks="$(pwd)/hg2git-marks.new" \
326 --export-pack-edges="$(pwd)/gfi-packs" \
327 --force 3>&- || _e2=$?
328 echo $_e2 >&3
332 exec 3>&-
333 [ "$_err1" = 0 -a "$_err2" = 0 ] || return 1
334 if [ -f hg2git-marks ]; then
335 rm -f hg2git-marks.old
336 mv hg2git-marks hg2git-marks.old
337 else
338 touch hg2git-marks.old
340 cat hg2git-marks.old hg2git-marks.new | \
341 LC_ALL=C sort -t : -k2,2n -u | \
342 sed -ne "/^:[1-9][0-9]* $octet20\$/p" > hg2git-marks
343 rm hg2git-marks.old hg2git-marks.new
344 rm -f hg2git-heads
345 git branch --no-color | \
346 while IFS= read -r _head; do
347 echo ":${_head#??} $(git rev-parse "refs/heads/${_head#??}")"
348 done > hg2git-heads