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