Added support for setting git version externally
This to help when building from source snapshots that typically don't have an entire repository.
I'd rather make override the whole TINI_VERSION. As-is, it seems a little surprising that we have a TINI_COMMIT variable that actually overrides the whole variable if set (including the dirty check, so not just the commit), and it's a bit wasteful that we run git to check what the commit & dirty flag is, even if we're not going to use it.
How about something like this:
if (NOT "${TINI_VERSION_GIT}" STREQUAL "")
# Version was set by the user directly
set(tini_VERSION_GIT " - ${TINI_VERSION_GIT}")
else()
# Extract git version and dirty-ness
execute_process (
COMMAND git --git-dir "${PROJECT_SOURCE_DIR}/.git" --work-tree "${PROJECT_SOURCE_DIR}" log -n 1 --date=local --pretty=format:%h
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
RESULT_VARIABLE git_version_check_ret
OUTPUT_VARIABLE tini_VERSION_GIT
)
execute_process(
COMMAND git --git-dir "${PROJECT_SOURCE_DIR}/.git" --work-tree "${PROJECT_SOURCE_DIR}" status --porcelain --untracked-files=no
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
OUTPUT_VARIABLE git_dirty_check_out
)
if("${git_version_check_ret}" EQUAL 0)
set(tini_VERSION_GIT " - git.${tini_VERSION_GIT}")
if(NOT "${git_dirty_check_out}" STREQUAL "")
set(tini_VERSION_GIT "${tini_VERSION_GIT}-dirty")
endif()
else()
set(tini_VERSION_GIT "")
endif()
endif()
I was trying for a minimal diff, But it looks like you wouldn't mind more if it was better. I largely followed your suggestion except kept the setting of the formatted result after this block and also conditionally run the dirty check only if the first git command works.
The intention of the patch is to facilitate the same build output (In this case the version string) when building a source snapshot. (like https://github.com/krallin/tini/archive/v0.19.0.tar.gz as it will produce a different version since the git commands will fail)