131 lines
3.2 KiB
Bash
Executable File
131 lines
3.2 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
set -euo pipefail
|
|
|
|
usage() {
|
|
printf '%s\n' \
|
|
'Usage: verify-gitea-release-tag.sh --tag TAG --event-sha SHA' \
|
|
' --allowed-signers FILE --notes-output FILE'
|
|
}
|
|
|
|
tag=
|
|
event_sha=
|
|
allowed_signers=
|
|
notes_output=
|
|
|
|
while (($# > 0)); do
|
|
case "$1" in
|
|
--tag)
|
|
tag=${2-}
|
|
shift 2
|
|
;;
|
|
--event-sha)
|
|
event_sha=${2-}
|
|
shift 2
|
|
;;
|
|
--allowed-signers)
|
|
allowed_signers=${2-}
|
|
shift 2
|
|
;;
|
|
--notes-output)
|
|
notes_output=${2-}
|
|
shift 2
|
|
;;
|
|
--help|-h)
|
|
usage
|
|
exit 0
|
|
;;
|
|
*)
|
|
printf 'verify-gitea-release-tag: unknown argument: %s\n' "$1" >&2
|
|
usage >&2
|
|
exit 2
|
|
;;
|
|
esac
|
|
done
|
|
|
|
if [[ -z "$tag" || -z "$event_sha" || -z "$allowed_signers" ||
|
|
-z "$notes_output" ]]; then
|
|
usage >&2
|
|
exit 2
|
|
fi
|
|
if [[ ! "$event_sha" =~ ^([0-9a-f]{40}|[0-9a-f]{64})$ ]]; then
|
|
printf '%s\n' \
|
|
'verify-gitea-release-tag: event SHA is not a full object id' >&2
|
|
exit 1
|
|
fi
|
|
if ! git check-ref-format "refs/tags/$tag"; then
|
|
printf '%s\n' 'verify-gitea-release-tag: invalid tag name' >&2
|
|
exit 1
|
|
fi
|
|
if [[ ! -f "$allowed_signers" || -L "$allowed_signers" ||
|
|
! -s "$allowed_signers" ]]; then
|
|
printf '%s\n' \
|
|
'verify-gitea-release-tag: allowed signers must be a non-empty regular file' \
|
|
>&2
|
|
exit 1
|
|
fi
|
|
|
|
tag_ref="refs/tags/$tag"
|
|
tag_object=$(git rev-parse --verify "$tag_ref^{tag}")
|
|
tag_target=$(
|
|
git cat-file tag "$tag_object" |
|
|
sed -n '1s/^object //p'
|
|
)
|
|
target_type=$(
|
|
git cat-file tag "$tag_object" |
|
|
sed -n '2s/^type //p'
|
|
)
|
|
if [[ "$target_type" != commit ||
|
|
! "$tag_target" =~ ^([0-9a-f]{40}|[0-9a-f]{64})$ ]]; then
|
|
printf '%s\n' \
|
|
'verify-gitea-release-tag: annotated tag must directly target a commit' >&2
|
|
exit 1
|
|
fi
|
|
git cat-file -e "$tag_target^{commit}"
|
|
|
|
signature=$(
|
|
git for-each-ref --count=1 --format='%(contents:signature)' "$tag_ref"
|
|
)
|
|
if [[ "$signature" != '-----BEGIN SSH SIGNATURE-----'* ]]; then
|
|
printf '%s\n' \
|
|
'verify-gitea-release-tag: annotated tag must use an SSH signature' >&2
|
|
exit 1
|
|
fi
|
|
|
|
git -c gpg.ssh.allowedSignersFile="$allowed_signers" \
|
|
verify-tag "$tag_ref" >&2
|
|
|
|
checked_out_commit=$(git rev-parse --verify 'HEAD^{commit}')
|
|
if [[ "$checked_out_commit" != "$tag_target" ]]; then
|
|
printf '%s\n' \
|
|
'verify-gitea-release-tag: checkout HEAD differs from the tag target' >&2
|
|
exit 1
|
|
fi
|
|
if [[ "$event_sha" != "$tag_object" && "$event_sha" != "$tag_target" ]]; then
|
|
printf '%s\n' \
|
|
'verify-gitea-release-tag: event SHA differs from the tag and target objects' \
|
|
>&2
|
|
exit 1
|
|
fi
|
|
|
|
subject=$(
|
|
git for-each-ref --count=1 --format='%(contents:subject)' "$tag_ref"
|
|
)
|
|
body=$(
|
|
git for-each-ref --count=1 --format='%(contents:body)' "$tag_ref"
|
|
)
|
|
if [[ -z "$subject" ]]; then
|
|
printf '%s\n' \
|
|
'verify-gitea-release-tag: annotated tag has no release-note subject' >&2
|
|
exit 1
|
|
fi
|
|
|
|
{
|
|
printf '%s\n' "$subject"
|
|
if [[ -n "$body" ]]; then
|
|
printf '\n%s\n' "$body"
|
|
fi
|
|
} >"$notes_output"
|
|
|
|
printf '%s\n' "$tag_target"
|