Skip to content

Commit 7403086

Browse files
ivanmurashkofacebook-github-bot
authored andcommitted
Use HG root as the root dir for result returned by Glean
Summary: Glean returns the relative paths at location results. The diff converts the relative results to absolute ones using `hg root` as the root folder Reviewed By: Wilfred Differential Revision: D78978423 fbshipit-source-id: bdceb53869903e2da9df95e3b9cda30a913c032c
1 parent 0026948 commit 7403086

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

glean/client/swift/GlassAccess.cpp

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
#include <folly/String.h>
1212
#include <folly/coro/BlockingWait.h>
1313
#include <glog/logging.h>
14+
#include <cstdlib>
15+
#include <memory>
16+
#include <stdexcept>
1417

1518
using namespace facebook;
1619
using apache::thrift::RpcOptions;
1720

1821
const auto GlassTimeoutMs = 900;
1922

20-
GlassAccess::GlassAccess() : client(nullptr) {}
23+
GlassAccess::GlassAccess() : client(nullptr), hgRoot_(getHgRoot()) {}
2124

2225
std::optional<protocol::LocationList> GlassAccess::usrToDefinition(
2326
const std::string& usr) {
@@ -63,7 +66,7 @@ std::optional<protocol::LocationList> GlassAccess::usrToDefinition(
6366
protocol::Range protocolRange(start, end);
6467

6568
// Create URI from repository and filepath using URI escaping
66-
std::string filepath = gleanLocation.filepath_ref().value();
69+
std::string filepath = hgRoot_ + "/" + gleanLocation.filepath_ref().value();
6770
std::string uri = "file://" + folly::uriEscape<std::string>(filepath);
6871
protocol::Location location(uri, protocolRange);
6972

@@ -89,3 +92,34 @@ std::optional<T> GlassAccess::runGlassMethod(
8992
}
9093
return {};
9194
}
95+
96+
std::string GlassAccess::getHgRoot() {
97+
// Execute 'hg root' command to get the mercurial repository root
98+
FILE* pipe = popen("hg root", "r");
99+
if (!pipe) {
100+
throw std::runtime_error("Failed to execute 'hg root' command");
101+
}
102+
103+
char buffer[1024];
104+
std::string result;
105+
while (fgets(buffer, sizeof(buffer), pipe) != nullptr) {
106+
result += buffer;
107+
}
108+
109+
int status = pclose(pipe);
110+
if (status != 0) {
111+
throw std::runtime_error(
112+
"'hg root' command failed with status: " + std::to_string(status));
113+
}
114+
115+
// Remove trailing newline if present
116+
if (!result.empty() && result.back() == '\n') {
117+
result.pop_back();
118+
}
119+
120+
if (result.empty()) {
121+
throw std::runtime_error("'hg root' command returned empty result");
122+
}
123+
124+
return result;
125+
}

glean/client/swift/GlassAccess.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,8 @@ class GlassAccess : public IGlassAccess {
3030
folly::Function<folly::coro::Task<T>()> f);
3131

3232
std::unique_ptr<apache::thrift::Client<::glean::GlassService>> client;
33+
34+
private:
35+
std::string getHgRoot();
36+
std::string hgRoot_;
3337
};

0 commit comments

Comments
 (0)