From 4d0b946926fb49337e808ccd500df3f482570142 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 13 May 2024 11:11:51 +0200 Subject: [PATCH] Fix changelog generation for pre-releases and recreating old releases --- lib/gittools | 20 +++-- test/tests_gittools.sh | 187 +++++++++++++++++++++++++++++++++++------ test/tests_integ.sh | 66 +++++++++++++++ 3 files changed, 243 insertions(+), 30 deletions(-) diff --git a/lib/gittools b/lib/gittools index 8cf03dd..830f582 100644 --- a/lib/gittools +++ b/lib/gittools @@ -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 diff --git a/test/tests_gittools.sh b/test/tests_gittools.sh index fc32ac7..7bfdf1a 100755 --- a/test/tests_gittools.sh +++ b/test/tests_gittools.sh @@ -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() { @@ -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 @@ -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" ;; *) @@ -71,28 +76,45 @@ 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" ;; *) @@ -100,7 +122,7 @@ git_mock() { ;; esac return 0 - elif [[ " $* " =~ " %(committerdate:short) " ]]; then + elif [[ " $* " == *" %(committerdate:short) "* ]]; then echo "2021-07-29" return 0 fi @@ -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 < + Change log text for release version v1.5.0 + + + Change log text for release version v1.2.4 + + + Change log text for release version v1.2.3 + + + Release description for version v0.9.0 + + + Commit message for v0.0.1 + +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 < + Change log text for release version v1.5.0 + + + Change log text for release version v1.2.4 + + + Change log text for release version v1.2.3 + + + Release description for version v0.9.0 + + + Commit message for v0.0.1 + +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 < + Change log text for release version v1.5.0-rc0 + + + Change log text for release version v1.2.4 + + + Change log text for release version v1.2.3 + + + Release description for version v0.9.0 + + + Commit message for v0.0.1 + +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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/test/tests_integ.sh b/test/tests_integ.sh index 0c2d41f..3750993 100755 --- a/test/tests_integ.sh +++ b/test/tests_integ.sh @@ -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" '' + assertContains "$pdsc" "Initial release 1.0.0" + assertNotContains "$pdsc" "Active development ..." + assertNotContains "$pdsc" '' + assertContains "$pdsc" "Pre-release" + assertContains "$pdsc" '' + 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