Skip to content

Commit 8a36768

Browse files
andreacampifacebook-github-bot
authored andcommitted
Postpone the initialization of the CAS client in hg until we know we need it
Summary: Most hg commands don't need CAS, initializing it is wasteful and also leads to more noise on stderr, which in turn is bad for integration tests. Simply postponing the initialization reduces the surface to a more manageable one. Reviewed By: chiara125 Differential Revision: D75939889 fbshipit-source-id: 5112c6db3974c471a278820f4875db79817b856b
1 parent c7556f2 commit 8a36768

43 files changed

Lines changed: 224 additions & 26 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

eden/scm/BUCK

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# fix the mercurial code to use fbcode-relative includes.
44
#
55

6-
load("@fbcode//eden/scm:targets.bzl", "fetch_as_eden", "gen_hgpython", "hg_binary")
6+
load("@fbcode//eden/scm:targets.bzl", "gen_hgpython", "hg_binary")
77
load("@fbcode_macros//build_defs:cpp_binary.bzl", "cpp_binary")
88
load("@fbcode_macros//build_defs:cpp_library.bzl", "cpp_library")
99
load("@fbcode_macros//build_defs:export_files.bzl", "export_file")
@@ -182,10 +182,6 @@ hg_binary(
182182
"DEFAULT": None,
183183
"ovr_config//build_mode/constraints:dev": PYTHON_EXTREME_LINK_GROUP_THRESHOLD,
184184
}),
185-
named_deps = select({
186-
"DEFAULT": {},
187-
"ovr_config//os:linux": {} if rust_oss.is_oss_build() else ({"cas_client": "//eden/scm/lib/cas-client/rich-client:rich-cas-client"} if fetch_as_eden() else {"cas_client": "//eden/scm/lib/cas-client/thin-client:thin-cas-client"}),
188-
}),
189185
)
190186

