#!/usr/bin/bash

# DESCRIPTION:
#
# Get <tag id> <tag name> new-line separated list of annotated tags sorted and filtered by rpm-git-tag-sort utility
# for each unique tag Name ("N" out of N-V-R). The rpm-git-tag-sort utility sorts the tags topologically (primary criterion),
# and by rpm version-release sort (secondary criterion) and it only outputs merged tags (relative to $GIT_HEAD).
#
# env: GIT_ROOT, GIT_HEAD

cd "$GIT_ROOT"

# get all the unique Names first
names="$(/usr/bin/git for-each-ref refs/tags --format="%(objecttype) %(tag)" | sed -En 's/^tag (.+)-[^-]+-[^-]+$/\1/p' | sort -ru)" || exit

# output reachable sorted tags for each unique Name
while read -r name; do
    rpm-git-tag-sort --start-commit="$GIT_HEAD" . "$name" || exit
done < <(echo "$names")
