From 37db4636c53a6d64a1d39ed1c6a2dbe56c89ce71 Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Mon, 5 Jun 2023 15:35:16 -0500 Subject: [PATCH] feat(version): add build field to vim.version() This exposes the git hash of the build to the lua table, generated by CMake. Closes #23863 --- cmake/GenerateVersion.cmake | 5 ++++- src/nvim/version.c | 6 ++++++ test/functional/api/version_spec.lua | 2 ++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/cmake/GenerateVersion.cmake b/cmake/GenerateVersion.cmake index ab046e93ba1a41..936a8f5e5a8565 100644 --- a/cmake/GenerateVersion.cmake +++ b/cmake/GenerateVersion.cmake @@ -14,6 +14,9 @@ if(RES) return() endif() +# Extract build info: "145-g0f9113907" => "g0f9113907" +string(REGEX REPLACE ".*\\-" "" NVIM_VERSION_BUILD "${GIT_TAG}") + # `git describe` annotates the most recent tagged release; for pre-release # builds we append that to the dev version. if(NVIM_VERSION_PRERELEASE) @@ -24,7 +27,7 @@ if(NVIM_VERSION_PRERELEASE) set(NVIM_VERSION "${NVIM_VERSION}-${NVIM_VERSION_GIT}") endif() -set(NVIM_VERSION_STRING "#define NVIM_VERSION_MEDIUM \"${NVIM_VERSION}\"\n") +set(NVIM_VERSION_STRING "#define NVIM_VERSION_MEDIUM \"${NVIM_VERSION}\"\n#define NVIM_VERSION_BUILD \"${NVIM_VERSION_BUILD}\"\n") string(SHA1 CURRENT_VERSION_HASH "${NVIM_VERSION_STRING}") if(EXISTS ${OUTPUT}) diff --git a/src/nvim/version.c b/src/nvim/version.c index c0e0ceef55f262..a111470ceff7d7 100644 --- a/src/nvim/version.c +++ b/src/nvim/version.c @@ -43,6 +43,7 @@ "." STR(NVIM_VERSION_MINOR) "." STR(NVIM_VERSION_PATCH) \ NVIM_VERSION_PRERELEASE #endif + #define NVIM_VERSION_LONG "NVIM " NVIM_VERSION_MEDIUM // NOLINT(bugprone-suspicious-missing-comma) char *Version = VIM_VERSION_SHORT; @@ -2560,6 +2561,11 @@ Dictionary version_dict(void) PUT(d, "major", INTEGER_OBJ(NVIM_VERSION_MAJOR)); PUT(d, "minor", INTEGER_OBJ(NVIM_VERSION_MINOR)); PUT(d, "patch", INTEGER_OBJ(NVIM_VERSION_PATCH)); +#ifndef NVIM_VERSION_BUILD + PUT(d, "build", NIL); +#else + PUT(d, "build", CSTR_AS_OBJ(NVIM_VERSION_BUILD)); +#endif PUT(d, "prerelease", BOOLEAN_OBJ(NVIM_VERSION_PRERELEASE[0] != '\0')); PUT(d, "api_level", INTEGER_OBJ(NVIM_API_LEVEL)); PUT(d, "api_compatible", INTEGER_OBJ(NVIM_API_LEVEL_COMPAT)); diff --git a/test/functional/api/version_spec.lua b/test/functional/api/version_spec.lua index 771192e9ab4138..1b3da660903496 100644 --- a/test/functional/api/version_spec.lua +++ b/test/functional/api/version_spec.lua @@ -34,6 +34,8 @@ describe("api_info()['version']", function() local minor = version['minor'] local patch = version['patch'] local prerelease = version['prerelease'] + local build = version['build'] + assert(build == nil or type(build) == 'string') eq("number", type(major)) eq("number", type(minor)) eq("number", type(patch))