Skip to content

Commit

Permalink
Pass request_id to hg blackbox logger
Browse files Browse the repository at this point in the history
Summary:
Pass request_id from the watchman query to HG using HGREQUESTID added by
D8057148.

Reviewed By: wez

Differential Revision: D8086720

fbshipit-source-id: 186dc311cf93e2866e9b7367fdfda27d8728eafb
  • Loading branch information
quark-zju authored and facebook-github-bot committed May 22, 2018
1 parent b76f499 commit 9ab8365
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 4 deletions.
7 changes: 4 additions & 3 deletions query/eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,8 @@ w_query_res w_query_execute(
// determine if SCM operations ocurred concurrent with query execution.
res.stateTransCountAtStartOfQuery = root->stateTransCount.load();
resultClock.scmMergeBaseWith = query->since_spec->scmMergeBaseWith;
resultClock.scmMergeBase = scm->mergeBaseWith(resultClock.scmMergeBaseWith);
resultClock.scmMergeBase =
scm->mergeBaseWith(resultClock.scmMergeBaseWith, requestId);

if (resultClock.scmMergeBase != query->since_spec->scmMergeBase) {
// The merge base is different, so on the assumption that a lot of
Expand All @@ -286,13 +287,13 @@ w_query_res w_query_execute(

auto scmMergeBase = resultClock.scmMergeBase;
disableFreshInstance = true;
generator = [root, scmMergeBase](
generator = [root, scmMergeBase, requestId](
w_query* q,
const std::shared_ptr<w_root_t>& r,
struct w_query_ctx* c) {
auto changedFiles =
root->view()->getSCM()->getFilesChangedSinceMergeBaseWith(
scmMergeBase);
scmMergeBase, requestId);

auto pathList = json_array_of_size(changedFiles.size());
for (auto& f : changedFiles) {
Expand Down
5 changes: 4 additions & 1 deletion scm/Mercurial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ std::string hgExecutablePath() {
return "hg";
}

ChildProcess::Options Mercurial::makeHgOptions(w_string /* requestId */) const {
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.
Expand All @@ -35,6 +35,9 @@ ChildProcess::Options Mercurial::makeHgOptions(w_string /* requestId */) const {
// 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);
}
opt.nullStdin();
opt.pipeStdout();
opt.pipeStderr();
Expand Down
75 changes: 75 additions & 0 deletions tests/integration/test_request_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
# no unicode literals
from __future__ import absolute_import, division, print_function

import os
import re
import subprocess

import WatchmanTestCase

Expand All @@ -28,3 +30,76 @@ def test_queryRequestId(self):
lambda: any(pat.match(l) for l in self.getServerLogContents()),
message="request_id logged",
)

def skipIfNoHgRequestIdSupport(self):
root = self.mkdtemp()
request_id = "bf8a47014bd1b66103a8ab0aece4be7ada871660"

env = os.environ.copy()
env["HGPLAIN"] = "1"
env["HGREQUESTID"] = request_id

subprocess.call(["hg", "init"], env=env, cwd=root)
subprocess.call(["hg", "log"], env=env, cwd=root)

try:
with open(os.path.join(root, ".hg/blackbox.log")) as f:
if request_id in f.read():
return
except IOError:
pass

self.skipTest("HGREQUESTID is not supported")

def test_scmHgRequestId(self):
self.skipIfNoHgRequestIdSupport()

root = self.mkdtemp()

# In this test, the repo does not necessarily need fsmonitor enabled,
# since watchman calls HGREQUESTID=... hg status and that would also
# have request_id logged without fsmonitor.
env = os.environ.copy()
env["HGPLAIN"] = "1"
subprocess.call(["hg", "init"], env=env, cwd=root)
subprocess.call(
[
"hg",
"commit",
"-mempty",
"-utest",
"-d0 0",
"--config=ui.allowemptycommit=1",
],
env=env,
cwd=root,
)
commit_hash = subprocess.check_output(
["hg", "log", "-r.", "-T{node}"], env=env, cwd=root
).decode("utf-8")

# Must watch the directory after it's an HG repo to perform scm-aware
# queries.
self.watchmanCommand("watch", root)
request_id = "4c05a798ea1acc7c97b75e61fec5f640d90f8209"

params = {
"fields": ["name"],
"request_id": request_id,
"since": {"scm": {"mergebase-with": commit_hash}},
}
self.watchmanCommand("query", root, params)

blackbox_path = os.path.join(root, ".hg", "blackbox.log")

def try_read_blackbox():
try:
with open(blackbox_path) as f:
return f.read()
except IOError:
return ""

self.assertWaitFor(
lambda: request_id in try_read_blackbox(),
message="request_id passed to and logged by hg",
)

0 comments on commit 9ab8365

Please sign in to comment.