git-*-verify: reject project names with leading _
[girocco.git] / shlib.sh
blob3cbec22e754e7dd9320e7e76aeccd43a2c52f537
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
52 unset GIT_USER_AGENT
53 if [ -n "$defined_cfg_git_client_ua" ]; then
54 GIT_USER_AGENT="$cfg_git_client_ua"
55 export GIT_USER_AGENT
58 # We cannot use a git() {} or nc_openbsd() {} function to redirect git
59 # and nc_openbsd to the desired executables because when using
60 # "ENV_VAR=xxx func" the various /bin/sh implementations behave in various
61 # different and unexpected ways:
62 # a) treat "ENV_VAR=xxx" like a separate, preceding "export ENV_VAR=xxx"
63 # b) treat "ENV_VAR=xxx" like a separate, prededing "ENV_VAR=xxx"
64 # c) treat "ENV_VAR=xxx" like a temporary setting only while running func
65 # None of these are good. We want a temporary "export ENV_VAR=xxx"
66 # setting only while running func which none of the /bin/sh's do.
68 # Instead we'd like to use an alias that provides the desired behavior without
69 # any of the bad (a), (b) or (c) effects.
71 # However, unfortunately, some of the crazy /bin/sh implementations do not
72 # recognize alias expansions when preceded by variable assignments!
74 # So we are left with git() {} and nc_openbsd() {} functions and in the
75 # case of git() {} we can compensate for (b) and (c) failing to export
76 # but not (a) and (b) persisting the values so the caller will simply
77 # have to beware and explicitly unset any variables that should not persist
78 # beyond the function call itself.
80 git() (
81 [ "${GIT_DIR+set}" = "set" ] && export GIT_DIR
82 [ "${GIT_SSL_NO_VERIFY+set}" = "set" ] && export GIT_SSL_NO_VERIFY
83 [ "${GIT_TRACE_PACKET+set}" = "set" ] && export GIT_TRACE_PACKET
84 [ "${GIT_USER_AGENT+set}" = "set" ] && export GIT_USER_AGENT
85 exec "$cfg_git_bin" "$@"
88 nc_openbsd() { "$cfg_nc_openbsd_bin" "$@"; }
90 _addrlist() {
91 _list=
92 for _addr in "$@"; do
93 [ -z "$_list" ] || _list="$_list, "
94 _list="$_list$_addr"
95 done
96 echo "$_list"
99 _sendmail() {
100 _mailer="${cfg_sendmail_bin:-/usr/sbin/sendmail}"
101 if [ -n "$cfg_sender" ]; then
102 "$_mailer" -i -f "$cfg_sender" "$@"
103 else
104 "$_mailer" -i "$@"
108 mail() {
109 _subject=
110 if [ "$1" = "-s" ]; then
111 shift
112 _subject="$1"
113 shift
116 echo "From: \"$cfg_name\" ($cfg_title) <$cfg_admin>"
117 echo "To: $(_addrlist "$@")"
118 [ -z "$_subject" ] || echo "Subject: $_subject"
119 echo "MIME-Version: 1.0"
120 echo "Content-Type: text/plain; charset=utf-8"
121 echo "Content-Transfer-Encoding: 8bit"
122 echo "Auto-Submitted: auto-generated"
123 echo ""
125 } | _sendmail "$@"
128 # bang CMD... will execute the command with well-defined failure mode;
129 # set bang_action to string of the failed action ('clone', 'update', ...);
130 # pre-set bang_once=1 to make sure jobs banging on a repo repeatedly will
131 # not spam the owner; re-define the bang_trap() function to do custom
132 # cleanup before bailing out
133 bang() {
134 if [ -n "$show_progress" ]; then
135 exec 3>&1
136 errcode=
137 read -r errcode <<-EOT || :
139 exec 4>&3 3>&1 1>&4 4>&-
140 { "$@" 3>&- || echo $? >&3; } 2>&1 | tee -a "$bang_log"
143 exec 3>&-
144 if [ -z "$errcode" ]; then
145 # All right. Cool.
146 return;
148 else
149 if "$@" >>"$bang_log" 2>&1; then
150 # All right. Cool.
151 return;
152 else
153 errcode="$?"
156 if ! [ -e .banged ] || [ -e .bangagain ]; then
157 rm -f .bangagain
158 bangmailok=true
159 ! [ -f HEAD -a -f config -a -d objects ] ||
160 bangmailok="$(GIT_DIR=. git config --bool gitweb.statusupdates 2>/dev/null || echo true)"
161 bangaddrs=''
162 [ "$bangmailok" = "false" -o -z "$mail" ] || bangaddrs="$mail"
163 [ -z "$cfg_admincc" -o "$cfg_admincc" = "0" -o -z "$cfg_admin" ] ||
164 if [ -z "$bangaddrs" ]; then bangaddrs="$cfg_admin"; else bangaddrs="$bangaddrs,$cfg_admin"; fi
165 [ -z "$bangaddrs" ] ||
167 echo "$* failed with error code $errcode"
168 echo ""
169 [ ! -n "$bang_once" ] || echo "you will not receive any more notifications until recovery"
170 echo "this status message may be disabled on the project admin page"
171 echo ""
172 echo "Log follows:"
173 echo ""
174 cat "$bang_log"
175 } | mail -s "[$cfg_name] $proj $bang_action failed" "$bangaddrs"
177 touch .banged
178 bang_trap
179 exit 1
182 # bang_eval CMD... will evaluate the command with well-defined failure mode;
183 # Identical to bang CMD... except the command is eval'd instead of executed.
184 bang_eval() {
185 bang eval "$*"
188 # Default bang settings:
189 bang_setup() {
190 bang_action="lame_programmer"
191 bang_once=
192 bang_trap() { :; }
193 bang_log="$(mktemp -t repomgr-XXXXXX)"
194 trap "rm \"\$bang_log\"" EXIT
198 # Progress report - if show_progress is set, shows the given message.
199 progress() {
200 [ ! -n "$show_progress" ] || echo "$@"
204 # Project config accessors; must be run in project directory
205 config_get() {
206 git config "gitweb.$1"
209 config_set() {
210 git config "gitweb.$1" "$2" && chgrp repo config && chmod g+w config
213 config_set_raw() {
214 git config "$1" "$2" && chgrp repo config && chmod g+w config
217 config_get_date_seconds() {
218 _dt="$(config_get "$1" || :)"
219 [ -n "$_dt" ] || return 1
220 _ds="$(perl -I@basedir@ -MGirocco::Util -e "print parse_rfc2822_date('$_dt')")"
221 [ -n "$_ds" ] || return 1
222 echo "$_ds"
225 # Tool for checking whether given number of seconds has not passed yet
226 check_interval() {
227 os="$(config_get_date_seconds "$1")" || return 1
228 ns="$(date +%s)"
229 [ $ns -lt $(($os+$2)) ]
233 # List all Git repositories, with given prefix if specified, one-per-line
234 get_repo_list() {
235 if [ -n "$1" ]; then
236 cut -d : -f 1,3 "$cfg_chroot"/etc/group | grep "^$1"
237 else
238 cut -d : -f 1,3 "$cfg_chroot"/etc/group
239 fi | while IFS=: read name id; do
240 [ $id -lt 65536 ] || echo "$name"
241 done
244 # returns true if the passed in git dir (defaults to ".") is a mirror using git fast-import
245 is_gfi_mirror() {
246 _gitdir="${1-.}"
247 # always return false for non-mirrors
248 [ ! -e "$_gitdir/.nofetch" ] || return 1
249 _url="$(GIT_DIR="$_gitdir" config_get baseurl 2>/dev/null || :)"
250 case "$_url" in
251 svn://* | svn+http://* | svn+https://*)
252 # Don't think git-svn currently uses git fast-import
253 return 1
255 darcs://*)
256 # darcs mirrors use git fast-import
257 return 0
259 bzr://*)
260 # bzr mirrors use git fast-import
261 return 0
263 hg+http://* | hg+https://*)
264 # hg mirrors use git fast-import
265 return 0
267 esac
268 # assume it does not use git fast-import
269 return 1
272 # returns true if the passed in git dir (defaults to ".") is a mirror using git-svn
273 is_svn_mirror() {
274 _gitdir="${1-.}"
275 # always return false for non-mirrors
276 [ ! -e "$_gitdir/.nofetch" ] || return 1
277 _url="$(GIT_DIR="$_gitdir" config_get baseurl 2>/dev/null || :)"
278 case "$_url" in
279 svn://* | svn+http://* | svn+https://*)
280 return 0
281 esac
282 return 1
285 # A well-known UTF-8 locale is required for some of the fast-import providers
286 # in order to avoid mangling characters. Ideally we could use "POSIX.UTF-8"
287 # but that is not reliably UTF-8 but rather usually US-ASCII.
288 # We parse the output of `locale -a` and select a suitable UTF-8 locale.
289 # If we cannot find one in the `locale -a` output then we just use a well-known
290 # UTF-8 locale and hope for the best. We set LC_ALL to our choice and export
291 # it. We only set this temporarily when running the fast-import providers.
292 set_utf8_locale() {
293 _guess_locale="$(locale -a | grep -viE '^(posix|c)(\..*)?$' | \
294 grep -iE '\.utf-?8$' | sed -e 's/\.[Uu][Tt][Ff]-*8$//' | \
295 sed -e '/en_US/ s/^/0 /; /en_US/ !s/^/1 /' | LC_ALL=C sort | \
296 head -n 1 | cut -d ' ' -f 2)"
297 LC_ALL="${_guess_locale:-en_US}.UTF-8"
298 export LC_ALL
301 # hg-fast-export | git fast-import with error handling in current directory GIT_DIR
302 git_hg_fetch() (
303 set_utf8_locale
304 _python="${PYTHON:-python}"
305 _err1=
306 _err2=
307 exec 3>&1
308 { read -r _err1 || :; read -r _err2 || :; } <<-EOT
310 exec 4>&3 3>&1 1>&4 4>&-
312 _e1=0
313 [ -f hg2git-marks ] || touch hg2git-marks
314 _af="$(git config hg.authorsfile || :)"
315 _cmd='GIT_DIR="$(pwd)" "$_python" "$cfg_basedir/bin/hg-fast-export.py" \
316 --repo "$(pwd)/repo.hg" \
317 --marks "$(pwd)/hg2git-marks" \
318 --mapping "$(pwd)/hg2git-mapping" \
319 --heads "$(pwd)/hg2git-heads" \
320 --status "$(pwd)/hg2git-state" \
321 -U unknown --force --flatten --hg-hash'
322 [ -z "$_af" ] || _cmd="$_cmd"' --authors "$_af"'
323 eval "$_cmd" 3>&- || _e1=$?
324 echo $_e1 >&3
325 } | \
327 _e2=0
328 rm -f hg2git-marks.new
329 git fast-import \
330 --export-marks="$(pwd)/hg2git-marks.new" \
331 --export-pack-edges="$(pwd)/gfi-packs" \
332 --force 3>&- || _e2=$?
333 echo $_e2 >&3
337 exec 3>&-
338 [ "$_err1" = 0 -a "$_err2" = 0 ] || return 1
339 if [ -f hg2git-marks ]; then
340 rm -f hg2git-marks.old
341 mv hg2git-marks hg2git-marks.old
342 else
343 touch hg2git-marks.old
345 cat hg2git-marks.old hg2git-marks.new | \
346 LC_ALL=C sort -t : -k2,2n -u | \
347 sed -ne "/^:[1-9][0-9]* $octet20\$/p" > hg2git-marks
348 rm hg2git-marks.old hg2git-marks.new
349 rm -f hg2git-heads
350 git branch --no-color | \
351 while IFS= read -r _head; do
352 echo ":${_head#??} $(git rev-parse "refs/heads/${_head#??}")"
353 done > hg2git-heads