Improve git commit hash lookup (#5225)

- Also get the branch name.
- Use rev-parse instead of describe to get a clean hash.
- Return the git hash and branch name in server_info for admin
  connections.
- Include git hash and branch name on separate lines in --version.
This commit is contained in:
Ed Hennis
2025-02-05 11:36:43 -05:00
committed by GitHub
parent 33e1c42599
commit f6d63082c0
5 changed files with 55 additions and 6 deletions

View File

@@ -19,13 +19,21 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
# make GIT_COMMIT_HASH define available to all sources
find_package(Git)
if(Git_FOUND)
execute_process(COMMAND ${GIT_EXECUTABLE} --git-dir=${CMAKE_CURRENT_SOURCE_DIR}/.git describe --always --abbrev=40
execute_process(COMMAND ${GIT_EXECUTABLE} --git-dir=${CMAKE_CURRENT_SOURCE_DIR}/.git rev-parse HEAD
OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE gch)
if(gch)
set(GIT_COMMIT_HASH "${gch}")
message(STATUS gch: ${GIT_COMMIT_HASH})
add_definitions(-DGIT_COMMIT_HASH="${GIT_COMMIT_HASH}")
endif()
execute_process(COMMAND ${GIT_EXECUTABLE} --git-dir=${CMAKE_CURRENT_SOURCE_DIR}/.git rev-parse --abbrev-ref HEAD
OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE gb)
if(gb)
set(GIT_BRANCH "${gb}")
message(STATUS gb: ${GIT_BRANCH})
add_definitions(-DGIT_BRANCH="${GIT_BRANCH}")
endif()
endif() #git
if(thread_safety_analysis)

View File

@@ -167,6 +167,7 @@ JSS(blobs_v2); // out: ValidatorList
JSS(books); // in: Subscribe, Unsubscribe
JSS(both); // in: Subscribe, Unsubscribe
JSS(both_sides); // in: Subscribe, Unsubscribe
JSS(branch); // out: server_info
JSS(broadcast); // out: SubmitTransaction
JSS(bridge_account); // in: LedgerEntry
JSS(build_path); // in: TransactionSign
@@ -290,6 +291,7 @@ JSS(frozen_balances); // out: GatewayBalances
JSS(full); // in: LedgerClearer, handlers/Ledger
JSS(full_reply); // out: PathFind
JSS(fullbelow_size); // out: GetCounts
JSS(git); // out: server_info
JSS(good); // out: RPCVersion
JSS(hash); // out: NetworkOPs, InboundLedger,
// LedgerToJson, STTx; field

View File

@@ -86,21 +86,42 @@ admin = 127.0.0.1
{
Env env(*this);
auto const result = env.rpc("server_info");
BEAST_EXPECT(!result[jss::result].isMember(jss::error));
BEAST_EXPECT(result[jss::result][jss::status] == "success");
BEAST_EXPECT(result[jss::result].isMember(jss::info));
auto const serverinfo = env.rpc("server_info");
BEAST_EXPECT(serverinfo.isMember(jss::result));
auto const& result = serverinfo[jss::result];
BEAST_EXPECT(!result.isMember(jss::error));
BEAST_EXPECT(result[jss::status] == "success");
BEAST_EXPECT(result.isMember(jss::info));
auto const& info = result[jss::info];
BEAST_EXPECT(info.isMember(jss::build_version));
// Git info is not guaranteed to be present
if (info.isMember(jss::git))
{
auto const& git = info[jss::git];
BEAST_EXPECT(
git.isMember(jss::hash) || git.isMember(jss::branch));
BEAST_EXPECT(
!git.isMember(jss::hash) ||
(git[jss::hash].isString() &&
git[jss::hash].asString().size() == 40));
BEAST_EXPECT(
!git.isMember(jss::branch) ||
(git[jss::branch].isString() &&
git[jss::branch].asString().size() != 0));
}
}
{
Env env(*this);
// Call NetworkOPs directly and set the admin flag to false.
// Expect that the admin ports are not included in the result.
auto const result =
env.app().getOPs().getServerInfo(true, false, 0);
// Expect that the admin ports are not included in the result.
auto const& ports = result[jss::ports];
BEAST_EXPECT(ports.isArray() && ports.size() == 0);
// Expect that git info is absent
BEAST_EXPECT(!result.isMember(jss::git));
}
{

View File

@@ -521,6 +521,12 @@ run(int argc, char** argv)
{
std::cout << "rippled version " << BuildInfo::getVersionString()
<< std::endl;
#ifdef GIT_COMMIT_HASH
std::cout << "Git commit hash: " << GIT_COMMIT_HASH << std::endl;
#endif
#ifdef GIT_BRANCH
std::cout << "Git build branch: " << GIT_BRANCH << std::endl;
#endif
return 0;
}

View File

@@ -2493,6 +2493,18 @@ NetworkOPsImp::getServerInfo(bool human, bool admin, bool counters)
x[jss::expiration] = "unknown";
}
}
#if defined(GIT_COMMIT_HASH) || defined(GIT_BRANCH)
{
auto& x = (info[jss::git] = Json::objectValue);
#ifdef GIT_COMMIT_HASH
x[jss::hash] = GIT_COMMIT_HASH;
#endif
#ifdef GIT_BRANCH
x[jss::branch] = GIT_BRANCH;
#endif
}
#endif
}
info[jss::io_latency_ms] =
static_cast<Json::UInt>(app_.getIOLatency().count());