config: make the leading $jailreporoot/ prefix optional on ssh: URLs
[girocco.git] / shlib.sh
blob3dde0cbbe5ad8f959e0d5aef8a672405499885a4
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 get_girocco_config_var_list() {
7 # Export all the variables from Girocco::Config to suitable var= lines
8 # prefixing them with 'cfg_'. E.g. $cfg_admin is admin's mail address now
9 # and also setting a 'defined_cfg_' prefix to 1 if they are not undef.
10 __girocco_conf="$GIROCCO_CONF"
11 [ -n "$__girocco_conf" ] || __girocco_conf="Girocco::Config"
12 [ -z "$basedir" ] || __girocco_extrainc="-I$basedir"
13 perl -I@basedir@ $__girocco_extrainc -M$__girocco_conf -le \
14 'foreach (keys %Girocco::Config::) {
15 my $val = ${$Girocco::Config::{$_}}; $val ||= "";
16 $val =~ s/([\\"!\$\`])/\\$1/gos;
17 $val =~ s/(?:\r\n|\r|\n)$//os;
18 print "cfg_$_=\"$val\"";
19 print "defined_cfg_$_=",
20 (defined(${$Girocco::Config::{$_}})?"1":"");
24 # If basedir has been replaced, and shlib_vars.sh exists, get the config
25 # definitions from it rather than running Perl.
26 if [ "@basedir@" = '@'basedir'@' ] || ! [ -r "@basedir@/shlib_vars.sh" ]; then
27 # Import all the variables from Girocco::Config to the local environment,
28 eval "$(get_girocco_config_var_list)"
29 else
30 # Import the variables from shlib_vars.sh which avoids needlessly
31 # running another copy of Perl
32 . "@basedir@/shlib_vars.sh"
35 git() (
36 # some poorly behaving /bin/sh implementations do not
37 # properly export variables when the command is actually
38 # a shell function -- they are set for the function call
39 # but not exported into the environment for anything the
40 # function executes. Export the two Git variables we
41 # care about if they are set. Since this is a subshell
42 # the export will be temporary.
43 [ -z "$GIT_DIR" ] || export GIT_DIR
44 [ -z "$GIT_SSL_NO_VERIFY" ] || export GIT_SSL_NO_VERIFY
45 exec "$cfg_git_bin" "$@"
48 nc_openbsd() { "$cfg_nc_openbsd_bin" "$@"; }
50 _addrlist() {
51 _list=
52 for _addr in "$@"; do
53 [ -z "$_list" ] || _list="$_list, "
54 _list="$_list$_addr"
55 done
56 echo "$_list"
59 _sendmail() {
60 _mailer="${cfg_sendmail_bin:-/usr/sbin/sendmail}"
61 if [ -n "$cfg_sender" ]; then
62 "$_mailer" -i -f "$cfg_sender" "$@"
63 else
64 "$_mailer" -i "$@"
68 mail() {
69 _subject=
70 if [ "$1" = "-s" ]; then
71 shift
72 _subject="$1"
73 shift
76 echo "From: \"$cfg_name\" ($cfg_title) <$cfg_admin>"
77 echo "To: $(_addrlist "$@")"
78 [ -z "$_subject" ] || echo "Subject: $_subject"
79 echo "Auto-Submitted: auto-generated"
80 echo ""
81 cat
82 } | _sendmail "$@"
85 # Supports both the bang CMD... and bang_eval CMD... functions
86 # Called when the command fails
87 # Should only be called from bang and bang_eval functions
88 bang_err() {
89 if ! [ -e .banged ]; then
90 bangmailok=true
91 ! [ -f HEAD -a -f config -a -d objects ] ||
92 bangmailok="$(GIT_DIR=. git config --bool gitweb.statusupdates 2>/dev/null || echo true)"
93 bangaddrs=''
94 [ "$bangmailok" = "false" -o -z "$mail" ] || bangaddrs="$mail"
95 [ -z "$cfg_admincc" -o "$cfg_admincc" = "0" -o -z "$cfg_admin" ] ||
96 if [ -z "$bangaddrs" ]; then bangaddrs="$cfg_admin"; else bangaddrs="$bangaddrs,$cfg_admin"; fi
97 [ -z "$bangaddrs" ] ||
99 echo "$* failed with error code $errcode"
100 echo ""
101 [ ! -n "$bang_once" ] || echo "you will not receive any more notifications until recovery"
102 echo "this status message may be disabled on the project admin page"
103 echo ""
104 echo "Log follows:"
105 echo ""
106 cat "$bang_log"
107 } | mail -s "[$cfg_name] $proj $bang_action failed" "$bangaddrs"
109 touch .banged
110 bang_trap
111 exit 1
114 # bang CMD... will execute the command with well-defined failure mode;
115 # set bang_action to string of the failed action ('clone', 'update', ...);
116 # pre-set bang_once=1 to make sure jobs banging on a repo repeatedly will
117 # not spam the owner; re-define the bang_trap() function to do custom
118 # cleanup before bailing out
119 bang() {
120 if [ -n "$show_progress" ]; then
121 exec 3>&1
122 errcode=
123 read -r errcode <<-EOT || :
125 exec 4>&3 3>&1 1>&4 4>&-
126 { "$@" 3>&- || echo $? >&3; } 2>&1 | tee -a "$bang_log"
129 exec 3>&-
130 if [ -z "$errcode" ]; then
131 # All right. Cool.
132 return;
134 else
135 if "$@" >>"$bang_log" 2>&1; then
136 # All right. Cool.
137 return;
138 else
139 errcode="$?"
142 bang_err "$@"
145 # bang_eval CMD... will evaluate the command with well-defined failure mode;
146 # Identical to bang CMD... except the command is eval'd instead of executed.
147 bang_eval() {
148 if [ -n "$show_progress" ]; then
149 exec 3>&1
150 errcode=
151 read -r errcode <<-EOT || :
153 exec 4>&3 3>&1 1>&4 4>&-
154 { eval "$*" 3>&- || echo $? >&3; } 2>&1 | tee -a "$bang_log"
157 exec 3>&-
158 if [ -z "$errcode" ]; then
159 # All right. Cool.
160 return;
162 else
163 if eval "$*" >>"$bang_log" 2>&1; then
164 # All right. Cool.
165 return;
166 else
167 errcode="$?"
170 bang_err "$@"
173 # Default bang settings:
174 bang_setup() {
175 bang_action="lame_programmer"
176 bang_once=
177 bang_trap() { :; }
178 bang_log="$(mktemp -t repomgr-XXXXXX)"
179 trap "rm \"\$bang_log\"" EXIT
183 # Progress report - if show_progress is set, shows the given message.
184 progress() {
185 [ ! -n "$show_progress" ] || echo "$@"
189 # Project config accessors; must be run in project directory
190 config_get() {
191 git config "gitweb.$1"
194 config_set() {
195 git config "gitweb.$1" "$2" && chgrp repo config && chmod g+w config
198 config_set_raw() {
199 git config "$1" "$2" && chgrp repo config && chmod g+w config
203 # Tool for checking whether given number of seconds has not passed yet
204 check_interval() {
205 od="$(config_get "$1" || :)"
206 [ -n "$od" ] || return 1
207 os="$(perl -I@basedir@ -MGirocco::Util -e "print parse_rfc2822_date('$od')")"
208 [ -n "$os" ] || return 1
209 ns="$(date +%s)"
210 [ $ns -lt $(($os+$2)) ]
214 # List all Git repositories, with given prefix if specified, one-per-line
215 get_repo_list() {
216 if [ -n "$1" ]; then
217 cut -d : -f 1,3 "$cfg_chroot"/etc/group | grep "^$1"
218 else
219 cut -d : -f 1,3 "$cfg_chroot"/etc/group
220 fi | while IFS=: read name id; do
221 [ $id -lt 65536 ] || echo "$name"
222 done