From: Marc Tobias Date: Wed, 4 May 2022 13:48:23 +0000 (+0200) Subject: add git commit hash to --version output X-Git-Tag: v4.1.0~47^2 X-Git-Url: https://git.openstreetmap.org/nominatim.git/commitdiff_plain/821dabb138b78be6fd776ab047b024d319805012 add git commit hash to --version output --- diff --git a/.github/workflows/ci-tests.yml b/.github/workflows/ci-tests.yml index b3d91cdf..6ebf1ab9 100644 --- a/.github/workflows/ci-tests.yml +++ b/.github/workflows/ci-tests.yml @@ -256,6 +256,10 @@ jobs: working-directory: /home/nominatim if: matrix.flavour == 'centos' + - name: Print version + run: nominatim --version + working-directory: /home/nominatim/nominatim-project + - name: Import run: nominatim import --osm-file ../test.pbf working-directory: /home/nominatim/nominatim-project diff --git a/CMakeLists.txt b/CMakeLists.txt index 1aa9c315..52bb9a01 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,6 +26,17 @@ set(NOMINATIM_VERSION "${NOMINATIM_VERSION_MAJOR}.${NOMINATIM_VERSION_MINOR}.${N add_definitions(-DNOMINATIM_VERSION="${NOMINATIM_VERSION}") +# Setting GIT_HASH +find_package(Git) +if (GIT_FOUND) + execute_process( + COMMAND "${GIT_EXECUTABLE}" log -1 --format=%h + WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} + OUTPUT_VARIABLE GIT_HASH + OUTPUT_STRIP_TRAILING_WHITESPACE + ERROR_QUIET + ) +endif() #----------------------------------------------------------------------------- # Configuration diff --git a/cmake/tool-installed.tmpl b/cmake/tool-installed.tmpl index 0b245dbb..8825daaf 100644 --- a/cmake/tool-installed.tmpl +++ b/cmake/tool-installed.tmpl @@ -7,6 +7,9 @@ sys.path.insert(1, '@NOMINATIM_LIBDIR@/lib-python') os.environ['NOMINATIM_NOMINATIM_TOOL'] = os.path.abspath(__file__) from nominatim import cli +from nominatim import version + +version.GIT_COMMIT_HASH = '@GIT_HASH@' exit(cli.nominatim(module_dir='@NOMINATIM_LIBDIR@/module', osm2pgsql_path='@NOMINATIM_LIBDIR@/osm2pgsql', diff --git a/cmake/tool.tmpl b/cmake/tool.tmpl index a6022402..c1ecd3f0 100755 --- a/cmake/tool.tmpl +++ b/cmake/tool.tmpl @@ -7,6 +7,9 @@ sys.path.insert(1, '@CMAKE_SOURCE_DIR@') os.environ['NOMINATIM_NOMINATIM_TOOL'] = os.path.abspath(__file__) from nominatim import cli +from nominatim import version + +version.GIT_COMMIT_HASH = '@GIT_HASH@' exit(cli.nominatim(module_dir='@CMAKE_BINARY_DIR@/module', osm2pgsql_path='@CMAKE_BINARY_DIR@/osm2pgsql/osm2pgsql', diff --git a/nominatim/cli.py b/nominatim/cli.py index 2ddf5882..01eb6119 100644 --- a/nominatim/cli.py +++ b/nominatim/cli.py @@ -38,8 +38,7 @@ class CommandlineParser: dest='subcommand') # Global arguments that only work if no sub-command given - self.parser.add_argument('--version', action='version', - version=CommandlineParser.nominatim_version_text(), + self.parser.add_argument('--version', action='store_true', help='Print Nominatim version and exit') # Arguments added to every sub-command @@ -61,7 +60,10 @@ class CommandlineParser: def nominatim_version_text(): """ Program name and version number as string """ - return "Nominatim version %s.%s.%s.%s\n" % version.NOMINATIM_VERSION + text = 'Nominatim version %s.%s.%s.%s' % version.NOMINATIM_VERSION + if version.GIT_COMMIT_HASH is not None: + text += ' (%s)' % version.GIT_COMMIT_HASH + return text def add_subcommand(self, name, cmd): """ Add a subcommand to the parser. The subcommand must be a class @@ -86,6 +88,10 @@ class CommandlineParser: except SystemExit: return 1 + if args.version: + print(CommandlineParser.nominatim_version_text()) + return 0 + if args.subcommand is None: self.parser.print_help() return 1 diff --git a/nominatim/version.py b/nominatim/version.py index b876002e..47fe3b30 100644 --- a/nominatim/version.py +++ b/nominatim/version.py @@ -28,3 +28,9 @@ NOMINATIM_VERSION = (4, 0, 99, 6) POSTGRESQL_REQUIRED_VERSION = (9, 5) POSTGIS_REQUIRED_VERSION = (2, 2) + +# Cmake sets a variabe @GIT_HASH@ by executing 'git --log'. It is not run +# on every execution of 'make'. +# cmake/tool-installed.tmpl is used to build the binary 'nominatim'. Inside +# there is a call to set the variable value below. +GIT_COMMIT_HASH = None diff --git a/test/python/cli/test_cli.py b/test/python/cli/test_cli.py index e8e18504..07d6c31f 100644 --- a/test/python/cli/test_cli.py +++ b/test/python/cli/test_cli.py @@ -29,7 +29,7 @@ def test_cli_help(cli_call, capsys): def test_cli_version(cli_call, capsys): """ Running nominatim tool --version prints a version string. """ - assert cli_call('--version') == 1 + assert cli_call('--version') == 0 captured = capsys.readouterr() assert captured.out.startswith('Nominatim version')