191187
hg_binary(

eden/scm/exec/hgmain/src/main.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,6 @@ fn main() {
110110
#[cfg(windows)]
111111
windows::enable_vt_processing().unwrap();
112112

113-
#[cfg(feature = "cas")]
114-
cas_client::init();
115-
116113
let mut io = clidispatch::io::IO::stdio();
117114

118115
let _ = io.setup_term();
@@ -176,12 +173,7 @@ pub fn drop_root(user: &str, group: &str) {
176173
let home_dir = unsafe { (*libc_user).pw_dir };
177174
if !home_dir.is_null() {
178175
// TODO: Audit that the environment access only happens in single-threaded code.
179-
unsafe {
180-
std::env::set_var(
181-
"HOME",
182-
unsafe { CStr::from_ptr(home_dir) }.to_str().unwrap(),
183-
)
184-
};
176+
unsafe { std::env::set_var("HOME", CStr::from_ptr(home_dir).to_str().unwrap()) };
185177
}
186178
// TODO: Audit that the environment access only happens in single-threaded code.
187179
unsafe { std::env::set_var("USER", user) };

eden/scm/lib/clidispatch/BUCK

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
load("@fbcode//eden/scm:targets.bzl", "fetch_as_eden")
12
load("@fbcode_macros//build_defs:rust_library.bzl", "rust_library")
3+
load("@fbcode_macros//build_defs/lib:rust_oss.bzl", "rust_oss")
24

35
oncall("sapling")
46

@@ -29,7 +31,14 @@ rust_library(
2931
}},
3032
features = [
3133
"eden",
32-
],
34+
] + select({
35+
"DEFAULT": [],
36+
"ovr_config//os:linux": [] if rust_oss.is_oss_build() else ["cas"],
37+
}),
38+
named_deps = select({
39+
"DEFAULT": {},
40+
"ovr_config//os:linux": {} if rust_oss.is_oss_build() else ({"cas_client": "//eden/scm/lib/cas-client/rich-client:rich-cas-client"} if fetch_as_eden() else {"cas_client": "//eden/scm/lib/cas-client/thin-client:thin-cas-client"}),
41+
}),
3342
deps = [
3443
"fbsource//third-party/rust:anyhow",
3544
"fbsource//third-party/rust:erased-serde",

eden/scm/lib/clidispatch/src/command.rs

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub struct CommandDefinition {
3434
flags_func: fn() -> Vec<Flag>,
3535
func: CommandFunc,
3636
synopsis: Option<String>,
37+
enable_cas: bool,
3738
}
3839

3940
impl CommandDefinition {
@@ -43,13 +44,15 @@ impl CommandDefinition {
4344
flags_func: fn() -> Vec<Flag>,
4445
func: CommandFunc,
4546
synopsis: Option<impl ToString>,
47+
enable_cas: bool,
4648
) -> Self {
4749
CommandDefinition {
4850
aliases: aliases.to_string(),
4951
doc: doc.to_string(),
5052
flags_func,
5153
func,
5254
synopsis: synopsis.map(|s| s.to_string()),
55+
enable_cas,
5356
}
5457
}
5558

@@ -78,6 +81,10 @@ impl CommandDefinition {
7881
.split('|')
7982
.find_map(|a| a.strip_prefix("legacy:"))
8083
}
84+
85+
pub fn enable_cas(&self) -> bool {
86+
self.enable_cas
87+
}
8188
}
8289

8390
#[derive(Default)]
@@ -129,7 +136,14 @@ impl Deref for CommandTable {
129136
}
130137

131138
pub trait Register<FN, T> {
132-
fn register(&mut self, f: FN, aliases: &str, doc: &str, synopsis: Option<&str>);
139+
fn register(
140+
&mut self,
141+
f: FN,
142+
aliases: &str,
143+
doc: &str,
144+
synopsis: Option<&str>,
145+
enable_cas: bool,
146+
);
133147
}
134148

135149
// OptionalRepo commands.
@@ -138,7 +152,14 @@ where
138152
S: TryFrom<ParseOutput, Error = anyhow::Error> + StructFlags,
139153
FN: Fn(ReqCtx<S>, Option<&Repo>) -> Result<u8> + 'static,
140154
{
141-
fn register(&mut self, f: FN, aliases: &str, doc: &str, synopsis: Option<&str>) {
155+
fn register(
156+
&mut self,
157+
f: FN,
158+
aliases: &str,
159+
doc: &str,
160+
synopsis: Option<&str>,
161+
enable_cas: bool,
162+
) {
142163
self.insert_aliases(aliases);
143164
let func = move |opts: ParseOutput, io: &IO, repo: &OptionalRepo| {
144165
f(
@@ -147,7 +168,7 @@ where
147168
)
148169
};
149170
let func = CommandFunc::OptionalRepo(Box::new(func));
150-
let def = CommandDefinition::new(aliases, doc, S::flags, func, synopsis);
171+
let def = CommandDefinition::new(aliases, doc, S::flags, func, synopsis, enable_cas);
151172
self.commands.insert(aliases.to_string(), def);
152173
}
153174
}
@@ -158,13 +179,20 @@ where
158179
S: TryFrom<ParseOutput, Error = anyhow::Error> + StructFlags,
159180
FN: Fn(ReqCtx<S>, &Repo) -> Result<u8> + 'static,
160181
{
161-
fn register(&mut self, f: FN, aliases: &str, doc: &str, synopsis: Option<&str>) {
182+
fn register(
183+
&mut self,
184+
f: FN,
185+
aliases: &str,
186+
doc: &str,
187+
synopsis: Option<&str>,
188+
enable_cas: bool,
189+
) {
162190
self.insert_aliases(aliases);
163191
let func = move |opts: ParseOutput, io: &IO, repo: &Repo| {
164192
f(ReqCtx::new(repo.config().clone(), opts, io.clone())?, repo)
165193
};
166194
let func = CommandFunc::Repo(Box::new(func));
167-
let def = CommandDefinition::new(aliases, doc, S::flags, func, synopsis);
195+
let def = CommandDefinition::new(aliases, doc, S::flags, func, synopsis, enable_cas);
168196
self.commands.insert(aliases.to_string(), def);
169197
}
170198
}
@@ -175,13 +203,20 @@ where
175203
S: TryFrom<ParseOutput, Error = anyhow::Error> + StructFlags,
176204
FN: Fn(ReqCtx<S>) -> Result<u8> + 'static,
177205
{
178-
fn register(&mut self, f: FN, aliases: &str, doc: &str, synopsis: Option<&str>) {
206+
fn register(
207+
&mut self,
208+
f: FN,
209+
aliases: &str,
210+
doc: &str,
211+
synopsis: Option<&str>,
212+
enable_cas: bool,
213+
) {
179214
self.insert_aliases(aliases);
180215
let func = move |opts: ParseOutput, io: &IO, config: &Arc<dyn Config>| {
181216
f(ReqCtx::new(config.clone(), opts, io.clone())?)
182217
};
183218
let func = CommandFunc::NoRepo(Box::new(func));
184-
let def = CommandDefinition::new(aliases, doc, S::flags, func, synopsis);
219+
let def = CommandDefinition::new(aliases, doc, S::flags, func, synopsis, enable_cas);
185220
self.commands.insert(aliases.to_string(), def);
186221
}
187222
}
@@ -192,7 +227,14 @@ where
192227
S: TryFrom<ParseOutput, Error = anyhow::Error> + StructFlags,
193228
FN: Fn(ReqCtx<S>, &Repo, &WorkingCopy) -> Result<u8> + 'static,
194229
{
195-
fn register(&mut self, f: FN, aliases: &str, doc: &str, synopsis: Option<&str>) {
230+
fn register(
231+
&mut self,
232+
f: FN,
233+
aliases: &str,
234+
doc: &str,
235+
synopsis: Option<&str>,
236+
enable_cas: bool,
237+
) {
196238
self.insert_aliases(aliases);
197239
let func = move |opts: ParseOutput, io: &IO, repo: &Repo, working_copy: &WorkingCopy| {
198240
f(
@@ -202,7 +244,7 @@ where
202244
)
203245
};
204246
let func = CommandFunc::WorkingCopy(Box::new(func));
205-
let def = CommandDefinition::new(aliases, doc, S::flags, func, synopsis);
247+
let def = CommandDefinition::new(aliases, doc, S::flags, func, synopsis, enable_cas);
206248
self.commands.insert(aliases.to_string(), def);
207249
}
208250
}

eden/scm/lib/clidispatch/src/dispatch.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,11 @@ impl Dispatcher {
450450
self.convert_to_repoless_config()?;
451451
}
452452

453+
#[cfg(feature = "cas")]
454+
if handler.enable_cas() {
455+
cas_client::init();
456+
}
457+
453458
hooks.run_pre(self.repo(), &self.args[1..])?;
454459

455460
let res = match handler.func() {

eden/scm/lib/commands/BUCK

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
load("@fbcode//eden/scm:targets.bzl", "fetch_as_eden")
12
load("@fbcode_macros//build_defs:rust_library.bzl", "rust_library")
3+
load("@fbcode_macros//build_defs/lib:rust_oss.bzl", "rust_oss")
24

35
oncall("sapling")
46

@@ -51,7 +53,14 @@ rust_library(
5153
features = [
5254
"eden",
5355
"fb",
54-
],
56+
] + select({
57+
"DEFAULT": [],
58+
"ovr_config//os:linux": [] if rust_oss.is_oss_build() else ["cas"],
59+
}),
60+
named_deps = select({
61+
"DEFAULT": {},
62+
"ovr_config//os:linux": {} if rust_oss.is_oss_build() else ({"cas_client": "//eden/scm/lib/cas-client/rich-client:rich-cas-client"} if fetch_as_eden() else {"cas_client": "//eden/scm/lib/cas-client/thin-client:thin-cas-client"}),
63+
}),
5564
deps = [
5665
"fbsource//third-party/rust:anyhow",
5766
"fbsource//third-party/rust:ctrlc",

eden/scm/lib/commands/commands/cmdclone/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,3 +789,7 @@ pub fn doc() -> &'static str {
789789
pub fn synopsis() -> Option<&'static str> {
790790
Some("[OPTION]... SOURCE [DEST]")
791791
}
792+
793+
pub fn enable_cas() -> bool {
794+
true
795+
}

eden/scm/lib/commands/commands/cmdconfig/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,3 +339,7 @@ pub fn synopsis() -> Option<&'static str> {
339339
fn short_name() -> &'static str {
340340
"config"
341341
}
342+
343+
pub fn enable_cas() -> bool {
344+
false
345+
}

eden/scm/lib/commands/commands/cmdconfigfile/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,7 @@ pub fn doc() -> &'static str {
8787
pub fn synopsis() -> Option<&'static str> {
8888
None
8989
}
90+
91+
pub fn enable_cas() -> bool {
92+
false
93+
}

eden/scm/lib/commands/commands/cmdgoto/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,3 +294,7 @@ Returns 0 on success, 1 if there are unresolved files."#
294294
pub fn synopsis() -> Option<&'static str> {
295295
Some("[OPTION]... [REV]")
296296
}
297+
298+
pub fn enable_cas() -> bool {
299+
true
300+
}

0 commit comments

Comments
 (0)