diff options
author | babenko <[email protected]> | 2025-04-07 13:47:42 +0300 |
---|---|---|
committer | babenko <[email protected]> | 2025-04-07 14:43:58 +0300 |
commit | a24849a5e16381ec0c969cd30331c8da1de23aba (patch) | |
tree | 0affd1ebd9fbd4a9b34837577fa90d04d7b4c592 | |
parent | b2ff25e46f24606806b7a40db9b22890800cef8f (diff) |
Better safety for GetRpcUserAgent
commit_hash:538e2bc8d0ac9c7a88ebb87f1a320b563329f28c
-rw-r--r-- | yt/yt/build/ya_version.cpp | 90 |
1 files changed, 39 insertions, 51 deletions
diff --git a/yt/yt/build/ya_version.cpp b/yt/yt/build/ya_version.cpp index 11a5dde2b50..defb5ec1394 100644 --- a/yt/yt/build/ya_version.cpp +++ b/yt/yt/build/ya_version.cpp @@ -101,60 +101,48 @@ TString GetYaBuildDate() return GetProgramBuildDate(); } -namespace { - -//////////////////////////////////////////////////////////////////////////////// - -TString BuildRpcUserAgent() noexcept +const TString& GetRpcUserAgent() { - // Fill user agent from build information. - // For trunk: - // - yt-cpp/r<revision> - // For YT release branch. - // - yt-cpp/<YT version string> - // For arbitrary branch different from trunk: - // - yt-cpp/<branch>~<commit> - // For local build from detached head or arc sync'ed state: - // - yt-cpp/local~<commit> - - TString branch(GetBranch()); - TStringStream out; - - out << "yt-cpp/"; - - if (branch == "trunk") { - int svnRevision = GetProgramSvnRevision(); - out << "trunk~r" << svnRevision; - } else if (branch.StartsWith("releases/yt")) { - // Simply re-use YT version string. It looks like the following: - // 20.3.7547269-stable-ya~bb57c034bfb47caa. - TString ytVersion(GetVersion()); - out << ytVersion; - } else { - auto commit = GetCommitHash(); - auto truncatedCommit = TruncateCommitHash(commit); - - // In detached head arc state branch seems to coincide with commit hash. - // Let's use that in order to distinguish detached head from regular branch state. - if (branch == commit) { - branch = "local"; + static const auto result = [&] { + // Fill user agent from build information. + // For trunk: + // - yt-cpp/r<revision> + // For YT release branch. + // - yt-cpp/<YT version string> + // For arbitrary branch different from trunk: + // - yt-cpp/<branch>~<commit> + // For local build from detached head or arc sync'ed state: + // - yt-cpp/local~<commit> + + TString branch(GetBranch()); + TStringStream out; + + out << "yt-cpp/"; + + if (branch == "trunk") { + int svnRevision = GetProgramSvnRevision(); + out << "trunk~r" << svnRevision; + } else if (branch.StartsWith("releases/yt")) { + // Simply re-use YT version string. It looks like the following: + // 20.3.7547269-stable-ya~bb57c034bfb47caa. + TString ytVersion(GetVersion()); + out << ytVersion; + } else { + auto commit = GetCommitHash(); + auto truncatedCommit = TruncateCommitHash(commit); + + // In detached head arc state branch seems to coincide with commit hash. + // Let's use that in order to distinguish detached head from regular branch state. + if (branch == commit) { + branch = "local"; + } + + out << branch << "~" << truncatedCommit; } - out << branch << "~" << truncatedCommit; - } - - return out.Str(); -} - -const TString CachedUserAgent = BuildRpcUserAgent(); - -//////////////////////////////////////////////////////////////////////////////// - -} // namespace - -const TString& GetRpcUserAgent() -{ - return CachedUserAgent; + return out.Str(); + }(); + return result; } //////////////////////////////////////////////////////////////////////////////// |