Util.pm: is_dns_hostname no more than 255 characters allowed
[girocco.git] / shlib.sh
blobf57b9584e8b8493aae9b470352070b753039b661
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 exec "$cfg_git_bin" "$@"
71 nc_openbsd() { "$cfg_nc_openbsd_bin" "$@"; }
73 _addrlist() {
74 _list=
75 for _addr in "$@"; do
76 [ -z "$_list" ] || _list="$_list, "
77 _list="$_list$_addr"
78 done
79 echo "$_list"
82 _sendmail() {
83 _mailer="${cfg_sendmail_bin:-/usr/sbin/sendmail}"
84 if [ -n "$cfg_sender" ]; then
85 "$_mailer" -i -f "$cfg_sender" "$@"
86 else
87 "$_mailer" -i "$@"
91 mail() {
92 _subject=
93 if [ "$1" = "-s" ]; then
94 shift
95 _subject="$1"
96 shift
99 echo "From: \"$cfg_name\" ($cfg_title) <$cfg_admin>"
100 echo "To: $(_addrlist "$@")"
101 [ -z "$_subject" ] || echo "Subject: $_subject"
102 echo "MIME-Version: 1.0"
103 echo "Content-Type: text/plain; charset=utf-8"
104 echo "Content-Transfer-Encoding: 8bit"
105 echo "Auto-Submitted: auto-generated"
106 echo ""
108 } | _sendmail "$@"
111 # bang CMD... will execute the command with well-defined failure mode;
112 # set bang_action to string of the failed action ('clone', 'update', ...);
113 # pre-set bang_once=1 to make sure jobs banging on a repo repeatedly will
114 # not spam the owner; re-define the bang_trap() function to do custom
115 # cleanup before bailing out
116 bang() {
117 if [ -n "$show_progress" ]; then
118 exec 3>&1
119 errcode=
120 read -r errcode <<-EOT || :
122 exec 4>&3 3>&1 1>&4 4>&-
123 { "$@" 3>&- || echo $? >&3; } 2>&1 | tee -a "$bang_log"
126 exec 3>&-
127 if [ -z "$errcode" ]; then
128 # All right. Cool.
129 return;
131 else
132 if "$@" >>"$bang_log" 2>&1; then
133 # All right. Cool.
134 return;
135 else
136 errcode="$?"
139 if ! [ -e .banged ] || [ -e .bangagain ]; then
140 rm -f .bangagain
141 bangmailok=true
142 ! [ -f HEAD -a -f config -a -d objects ] ||
143 bangmailok="$(GIT_DIR=. git config --bool gitweb.statusupdates 2>/dev/null || echo true)"
144 bangaddrs=''
145 [ "$bangmailok" = "false" -o -z "$mail" ] || bangaddrs="$mail"
146 [ -z "$cfg_admincc" -o "$cfg_admincc" = "0" -o -z "$cfg_admin" ] ||
147 if [ -z "$bangaddrs" ]; then bangaddrs="$cfg_admin"; else bangaddrs="$bangaddrs,$cfg_admin"; fi
148 [ -z "$bangaddrs" ] ||
150 echo "$* failed with error code $errcode"
151 echo ""
152 [ ! -n "$bang_once" ] || echo "you will not receive any more notifications until recovery"
153 echo "this status message may be disabled on the project admin page"
154 echo ""
155 echo "Log follows:"
156 echo ""
157 cat "$bang_log"
158 } | mail -s "[$cfg_name] $proj $bang_action failed" "$bangaddrs"
160 touch .banged
161 bang_trap
162 exit 1
165 # bang_eval CMD... will evaluate the command with well-defined failure mode;
166 # Identical to bang CMD... except the command is eval'd instead of executed.
167 bang_eval() {
168 bang eval "$*"
171 # Default bang settings:
172 bang_setup() {
173 bang_action="lame_programmer"
174 bang_once=
175 bang_trap() { :; }
176 bang_log="$(mktemp -t repomgr-XXXXXX)"
177 trap "rm \"\$bang_log\"" EXIT
181 # Progress report - if show_progress is set, shows the given message.
182 progress() {
183 [ ! -n "$show_progress" ] || echo "$@"
187 # Project config accessors; must be run in project directory
188 config_get() {
189 git config "gitweb.$1"
192 config_set() {
193 git config "gitweb.$1" "$2" && chgrp repo config && chmod g+w config
196 config_set_raw() {
197 git config "$1" "$2" && chgrp repo config && chmod g+w config
200 config_get_date_seconds() {
201 _dt="$(config_get "$1" || :)"
202 [ -n "$_dt" ] || return 1
203 _ds="$(perl -I@basedir@ -MGirocco::Util -e "print parse_rfc2822_date('$_dt')")"
204 [ -n "$_ds" ] || return 1
205 echo "$_ds"
208 # Tool for checking whether given number of seconds has not passed yet
209 check_interval() {
210 os="$(config_get_date_seconds "$1")" || return 1
211 ns="$(date +%s)"
212 [ $ns -lt $(($os+$2)) ]
216 # List all Git repositories, with given prefix if specified, one-per-line
217 get_repo_list() {
218 if [ -n "$1" ]; then
219 cut -d : -f 1,3 "$cfg_chroot"/etc/group | grep "^$1"
220 else
221 cut -d : -f 1,3 "$cfg_chroot"/etc/group
222 fi | while IFS=: read name id; do
223 [ $id -lt 65536 ] || echo "$name"
224 done
227 # returns true if the passed in git dir (defaults to ".") is a mirror using git fast-import
228 is_gfi_mirror() {
229 _gitdir="${1-.}"
230 # always return false for non-mirrors
231 [ ! -e "$_gitdir/.nofetch" ] || return 1
232 _url="$(GIT_DIR="$_gitdir" config_get baseurl 2>/dev/null || :)"
233 case "$_url" in
234 svn://* | svn+http://* | svn+https://*)
235 # Don't think git-svn currently uses git fast-import
236 return 1
238 darcs://*)
239 # darcs mirrors use git fast-import
240 return 0
242 bzr://*)
243 # bzr mirrors use git fast-import
244 return 0
246 hg+http://* | hg+https://*)
247 # hg mirrors use git fast-import
248 return 0
250 esac
251 # assume it does not use git fast-import
252 return 1
255 # returns true if the passed in git dir (defaults to ".") is a mirror using git-svn
256 is_svn_mirror() {
257 _gitdir="${1-.}"
258 # always return false for non-mirrors
259 [ ! -e "$_gitdir/.nofetch" ] || return 1
260 _url="$(GIT_DIR="$_gitdir" config_get baseurl 2>/dev/null || :)"
261 case "$_url" in
262 svn://* | svn+http://* | svn+https://*)
263 return 0
264 esac
265 return 1
268 # A well-known UTF-8 locale is required for some of the fast-import providers
269 # in order to avoid mangling characters. Ideally we could use "POSIX.UTF-8"
270 # but that is not reliably UTF-8 but rather usually US-ASCII.
271 # We parse the output of `locale -a` and select a suitable UTF-8 locale.
272 # If we cannot find one in the `locale -a` output then we just use a well-known
273 # UTF-8 locale and hope for the best. We set LC_ALL to our choice and export
274 # it. We only set this temporarily when running the fast-import providers.
275 set_utf8_locale() {
276 _guess_locale="$(locale -a | grep -viE '^(posix|c)(\..*)?$' | \
277 grep -iE '\.utf-?8$' | sed -e 's/\.[Uu][Tt][Ff]-*8$//' | \
278 sed -e '/en_US/ s/^/0 /; /en_US/ !s/^/1 /' | LC_ALL=C sort | \
279 head -n 1 | cut -d ' ' -f 2)"
280 LC_ALL="${_guess_locale:-en_US}.UTF-8"
281 export LC_ALL
284 # hg-fast-export | git fast-import with error handling in current directory GIT_DIR
285 git_hg_fetch() (
286 set_utf8_locale
287 _python="${PYTHON:-python}"
288 _err1=
289 _err2=
290 exec 3>&1
291 { read -r _err1 || :; read -r _err2 || :; } <<-EOT
293 exec 4>&3 3>&1 1>&4 4>&-
295 _e1=0
296 [ -f hg2git-marks ] || touch hg2git-marks
297 _af="$(git config hg.authorsfile || :)"
298 _cmd='GIT_DIR="$(pwd)" "$_python" "$cfg_basedir/bin/hg-fast-export.py" \
299 --repo "$(pwd)/repo.hg" \
300 --marks "$(pwd)/hg2git-marks" \
301 --mapping "$(pwd)/hg2git-mapping" \
302 --heads "$(pwd)/hg2git-heads" \
303 --status "$(pwd)/hg2git-state" \
304 -U unknown --force --flatten --hg-hash'
305 [ -z "$_af" ] || _cmd="$_cmd"' --authors "$_af"'
306 eval "$_cmd" 3>&- || _e1=$?
307 echo $_e1 >&3
308 } | \
310 _e2=0
311 rm -f hg2git-marks.new
312 git fast-import \
313 --export-marks="$(pwd)/hg2git-marks.new" \
314 --export-pack-edges="$(pwd)/gfi-packs" \
315 --force 3>&- || _e2=$?
316 echo $_e2 >&3
320 exec 3>&-
321 [ "$_err1" = 0 -a "$_err2" = 0 ] || return 1
322 if [ -f hg2git-marks ]; then
323 rm -f hg2git-marks.old
324 mv hg2git-marks hg2git-marks.old
325 else
326 touch hg2git-marks.old
328 cat hg2git-marks.old hg2git-marks.new | \
329 LC_ALL=C sort -t : -k2,2n -u | \
330 sed -ne "/^:[1-9][0-9]* $octet20\$/p" > hg2git-marks
331 rm hg2git-marks.old hg2git-marks.new
332 rm -f hg2git-heads
333 git branch --no-color | \
334 while IFS= read -r _head; do
335 echo ":${_head#??} $(git rev-parse "refs/heads/${_head#??}")"
336 done > hg2git-heads