104 lines
2.6 KiB
Bash
Executable File
104 lines
2.6 KiB
Bash
Executable File
#!/bin/sh
|
|
set -eu
|
|
|
|
usage() {
|
|
echo "usage: $0 ROOT OWNER PROJECT DEFAULT_BRANCH BADGE..." >&2
|
|
exit 2
|
|
}
|
|
|
|
[ "$#" -ge 5 ] || usage
|
|
root=$1
|
|
owner=$2
|
|
project=$3
|
|
default_branch=$4
|
|
shift 4
|
|
|
|
case "$root" in
|
|
/*) ;;
|
|
*) echo "publication root must be absolute: $root" >&2; exit 2 ;;
|
|
esac
|
|
[ -d "$root" ] || { echo "publication root is not a directory: $root" >&2; exit 2; }
|
|
[ -w "$root" ] || { echo "publication root is not writable: $root" >&2; exit 2; }
|
|
|
|
validate_component() {
|
|
component_name=$1
|
|
component_value=$2
|
|
case "$component_value" in
|
|
''|.|..|*[!A-Za-z0-9._-]*)
|
|
echo "$component_name is not a safe path component: $component_value" >&2
|
|
exit 2
|
|
;;
|
|
esac
|
|
}
|
|
|
|
validate_component owner "$owner"
|
|
validate_component project "$project"
|
|
validate_component default-branch "$default_branch"
|
|
|
|
badges=
|
|
for name in "$@"; do
|
|
case "$name" in
|
|
coverage|test|version) ;;
|
|
*) echo "unsupported badge name: $name" >&2; exit 2 ;;
|
|
esac
|
|
case " $badges " in
|
|
*" $name "*) echo "duplicate badge name: $name" >&2; exit 2 ;;
|
|
esac
|
|
source_file="coverage/$name.svg"
|
|
if [ -L "$source_file" ] || [ ! -f "$source_file" ] || [ ! -s "$source_file" ]; then
|
|
echo "badge source must be a regular, nonempty, non-symlink file: $source_file" >&2
|
|
exit 2
|
|
fi
|
|
badges="$badges $name"
|
|
done
|
|
|
|
ensure_directory() {
|
|
directory=$1
|
|
description=$2
|
|
if [ -L "$directory" ]; then
|
|
echo "$description must not be a symlink: $directory" >&2
|
|
exit 2
|
|
fi
|
|
if [ -e "$directory" ] && [ ! -d "$directory" ]; then
|
|
echo "$description is not a directory: $directory" >&2
|
|
exit 2
|
|
fi
|
|
if [ ! -d "$directory" ]; then
|
|
mkdir "$directory"
|
|
fi
|
|
chmod 0755 "$directory"
|
|
}
|
|
|
|
owner_dir="$root/$owner"
|
|
ensure_directory "$owner_dir" "owner path"
|
|
project_dir="$owner_dir/$project"
|
|
ensure_directory "$project_dir" "project path"
|
|
destination="$project_dir/$default_branch"
|
|
|
|
if [ -L "$destination" ]; then
|
|
rm -f "$destination"
|
|
elif [ -e "$destination" ] && [ ! -d "$destination" ]; then
|
|
echo "default-branch path is not a directory: $destination" >&2
|
|
exit 2
|
|
fi
|
|
ensure_directory "$destination" "default-branch path"
|
|
|
|
temporary=
|
|
cleanup() {
|
|
if [ -n "$temporary" ]; then
|
|
rm -f "$temporary"
|
|
fi
|
|
}
|
|
trap cleanup EXIT HUP INT TERM
|
|
|
|
for name in $badges; do
|
|
source_file="coverage/$name.svg"
|
|
temporary="$destination/.$name.svg.tmp.$$"
|
|
cp "$source_file" "$temporary"
|
|
chmod 0644 "$temporary"
|
|
mv "$temporary" "$destination/$name.svg"
|
|
temporary=
|
|
done
|
|
|
|
trap - EXIT HUP INT TERM
|