Project.pm: enable repack.writebitmaps when creating repositories
[girocco.git] / shlib.sh
blob880332991aa061e9dd4728b6724bad75daee8b83
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 # A well-known UTF-8 locale is required for some of the fast-import providers
7 # in order to avoid mangling characters. Ideally we could use "POSIX.UTF-8"
8 # but that is not reliably UTF-8 but rather usually US-ASCII.
9 # So we choose a well-known UTF-8 locale that should be understood everywhere
10 # and always means UTF-8. We only set LC_ALL to this when running the
11 # fast-import providers. Also to avoid complaints from sort we temporarily set
12 # the locale to "C" when running sort.
13 use_locale=en_US.UTF-8
15 # SHA-1 pattern
16 octet='[0-9a-f][0-9a-f]'
17 octet4="$octet$octet$octet$octet"
18 octet19="$octet4$octet4$octet4$octet4$octet$octet$octet"
19 octet20="$octet4$octet4$octet4$octet4$octet4"
21 get_girocco_config_var_list() {
22 # Export all the variables from Girocco::Config to suitable var= lines
23 # prefixing them with 'cfg_'. E.g. $cfg_admin is admin's mail address now
24 # and also setting a 'defined_cfg_' prefix to 1 if they are not undef.
25 __girocco_conf="$GIROCCO_CONF"
26 [ -n "$__girocco_conf" ] || __girocco_conf="Girocco::Config"
27 [ -z "$basedir" ] || __girocco_extrainc="-I$basedir"
28 perl -I@basedir@ $__girocco_extrainc -M$__girocco_conf -le \
29 'foreach (sort {uc($a) cmp uc($b)} keys %Girocco::Config::) {
30 my $val = ${$Girocco::Config::{$_}}; defined($val) or $val="";
31 $val =~ s/([\\"\$\`])/\\$1/gos;
32 $val =~ s/(?:\r\n|\r|\n)$//os;
33 print "cfg_$_=\"$val\"";
34 print "defined_cfg_$_=",
35 (defined(${$Girocco::Config::{$_}})?"1":"");
39 # If basedir has been replaced, and shlib_vars.sh exists, get the config
40 # definitions from it rather than running Perl.
41 if [ "@basedir@" = '@'basedir'@' ] || ! [ -r "@basedir@/shlib_vars.sh" ]; then
42 # Import all the variables from Girocco::Config to the local environment,
43 eval "$(get_girocco_config_var_list)"
44 else
45 # Import the variables from shlib_vars.sh which avoids needlessly
46 # running another copy of Perl
47 . "@basedir@/shlib_vars.sh"
50 # We cannot use a git() {} or nc_openbsd() {} function to redirect git
51 # and nc_openbsd to the desired executables because when using
52 # "ENV_VAR=xxx func" the various /bin/sh implementations behave in various
53 # different and unexpected ways:
54 # a) treat "ENV_VAR=xxx" like a separate, preceding "export ENV_VAR=xxx"
55 # b) treat "ENV_VAR=xxx" like a separate, prededing "ENV_VAR=xxx"
56 # c) treat "ENV_VAR=xxx" like a temporary setting only while running func
57 # None of these are good. We want a temporary "export ENV_VAR=xxx"
58 # setting only while running func which none of the /bin/sh's do.
60 # Instead we'd like to use an alias that provides the desired behavior without
61 # any of the bad (a), (b) or (c) effects.
63 # However, unfortunately, some of the crazy /bin/sh implementations do not
64 # recognize alias expansions when preceded by variable assignments!
66 # So we are left with git() {} and nc_openbsd() {} functions and in the
67 # case of git() {} we can compensate for (b) and (c) failing to export
68 # but not (a) and (b) persisting the values so the caller will simply
69 # have to beware and explicitly unset any variables that should not persist
70 # beyond the function call itself.
72 git() (
73 [ "${GIT_DIR+set}" = "set" ] && export GIT_DIR
74 [ "${GIT_SSL_NO_VERIFY+set}" = "set" ] && export GIT_SSL_NO_VERIFY
75 [ "${GIT_TRACE_PACKET+set}" = "set" ] && export GIT_TRACE_PACKET
76 [ "${GIT_USER_AGENT+set}" = "set" ] && export GIT_USER_AGENT
77 exec "$cfg_git_bin" "$@"
80 nc_openbsd() { "$cfg_nc_openbsd_bin" "$@"; }
82 _addrlist() {
83 _list=
84 for _addr in "$@"; do
85 [ -z "$_list" ] || _list="$_list, "
86 _list="$_list$_addr"
87 done
88 echo "$_list"
91 _sendmail() {
92 _mailer="${cfg_sendmail_bin:-/usr/sbin/sendmail}"
93 if [ -n "$cfg_sender" ]; then
94 "$_mailer" -i -f "$cfg_sender" "$@"
95 else
96 "$_mailer" -i "$@"
100 mail() {
101 _subject=
102 if [ "$1" = "-s" ]; then
103 shift
104 _subject="$1"
105 shift
108 echo "From: \"$cfg_name\" ($cfg_title) <$cfg_admin>"
109 echo "To: $(_addrlist "$@")"
110 [ -z "$_subject" ] || echo "Subject: $_subject"
111 echo "MIME-Version: 1.0"
112 echo "Content-Type: text/plain; charset=utf-8"
113 echo "Content-Transfer-Encoding: 8bit"
114 echo "Auto-Submitted: auto-generated"
115 echo ""
117 } | _sendmail "$@"
120 # bang CMD... will execute the command with well-defined failure mode;
121 # set bang_action to string of the failed action ('clone', 'update', ...);
122 # pre-set bang_once=1 to make sure jobs banging on a repo repeatedly will
123 # not spam the owner; re-define the bang_trap() function to do custom
124 # cleanup before bailing out
125 bang() {
126 if [ -n "$show_progress" ]; then
127 exec 3>&1
128 errcode=
129 read -r errcode <<-EOT || :
131 exec 4>&3 3>&1 1>&4 4>&-
132 { "$@" 3>&- || echo $? >&3; } 2>&1 | tee -a "$bang_log"
135 exec 3>&-
136 if [ -z "$errcode" ]; then
137 # All right. Cool.
138 return;
140 else
141 if "$@" >>"$bang_log" 2>&1; then
142 # All right. Cool.
143 return;
144 else
145 errcode="$?"
148 if ! [ -e .banged ] || [ -e .bangagain ]; then
149 rm -f .bangagain
150 bangmailok=true
151 ! [ -f HEAD -a -f config -a -d objects ] ||
152 bangmailok="$(GIT_DIR=. git config --bool gitweb.statusupdates 2>/dev/null || echo true)"
153 bangaddrs=''
154 [ "$bangmailok" = "false" -o -z "$mail" ] || bangaddrs="$mail"
155 [ -z "$cfg_admincc" -o "$cfg_admincc" = "0" -o -z "$cfg_admin" ] ||
156 if [ -z "$bangaddrs" ]; then bangaddrs="$cfg_admin"; else bangaddrs="$bangaddrs,$cfg_admin"; fi
157 [ -z "$bangaddrs" ] ||
159 echo "$* failed with error code $errcode"
160 echo ""
161 [ ! -n "$bang_once" ] || echo "you will not receive any more notifications until recovery"
162 echo "this status message may be disabled on the project admin page"
163 echo ""
164 echo "Log follows:"
165 echo ""
166 cat "$bang_log"
167 } | mail -s "[$cfg_name] $proj $bang_action failed" "$bangaddrs"
169 touch .banged
170 bang_trap
171 exit 1
174 # bang_eval CMD... will evaluate the command with well-defined failure mode;
175 # Identical to bang CMD... except the command is eval'd instead of executed.
176 bang_eval() {
177 bang eval "$*"
180 # Default bang settings:
181 bang_setup() {
182 bang_action="lame_programmer"
183 bang_once=
184 bang_trap() { :; }
185 bang_log="$(mktemp -t repomgr-XXXXXX)"
186 trap "rm \"\$bang_log\"" EXIT
190 # Progress report - if show_progress is set, shows the given message.
191 progress() {
192 [ ! -n "$show_progress" ] || echo "$@"
196 # Project config accessors; must be run in project directory
197 config_get() {
198 git config "gitweb.$1"
201 config_set() {
202 git config "gitweb.$1" "$2" && chgrp repo config && chmod g+w config
205 config_set_raw() {
206 git config "$1" "$2" && chgrp repo config && chmod g+w config
209 config_get_date_seconds() {
210 _dt="$(config_get "$1" || :)"
211 [ -n "$_dt" ] || return 1
212 _ds="$(perl -I@basedir@ -MGirocco::Util -e "print parse_rfc2822_date('$_dt')")"
213 [ -n "$_ds" ] || return 1
214 echo "$_ds"
217 # Tool for checking whether given number of seconds has not passed yet
218 check_interval() {
219 os="$(config_get_date_seconds "$1")" || return 1
220 ns="$(date +%s)"
221 [ $ns -lt $(($os+$2)) ]
225 # List all Git repositories, with given prefix if specified, one-per-line
226 get_repo_list() {
227 if [ -n "$1" ]; then
228 cut -d : -f 1,3 "$cfg_chroot"/etc/group | grep "^$1"
229 else
230 cut -d : -f 1,3 "$cfg_chroot"/etc/group
231 fi | while IFS=: read name id; do
232 [ $id -lt 65536 ] || echo "$name"
233 done
236 # returns true if the passed in git dir (defaults to ".") is a mirror using git fast-import
237 is_gfi_mirror() {
238 _gitdir="${1-.}"
239 # always return false for non-mirrors
240 [ ! -e "$_gitdir/.nofetch" ] || return 1
241 _url="$(GIT_DIR="$_gitdir" config_get baseurl 2>/dev/null || :)"
242 case "$_url" in
243 svn://* | svn+http://* | svn+https://*)
244 # Don't think git-svn currently uses git fast-import
245 return 1
247 darcs://*)
248 # darcs mirrors use git fast-import
249 return 0
251 bzr://*)
252 # bzr mirrors use git fast-import
253 return 0
255 hg+http://* | hg+https://*)
256 # hg mirrors use git fast-import
257 return 0
259 esac
260 # assume it does not use git fast-import
261 return 1
264 # returns true if the passed in git dir (defaults to ".") is a mirror using git-svn
265 is_svn_mirror() {
266 _gitdir="${1-.}"
267 # always return false for non-mirrors
268 [ ! -e "$_gitdir/.nofetch" ] || return 1
269 _url="$(GIT_DIR="$_gitdir" config_get baseurl 2>/dev/null || :)"
270 case "$_url" in
271 svn://* | svn+http://* | svn+https://*)
272 return 0
273 esac
274 return 1
277 # hg-fast-export | git fast-import with error handling in current directory GIT_DIR
278 git_hg_fetch() (
279 LC_ALL="$use_locale"
280 export LC_ALL
281 _python="${PYTHON:-python}"
282 _err1=
283 _err2=
284 exec 3>&1
285 { read -r _err1 || :; read -r _err2 || :; } <<-EOT
287 exec 4>&3 3>&1 1>&4 4>&-
289 _e1=0
290 [ -f hg2git-marks ] || touch hg2git-marks
291 _af="$(git config hg.authorsfile || :)"
292 _cmd='GIT_DIR="$(pwd)" "$_python" "$cfg_basedir/bin/hg-fast-export.py" \
293 --repo "$(pwd)/repo.hg" \
294 --marks "$(pwd)/hg2git-marks" \
295 --mapping "$(pwd)/hg2git-mapping" \
296 --heads "$(pwd)/hg2git-heads" \
297 --status "$(pwd)/hg2git-state" \
298 -U unknown --force --flatten --hg-hash'
299 [ -z "$_af" ] || _cmd="$_cmd"' --authors "$_af"'
300 eval "$_cmd" 3>&- || _e1=$?
301 echo $_e1 >&3
302 } | \
304 _e2=0
305 rm -f hg2git-marks.new
306 git fast-import \
307 --export-marks="$(pwd)/hg2git-marks.new" \
308 --export-pack-edges="$(pwd)/gfi-packs" \
309 --force 3>&- || _e2=$?
310 echo $_e2 >&3
314 exec 3>&-
315 [ "$_err1" = 0 -a "$_err2" = 0 ] || return 1
316 if [ -f hg2git-marks ]; then
317 rm -f hg2git-marks.old
318 mv hg2git-marks hg2git-marks.old
319 else
320 touch hg2git-marks.old
322 cat hg2git-marks.old hg2git-marks.new | \
323 LC_ALL=C sort -t : -k2,2n -u | \
324 sed -ne "/^:[1-9][0-9]* $octet20\$/p" > hg2git-marks
325 rm hg2git-marks.old hg2git-marks.new
326 rm -f hg2git-heads
327 git branch --no-color | \
328 while IFS= read -r _head; do
329 echo ":${_head#??} $(git rev-parse "refs/heads/${_head#??}")"
330 done > hg2git-heads