build: Use git describe for version strings.

When building in a git repo:
  - If HEAD corresponds to an annotated tag, (i.e. git_get_exact_tag()
    returns truthy) the current build is considered a "release" build:
    NVIM_VERSION_MEDIUM is directly assigned the tagged version name,
    and NVIM_VERSION_* defines are ignored.
  - If HEAD is not a tagged release, then NVIM_VERSION_MEDIUM is
    directly assigned the result of `git describe`.

If git (or the repo) is not available:
  - The NVIM_VERSION_* defines are used to define NVIM_VERSION_MEDIUM.

Sample outputs for `nvim --version` and `nvim +version`:

    Building with git @ non-tagged commit e66df14:
      NVIM v0.1.0-1-ge66df14 (compiled Nov  1 2015 19:10:30)
      Commit: e66df148f9401be17adab324a6e41d927aae20b3

    Building with git @ v0.1.1 tag:
      NVIM v0.1.1 (compiled Nov  1 2015 19:03:52)
      [no "Commit:" line]

    Building this commit _not_ in a git repo:
      NVIM 0.1.0-dev (compiled Nov  1 2015 19:16:11)
      [no "Commit:" line]
This commit is contained in:
Justin M. Keyes 2015-11-01 17:08:38 -05:00
parent c4826c3003
commit d0401b04a4
4 changed files with 23 additions and 20 deletions

View File

@ -47,24 +47,26 @@ if(NOT CMAKE_BUILD_TYPE)
endif()
# Version tokens
# - In a git repo, these tokens are _ignored_.
# - If the current HEAD is tagged, the tag name is used.
# - Otherwise the result of `git describe` is used.
# - If not in a git repo (e.g. a tarball) these tokens set the version string.
set(NVIM_VERSION_MAJOR 0)
set(NVIM_VERSION_MINOR 1)
set(NVIM_VERSION_PATCH 0)
set(NVIM_VERSION_PRERELEASE "-dev")
file(TO_CMAKE_PATH ${CMAKE_CURRENT_LIST_DIR}/.git FORCED_GIT_DIR)
include(GetGitRevisionDescription)
if(NVIM_VERSION_PRERELEASE)
get_git_head_revision(GIT_REFSPEC NVIM_VERSION_COMMIT)
# TODO(justinmk): UTC time would be nice here #1071
git_timestamp(GIT_TIMESTAMP)
if(GIT_TIMESTAMP)
set(NVIM_VERSION_BUILD "+${GIT_TIMESTAMP}")
get_git_head_revision(GIT_REFSPEC NVIM_VERSION_COMMIT)
if(NVIM_VERSION_COMMIT) # is a git repo
git_get_exact_tag(NVIM_VERSION_MEDIUM)
if(NVIM_VERSION_MEDIUM) # is a tagged release
unset(NVIM_VERSION_COMMIT)
else() # is a dev build
git_describe(NVIM_VERSION_MEDIUM)
get_git_head_revision(GIT_REFSPEC NVIM_VERSION_COMMIT)
endif()
else()
# If possible, get the Git tag for the current revision.
git_get_exact_tag(NVIM_VERSION_COMMIT)
set(NVIM_VERSION_BUILD "")
endif()
set(NVIM_VERSION_BUILD_TYPE "${CMAKE_BUILD_TYPE}")

View File

@ -5,8 +5,9 @@
#define NVIM_VERSION_MINOR @NVIM_VERSION_MINOR@
#define NVIM_VERSION_PATCH @NVIM_VERSION_PATCH@
#define NVIM_VERSION_PRERELEASE "@NVIM_VERSION_PRERELEASE@"
#define NVIM_VERSION_BUILD "@NVIM_VERSION_BUILD@"
#cmakedefine NVIM_VERSION_COMMIT "@NVIM_VERSION_COMMIT@"
#cmakedefine NVIM_VERSION_MEDIUM "@NVIM_VERSION_MEDIUM@"
#define NVIM_VERSION_CFLAGS "@NVIM_VERSION_CFLAGS@"
#define NVIM_VERSION_BUILD_TYPE "@NVIM_VERSION_BUILD_TYPE@"

View File

@ -25,17 +25,18 @@
#define STR_(x) #x
#define STR(x) STR_(x)
// for the startup-screen ( ":intro" command )
#define NVIM_VERSION_MEDIUM STR(NVIM_VERSION_MAJOR) "." STR(NVIM_VERSION_MINOR)
// for the ":version" command and "nvim --version"
#define NVIM_VERSION_LONG "NVIM " NVIM_VERSION_MEDIUM "." STR(NVIM_VERSION_PATCH) NVIM_VERSION_PRERELEASE NVIM_VERSION_BUILD
// for ":version", ":intro", and "nvim --version"
#ifndef NVIM_VERSION_MEDIUM
#define NVIM_VERSION_MEDIUM STR(NVIM_VERSION_MAJOR) "." STR(NVIM_VERSION_MINOR)\
"." STR(NVIM_VERSION_PATCH) NVIM_VERSION_PRERELEASE
#endif
#define NVIM_VERSION_LONG "NVIM " NVIM_VERSION_MEDIUM
char *Version = VIM_VERSION_SHORT;
char *longVersion = NVIM_VERSION_LONG;
char *longVersionWithDate = NVIM_VERSION_LONG " (compiled " __DATE__ " " __TIME__ ")";
char *mediumVersion = NVIM_VERSION_MEDIUM;
char *longVersionWithDate = NVIM_VERSION_LONG \
" (compiled " __DATE__ " " __TIME__ ")";
#ifdef NVIM_VERSION_COMMIT
char *version_commit = "Commit: " NVIM_VERSION_COMMIT;
#endif

View File

@ -3,7 +3,6 @@
// defined in version.c
extern char* Version;
extern char* mediumVersion;
extern char* longVersion;
//