Skip to content

Commit

Permalink
Fix changelog generation for pre-releases and recreating old releases
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name authored and JonatanAntoni committed May 13, 2024
1 parent d773605 commit 4d0b946
Show file tree
Hide file tree
Showing 3 changed files with 243 additions and 30 deletions.
20 changes: 14 additions & 6 deletions lib/gittools
Original file line number Diff line number Diff line change
Expand Up @@ -232,17 +232,25 @@ function git_changelog {
shift # past argument
done

local HEADSHA
HEADSHA=$("${UTILITY_GIT}" rev-list -n 1 "HEAD")
local LATEST
LATEST=$(git_describe "${PREFIX}")

mapfile -t TAGS < <(\
"${UTILITY_GIT}" -c 'versionsort.suffix=-' for-each-ref --format "%(refname)" --sort="-v:refname" "refs/tags/${PREFIX}*"\
"${UTILITY_GIT}" -c 'versionsort.suffix=-' for-each-ref --format "%(refname)" --sort="-v:refname" "refs/tags/${PREFIX}*" --merged \
)
for index in "${!TAGS[@]}"; do [[ ${TAGS[$index]} =~ v.*-.*$ ]] && unset -v 'TAGS[$index]'; done

local LATEST
LATEST=$(git_describe "${PREFIX}")
for index in "${!TAGS[@]}"; do
if [[ "${TAGS[${index}]}" =~ ${PREFIX}.*-.*$ ]]; then
if [[ $("${UTILITY_GIT}" rev-list -n 1 "${TAGS[${index}]}") != "${HEADSHA}" ]]; then
unset -v 'TAGS[${index}]'
fi
fi
done

"git_changelog_${FORMAT}_head"

if [[ -n "${DEVDROP}" ]] && ! "${UTILITY_GIT}" rev-list "${PREFIX}${LATEST}" 1>/dev/null 2>/dev/null; then
if [[ -n "${DEVDROP}" ]] && ! "${UTILITY_GIT}" rev-list "${PREFIX}${LATEST}" 1>/dev/null 2>/dev/null; then
"git_changelog_${FORMAT}" "${LATEST}" "" "${DEVDROP}"
fi

Expand Down
187 changes: 163 additions & 24 deletions test/tests_gittools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,26 @@
. "$(dirname "$0")/../lib/gittools"

setUp() {
PACK_CHANGELOG_MODE="full"
TESTDIR="${SHUNIT_TMPDIR}/${_shunit_test_}"
mkdir -p "${TESTDIR}"
pushd "${TESTDIR}" >/dev/null
pushd "${TESTDIR}" >/dev/null || exit

declare -A GIT_MOCK_REVLIST
GIT_MOCK_REFS=( \
"refs/tags/v1.5.0" \
"refs/tags/v1.5.0-dev" \
"refs/tags/v1.2.4" \
"refs/tags/v1.2.3" \
"refs/tags/v0.9.0" \
"refs/tags/v0.0.1" \
)
}

tearDown() {
unset GIT_MOCK_DESCRIBE
unset GIT_MOCK_REVPARSE
unset GIT_MOCK_REVLIST
PACK_CHANGELOG_MODE="full"
unset GIT_MOCK_REVLIST
}

git_mock() {
Expand All @@ -36,7 +46,7 @@ git_mock() {
shift
;;
'rev-parse')
return ${GIT_MOCK_REVPARSE-0}
return "${GIT_MOCK_REVPARSE-0}"
;;
'describe')
if [ -n "${GIT_MOCK_DESCRIBE+x}" ]; then
Expand All @@ -50,17 +60,12 @@ git_mock() {
fi
;;
'for-each-ref')
if [[ " $* " =~ " --format %(refname)" ]]; then
echo "refs/tags/v1.5.0"
echo "refs/tags/v1.5.0-dev"
echo "refs/tags/v1.2.4"
echo "refs/tags/v1.2.3"
echo "refs/tags/v0.9.0"
echo "refs/tags/v0.0.1"
if [[ " $* " == *" --format %(refname)"* ]]; then
printf '%s\n' "${GIT_MOCK_REFS[@]}"
return 0
elif [[ " $* " =~ " --format %(objecttype)" ]]; then
case "${@: -1}" in
"refs/tags/v1.5.0"|"refs/tags/v1.2.4"|"refs/tags/v1.2.3")
elif [[ " $* " == *" --format %(objecttype)"* ]]; then
case "${*: -1}" in
refs/tags/v1.5.0*|refs/tags/v1.2.4|refs/tags/v1.2.3)
echo "tag"
;;
*)
Expand All @@ -71,36 +76,53 @@ git_mock() {
fi
;;
'rev-list')
return ${GIT_MOCK_REVLIST-1}
shift
while [[ $# -gt 0 ]]; do
case $1 in
"-n")
shift
shift
;;
*)
if [ "${GIT_MOCK_REVLIST[$1]+x}" ]; then
echo "${GIT_MOCK_REVLIST[$1]}"
return 0
else
return 1
fi
;;
esac
done
return 1
;;
'tag')
if [[ " $* " =~ " %(contents) " ]]; then
if [[ " $* " == *" %(contents) "* ]]; then
case "${@: -1}" in
"v1.5.0"|"v1.2.4"|"v1.2.3")
echo "Change log text for release version ${@: -1}"
v1.5.0*|v1.2.4|v1.2.3)
echo "Change log text for release version ${*: -1}"
;;
*)
echo "Commit message for ${@: -1}"
echo "Commit message for ${*: -1}"
;;
esac
return 0
elif [[ " $* " =~ " %(taggerdate:short) " ]]; then
elif [[ " $* " == *" %(taggerdate:short) "* ]]; then
case "${@: -1}" in
"v1.5.0")
v1.5.0*)
echo "2022-08-03"
;;
"v1.2.4")
v1.2.4)
echo "2022-06-27"
;;
"v1.2.3")
v1.2.3)
echo "2022-06-15"
;;
*)
return 1
;;
esac
return 0
elif [[ " $* " =~ " %(committerdate:short) " ]]; then
elif [[ " $* " == *" %(committerdate:short) "* ]]; then
echo "2021-07-29"
return 0
fi
Expand Down Expand Up @@ -192,6 +214,111 @@ test_git_changelog_pdsc() {
UTILITY_GHCLI="ghcli_mock"

GIT_MOCK_DESCRIBE="v1.5.0-3-g1abcdef"
declare -A GIT_MOCK_REVLIST
GIT_MOCK_REVLIST["HEAD"]="deadbeef"

changelog=$(git_changelog -f pdsc -p v)

read -r -d '' expected <<EOF
<release version="1.5.0" date="2022-08-03" tag="v1.5.0">
Change log text for release version v1.5.0
</release>
<release version="1.2.4" date="2022-06-27" tag="v1.2.4">
Change log text for release version v1.2.4
</release>
<release version="1.2.3" date="2022-06-15" tag="v1.2.3">
Change log text for release version v1.2.3
</release>
<release version="0.9.0" date="2021-07-29" tag="v0.9.0">
Release description for version v0.9.0
</release>
<release version="0.0.1" date="2021-07-29" tag="v0.0.1">
Commit message for v0.0.1
</release>
EOF

assertEquals "${expected}" "${changelog}"
}

