forked from facebook/watchman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mercurial.cpp
330 lines (296 loc) · 10.6 KB
/
Mercurial.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* Copyright 2017-present Facebook, Inc.
* Licensed under the Apache License, Version 2.0 */
#include "watchman.h"
#include "Mercurial.h"
#include <folly/String.h>
#include <chrono>
#include <cmath>
#include <cstdio>
#include "ChildProcess.h"
#include "Logging.h"
// Capability indicating support for the mercurial SCM
W_CAP_REG("scm-hg")
using namespace std::chrono;
using folly::to;
namespace {
using namespace watchman;
void replaceEmbeddedNulls(std::string& str) {
std::replace(str.begin(), str.end(), '\0', '\n');
}
std::string hgExecutablePath() {
auto hg = getenv("EDEN_HG_BINARY");
if (hg && strlen(hg) > 0) {
return hg;
}
return "hg";
}
struct MercurialResult {
w_string output;
};
MercurialResult runMercurial(
std::vector<w_string_piece> cmdline,
ChildProcess::Options options,
folly::StringPiece description) {
ChildProcess proc{cmdline, std::move(options)};
auto outputs = proc.communicate();
auto status = proc.wait();
if (status) {
auto output = folly::StringPiece{outputs.first}.str();
auto error = folly::StringPiece{outputs.second}.str();
replaceEmbeddedNulls(output);
replaceEmbeddedNulls(error);
throw SCMError{"failed to ",
description,
"\ncmd = ",
folly::join(" ", cmdline),
"\nstdout = ",
output,
"\nstderr = ",
error};
}
return MercurialResult{std::move(outputs.first)};
}
} // namespace
namespace watchman {
ChildProcess::Options Mercurial::makeHgOptions(w_string requestId) const {
ChildProcess::Options opt;
// Ensure that the hgrc doesn't mess with the behavior
// of the commands that we're runing.
opt.environment().set("HGPLAIN", w_string("1"));
// Ensure that we do not telemetry log profiling data for the commands we are
// running by default. This is to avoid a significant increase in the rate of
// logging.
if (!cfg_get_bool("enable_hg_telemetry_logging", false)) {
opt.environment().set("NOSCMLOG", w_string("1"));
}
// chg can elect to kill all children if an error occurs in any child.
// This can cause commands we spawn to fail transiently. While we'd
// love to have the lowest latency, the transient failure causes problems
// with our ability to deliver notifications to our clients in a timely
// manner, so we disable the use of chg for the mercurial processes
// that we spawn.
opt.environment().set("CHGDISABLE", w_string("1"));
// This method is called from the eden watcher and can trigger before
// mercurial has finalized writing out its history data. Setting this
// environmental variable allows us to break the view isolation and read
// information about the commit before the transaction is complete.
opt.environment().set("HG_PENDING", getRootPath());
if (requestId && !requestId.empty()) {
opt.environment().set("HGREQUESTID", requestId);
}
// Default to strict hg status. HGDETECTRACE is used by some deployments
// of mercurial to cause `hg status` to error out if it detects mutation
// of the working copy that is happening currently with the status call.
// This has to be opt-in behavior as it changes the semantics of the status
// CLI invocation. Watchman is ready to handle this case in a reasonably
// defined manner, so we are safe to enable it.
if (cfg_get_bool("fsmonitor.detectrace", true)) {
opt.environment().set("HGDETECTRACE", w_string("1"));
}
// Ensure that mercurial uses this path to communicate with us,
// rather than whatever is hardcoded in its config.
opt.environment().set("WATCHMAN_SOCK", get_sock_name_legacy());
opt.nullStdin();
opt.pipeStdout();
opt.pipeStderr();
opt.chdir(getRootPath());
return opt;
}
Mercurial::Mercurial(w_string_piece rootPath, w_string_piece scmRoot)
: SCM(rootPath, scmRoot),
dirStatePath_(to<std::string>(getSCMRoot(), "/.hg/dirstate")),
commitsPrior_(Configuration(), "scm_hg_commits_prior", 32, 10),
mergeBases_(Configuration(), "scm_hg_mergebase", 32, 10),
filesChangedBetweenCommits_(
Configuration(),
"scm_hg_files_between_commits",
32,
10),
filesChangedSinceMergeBaseWith_(
Configuration(),
"scm_hg_files_since_mergebase",
32,
10) {}
struct timespec Mercurial::getDirStateMtime() const {
try {
auto info = getFileInformation(
dirStatePath_.c_str(), CaseSensitivity::CaseSensitive);
return info.mtime;
} catch (const std::system_error&) {
// Failed to stat, so assume the current time
struct timeval now;
gettimeofday(&now, nullptr);
struct timespec ts;
ts.tv_sec = now.tv_sec;
ts.tv_nsec = now.tv_usec * 1000;
return ts;
}
}
w_string Mercurial::mergeBaseWith(w_string_piece commitId, w_string requestId)
const {
auto mtime = getDirStateMtime();
auto key =
folly::to<std::string>(commitId, ":", mtime.tv_sec, ":", mtime.tv_nsec);
auto commit = folly::to<std::string>(commitId);
return mergeBases_
.get(
key,
[this, commit, requestId](const std::string&) {
auto revset = to<std::string>("ancestor(.,", commit, ")");
auto result = runMercurial(
{hgExecutablePath(), "log", "-T", "{node}", "-r", revset},
makeHgOptions(requestId),
"query for the merge base");
if (result.output.size() != 40) {
throw SCMError(
"expected merge base to be a 40 character string, got ",
result.output);
}
return folly::makeFuture(result.output);
})
.get()
->value();
}
std::vector<w_string> Mercurial::getFilesChangedSinceMergeBaseWith(
w_string_piece commitId,
w_string requestId) const {
auto mtime = getDirStateMtime();
auto key =
folly::to<std::string>(commitId, ":", mtime.tv_sec, ":", mtime.tv_nsec);
auto commitCopy = folly::to<std::string>(commitId);
return filesChangedSinceMergeBaseWith_
.get(
key,
[this, commit = std::move(commitCopy), requestId](
const std::string&) {
auto result = runMercurial(
{hgExecutablePath(),
"--traceback",
"status",
"-n",
"--rev",
commit,
// The "" argument at the end causes paths to be printed out
// relative to the cwd (set to root path above).
""},
makeHgOptions(requestId),
"query for files changed since merge base");
std::vector<w_string> lines;
w_string_piece(result.output).split(lines, '\n');
return folly::makeFuture(lines);
})
.get()
->value();
}
SCM::StatusResult Mercurial::getFilesChangedBetweenCommits(
w_string_piece commitApiece,
w_string_piece commitBpiece,
w_string requestId) const {
auto mtime = getDirStateMtime();
auto commitA = folly::to<std::string>(commitApiece);
auto commitB = folly::to<std::string>(commitBpiece);
auto key = folly::to<std::string>(
commitA, ":", commitB, ":", mtime.tv_sec, ":", mtime.tv_nsec);
return filesChangedBetweenCommits_
.get(
key,
[this, commitA, commitB, requestId](const std::string&) {
auto hgresult = runMercurial(
{hgExecutablePath(),
"--traceback",
"status",
"--print0",
"--rev",
commitA,
"--rev",
commitB,
// The "" argument at the end causes paths to be printed out
// relative to the cwd (set to root path above).
""},
makeHgOptions(requestId),
"get files changed between commits");
std::vector<w_string> lines;
w_string_piece(hgresult.output).split(lines, '\0');
SCM::StatusResult result;
log(DBG, "processing ", lines.size(), " status lines\n");
for (auto& line : lines) {
if (line.size() < 3) {
continue;
}
w_string fileName(line.data() + 2, line.size() - 2);
switch (line.data()[0]) {
case 'A':
result.addedFiles.emplace_back(std::move(fileName));
break;
case 'D':
result.removedFiles.emplace_back(std::move(fileName));
break;
default:
result.changedFiles.emplace_back(std::move(fileName));
}
}
return folly::makeFuture(result);
})
.get()
->value();
}
time_point<system_clock> Mercurial::getCommitDate(
w_string_piece commitId,
w_string requestId) const {
auto result = runMercurial(
{hgExecutablePath(),
"--traceback",
"log",
"-r",
commitId.data(),
"-T",
"{date}\n"},
makeHgOptions(requestId),
"get commit date");
return Mercurial::convertCommitDate(result.output.c_str());
}
time_point<system_clock> Mercurial::convertCommitDate(const char* commitDate) {
double date;
if (std::sscanf(commitDate, "%lf", &date) != 1) {
throw std::runtime_error(to<std::string>(
"failed to parse date value `", commitDate, "` into a double"));
}
return system_clock::from_time_t(date);
}
std::vector<w_string> Mercurial::getCommitsPriorToAndIncluding(
w_string_piece commitId,
int numCommits,
w_string requestId) const {
auto mtime = getDirStateMtime();
auto key = folly::to<std::string>(
commitId, ":", numCommits, ":", mtime.tv_sec, ":", mtime.tv_nsec);
auto commitCopy = folly::to<std::string>(commitId);
return commitsPrior_
.get(
key,
[this, commit = std::move(commitCopy), numCommits, requestId](
const std::string&) {
auto revset = to<std::string>(
"reverse(last(_firstancestors(",
commit,
"), ",
numCommits,
"))\n");
auto result = runMercurial(
{hgExecutablePath(),
"--traceback",
"log",
"-r",
revset,
"-T",
"{node}\n"},
makeHgOptions(requestId),
"get prior commits");
std::vector<w_string> lines;
w_string_piece(result.output).split(lines, '\n');
return folly::makeFuture(lines);
})
.get()
->value();
}
} // namespace watchman