gc: avoid unnecessary garbage collections
[girocco.git] / shlib.sh
blob2cfc180f8048c077bd2d3ceab4851ad4374a28fa
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 (sort {uc($a) cmp uc($b)} keys %Girocco::Config::) {
15 my $val = ${$Girocco::Config::{$_}}; defined($val) or $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 "MIME-Version: 1.0"
80 echo "Content-Type: text/plain; charset=utf-8"
81 echo "Content-Transfer-Encoding: 8bit"
82 echo "Auto-Submitted: auto-generated"
83 echo ""
84 cat
85 } | _sendmail "$@"
88 # Supports both the bang CMD... and bang_eval CMD... functions
89 # Called when the command fails
90 # Should only be called from bang and bang_eval functions
91 bang_err() {
92 if ! [ -e .banged ]; then
93 bangmailok=true
94 ! [ -f HEAD -a -f config -a -d objects ] ||
95 bangmailok="$(GIT_DIR=. git config --bool gitweb.statusupdates 2>/dev/null || echo true)"
96 bangaddrs=''
97 [ "$bangmailok" = "false" -o -z "$mail" ] || bangaddrs="$mail"
98 [ -z "$cfg_admincc" -o "$cfg_admincc" = "0" -o -z "$cfg_admin" ] ||
99 if [ -z "$bangaddrs" ]; then bangaddrs="$cfg_admin"; else bangaddrs="$bangaddrs,$cfg_admin"; fi
100 [ -z "$bangaddrs" ] ||
102 echo "$* failed with error code $errcode"
103 echo ""
104 [ ! -n "$bang_once" ] || echo "you will not receive any more notifications until recovery"
105 echo "this status message may be disabled on the project admin page"
106 echo ""
107 echo "Log follows:"
108 echo ""
109 cat "$bang_log"
110 } | mail -s "[$cfg_name] $proj $bang_action failed" "$bangaddrs"
112 touch .banged
113 bang_trap
114 exit 1
117 # bang CMD... will execute the command with well-defined failure mode;
118 # set bang_action to string of the failed action ('clone', 'update', ...);
119 # pre-set bang_once=1 to make sure jobs banging on a repo repeatedly will
120 # not spam the owner; re-define the bang_trap() function to do custom
121 # cleanup before bailing out
122 bang() {
123 if [ -n "$show_progress" ]; then
124 exec 3>&1
125 errcode=
126 read -r errcode <<-EOT || :
128 exec 4>&3 3>&1 1>&4 4>&-
129 { "$@" 3>&- || echo $? >&3; } 2>&1 | tee -a "$bang_log"
132 exec 3>&-
133 if [ -z "$errcode" ]; then
134 # All right. Cool.
135 return;
137 else
138 if "$@" >>"$bang_log" 2>&1; then
139 # All right. Cool.
140 return;
141 else
142 errcode="$?"
145 bang_err "$@"
148 # bang_eval CMD... will evaluate the command with well-defined failure mode;
149 # Identical to bang CMD... except the command is eval'd instead of executed.
150 bang_eval() {
151 if [ -n "$show_progress" ]; then
152 exec 3>&1
153 errcode=
154 read -r errcode <<-EOT || :
156 exec 4>&3 3>&1 1>&4 4>&-
157 { eval "$*" 3>&- || echo $? >&3; } 2>&1 | tee -a "$bang_log"
160 exec 3>&-
161 if [ -z "$errcode" ]; then
162 # All right. Cool.
163 return;
165 else
166 if eval "$*" >>"$bang_log" 2>&1; then
167 # All right. Cool.
168 return;
169 else
170 errcode="$?"
173 bang_err "$@"
176 # Default bang settings:
177 bang_setup() {
178 bang_action="lame_programmer"
179 bang_once=
180 bang_trap() { :; }
181 bang_log="$(mktemp -t repomgr-XXXXXX)"
182 trap "rm \"\$bang_log\"" EXIT
186 # Progress report - if show_progress is set, shows the given message.
187 progress() {
188 [ ! -n "$show_progress" ] || echo "$@"
192 # Project config accessors; must be run in project directory
193 config_get() {
194 git config "gitweb.$1"
197 config_set() {
198 git config "gitweb.$1" "$2" && chgrp repo config && chmod g+w config
201 config_set_raw() {
202 git config "$1" "$2" && chgrp repo config && chmod g+w config
205 config_get_date_seconds() {
206 _dt="$(config_get "$1" || :)"
207 [ -n "$_dt" ] || return 1
208 _ds="$(perl -I@basedir@ -MGirocco::Util -e "print parse_rfc2822_date('$_dt')")"
209 [ -n "$_ds" ] || return 1
210 echo "$_ds"
213 # Tool for checking whether given number of seconds has not passed yet
214 check_interval() {
215 os="$(config_get_date_seconds "$1")" || return 1
216 ns="$(date +%s)"
217 [ $ns -lt $(($os+$2)) ]
221 # List all Git repositories, with given prefix if specified, one-per-line
222 get_repo_list() {
223 if [ -n "$1" ]; then
224 cut -d : -f 1,3 "$cfg_chroot"/etc/group | grep "^$1"
225 else
226 cut -d : -f 1,3 "$cfg_chroot"/etc/group
227 fi | while IFS=: read name id; do
228 [ $id -lt 65536 ] || echo "$name"
229 done
232 # returns true if the passed in git dir (defaults to ".") is a mirror using git fast-import
233 is_gfi_mirror() {
234 _gitdir="${1-.}"
235 # always return false for non-mirrors
236 [ ! -e "$_gitdir/.nofetch" ] || return 1
237 _url="$(GIT_DIR="$_gitdir" config_get baseurl 2>/dev/null || :)"
238 case "$_url" in
239 svn://* | svn+http://* | svn+https://*)
240 # Don't think git-svn currently uses git fast-import
241 return 1
243 darcs://*)
244 # darcs mirrors use git fast-import
245 return 0
247 bzr://*)
248 # bzr mirrors use git fast-import
249 return 0
251 hg+http://* | hg+https://*)
252 # hg mirrors use git fast-import
253 return 0
255 esac
256 # assume it does not use git fast-import
257 return 1
260 # hg-fast-export | git fast-import with error handling in current directory GIT_DIR
261 git_hg_fetch() {
262 _python="${PYTHON:-python}"
263 _err1=
264 _err2=
265 exec 3>&1
266 { read -r _err1 || :; read -r _err2 || :; } <<-EOT
268 exec 4>&3 3>&1 1>&4 4>&-
270 _e1=0
271 [ -f hg2git-marks ] || touch hg2git-marks
272 _af="$(git config hg.authorsfile || :)"
273 _cmd='GIT_DIR="$(pwd)" "$_python" "$cfg_basedir/bin/hg-fast-export.py" \
274 --repo "$(pwd)/repo.hg" \
275 --marks "$(pwd)/hg2git-marks" \
276 --mapping "$(pwd)/hg2git-mapping" \
277 --heads "$(pwd)/hg2git-heads" \
278 --status "$(pwd)/hg2git-state" \
279 -U unknown --force --flatten'
280 [ -z "$_af" ] || _cmd="$_cmd"' --authors "$_af"'
281 eval "$_cmd" 3>&- || _e1=$?
282 echo $_e1 >&3
283 } | \
285 _e2=0
286 rm -f hg2git-marks.new
287 git fast-import \
288 --export-marks="$(pwd)/hg2git-marks.new" \
289 --export-pack-edges="$(pwd)/gfi-packs" \
290 --force 3>&- || _e2=$?
291 echo $_e2 >&3
295 exec 3>&-
296 [ "$_err1" = 0 -a "$_err2" = 0 ] || return 1
297 if [ -f hg2git-marks ]; then
298 rm -f hg2git-marks.old
299 mv hg2git-marks hg2git-marks.old
300 else
301 touch hg2git-marks.old
303 cat hg2git-marks.old hg2git-marks.new | LC_ALL=C uniq > hg2git-marks
304 rm hg2git-marks.old hg2git-marks.new
305 rm -f hg2git-heads
306 git branch --no-color | \
307 while IFS= read -r _head; do
308 echo ":${_head#??} $(git rev-parse "refs/heads/${_head#??}")"
309 done > hg2git-heads