Skip to content

Commit

Permalink
fix: git describe fallback with no tag (#34)
Browse files Browse the repository at this point in the history
Signed-off-by: Cristian Le <cristian.le@mpsd.mpg.de>
  • Loading branch information
LecrisUT committed Feb 5, 2024
1 parent eac47a4 commit d412a6a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 23 deletions.
20 changes: 18 additions & 2 deletions cmake/DynamicVersion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,15 @@ function(get_dynamic_version)
WORKING_DIRECTORY ${ARGS_PROJECT_SOURCE}
OUTPUT_VARIABLE describe-name
OUTPUT_STRIP_TRAILING_WHITESPACE
COMMAND_ERROR_IS_FATAL ANY)
RESULT_VARIABLE git_describe_result
)
if (NOT git_status_result EQUAL 0)
message(${error_message_type}
"Git describe failed:\n"
" Source: ${ARGS_PROJECT_SOURCE}"
)
return()
endif ()
# Match any part containing digits and periods (strips out rc and so on)
if (NOT describe-name MATCHES "^(v?([0-9\\.]+)(-?[a-zA-z0-9]*)?(-([0-9]+)-g([a-f0-9]+))?)$")
message(${error_message_type}
Expand Down Expand Up @@ -554,7 +562,15 @@ function(get_dynamic_version)
WORKING_DIRECTORY ${ARGS_PROJECT_SOURCE}
OUTPUT_VARIABLE commit
OUTPUT_STRIP_TRAILING_WHITESPACE
COMMAND_ERROR_IS_FATAL ANY)
RESULT_VARIABLE git_rev_parser_result
)
if (NOT git_rev_parser_result EQUAL 0)
message(${error_message_type}
"Could not get current git commit:\n"
" Source: ${ARGS_PROJECT_SOURCE}"
)
return()
endif ()
message(DEBUG "Found version git repo")
endif ()

Expand Down
67 changes: 46 additions & 21 deletions test/DynamicVersion/test_fallback.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ rlJournalStart
rlRun "rsync -r ./ $tmp" 0 "Copy test files"
rlRun "pushd $tmp"
rlRun "build_dir=./build" 0 "Set build_dir"
rlRun "configure_args=\"-B \${build_dir} -G Ninja --log-context\"" 0 "Set configure_args"
rlRun "build_args=\"--build \${build_dir} -v\"" 0 "Set build_args"
[[ -n "$CMakeExtraUtils_ROOT" ]] && rlRun "configure_args=\"\${configure_args} -DCMakeExtraUtils_ROOT=\${CMakeExtraUtils_ROOT}\"" 0 "Add CMakeExtraUtils_ROOT"
rlRun "configure_args=\"-B \${build_dir} -G Ninja --log-context\"" 0 "Set configure_args"
rlRun "build_args=\"--build \${build_dir} -v\"" 0 "Set build_args"
[[ -n "$CMakeExtraUtils_ROOT" ]] && rlRun "configure_args=\"\${configure_args} -DCMakeExtraUtils_ROOT=\${CMakeExtraUtils_ROOT}\"" 0 "Add CMakeExtraUtils_ROOT"
rlRun "set -o pipefail"
rlPhaseEnd

Expand All @@ -20,8 +20,8 @@ rlJournalStart
rlPhaseEnd

rlPhaseStartTest "With fallback version"
[[ -d ${build_dir} ]] && rlRun "rm -rf ${build_dir}" 0 "Clean the build directory"
rlRun "fallback_version='0.1.2'" 0 "Set fallback_version"
[[ -d ${build_dir} ]] && rlRun "rm -rf ${build_dir}" 0 "Clean the build directory"
rlRun "fallback_version='0.1.2'" 0 "Set fallback_version"
rlRun -s "cmake ${configure_args} -DFALLBACK_VERSION=${fallback_version}" 0 "CMake configure"
rlAssertGrep "^\[TestProject\] version: ${fallback_version}\$" $rlRun_LOG
rlAssertGrep "^\[TestProject\] version-full: ${fallback_version}\$" $rlRun_LOG
Expand All @@ -33,18 +33,18 @@ rlJournalStart
rlRun -s "${build_dir}/version" 0 "Run ./version"
rlAssertGrep "^version: ${fallback_version}\$" $rlRun_LOG
rlAssertGrep "^version-full: ${fallback_version}\$" $rlRun_LOG
rlRun -s "${build_dir}/commit" 0 "Run ./commit"
rlAssertGrep "^version: ${fallback_version}\$" $rlRun_LOG
rlAssertGrep "^version-full: ${fallback_version}\$" $rlRun_LOG
rlAssertGrep "^commit: commit-NOTFOUND\$" $rlRun_LOG
rlAssertGrep "^short-hash: short-hash-NOTFOUND\$" $rlRun_LOG
rlAssertGrep "^describe: describe-NOTFOUND\$" $rlRun_LOG
rlAssertGrep "^distance: distance-NOTFOUND\$" $rlRun_LOG
rlRun -s "${build_dir}/commit" 0 "Run ./commit"
rlAssertGrep "^version: ${fallback_version}\$" $rlRun_LOG
rlAssertGrep "^version-full: ${fallback_version}\$" $rlRun_LOG
rlAssertGrep "^commit: commit-NOTFOUND\$" $rlRun_LOG
rlAssertGrep "^short-hash: short-hash-NOTFOUND\$" $rlRun_LOG
rlAssertGrep "^describe: describe-NOTFOUND\$" $rlRun_LOG
rlAssertGrep "^distance: distance-NOTFOUND\$" $rlRun_LOG
rlPhaseEnd

