Skip to content

Commit 53f3e7c

Browse files
authored
version: simplify its logic (#1068)
1 parent e55d13c commit 53f3e7c

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed

src/Cmd/Version.cc

+33-24
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,29 @@
1414
#ifndef CABIN_CABIN_PKG_VERSION
1515
# error "CABIN_CABIN_PKG_VERSION is not defined"
1616
#endif
17+
1718
#ifndef CABIN_CABIN_COMMIT_SHORT_HASH
1819
# error "CABIN_CABIN_COMMIT_SHORT_HASH is not defined"
20+
#else
21+
# define COMMIT_SHORT_HASH CABIN_CABIN_COMMIT_SHORT_HASH
1922
#endif
23+
2024
#ifndef CABIN_CABIN_COMMIT_HASH
2125
# error "CABIN_CABIN_COMMIT_HASH is not defined"
26+
#else
27+
# define COMMIT_HASH CABIN_CABIN_COMMIT_HASH
2228
#endif
29+
2330
#ifndef CABIN_CABIN_COMMIT_DATE
2431
# error "CABIN_CABIN_COMMIT_DATE is not defined"
32+
#else
33+
# define COMMIT_DATE CABIN_CABIN_COMMIT_DATE
34+
#endif
35+
36+
#if defined(__GNUC__) && !defined(__clang__)
37+
# define COMPILER_VERSION "GCC " __VERSION__
38+
#else
39+
# define COMPILER_VERSION __VERSION__
2540
#endif
2641

2742
const Subcmd VERSION_CMD = //
@@ -30,17 +45,18 @@ const Subcmd VERSION_CMD = //
3045
.setMainFn(versionMain);
3146

3247
static consteval std::string_view
33-
checkAvailability(const std::string_view str) noexcept {
34-
return str.empty() ? "unavailable" : str;
48+
commitInfo() noexcept {
49+
if (sizeof(COMMIT_SHORT_HASH) <= 1 && sizeof(COMMIT_DATE) <= 1) {
50+
return "\n";
51+
} else if (sizeof(COMMIT_SHORT_HASH) <= 1) {
52+
return " (" COMMIT_DATE ")\n";
53+
} else if (sizeof(COMMIT_DATE) <= 1) {
54+
return " (" COMMIT_SHORT_HASH ")\n";
55+
} else {
56+
return " (" COMMIT_SHORT_HASH " " COMMIT_DATE ")\n";
57+
}
3558
}
3659

37-
static constinit const std::string_view COMMIT_SHORT_HASH =
38-
checkAvailability(CABIN_CABIN_COMMIT_SHORT_HASH);
39-
static constinit const std::string_view COMMIT_HASH =
40-
checkAvailability(CABIN_CABIN_COMMIT_HASH);
41-
static constinit const std::string_view COMMIT_DATE =
42-
checkAvailability(CABIN_CABIN_COMMIT_DATE);
43-
4460
static consteval char
4561
firstMonthChar(const std::string_view month) noexcept {
4662
return (month[0] == 'O' || month[0] == 'N' || month[0] == 'D') ? '1' : '0';
@@ -128,22 +144,15 @@ versionMain(const std::span<const std::string_view> args) noexcept {
128144
}
129145
}
130146

131-
std::cout << "cabin " << CABIN_CABIN_PKG_VERSION;
132-
if (COMMIT_SHORT_HASH == "unavailable" && COMMIT_DATE == "unavailable") {
133-
std::cout << '\n';
134-
} else if (COMMIT_SHORT_HASH == "unavailable") {
135-
std::cout << " (" << COMMIT_DATE << ")\n";
136-
} else if (COMMIT_DATE == "unavailable") {
137-
std::cout << " (" << COMMIT_SHORT_HASH << ")\n";
138-
} else {
139-
std::cout << " (" << COMMIT_SHORT_HASH << ' ' << COMMIT_DATE << ")\n";
140-
}
141-
147+
std::cout << "cabin " CABIN_CABIN_PKG_VERSION << commitInfo();
142148
if (isVerbose()) {
143-
std::cout << "release: " << CABIN_CABIN_PKG_VERSION << '\n'
144-
<< "commit-hash: " << COMMIT_HASH << '\n'
145-
<< "commit-date: " << COMMIT_DATE << '\n'
146-
<< "compiler: " << __VERSION__ << '\n'
149+
std::cout << "release: " CABIN_CABIN_PKG_VERSION
150+
"\n"
151+
"commit-hash: " COMMIT_HASH
152+
"\n"
153+
"commit-date: " COMMIT_DATE
154+
"\n"
155+
"compiler: " COMPILER_VERSION "\n"
147156
<< "compile-date: " << COMPILE_DATE << '\n'
148157
<< "libgit2: " << git2::Version() << '\n'
149158
<< "libcurl: " << curl::Version() << '\n';

0 commit comments

Comments
 (0)