shlib.sh: be more robust generating new hg2git-marks file
[girocco.git] / shlib.sh
blobdb8433975ed089936d04533a77ecf2ae0aaf4ea1
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 git() (
51 # some poorly behaving /bin/sh implementations do not
52 # properly export variables when the command is actually
53 # a shell function -- they are set for the function call
54 # but not exported into the environment for anything the
55 # function executes. Export the two Git variables we
56 # care about if they are set. Since this is a subshell
57 # the export will be temporary.
58 [ -z "$GIT_DIR" ] || export GIT_DIR
59 [ -z "$GIT_SSL_NO_VERIFY" ] || export GIT_SSL_NO_VERIFY
60 exec "$cfg_git_bin" "$@"
63 nc_openbsd() { "$cfg_nc_openbsd_bin" "$@"; }
65 _addrlist() {
66 _list=
67 for _addr in "$@"; do
68 [ -z "$_list" ] || _list="$_list, "
69 _list="$_list$_addr"
70 done
71 echo "$_list"
74 _sendmail() {
75 _mailer="${cfg_sendmail_bin:-/usr/sbin/sendmail}"
76 if [ -n "$cfg_sender" ]; then
77 "$_mailer" -i -f "$cfg_sender" "$@"
78 else
79 "$_mailer" -i "$@"
83 mail() {
84 _subject=
85 if [ "$1" = "-s" ]; then
86 shift
87 _subject="$1"
88 shift
91 echo "From: \"$cfg_name\" ($cfg_title) <$cfg_admin>"
92 echo "To: $(_addrlist "$@")"
93 [ -z "$_subject" ] || echo "Subject: $_subject"
94 echo "MIME-Version: 1.0"
95 echo "Content-Type: text/plain; charset=utf-8"
96 echo "Content-Transfer-Encoding: 8bit"
97 echo "Auto-Submitted: auto-generated"
98 echo ""
99 cat
100 } | _sendmail "$@"
103 # bang CMD... will execute the command with well-defined failure mode;
104 # set bang_action to string of the failed action ('clone', 'update', ...);
105 # pre-set bang_once=1 to make sure jobs banging on a repo repeatedly will
106 # not spam the owner; re-define the bang_trap() function to do custom
107 # cleanup before bailing out
108 bang() {
109 if [ -n "$show_progress" ]; then
110 exec 3>&1
111 errcode=
112 read -r errcode <<-EOT || :
114 exec 4>&3 3>&1 1>&4 4>&-
115 { "$@" 3>&- || echo $? >&3; } 2>&1 | tee -a "$bang_log"
118 exec 3>&-
119 if [ -z "$errcode" ]; then
120 # All right. Cool.
121 return;
123 else
124 if "$@" >>"$bang_log" 2>&1; then
125 # All right. Cool.
126 return;
127 else
128 errcode="$?"
131 if ! [ -e .banged ] || [ -e .bangagain ]; then
132 rm -f .bangagain
133 bangmailok=true
134 ! [ -f HEAD -a -f config -a -d objects ] ||
135 bangmailok="$(GIT_DIR=. git config --bool gitweb.statusupdates 2>/dev/null || echo true)"
136 bangaddrs=''
137 [ "$bangmailok" = "false" -o -z "$mail" ] || bangaddrs="$mail"
138 [ -z "$cfg_admincc" -o "$cfg_admincc" = "0" -o -z "$cfg_admin" ] ||
139 if [ -z "$bangaddrs" ]; then bangaddrs="$cfg_admin"; else bangaddrs="$bangaddrs,$cfg_admin"; fi
140 [ -z "$bangaddrs" ] ||
142 echo "$* failed with error code $errcode"
143 echo ""
144 [ ! -n "$bang_once" ] || echo "you will not receive any more notifications until recovery"
145 echo "this status message may be disabled on the project admin page"
146 echo ""
147 echo "Log follows:"
148 echo ""
149 cat "$bang_log"
150 } | mail -s "[$cfg_name] $proj $bang_action failed" "$bangaddrs"
152 touch .banged
153 bang_trap
154 exit 1
157 # bang_eval CMD... will evaluate the command with well-defined failure mode;
158 # Identical to bang CMD... except the command is eval'd instead of executed.
159 bang_eval() {
160 bang eval "$*"
163 # Default bang settings:
164 bang_setup() {
165 bang_action="lame_programmer"
166 bang_once=
167 bang_trap() { :; }
168 bang_log="$(mktemp -t repomgr-XXXXXX)"
169 trap "rm \"\$bang_log\"" EXIT
173 # Progress report - if show_progress is set, shows the given message.
174 progress() {
175 [ ! -n "$show_progress" ] || echo "$@"
179 # Project config accessors; must be run in project directory
180 config_get() {
181 git config "gitweb.$1"
184 config_set() {
185 git config "gitweb.$1" "$2" && chgrp repo config && chmod g+w config
188 config_set_raw() {
189 git config "$1" "$2" && chgrp repo config && chmod g+w config
192 config_get_date_seconds() {
193 _dt="$(config_get "$1" || :)"
194 [ -n "$_dt" ] || return 1
195 _ds="$(perl -I@basedir@ -MGirocco::Util -e "print parse_rfc2822_date('$_dt')")"
196 [ -n "$_ds" ] || return 1
197 echo "$_ds"
200 # Tool for checking whether given number of seconds has not passed yet
201 check_interval() {
202 os="$(config_get_date_seconds "$1")" || return 1
203 ns="$(date +%s)"
204 [ $ns -lt $(($os+$2)) ]
208 # List all Git repositories, with given prefix if specified, one-per-line
209 get_repo_list() {
210 if [ -n "$1" ]; then
211 cut -d : -f 1,3 "$cfg_chroot"/etc/group | grep "^$1"
212 else
213 cut -d : -f 1,3 "$cfg_chroot"/etc/group
214 fi | while IFS=: read name id; do
215 [ $id -lt 65536 ] || echo "$name"
216 done
219 # returns true if the passed in git dir (defaults to ".") is a mirror using git fast-import
220 is_gfi_mirror() {
221 _gitdir="${1-.}"
222 # always return false for non-mirrors
223 [ ! -e "$_gitdir/.nofetch" ] || return 1
224 _url="$(GIT_DIR="$_gitdir" config_get baseurl 2>/dev/null || :)"
225 case "$_url" in
226 svn://* | svn+http://* | svn+https://*)
227 # Don't think git-svn currently uses git fast-import
228 return 1
230 darcs://*)
231 # darcs mirrors use git fast-import
232 return 0
234 bzr://*)
235 # bzr mirrors use git fast-import
236 return 0
238 hg+http://* | hg+https://*)
239 # hg mirrors use git fast-import
240 return 0
242 esac
243 # assume it does not use git fast-import
244 return 1
247 # hg-fast-export | git fast-import with error handling in current directory GIT_DIR
248 git_hg_fetch() (
249 LC_ALL="$use_locale"
250 export LC_ALL
251 _python="${PYTHON:-python}"
252 _err1=
253 _err2=
254 exec 3>&1
255 { read -r _err1 || :; read -r _err2 || :; } <<-EOT
257 exec 4>&3 3>&1 1>&4 4>&-
259 _e1=0
260 [ -f hg2git-marks ] || touch hg2git-marks
261 _af="$(git config hg.authorsfile || :)"
262 _cmd='GIT_DIR="$(pwd)" "$_python" "$cfg_basedir/bin/hg-fast-export.py" \
263 --repo "$(pwd)/repo.hg" \
264 --marks "$(pwd)/hg2git-marks" \
265 --mapping "$(pwd)/hg2git-mapping" \
266 --heads "$(pwd)/hg2git-heads" \
267 --status "$(pwd)/hg2git-state" \
268 -U unknown --force --flatten --hg-hash'
269 [ -z "$_af" ] || _cmd="$_cmd"' --authors "$_af"'
270 eval "$_cmd" 3>&- || _e1=$?
271 echo $_e1 >&3
272 } | \
274 _e2=0
275 rm -f hg2git-marks.new
276 git fast-import \
277 --export-marks="$(pwd)/hg2git-marks.new" \
278 --export-pack-edges="$(pwd)/gfi-packs" \
279 --force 3>&- || _e2=$?
280 echo $_e2 >&3
284 exec 3>&-
285 [ "$_err1" = 0 -a "$_err2" = 0 ] || return 1
286 if [ -f hg2git-marks ]; then
287 rm -f hg2git-marks.old
288 mv hg2git-marks hg2git-marks.old
289 else
290 touch hg2git-marks.old
292 cat hg2git-marks.old hg2git-marks.new | \
293 LC_ALL=C sort -t : -k2,2n -u | \
294 sed -ne "/^:[1-9][0-9]* $octet20\$/p" > hg2git-marks
295 rm hg2git-marks.old hg2git-marks.new
296 rm -f hg2git-heads
297 git branch --no-color | \
298 while IFS= read -r _head; do
299 echo ":${_head#??} $(git rev-parse "refs/heads/${_head#??}")"
300 done > hg2git-heads