test_git_changelog_pdsc_release() {
UTILITY_GIT="git_mock"
UTILITY_GHCLI="ghcli_mock"

GIT_MOCK_DESCRIBE="v1.5.0-0-g1abcdef"
declare -A GIT_MOCK_REVLIST
GIT_MOCK_REVLIST["HEAD"]="deadbeef"
GIT_MOCK_REVLIST["refs/tags/v1.5.0"]="deadbeef"

changelog=$(git_changelog -f pdsc -p v)

read -r -d '' expected <<EOF
<release version="1.5.0" date="2022-08-03" tag="v1.5.0">
Change log text for release version v1.5.0
</release>
<release version="1.2.4" date="2022-06-27" tag="v1.2.4">
Change log text for release version v1.2.4
</release>
<release version="1.2.3" date="2022-06-15" tag="v1.2.3">
Change log text for release version v1.2.3
</release>
<release version="0.9.0" date="2021-07-29" tag="v0.9.0">
Release description for version v0.9.0
</release>
<release version="0.0.1" date="2021-07-29" tag="v0.0.1">
Commit message for v0.0.1
</release>
EOF

assertEquals "${expected}" "${changelog}"
}

test_git_changelog_pdsc_prerelease() {
UTILITY_GIT="git_mock"
UTILITY_GHCLI="ghcli_mock"

GIT_MOCK_REFS=( \
"refs/tags/v1.5.0-rc0" \
"refs/tags/v1.5.0-dev" \
"refs/tags/v1.2.4" \
"refs/tags/v1.2.3" \
"refs/tags/v0.9.0" \
"refs/tags/v0.0.1" \
)
GIT_MOCK_DESCRIBE="v1.5.0-rc0-0-g1abcdef"
declare -A GIT_MOCK_REVLIST
GIT_MOCK_REVLIST["HEAD"]="deadbeef"
GIT_MOCK_REVLIST["refs/tags/v1.5.0-rc0"]="deadbeef"

changelog=$(git_changelog -f pdsc -p v)

read -r -d '' expected <<EOF
<release version="1.5.0-rc0" date="2022-08-03" tag="v1.5.0-rc0">
Change log text for release version v1.5.0-rc0
</release>
<release version="1.2.4" date="2022-06-27" tag="v1.2.4">
Change log text for release version v1.2.4
</release>
<release version="1.2.3" date="2022-06-15" tag="v1.2.3">
Change log text for release version v1.2.3
</release>
<release version="0.9.0" date="2021-07-29" tag="v0.9.0">
Release description for version v0.9.0
</release>
<release version="0.0.1" date="2021-07-29" tag="v0.0.1">
Commit message for v0.0.1
</release>
EOF

assertEquals "${expected}" "${changelog}"
}

