From 557927010859899f6da1a2cd48e31a1eb1485e4a Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Wed, 2 Mar 2016 14:39:30 +0100 Subject: [PATCH] Handle fallback version "0+unknown" correctly --- src/gitversion/parser.cpp | 19 ++++++++++++------- test/gitversion/ParserTest.cpp | 12 ++++++++++-- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/gitversion/parser.cpp b/src/gitversion/parser.cpp index 57bd4dc9..c12ad0fa 100644 --- a/src/gitversion/parser.cpp +++ b/src/gitversion/parser.cpp @@ -21,7 +21,7 @@ namespace gitversion { tie(result.majorVersion, result.minorVersion, result.versionTag) = _extractMajorMinorTag(versionNumber); result.isDevVersion = (versionInfo != none); result.isStableVersion = !result.isDevVersion && (result.versionTag == "" || result.versionTag == "stable"); - if (versionInfo != none) { + if (versionInfo != none && *versionInfo != "unknown") { result.gitCommitId = _extractGitCommitId(*versionInfo); } else { result.gitCommitId = ""; @@ -47,20 +47,25 @@ namespace gitversion { }; tuple Parser::_extractMajorMinorTag(const string &versionNumber) { - regex splitRegex("^([0-9]+)\\.([0-9]+)\\.[0-9\\.]*(-(.*))?$"); + regex splitRegex("^([0-9]+)(\\.([0-9]+))?(\\.[0-9\\.]*)?(-(.*))?$"); smatch match; regex_match(versionNumber, match, splitRegex); if(match[0] != versionNumber) { throw std::logic_error("First match group should be whole string"); } - if(match.size() != 5) { + if(match.size() != 7) { throw std::logic_error("Wrong number of match groups"); } - if(match[3].matched) { - return std::make_tuple(match[1], match[2], match[4]); - } else { - return std::make_tuple(match[1], match[2], ""); + string major = match[1]; + string minor = "0"; + if (match[3].matched) { + minor = match[3]; } + string tag = ""; + if (match[6].matched) { + tag = match[6]; + } + return std::make_tuple(major, minor, tag); }; string Parser::_extractGitCommitId(const string &versionInfo) { diff --git a/test/gitversion/ParserTest.cpp b/test/gitversion/ParserTest.cpp index 3a2aad54..9102f492 100644 --- a/test/gitversion/ParserTest.cpp +++ b/test/gitversion/ParserTest.cpp @@ -3,6 +3,16 @@ using namespace gitversion; +TEST(ParserTest, TestUnknownVersion) { + VersionInfo info = Parser::parse("0+unknown"); + EXPECT_EQ("0", info.majorVersion); + EXPECT_EQ("0", info.minorVersion); + EXPECT_TRUE( info.isDevVersion); + EXPECT_FALSE( info.isStableVersion); + EXPECT_EQ("", info.gitCommitId); + EXPECT_EQ("", info.versionTag); +} + TEST(ParserTest, TestReleaseVersion_1) { VersionInfo info = Parser::parse("0.9.2"); EXPECT_EQ("0", info.majorVersion); @@ -183,5 +193,3 @@ TEST(ParserTest, TestDirtyDevVersion_RCTag) { EXPECT_EQ("0123abcdef", info.gitCommitId); EXPECT_EQ("rc1", info.versionTag); } - -//TODO Dirty non-dev version 0.7.0+0.g5753e4f.dirty \ No newline at end of file