gc.sh: optimize darcs repositories
[girocco.git] / shlib.sh
blob95bdf94caa0b70a8561bb052c35f0d6f0629e66a
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.
7 # Import all the variables from Girocco::Config to the local environment,
8 # prefixing them with 'cfg_'. E.g. $cfg_admin is admin's mail address now.
9 __girocco_conf="$GIROCCO_CONF"
10 [ -n "$__girocco_conf" ] || __girocco_conf="Girocco::Config"
11 [ -z "$basedir" ] || __girocco_extrainc="-I$basedir"
12 eval $(perl -I@basedir@ $__girocco_extrainc -M$__girocco_conf -le \
13 'foreach (keys %Girocco::Config::) {
14 my $val = ${$Girocco::Config::{$_}}; $val ||= "";
15 print "cfg_$_=\"$val\"";
16 }')
19 git() (
20 # some poorly behaving /bin/sh implementations do not
21 # properly export variables when the command is actually
22 # a shell function -- they are set for the function call
23 # but not exported into the environment for anything the
24 # function executes. Export the two Git variables we
25 # care about if they are set. Since this is a subshell
26 # the export will be temporary.
27 [ -z "$GIT_DIR" ] || export GIT_DIR
28 [ -z "$GIT_SSL_NO_VERIFY" ] || export GIT_SSL_NO_VERIFY
29 exec "$cfg_git_bin" "$@"
32 nc_openbsd() { "$cfg_nc_openbsd_bin" "$@"; }
34 _addrlist() {
35 _list=
36 for _addr in "$@"; do
37 [ -z "$_list" ] || _list="$_list, "
38 _list="$_list$_addr"
39 done
40 echo "$_list"
43 _sendmail() {
44 _mailer="${cfg_sendmail_bin:-/usr/sbin/sendmail}"
45 if [ -n "$cfg_sender" ]; then
46 "$_mailer" -i -f "$cfg_sender" "$@"
47 else
48 "$_mailer" -i "$@"
52 mail() {
53 _subject=
54 if [ "$1" = "-s" ]; then
55 shift
56 _subject="$1"
57 shift
60 echo "From: \"$cfg_name\" ($cfg_title) <$cfg_admin>"
61 echo "To: $(_addrlist "$@")"
62 [ -z "$_subject" ] || echo "Subject: $_subject"
63 echo "Auto-Submitted: auto-generated"
64 echo ""
65 cat
66 } | _sendmail "$@"
69 # Supports both the bang CMD... and bang_eval CMD... functions
70 # Called when the command fails
71 # Should only be called from bang and bang_eval functions
72 bang_err() {
73 if ! [ -e .banged ]; then
74 bangmailok=true
75 ! [ -f HEAD -a -f config -a -d objects ] ||
76 bangmailok="$(GIT_DIR=. git config --bool gitweb.statusupdates 2>/dev/null || echo true)"
77 bangaddrs=''
78 [ "$bangmailok" = "false" -o -z "$mail" ] || bangaddrs="$mail"
79 [ -z "$cfg_admincc" -o "$cfg_admincc" = "0" -o -z "$cfg_admin" ] ||
80 if [ -z "$bangaddrs" ]; then bangaddrs="$cfg_admin"; else bangaddrs="$bangaddrs,$cfg_admin"; fi
81 [ -z "$bangaddrs" ] ||
83 echo "$* failed with error code $errcode"
84 echo ""
85 [ ! -n "$bang_once" ] || echo "you will not receive any more notifications until recovery"
86 echo "this status message may be disabled on the project admin page"
87 echo ""
88 echo "Log follows:"
89 echo ""
90 cat "$bang_log"
91 } | mail -s "[$cfg_name] $proj $bang_action failed" "$bangaddrs"
93 touch .banged
94 bang_trap
95 exit 1
98 # bang CMD... will execute the command with well-defined failure mode;
99 # set bang_action to string of the failed action ('clone', 'update', ...);
100 # pre-set bang_once=1 to make sure jobs banging on a repo repeatedly will
101 # not spam the owner; re-define the bang_trap() function to do custom
102 # cleanup before bailing out
103 bang() {
104 if [ -n "$show_progress" ]; then
105 exec 3>&1
106 errcode=
107 read -r errcode <<-EOT || :
109 exec 4>&3 3>&1 1>&4 4>&-
110 { "$@" 3>&- || echo $? >&3; } 2>&1 | tee -a "$bang_log"
113 exec 3>&-
114 if [ -z "$errcode" ]; then
115 # All right. Cool.
116 return;
118 else
119 if "$@" >>"$bang_log" 2>&1; then
120 # All right. Cool.
121 return;
122 else
123 errcode="$?"
126 bang_err "$@"
129 # bang_eval CMD... will evaluate the command with well-defined failure mode;
130 # Identical to bang CMD... except the command is eval'd instead of executed.
131 bang_eval() {
132 if [ -n "$show_progress" ]; then
133 exec 3>&1
134 errcode=
135 read -r errcode <<-EOT || :
137 exec 4>&3 3>&1 1>&4 4>&-
138 { eval "$*" 3>&- || echo $? >&3; } 2>&1 | tee -a "$bang_log"
141 exec 3>&-
142 if [ -z "$errcode" ]; then
143 # All right. Cool.
144 return;
146 else
147 if eval "$*" >>"$bang_log" 2>&1; then
148 # All right. Cool.
149 return;
150 else
151 errcode="$?"
154 bang_err "$@"
157 # Default bang settings:
158 bang_setup() {
159 bang_action="lame_programmer"
160 bang_once=
161 bang_trap() { :; }
162 bang_log="$(mktemp -t repomgr-XXXXXX)"
163 trap "rm \"\$bang_log\"" EXIT
167 # Progress report - if show_progress is set, shows the given message.
168 progress() {
169 [ ! -n "$show_progress" ] || echo "$@"
173 # Project config accessors; must be run in project directory
174 config_get() {
175 git config "gitweb.$1"
178 config_set() {
179 git config "gitweb.$1" "$2" && chgrp repo config && chmod g+w config
182 config_set_raw() {
183 git config "$1" "$2" && chgrp repo config && chmod g+w config
187 # Tool for checking whether given number of seconds has not passed yet
188 check_interval() {
189 od="$(config_get "$1" || :)"
190 [ -n "$od" ] || return 1
191 os="$(perl -I@basedir@ -MGirocco::Util -e "print parse_rfc2822_date('$od')")"
192 [ -n "$os" ] || return 1
193 ns="$(date +%s)"
194 [ $ns -lt $(($os+$2)) ]
198 # List all Git repositories, with given prefix if specified, one-per-line
199 get_repo_list() {
200 if [ -n "$1" ]; then
201 cut -d : -f 1,3 "$cfg_chroot"/etc/group | grep "^$1"
202 else
203 cut -d : -f 1,3 "$cfg_chroot"/etc/group
204 fi | while IFS=: read name id; do
205 [ $id -lt 65536 ] || echo "$name"
206 done