test_git_changelog_pdsc_with_default_devlog() {
UTILITY_GIT="git_mock"
UTILITY_GHCLI="ghcli_mock"

GIT_MOCK_DESCRIBE="v1.5.0-3-g1abcdef"
declare -A GIT_MOCK_REVLIST
GIT_MOCK_REVLIST["HEAD"]="deadbeef"

changelog=$(git_changelog -f pdsc -d -p v)

Expand Down Expand Up @@ -223,6 +350,8 @@ test_git_changelog_pdsc_with_empty_devlog() {
UTILITY_GIT="git_mock"

GIT_MOCK_DESCRIBE="v1.5.0-3-g1abcdef"
declare -A GIT_MOCK_REVLIST
GIT_MOCK_REVLIST["HEAD"]="deadbeef"

changelog=$(git_changelog -f pdsc -d "" -p v)

Expand Down Expand Up @@ -254,6 +383,8 @@ test_git_changelog_pdsc_with_devlog() {
UTILITY_GIT="git_mock"

GIT_MOCK_DESCRIBE="v1.5.0-3-g1abcdef"
declare -A GIT_MOCK_REVLIST
GIT_MOCK_REVLIST["HEAD"]="deadbeef"

changelog=$(git_changelog -f pdsc -d "Custom dev log" -p v)

Expand Down Expand Up @@ -285,6 +416,8 @@ test_git_changelog_html() {
UTILITY_GIT="git_mock"

GIT_MOCK_DESCRIBE="v1.5.0-3-g1abcdef"
declare -A GIT_MOCK_REVLIST
GIT_MOCK_REVLIST["HEAD"]="deadbeef"

changelog=$(git_changelog -f html -p v)

Expand Down Expand Up @@ -338,6 +471,9 @@ test_git_changelog_fail_tag() {
UTILITY_GHCLI="ghcli_mock"

GIT_MOCK_DESCRIBE="v1.5.0-3-g1abcdef"
declare -A GIT_MOCK_REVLIST
GIT_MOCK_REVLIST["HEAD"]="deadbeef"
GIT_MOCK_REVLIST["refs/tags/v1.5.0-dev"]="3faee"
PACK_CHANGELOG_MODE="tag"

changelog=$(git_changelog -f text -p v 2>&1)
Expand All @@ -352,6 +488,9 @@ test_git_changelog_fail_release() {
UTILITY_GHCLI="ghcli_mock"

GIT_MOCK_DESCRIBE="v1.5.0-3-g1abcdef"
declare -A GIT_MOCK_REVLIST
GIT_MOCK_REVLIST["HEAD"]="deadbeef"

PACK_CHANGELOG_MODE="release"

changelog=$(git_changelog -f text -p v 2>&1)
Expand Down
66 changes: 66 additions & 0 deletions test/tests_integ.sh
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,72 @@ test_integ_with_git_release() {
assertNotContains "$pdsc" "Active development ..."
}

test_integ_with_git_recreate_release() {
mkdir -p test_integ_with_git
tar -xjf "${DIRNAME}/test_integ_with_git.tbz2" -C test_integ_with_git
cd test_integ_with_git || return

git --git-dir="$(pwd)/.git" clean -fdxq
export GIT_COMMITTER_DATE="2023-06-16T13:00:00Z"
git --git-dir="$(pwd)/.git" tag -m "Release v1.1.0" v1.1.0
git --git-dir="$(pwd)/.git" checkout -fq v1.0.0

./gen_pack.sh --verbose -k

assertTrue "Pack description file missing" "[ -f build/ARM.GenPack.pdsc ]"
assertTrue "Pack checksum file missing" "[ -f build/ARM.GenPack.sha1 ]"
assertTrue "LICENSE file" "[ -f build/LICENSE ]"
assertTrue "Doc top level index missing" "[ -f build/doc/index.html ]"
assertFalse "Doxyfile found in build" "[ -f build/doc/test.dxy ]"
assertTrue "Doc index file missing" "[ -f build/doc/html/index.html ]"
assertTrue "Header file missing" "[ -f build/inc/test.h ]"
assertTrue "Source file missing" "[ -f build/src/test.c ]"
assertTrue "Pack archive missing" "[ -f output/ARM.GenPack.1.0.0.pack ]"

assertTrue "Checksum file verification failed" "cd build; sha1sum ARM.GenPack.sha1"

pdsc=$(cat build/ARM.GenPack.pdsc)
assertContains "$pdsc" '<release version="1.0.0" date="2023-05-22" tag="v1.0.0">'
assertContains "$pdsc" "Initial release 1.0.0"
assertNotContains "$pdsc" "Active development ..."
assertNotContains "$pdsc" '<release version="1.1.0"'
assertNotContains "$pdsc" "Release v1.1.0"
}

test_integ_with_git_release_candidate() {
mkdir -p test_integ_with_git
tar -xjf "${DIRNAME}/test_integ_with_git.tbz2" -C test_integ_with_git
cd test_integ_with_git || return

git --git-dir="$(pwd)/.git" clean -fdxq
git --git-dir="$(pwd)/.git" checkout -fq v1.0.0
export GIT_COMMITTER_DATE="2023-05-22T16:00:00Z"
git --git-dir="$(pwd)/.git" tag -m "Pre-release" v1.0.0-rc0
export GIT_COMMITTER_DATE="2022-08-04T16:00:00Z"
git --git-dir="$(pwd)/.git" tag -m "Old release" v0.9.0 v1.0.0^
git --git-dir="$(pwd)/.git" tag -d v1.0.0

./gen_pack.sh --verbose -k

assertTrue "Pack description file missing" "[ -f build/ARM.GenPack.pdsc ]"
assertTrue "Pack checksum file missing" "[ -f build/ARM.GenPack.sha1 ]"
assertTrue "LICENSE file" "[ -f build/LICENSE ]"
assertTrue "Doc top level index missing" "[ -f build/doc/index.html ]"
assertFalse "Doxyfile found in build" "[ -f build/doc/test.dxy ]"
assertTrue "Doc index file missing" "[ -f build/doc/html/index.html ]"
assertTrue "Header file missing" "[ -f build/inc/test.h ]"
assertTrue "Source file missing" "[ -f build/src/test.c ]"
assertTrue "Pack archive missing" "[ -f output/ARM.GenPack.1.0.0-rc0.pack ]"

assertTrue "Checksum file verification failed" "cd build; sha1sum ARM.GenPack.sha1"

pdsc=$(cat build/ARM.GenPack.pdsc)
assertContains "$pdsc" '<release version="1.0.0-rc0" date="2023-05-22" tag="v1.0.0-rc0">'
assertContains "$pdsc" "Pre-release"
assertContains "$pdsc" '<release version="0.9.0" date="2022-08-04" tag="v0.9.0">'
assertContains "$pdsc" "Old release"
}

test_integ_with_git_prerelease() {
mkdir -p test_integ_with_git
tar -xjf "${DIRNAME}/test_integ_with_git.tbz2" -C test_integ_with_git
Expand Down

0 comments on commit 4d0b946

Please sign in to comment.