3 # This is generic shell library for all the scripts used by Girocco;
4 # most importantly, it introduces all the $cfg_* shell variables.
6 # A well-known UTF-8 locale is required for some of the fast-import providers
7 # in order to avoid mangling characters. Ideally we could use "POSIX.UTF-8"
8 # but that is not reliably UTF-8 but rather usually US-ASCII.
9 # So we choose a well-known UTF-8 locale that should be understood everywhere
10 # and always means UTF-8. We only set LC_ALL to this when running the
11 # fast-import providers. Also to avoid complaints from sort we temporarily set
12 # the locale to "C" when running sort.
13 use_locale
=en_US.UTF-8
16 octet
='[0-9a-f][0-9a-f]'
17 octet4
="$octet$octet$octet$octet"
18 octet19
="$octet4$octet4$octet4$octet4$octet$octet$octet"
19 octet20
="$octet4$octet4$octet4$octet4$octet4"
21 get_girocco_config_var_list
() {
22 # Export all the variables from Girocco::Config to suitable var= lines
23 # prefixing them with 'cfg_'. E.g. $cfg_admin is admin's mail address now
24 # and also setting a 'defined_cfg_' prefix to 1 if they are not undef.
25 __girocco_conf
="$GIROCCO_CONF"
26 [ -n "$__girocco_conf" ] || __girocco_conf
="Girocco::Config"
27 [ -z "$basedir" ] || __girocco_extrainc
="-I$basedir"
28 perl
-I@basedir@
$__girocco_extrainc -M$__girocco_conf -le \
29 'foreach (sort {uc($a) cmp uc($b)} keys %Girocco::Config::) {
30 my $val = ${$Girocco::Config::{$_}}; defined($val) or $val="";
31 $val =~ s/([\\"\$\`])/\\$1/gos;
32 $val =~ s/(?:\r\n|\r|\n)$//os;
33 print "cfg_$_=\"$val\"";
34 print "defined_cfg_$_=",
35 (defined(${$Girocco::Config::{$_}})?"1":"");
39 # If basedir has been replaced, and shlib_vars.sh exists, get the config
40 # definitions from it rather than running Perl.
41 if [ "@basedir@" = '@'basedir
'@' ] ||
! [ -r "@basedir@/shlib_vars.sh" ]; then
42 # Import all the variables from Girocco::Config to the local environment,
43 eval "$(get_girocco_config_var_list)"
45 # Import the variables from shlib_vars.sh which avoids needlessly
46 # running another copy of Perl
47 .
"@basedir@/shlib_vars.sh"
50 # We cannot use a git() {} or nc_openbsd() {} function to redirect git
51 # and nc_openbsd to the desired executables because when using
52 # "ENV_VAR=xxx func" the various /bin/sh implementations behave in various
53 # different and unexpected ways:
54 # a) treat "ENV_VAR=xxx" like a separate, preceding "export ENV_VAR=xxx"
55 # b) treat "ENV_VAR=xxx" like a separate, prededing "ENV_VAR=xxx"
56 # c) treat "ENV_VAR=xxx" like a temporary setting only while running func
57 # None of these are good. We want a temporary "export ENV_VAR=xxx"
58 # setting only while running func which none of the /bin/sh's do.
60 # Instead we'd like to use an alias that provides the desired behavior without
61 # any of the bad (a), (b) or (c) effects.
63 # However, unfortunately, some of the crazy /bin/sh implementations do not
64 # recognize alias expansions when preceded by variable assignments!
66 # So we are left with git() {} and nc_openbsd() {} functions and in the
67 # case of git() {} we can compensate for (b) and (c) failing to export
68 # but not (a) and (b) persisting the values so the caller will simply
69 # have to beware and explicitly unset any variables that should not persist
70 # beyond the function call itself.
73 [ "${GIT_DIR+set}" = "set" ] && export GIT_DIR
74 [ "${GIT_SSL_NO_VERIFY+set}" = "set" ] && export GIT_SSL_NO_VERIFY
75 [ "${GIT_TRACE_PACKET+set}" = "set" ] && export GIT_TRACE_PACKET
76 [ "${GIT_USER_AGENT+set}" = "set" ] && export GIT_USER_AGENT
77 exec "$cfg_git_bin" "$@"
80 nc_openbsd
() { "$cfg_nc_openbsd_bin" "$@"; }
85 [ -z "$_list" ] || _list
="$_list, "
92 _mailer
="${cfg_sendmail_bin:-/usr/sbin/sendmail}"
93 if [ -n "$cfg_sender" ]; then
94 "$_mailer" -i -f "$cfg_sender" "$@"
102 if [ "$1" = "-s" ]; then
108 echo "From: \"$cfg_name\" ($cfg_title) <$cfg_admin>"
109 echo "To: $(_addrlist "$@
")"
110 [ -z "$_subject" ] ||
echo "Subject: $_subject"
111 echo "MIME-Version: 1.0"
112 echo "Content-Type: text/plain; charset=utf-8"
113 echo "Content-Transfer-Encoding: 8bit"
114 echo "Auto-Submitted: auto-generated"
120 # bang CMD... will execute the command with well-defined failure mode;
121 # set bang_action to string of the failed action ('clone', 'update', ...);
122 # pre-set bang_once=1 to make sure jobs banging on a repo repeatedly will
123 # not spam the owner; re-define the bang_trap() function to do custom
124 # cleanup before bailing out
126 if [ -n "$show_progress" ]; then
129 read -r errcode
<<-EOT || :
131 exec 4>&3 3>&1 1>&4 4>&-
132 { "$@" 3>&- || echo $? >&3; } 2>&1 | tee -a "$bang_log"
136 if [ -z "$errcode" ]; then
141 if "$@" >>"$bang_log" 2>&1; then
148 if ! [ -e .banged
] ||
[ -e .bangagain
]; then
151 ! [ -f HEAD
-a -f config
-a -d objects
] ||
152 bangmailok
="$(GIT_DIR=. git config --bool gitweb.statusupdates 2>/dev/null || echo true)"
154 [ "$bangmailok" = "false" -o -z "$mail" ] || bangaddrs
="$mail"
155 [ -z "$cfg_admincc" -o "$cfg_admincc" = "0" -o -z "$cfg_admin" ] ||
156 if [ -z "$bangaddrs" ]; then bangaddrs
="$cfg_admin"; else bangaddrs
="$bangaddrs,$cfg_admin"; fi
157 [ -z "$bangaddrs" ] ||
159 echo "$* failed with error code $errcode"
161 [ ! -n "$bang_once" ] ||
echo "you will not receive any more notifications until recovery"
162 echo "this status message may be disabled on the project admin page"
167 } |
mail -s "[$cfg_name] $proj $bang_action failed" "$bangaddrs"
174 # bang_eval CMD... will evaluate the command with well-defined failure mode;
175 # Identical to bang CMD... except the command is eval'd instead of executed.
180 # Default bang settings:
182 bang_action
="lame_programmer"
185 bang_log
="$(mktemp -t repomgr-XXXXXX)"
186 trap "rm \"\$bang_log\"" EXIT
190 # Progress report - if show_progress is set, shows the given message.
192 [ ! -n "$show_progress" ] ||
echo "$@"
196 # Project config accessors; must be run in project directory
198 git config
"gitweb.$1"
202 git config
"gitweb.$1" "$2" && chgrp repo config
&& chmod g
+w config
206 git config
"$1" "$2" && chgrp repo config
&& chmod g
+w config
209 config_get_date_seconds
() {
210 _dt
="$(config_get "$1" || :)"
211 [ -n "$_dt" ] ||
return 1
212 _ds
="$(perl -I@basedir@ -MGirocco::Util -e "print parse_rfc2822_date
('$_dt')")"
213 [ -n "$_ds" ] ||
return 1
217 # Tool for checking whether given number of seconds has not passed yet
219 os
="$(config_get_date_seconds "$1")" ||
return 1
221 [ $ns -lt $
(($os+$2)) ]
225 # List all Git repositories, with given prefix if specified, one-per-line
228 cut
-d : -f 1,3 "$cfg_chroot"/etc
/group |
grep "^$1"
230 cut
-d : -f 1,3 "$cfg_chroot"/etc
/group
231 fi |
while IFS
=: read name id
; do
232 [ $id -lt 65536 ] ||
echo "$name"
236 # returns true if the passed in git dir (defaults to ".") is a mirror using git fast-import
239 # always return false for non-mirrors
240 [ ! -e "$_gitdir/.nofetch" ] ||
return 1
241 _url
="$(GIT_DIR="$_gitdir" config_get baseurl 2>/dev/null || :)"
243 svn
://* | svn
+http
://* | svn
+https
://*)
244 # Don't think git-svn currently uses git fast-import
248 # darcs mirrors use git fast-import
252 # bzr mirrors use git fast-import
255 hg
+http
://* | hg
+https
://*)
256 # hg mirrors use git fast-import
260 # assume it does not use git fast-import
264 # returns true if the passed in git dir (defaults to ".") is a mirror using git-svn
267 # always return false for non-mirrors
268 [ ! -e "$_gitdir/.nofetch" ] ||
return 1
269 _url
="$(GIT_DIR="$_gitdir" config_get baseurl 2>/dev/null || :)"
271 svn
://* | svn
+http
://* | svn
+https
://*)
277 # hg-fast-export | git fast-import with error handling in current directory GIT_DIR
281 _python
="${PYTHON:-python}"
285 { read -r _err1 ||
:; read -r _err2 ||
:; } <<-EOT
287 exec 4>&3 3>&1 1>&4 4>&-
290 [ -f hg2git-marks ] || touch hg2git-marks
291 _af="$(git config hg.authorsfile || :)"
292 _cmd='GIT_DIR="$(pwd)" "$_python" "$cfg_basedir/bin/hg-fast-export.py" \
293 --repo "$(pwd)/repo.hg" \
294 --marks "$(pwd)/hg2git-marks" \
295 --mapping "$(pwd)/hg2git-mapping" \
296 --heads "$(pwd)/hg2git-heads" \
297 --status "$(pwd)/hg2git-state" \
298 -U unknown --force --flatten --hg-hash'
299 [ -z "$_af" ] || _cmd="$_cmd"' --authors "$_af"'
300 eval "$_cmd" 3>&- || _e1=$?
305 rm -f hg2git-marks.new
307 --export-marks="$(pwd)/hg2git-marks.new" \
308 --export-pack-edges="$(pwd)/gfi-packs" \
309 --force 3>&- || _e2=$?
315 [ "$_err1" = 0 -a "$_err2" = 0 ] ||
return 1
316 if [ -f hg2git-marks
]; then
317 rm -f hg2git-marks.old
318 mv hg2git-marks hg2git-marks.old
320 touch hg2git-marks.old
322 cat hg2git-marks.old hg2git-marks.new | \
323 LC_ALL
=C
sort -t : -k2,2n
-u | \
324 sed -ne "/^:[1-9][0-9]* $octet20\$/p" > hg2git-marks
325 rm hg2git-marks.old hg2git-marks.new
327 git branch
--no-color | \
328 while IFS
= read -r _head
; do
329 echo ":${_head#??} $(git rev-parse "refs
/heads
/${_head#??}")"