Skip to content

Commit 0a3a6d5

Browse files
Muir Mandersfacebook-github-bot
authored andcommitted
configparser: sniff identity when loading config
Summary: Now configparser::hg::load will sniff the identity from either the repo_path, if provided, or else the environment. This only means that it will load the repo config from .sl instead of .hg as appropriate. I also tweaked eagerepo to sniff the identity instead of assuming ".hg" to aid in testing. Reviewed By: DurhamG Differential Revision: D39040645 fbshipit-source-id: fce46d1537028634b644bfd3dc4de684b9f88410
1 parent 3c6d244 commit 0a3a6d5

File tree

6 files changed

+43
-26
lines changed

6 files changed

+43
-26
lines changed

eden/scm/lib/configparser/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ hgtime = { version = "0.1.0", path = "../hgtime", optional = true }
2121
hostcaps = { version = "0.1.0", git = "https://github.com/facebookexperimental/rust-shed.git", branch = "main" }
2222
hostname = "0.3"
2323
http-client = { version = "0.1.0", path = "../http-client", optional = true }
24+
identity = { version = "0.1.0", path = "../identity" }
2425
indexmap = { version = "1.8.0", features = ["rayon", "serde-1"] }
2526
minibytes = { version = "0.1.0", path = "../minibytes" }
2627
pest-hgrc = { version = "0.1.0", path = "pest-hgrc" }

eden/scm/lib/configparser/src/hg.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,12 @@ impl ConfigSetHgExt for ConfigSet {
262262
) -> Result<SupersetVerification, Errors> {
263263
tracing::info!(?repo_path, "loading config");
264264

265-
let repo_path = repo_path.map(|p| p.join(".hg"));
265+
let ident = repo_path
266+
.map(|p| identity::must_sniff_dir(p).map_err(|e| Errors(vec![Error::Other(e)])))
267+
.transpose()?;
268+
let ident = ident.unwrap_or_else(identity::sniff_env);
269+
270+
let repo_path = repo_path.map(|p| p.join(ident.dot_dir()));
266271

267272
let mut errors = vec![];
268273

eden/scm/lib/eagerepo/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ edenapi_trait = { version = "0.1.0", path = "../edenapi/trait" }
1313
fail = { version = "0.4", features = ["failpoints"] }
1414
futures = { version = "0.3.13", features = ["async-await", "compat"] }
1515
http = "0.2"
16+
identity = { version = "0.1.0", path = "../identity" }
1617
metalog = { version = "0.1.0", path = "../metalog" }
1718
minibytes = { version = "0.1.0", path = "../minibytes" }
1819
nonblocking = { version = "0.1.0", path = "../nonblocking" }

eden/scm/lib/eagerepo/src/eager_repo.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,9 @@ impl EagerRepoStore {
128128
impl EagerRepo {
129129
/// Open an [`EagerRepo`] at the given directory. Create an empty repo on demand.
130130
pub fn open(dir: &Path) -> Result<Self> {
131+
let ident = identity::sniff_dir(dir)?.unwrap_or_else(identity::sniff_env);
131132
// Attempt to match directory layout of a real client repo.
132-
let hg_dir = dir.join(".hg");
133+
let hg_dir = dir.join(ident.dot_dir());
133134
let store_dir = hg_dir.join("store");
134135
let dag = Dag::open(store_dir.join("segments/v1"))?;
135136
let store = EagerRepoStore::open(&store_dir.join("hgcommits/v1"))?;

eden/scm/lib/identity/src/lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use std::io;
1111
use std::path::Path;
1212
use std::path::PathBuf;
1313

14+
use anyhow::Context;
15+
use anyhow::Error;
1416
use anyhow::Result;
1517
use once_cell::sync::Lazy;
1618
use parking_lot::RwLock;
@@ -118,7 +120,9 @@ pub fn sniff_dir(path: &Path) -> Result<Option<Identity>> {
118120
// don't infer the wrong identity. Ideally this would
119121
// be an allowlist of errors, but unstable errors like
120122
// NotADirectory are unmatchable for now.
121-
return Err(err.into());
123+
return Err::<_, Error>(err.into()).with_context(|| {
124+
format!("error sniffing {} for identity", test_path.display())
125+
});
122126
}
123127
_ => {}
124128
};
@@ -127,6 +131,11 @@ pub fn sniff_dir(path: &Path) -> Result<Option<Identity>> {
127131
Ok(None)
128132
}
129133

134+
/// Like sniff_dir, but returns an error instead of None.
135+
pub fn must_sniff_dir(path: &Path) -> Result<Identity> {
136+
sniff_dir(path)?.with_context(|| format!("repo {} missing dot dir", path.display()))
137+
}
138+
130139
/// Recursively sniff path and its ancestors for the first directory
131140
/// containing a ".hg" or ".sl" directory. The ancestor directory and
132141
/// corresponding Identity are returned, if any. Only permission

eden/scm/tests/test-identity.t

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,42 @@
77
$ newrepo
88
$ mv .hg .sl
99

10-
Command doesn't work, but we don't get "not inside a repository error":
11-
$ LOG=identity=debug hg root
12-
DEBUG identity: sniffing for repo root start=$TESTTMP/repo1
13-
DEBUG identity: sniffed repo dir id=sl path=$TESTTMP/repo1
14-
DEBUG identity: sniffed repo dir id=sl path=$TESTTMP/repo1
15-
hg: parse errors: required config not found at $TESTTMP/repo1/.hg/hgrc.dynamic
16-
17-
[255]
10+
"root" works in a .sl repo.
11+
$ hg root
12+
$TESTTMP/repo1
1813

1914
$ cd ..
2015

2116

2217
$ mkdir sapling
2318
$ cd sapling
24-
Doesn't work yet, but we create a .sl directory.
25-
$ HGIDENTITY=sl hg init 2>&1 | grep error
26-
error.RustError: required config not found at $TESTTMP/sapling/.hg/hgrc.dynamic
19+
Init can create a ".sl" repo.
20+
$ HGIDENTITY=sl hg init
2721
$ ls .hg
2822
$ ls .sl
2923
00changelog.i
24+
hgrc.dynamic
3025
reponame
3126
requires
3227
store
3328

3429
$ cd ..
3530

36-
$ newrepo clone_me
31+
$ newremoterepo clone_me
32+
$ setconfig paths.default=test:clone_me
3733
$ touch foo
38-
$ hg commit -A -m foo -q
39-
$ cd ..
40-
Doesn't work yet, but tries to create a .sl repo.
41-
$ HGIDENTITY=sl LOG=identity=debug hg clone eager:clone_me cloned
42-
DEBUG identity: sniffing for repo root start=$TESTTMP
43-
Cloning reponame-default into $TESTTMP/cloned
44-
INFO identity: sniffed identity from env identity="sl"
45-
DEBUG identity: sniffed repo dir id=sl path=$TESTTMP/cloned
46-
hg: parse errors: required config not found at $TESTTMP/cloned/.hg/hgrc.dynamic
47-
48-
[255]
34+
$ hg commit -Aq -m foo
35+
$ hg push -r . --to master --create -q
36+
37+
Clone can create a ".sl" repo.
38+
$ HGIDENTITY=sl hg clone -Uq eager:clone_me cloned
39+
$ ls cloned/.hg
40+
$ ls cloned/.sl
41+
00changelog.i
42+
dirstate
43+
hgrc
44+
hgrc.dynamic
45+
reponame
46+
requires
47+
store
48+
treestate

0 commit comments

Comments
 (0)