Show number of commits and git id in version number if it is a dev version

This commit is contained in:
Sebastian Messmer 2015-09-18 17:34:56 +02:00
parent 4cf1237bb8
commit fc7316f06c
6 changed files with 80 additions and 34 deletions

View File

@ -24,7 +24,7 @@ using std::endl;
int main (int argc, char *argv[]) int main (int argc, char *argv[])
{ {
cout << "CryFS Version "<<version::VERSION.toString() << endl; cout << "CryFS Version "<<version::VERSION.toString() << endl;
if (version::COMMITS_SINCE_TAG != 0) { if (version::VERSION.is_dev()) {
cout << "WARNING! This is a development version based on git commit " << version::GIT_COMMIT_ID << ". Please do not use in production!" << endl; cout << "WARNING! This is a development version based on git commit " << version::GIT_COMMIT_ID << ". Please do not use in production!" << endl;
} else if (!version::VERSION.is_stable()) { } else if (!version::VERSION.is_stable()) {
cout << "WARNING! This is an experimental version. Please backup your data frequently!" << endl; cout << "WARNING! This is an experimental version. Please backup your data frequently!" << endl;

View File

@ -19,8 +19,10 @@ namespace version {
class Version { class Version {
public: public:
constexpr Version(unsigned int major, unsigned int minor, VersionTag tag) constexpr Version(unsigned int major, unsigned int minor, VersionTag tag, unsigned int commitsSinceVersion,
: _major(major), _minor(minor), _tag(tag) { } const cpputils::const_string &gitCommitId)
: _major(major), _minor(minor), _tag(tag), _commitsSinceVersion(commitsSinceVersion),
_gitCommitId(gitCommitId) { }
constexpr unsigned int major() { constexpr unsigned int major() {
return _major; return _major;
@ -34,8 +36,12 @@ namespace version {
return _tag; return _tag;
} }
constexpr bool is_dev() {
return _commitsSinceVersion != 0;
}
constexpr bool is_stable() { constexpr bool is_stable() {
return _tag == VersionTag::FINAL; return (!is_dev()) && _tag == VersionTag::FINAL;
} }
constexpr bool operator==(const Version &rhs) { constexpr bool operator==(const Version &rhs) {
@ -47,14 +53,24 @@ namespace version {
} }
std::string toString() const { std::string toString() const {
return std::to_string(_major) + "." + std::to_string(_minor) + VersionTagToString(_tag).toStdString(); if (is_dev()) {
return _versionTagString() + "-dev" + std::to_string(_commitsSinceVersion) + "-" + _gitCommitId.toStdString();
} else {
return _versionTagString();
}
} }
private: private:
std::string _versionTagString() const {
return std::to_string(_major) + "." + std::to_string(_minor) + VersionTagToString(_tag).toStdString();
}
const unsigned int _major; const unsigned int _major;
const unsigned int _minor; const unsigned int _minor;
const VersionTag _tag; const VersionTag _tag;
const unsigned int _commitsSinceVersion;
const cpputils::const_string _gitCommitId;
}; };
} }

View File

@ -11,7 +11,9 @@ namespace git_version_builder {
namespace version { namespace version {
constexpr unsigned int COMMITS_SINCE_TAG = git_version_builder::version::COMMITS_SINCE_TAG; constexpr unsigned int COMMITS_SINCE_TAG = git_version_builder::version::COMMITS_SINCE_TAG;
constexpr const char *GIT_COMMIT_ID = git_version_builder::version::GIT_COMMIT_ID; constexpr const char *GIT_COMMIT_ID = git_version_builder::version::GIT_COMMIT_ID;
constexpr const Version VERSION = VersionParser::parse(git_version_builder::version::TAG_NAME); constexpr const Version VERSION = VersionParser::parse(git_version_builder::version::TAG_NAME,
git_version_builder::version::COMMITS_SINCE_TAG,
git_version_builder::version::GIT_COMMIT_ID);
} }
#endif #endif

View File

@ -8,10 +8,14 @@
namespace version { namespace version {
class VersionParser { class VersionParser {
public: public:
static constexpr Version parse(const cpputils::const_string &input) { static constexpr Version parse(const cpputils::const_string &tagName, unsigned int commitsSinceVersion,
return Version(VersionParser::extractMajor(input), const cpputils::const_string &gitCommitId) {
VersionParser::extractMinor(input), return Version(VersionParser::extractMajor(tagName),
parseTag(extractTag(input))); VersionParser::extractMinor(tagName),
parseTag(extractTag(tagName)),
commitsSinceVersion,
gitCommitId
);
} }
static constexpr unsigned int extractMajor(const cpputils::const_string &input) { static constexpr unsigned int extractMajor(const cpputils::const_string &input) {
@ -20,7 +24,8 @@ namespace version {
static constexpr unsigned int extractMinor(const cpputils::const_string &input) { static constexpr unsigned int extractMinor(const cpputils::const_string &input) {
return (input.dropUIntPrefix()[0] == '.') ? input.dropUIntPrefix().dropPrefix(1).parseUIntPrefix() return (input.dropUIntPrefix()[0] == '.') ? input.dropUIntPrefix().dropPrefix(1).parseUIntPrefix()
: throw std::logic_error("Minor version should be separated by a dot"); : throw std::logic_error(
"Minor version should be separated by a dot");
} }
static constexpr cpputils::const_string extractTag(const cpputils::const_string &input) { static constexpr cpputils::const_string extractTag(const cpputils::const_string &input) {

View File

@ -11,24 +11,44 @@ static_assert(const_string("beta") == VersionTagToString(VersionTag::BETA), "Ver
static_assert(const_string("rc1") == VersionTagToString(VersionTag::RC1), "VersionTag::RC1 toString"); static_assert(const_string("rc1") == VersionTagToString(VersionTag::RC1), "VersionTag::RC1 toString");
static_assert(const_string("") == VersionTagToString(VersionTag::FINAL), "VersionTag::FINAL toString"); static_assert(const_string("") == VersionTagToString(VersionTag::FINAL), "VersionTag::FINAL toString");
static_assert(Version(1, 0, VersionTag::ALPHA) == Version(1, 0, VersionTag::ALPHA), "Equality for equals"); static_assert(Version(1, 0, VersionTag::ALPHA, 0, "commitid") == Version(1, 0, VersionTag::ALPHA, 0, "commitid"), "Equality for equals");
static_assert(!(Version(1, 0, VersionTag::ALPHA) != Version(1, 0, VersionTag::ALPHA)), "Inequality for equals"); static_assert(Version(0, 8, VersionTag::FINAL, 2, "commitid") == Version(0, 8, VersionTag::FINAL, 2, "commitid"), "Equality for equals");
static_assert(!(Version(1, 0, VersionTag::ALPHA, 0, "commitid") != Version(1, 0, VersionTag::ALPHA, 0, "commitid")), "Inequality for equals");
static_assert(!(Version(0, 8, VersionTag::FINAL, 2, "commitid") != Version(0, 8, VersionTag::FINAL, 2, "commitid")), "Inequality for equals");
static_assert(!(Version(1, 0, VersionTag::ALPHA) == Version(2, 0, VersionTag::ALPHA)), "Equality for inequal major"); static_assert(!(Version(1, 0, VersionTag::ALPHA, 0, "commitid") == Version(2, 0, VersionTag::ALPHA, 0, "commitid")), "Equality for inequal major");
static_assert(!(Version(1, 0, VersionTag::ALPHA) == Version(1, 1, VersionTag::ALPHA)), "Equality for inequal minor"); static_assert(!(Version(1, 0, VersionTag::ALPHA, 0, "commitid") == Version(1, 1, VersionTag::ALPHA, 0, "commitid")), "Equality for inequal minor");
static_assert(!(Version(1, 0, VersionTag::ALPHA) == Version(1, 0, VersionTag::FINAL)), "Equality for inequal tag"); static_assert(!(Version(1, 0, VersionTag::ALPHA, 0, "commitid") == Version(1, 0, VersionTag::FINAL, 0, "commitid")), "Equality for inequal tag");
static_assert(Version(1, 0, VersionTag::ALPHA) != Version(2, 0, VersionTag::ALPHA), "Inequality for inequal major"); static_assert(!(Version(1, 0, VersionTag::ALPHA, 0, "commitid") == Version(1, 0, VersionTag::FINAL, 1, "commitid")), "Equality for inequal commitsSinceVersion");
static_assert(Version(1, 0, VersionTag::ALPHA) != Version(1, 1, VersionTag::ALPHA), "Inequality for inequal minor"); static_assert(!(Version(1, 0, VersionTag::ALPHA, 0, "commitid") == Version(1, 0, VersionTag::FINAL, 0, "commitid2")), "Equality for inequal gitCommitId");
static_assert(Version(1, 0, VersionTag::ALPHA) != Version(1, 0, VersionTag::FINAL), "Inequality for inequal tag"); static_assert(Version(1, 0, VersionTag::ALPHA, 0, "commitid") != Version(2, 0, VersionTag::ALPHA, 0, "commitid"), "Inequality for inequal major");
static_assert(Version(1, 0, VersionTag::ALPHA, 0, "commitid") != Version(1, 1, VersionTag::ALPHA, 0, "commitid"), "Inequality for inequal minor");
static_assert(Version(1, 0, VersionTag::ALPHA, 0, "commitid") != Version(1, 0, VersionTag::FINAL, 0, "commitid"), "Inequality for inequal tag");
static_assert(Version(1, 0, VersionTag::ALPHA, 0, "commitid") != Version(1, 0, VersionTag::FINAL, 1, "commitid"), "Inequality for inequal commitsSinceVersion");
static_assert(Version(1, 0, VersionTag::ALPHA, 0, "commitid") != Version(1, 0, VersionTag::FINAL, 0, "commitid2"), "Inequality for inequal gitCommitId");
static_assert(!Version(1, 0, VersionTag::ALPHA).is_stable(), "Alpha is not stable"); static_assert(!Version(1, 0, VersionTag::ALPHA, 0, "commitid").is_stable(), "Alpha is not stable");
static_assert(!Version(1, 0, VersionTag::BETA).is_stable(), "Beta is not stable"); static_assert(!Version(1, 0, VersionTag::BETA, 0, "commitid").is_stable(), "Beta is not stable");
static_assert(!Version(1, 0, VersionTag::RC1).is_stable(), "RC1 is not stable"); static_assert(!Version(1, 0, VersionTag::RC1, 0, "commitid").is_stable(), "RC1 is not stable");
static_assert(Version(1, 0, VersionTag::FINAL).is_stable(), "Final is stable"); static_assert(Version(1, 0, VersionTag::FINAL, 0, "commitid").is_stable(), "Final is stable");
static_assert(!Version(1, 0, VersionTag::FINAL, 1, "commitid").is_stable(), "Final is not stable if there have been commits since");
static_assert(!Version(1, 0, VersionTag::FINAL, 0, "commitid").is_dev(), "Is not dev version when there haven't been commits since the last tag");
static_assert(!Version(1, 0, VersionTag::ALPHA, 0, "commitid").is_dev(), "Is not dev version when there haven't been commits since the last tag, also for alpha versions");
static_assert(Version(1, 0, VersionTag::ALPHA, 1, "commitid").is_dev(), "Is dev version when there haven't been commits since the last tag");
static_assert(Version(1, 0, VersionTag::FINAL, 1, "commitid").is_dev(), "Is dev version when there haven't been commits since the last tag, also for final versions");
static_assert(Version(1, 0, VersionTag::ALPHA, 103, "commitid").is_dev(), "Is dev version when there haven't been commits since the last tag, also for higher commit counts");
TEST(VersionTest, ToString) { TEST(VersionTest, ToString) {
EXPECT_EQ("0.8alpha", Version(0, 8, VersionTag::ALPHA).toString()); EXPECT_EQ("0.8alpha", Version(0, 8, VersionTag::ALPHA, 0, "commitid").toString());
EXPECT_EQ("1.2beta", Version(1, 2, VersionTag::BETA).toString()); EXPECT_EQ("1.2beta", Version(1, 2, VersionTag::BETA, 0, "commitid").toString());
EXPECT_EQ("12.0rc1", Version(12, 0, VersionTag::RC1).toString()); EXPECT_EQ("12.0rc1", Version(12, 0, VersionTag::RC1, 0, "commitid").toString());
EXPECT_EQ("12.34", Version(12, 34, VersionTag::FINAL).toString()); EXPECT_EQ("12.34", Version(12, 34, VersionTag::FINAL, 0, "commitid").toString());
}
TEST(VersionTest, ToString_WithCommitsSinceVersion) {
EXPECT_EQ("0.8alpha-dev2-commitid1", Version(0, 8, VersionTag::ALPHA, 2, "commitid1").toString());
EXPECT_EQ("1.2beta-dev1-commitid2", Version(1, 2, VersionTag::BETA, 1, "commitid2").toString());
EXPECT_EQ("12.0rc1-dev5-commitid3", Version(12, 0, VersionTag::RC1, 5, "commitid3").toString());
EXPECT_EQ("12.34-dev103-commitid4", Version(12, 34, VersionTag::FINAL, 103, "commitid4").toString());
} }

View File

@ -29,10 +29,13 @@ static_assert(VersionTag::BETA == VersionParser::parseTag("beta"), "beta version
static_assert(VersionTag::RC1 == VersionParser::parseTag("rc1"), "rc1 version tag should be parseable"); static_assert(VersionTag::RC1 == VersionParser::parseTag("rc1"), "rc1 version tag should be parseable");
static_assert(VersionTag::FINAL == VersionParser::parseTag(""), "final version tag should be parseable"); static_assert(VersionTag::FINAL == VersionParser::parseTag(""), "final version tag should be parseable");
static_assert(Version(1, 0, VersionTag::ALPHA) == VersionParser::parse("1.0alpha"), "1.0alpha should parse correctly"); static_assert(Version(1, 0, VersionTag::ALPHA, 0, "commitid") == VersionParser::parse("1.0alpha", 0, "commitid"), "1.0alpha should parse correctly");
static_assert(Version(12, 34, VersionTag::BETA) == VersionParser::parse("12.34beta"), "12.34beta should parse correctly"); static_assert(Version(12, 34, VersionTag::BETA, 0, "commitid") == VersionParser::parse("12.34beta", 0, "commitid"), "12.34beta should parse correctly");
static_assert(Version(0, 8, VersionTag::RC1) == VersionParser::parse("0.8rc1"), "0.8rc1 should parse correctly"); static_assert(Version(0, 8, VersionTag::RC1, 0, "commitid") == VersionParser::parse("0.8rc1", 0, "commitid"), "0.8rc1 should parse correctly");
static_assert(Version(1, 2, VersionTag::FINAL) == VersionParser::parse("1.2"), "1.2 should parse correctly"); static_assert(Version(1, 2, VersionTag::FINAL, 0, "commitid") == VersionParser::parse("1.2", 0, "commitid"), "1.2 should parse correctly");
static_assert(Version(1, 2, VersionTag::FINAL) == VersionParser::parse("1.02"), "1.02 should parse correctly"); static_assert(Version(1, 2, VersionTag::FINAL, 0, "commitid") == VersionParser::parse("1.02", 0, "commitid"), "1.02 should parse correctly");
static_assert(Version(1, 20, VersionTag::FINAL) == VersionParser::parse("1.20"), "1.20 should parse correctly"); static_assert(Version(1, 20, VersionTag::FINAL, 0, "commitid") == VersionParser::parse("1.20", 0, "commitid"), "1.20 should parse correctly");
static_assert(Version(1, 20, VersionTag::FINAL) == VersionParser::parse("1.020"), "1.020 should parse correctly"); static_assert(Version(1, 20, VersionTag::FINAL, 0, "commitid") == VersionParser::parse("1.020", 0, "commitid"), "1.020 should parse correctly");
static_assert(Version(1, 20, VersionTag::FINAL, 103, "commitid") == VersionParser::parse("1.020", 103, "commitid"), "commitsSinceVersion should parse correctly");
static_assert(Version(1, 20, VersionTag::FINAL, 103, "other_commitid") == VersionParser::parse("1.020", 103, "other_commitid"), "commitId should parse correctly");