rping/scripts/publish-coverage-badges.sh

85 lines
2.2 KiB
Bash
Executable File

#!/bin/sh
set -eu
usage() {
echo "usage: $0 ABSOLUTE_ROOT OWNER PROJECT DEFAULT_BRANCH NAME..." >&2
exit 2
}
[ "$#" -ge 5 ] || usage
root=$1
owner=$2
project=$3
branch=$4
shift 4
case $root in /*) ;; *) echo "publication root must be absolute" >&2; exit 1 ;; esac
[ -d "$root" ] && [ ! -L "$root" ] && [ -w "$root" ] || {
echo "publication root must be an existing writable directory, not a symlink" >&2
exit 1
}
for component in "$owner" "$project" "$branch"; do
case $component in
''|*[!A-Za-z0-9._-]*) echo "invalid publication path component: $component" >&2; exit 1 ;;
esac
done
seen=' '
for name in "$@"; do
case $name in coverage|test|version) ;; *) echo "invalid badge name: $name" >&2; exit 1 ;; esac
case $seen in *" $name "*) echo "duplicate badge name: $name" >&2; exit 1 ;; esac
seen="$seen$name "
source_file="coverage/$name.svg"
[ -f "$source_file" ] && [ ! -L "$source_file" ] && [ -s "$source_file" ] || {
echo "invalid badge source: $source_file" >&2
exit 1
}
done
owner_dir=$root/$owner
project_dir=$owner_dir/$project
destination=$project_dir/$branch
for directory in "$owner_dir" "$project_dir"; do
[ ! -L "$directory" ] || { echo "refusing symlink directory: $directory" >&2; exit 1; }
[ ! -e "$directory" ] || [ -d "$directory" ] || {
echo "publication component is not a directory: $directory" >&2
exit 1
}
mkdir -p "$directory"
chmod 0755 "$directory"
done
if [ -L "$destination" ]; then
unlink "$destination"
fi
[ ! -e "$destination" ] || [ -d "$destination" ] || {
echo "publication destination is not a directory: $destination" >&2
exit 1
}
mkdir -p "$destination"
chmod 0755 "$destination"
cleanup() {
for name in "$@"; do
temporary=$destination/.$name.svg.tmp-$$
[ ! -e "$temporary" ] || rm -f "$temporary"
done
}
trap cleanup 0
trap 'exit 1' HUP INT TERM
for name in "$@"; do
temporary=$destination/.$name.svg.tmp-$$
(umask 077; set -C; : > "$temporary") || {
echo "cannot create publication temporary: $temporary" >&2
exit 1
}
cp "coverage/$name.svg" "$temporary"
chmod 0644 "$temporary"
mv -f "$temporary" "$destination/$name.svg"
done
trap - 0 HUP INT TERM