gitweb_config: make sure the home_link is not empty
[girocco.git] / shlib.sh
blobe80a99f7800933c91c437ff78277d5c2bca0516f
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 # bang CMD... will execute the command with well-defined failure mode;
89 # set bang_action to string of the failed action ('clone', 'update', ...);
90 # pre-set bang_once=1 to make sure jobs banging on a repo repeatedly will
91 # not spam the owner; re-define the bang_trap() function to do custom
92 # cleanup before bailing out
93 bang() {
94 if [ -n "$show_progress" ]; then
95 exec 3>&1
96 errcode=
97 read -r errcode <<-EOT || :
99 exec 4>&3 3>&1 1>&4 4>&-
100 { "$@" 3>&- || echo $? >&3; } 2>&1 | tee -a "$bang_log"
103 exec 3>&-
104 if [ -z "$errcode" ]; then
105 # All right. Cool.
106 return;
108 else
109 if "$@" >>"$bang_log" 2>&1; then
110 # All right. Cool.
111 return;
112 else
113 errcode="$?"
116 if ! [ -e .banged ] || [ -e .bangagain ]; then
117 rm -f .bangagain
118 bangmailok=true
119 ! [ -f HEAD -a -f config -a -d objects ] ||
120 bangmailok="$(GIT_DIR=. git config --bool gitweb.statusupdates 2>/dev/null || echo true)"
121 bangaddrs=''
122 [ "$bangmailok" = "false" -o -z "$mail" ] || bangaddrs="$mail"
123 [ -z "$cfg_admincc" -o "$cfg_admincc" = "0" -o -z "$cfg_admin" ] ||
124 if [ -z "$bangaddrs" ]; then bangaddrs="$cfg_admin"; else bangaddrs="$bangaddrs,$cfg_admin"; fi
125 [ -z "$bangaddrs" ] ||
127 echo "$* failed with error code $errcode"
128 echo ""
129 [ ! -n "$bang_once" ] || echo "you will not receive any more notifications until recovery"
130 echo "this status message may be disabled on the project admin page"
131 echo ""
132 echo "Log follows:"
133 echo ""
134 cat "$bang_log"
135 } | mail -s "[$cfg_name] $proj $bang_action failed" "$bangaddrs"
137 touch .banged
138 bang_trap
139 exit 1
142 # bang_eval CMD... will evaluate the command with well-defined failure mode;
143 # Identical to bang CMD... except the command is eval'd instead of executed.
144 bang_eval() {
145 bang eval "$*"
148 # Default bang settings:
149 bang_setup() {
150 bang_action="lame_programmer"
151 bang_once=
152 bang_trap() { :; }
153 bang_log="$(mktemp -t repomgr-XXXXXX)"
154 trap "rm \"\$bang_log\"" EXIT
158 # Progress report - if show_progress is set, shows the given message.
159 progress() {
160 [ ! -n "$show_progress" ] || echo "$@"
164 # Project config accessors; must be run in project directory
165 config_get() {
166 git config "gitweb.$1"
169 config_set() {
170 git config "gitweb.$1" "$2" && chgrp repo config && chmod g+w config
173 config_set_raw() {
174 git config "$1" "$2" && chgrp repo config && chmod g+w config
177 config_get_date_seconds() {
178 _dt="$(config_get "$1" || :)"
179 [ -n "$_dt" ] || return 1
180 _ds="$(perl -I@basedir@ -MGirocco::Util -e "print parse_rfc2822_date('$_dt')")"
181 [ -n "$_ds" ] || return 1
182 echo "$_ds"
185 # Tool for checking whether given number of seconds has not passed yet
186 check_interval() {
187 os="$(config_get_date_seconds "$1")" || return 1
188 ns="$(date +%s)"
189 [ $ns -lt $(($os+$2)) ]
193 # List all Git repositories, with given prefix if specified, one-per-line
194 get_repo_list() {
195 if [ -n "$1" ]; then
196 cut -d : -f 1,3 "$cfg_chroot"/etc/group | grep "^$1"
197 else
198 cut -d : -f 1,3 "$cfg_chroot"/etc/group
199 fi | while IFS=: read name id; do
200 [ $id -lt 65536 ] || echo "$name"
201 done
204 # returns true if the passed in git dir (defaults to ".") is a mirror using git fast-import
205 is_gfi_mirror() {
206 _gitdir="${1-.}"
207 # always return false for non-mirrors
208 [ ! -e "$_gitdir/.nofetch" ] || return 1
209 _url="$(GIT_DIR="$_gitdir" config_get baseurl 2>/dev/null || :)"
210 case "$_url" in
211 svn://* | svn+http://* | svn+https://*)
212 # Don't think git-svn currently uses git fast-import
213 return 1
215 darcs://*)
216 # darcs mirrors use git fast-import
217 return 0
219 bzr://*)
220 # bzr mirrors use git fast-import
221 return 0
223 hg+http://* | hg+https://*)
224 # hg mirrors use git fast-import
225 return 0
227 esac
228 # assume it does not use git fast-import
229 return 1
232 # hg-fast-export | git fast-import with error handling in current directory GIT_DIR
233 git_hg_fetch() {
234 _python="${PYTHON:-python}"
235 _err1=
236 _err2=
237 exec 3>&1
238 { read -r _err1 || :; read -r _err2 || :; } <<-EOT
240 exec 4>&3 3>&1 1>&4 4>&-
242 _e1=0
243 [ -f hg2git-marks ] || touch hg2git-marks
244 _af="$(git config hg.authorsfile || :)"
245 _cmd='GIT_DIR="$(pwd)" "$_python" "$cfg_basedir/bin/hg-fast-export.py" \
246 --repo "$(pwd)/repo.hg" \
247 --marks "$(pwd)/hg2git-marks" \
248 --mapping "$(pwd)/hg2git-mapping" \
249 --heads "$(pwd)/hg2git-heads" \
250 --status "$(pwd)/hg2git-state" \
251 -U unknown --force --flatten --hg-hash'
252 [ -z "$_af" ] || _cmd="$_cmd"' --authors "$_af"'
253 eval "$_cmd" 3>&- || _e1=$?
254 echo $_e1 >&3
255 } | \
257 _e2=0
258 rm -f hg2git-marks.new
259 git fast-import \
260 --export-marks="$(pwd)/hg2git-marks.new" \
261 --export-pack-edges="$(pwd)/gfi-packs" \
262 --force 3>&- || _e2=$?
263 echo $_e2 >&3
267 exec 3>&-
268 [ "$_err1" = 0 -a "$_err2" = 0 ] || return 1
269 if [ -f hg2git-marks ]; then
270 rm -f hg2git-marks.old
271 mv hg2git-marks hg2git-marks.old
272 else
273 touch hg2git-marks.old
275 cat hg2git-marks.old hg2git-marks.new | LC_ALL=C uniq > hg2git-marks
276 rm hg2git-marks.old hg2git-marks.new
277 rm -f hg2git-heads
278 git branch --no-color | \
279 while IFS= read -r _head; do
280 echo ":${_head#??} $(git rev-parse "refs/heads/${_head#??}")"
281 done > hg2git-heads