rlPhaseStartTest "With fallback version + commit"
[[ -d ${build_dir} ]] && rlRun "rm -rf ${build_dir}" 0 "Clean the build directory"
rlRun "fallback_commit='dummy'" 0 "Set fallback_commit"
[[ -d ${build_dir} ]] && rlRun "rm -rf ${build_dir}" 0 "Clean the build directory"
rlRun "fallback_commit='dummy'" 0 "Set fallback_commit"
rlRun -s "cmake ${configure_args} -DFALLBACK_VERSION=${fallback_version} -DFALLBACK_HASH=${fallback_commit}" 0 "CMake configure"
rlAssertGrep "^\[TestProject\] version: ${fallback_version}\$" $rlRun_LOG
rlAssertGrep "^\[TestProject\] version-full: ${fallback_version}\$" $rlRun_LOG
Expand All @@ -56,13 +56,38 @@ rlJournalStart
rlRun -s "${build_dir}/version" 0 "Run ./version"
rlAssertGrep "^version: ${fallback_version}\$" $rlRun_LOG
rlAssertGrep "^version-full: ${fallback_version}\$" $rlRun_LOG
rlRun -s "${build_dir}/commit" 0 "Run ./commit"
rlAssertGrep "^version: ${fallback_version}\$" $rlRun_LOG
rlAssertGrep "^version-full: ${fallback_version}\$" $rlRun_LOG
rlAssertGrep "^commit: ${fallback_commit}\$" $rlRun_LOG
rlAssertGrep "^short-hash: short-hash-NOTFOUND\$" $rlRun_LOG
rlAssertGrep "^describe: describe-NOTFOUND\$" $rlRun_LOG
rlAssertGrep "^distance: distance-NOTFOUND\$" $rlRun_LOG
rlRun -s "${build_dir}/commit" 0 "Run ./commit"
rlAssertGrep "^version: ${fallback_version}\$" $rlRun_LOG
rlAssertGrep "^version-full: ${fallback_version}\$" $rlRun_LOG
rlAssertGrep "^commit: ${fallback_commit}\$" $rlRun_LOG
rlAssertGrep "^short-hash: short-hash-NOTFOUND\$" $rlRun_LOG
rlAssertGrep "^describe: describe-NOTFOUND\$" $rlRun_LOG
rlAssertGrep "^distance: distance-NOTFOUND\$" $rlRun_LOG
rlPhaseEnd

rlPhaseStartTest "With fallback version + git repo, no tag"
[[ -d ${build_dir} ]] && rlRun "rm -rf ${build_dir}" 0 "Clean the build directory"
rlRun "git init"
rlRun "git add CMakeLists.txt .git_archival.txt src pyproject.toml" 0 "Git add basic files"
rlRun "git commit -m 'Initial commit'" 0 "Git commit (initial)"
rlRun -s "cmake ${configure_args} -DFALLBACK_VERSION=${fallback_version}" 0 "CMake configure"
rlAssertGrep "^\[TestProject\] version: ${fallback_version}\$" $rlRun_LOG
rlAssertGrep "^\[TestProject\] version-full: ${fallback_version}\$" $rlRun_LOG
rlAssertGrep "^\[TestProject\] commit: commit-NOTFOUND\$" $rlRun_LOG
rlAssertGrep "^\[TestProject\] short-hash: short-hash-NOTFOUND\$" $rlRun_LOG
rlAssertGrep "^\[TestProject\] describe: describe-NOTFOUND\$" $rlRun_LOG
rlAssertGrep "^\[TestProject\] distance: distance-NOTFOUND\$" $rlRun_LOG
rlRun -s "cmake ${build_args}" 0 "CMake build"
rlRun -s "${build_dir}/version" 0 "Run ./version"
rlAssertGrep "^version: ${fallback_version}\$" $rlRun_LOG
rlAssertGrep "^version-full: ${fallback_version}\$" $rlRun_LOG
rlRun -s "${build_dir}/commit" 0 "Run ./commit"
rlAssertGrep "^version: ${fallback_version}\$" $rlRun_LOG
rlAssertGrep "^version-full: ${fallback_version}\$" $rlRun_LOG
rlAssertGrep "^commit: commit-NOTFOUND\$" $rlRun_LOG
rlAssertGrep "^short-hash: short-hash-NOTFOUND\$" $rlRun_LOG
rlAssertGrep "^describe: describe-NOTFOUND\$" $rlRun_LOG
rlAssertGrep "^distance: distance-NOTFOUND\$" $rlRun_LOG
rlPhaseEnd

rlPhaseStartCleanup
Expand Down

0 comments on commit d412a6a

Please sign in to comment.