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