Skip to content

Commit cb8fa0b

Browse files
committed
determine filesystem case (#301)
1 parent 43272f3 commit cb8fa0b

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

git-worktree/src/fs.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,37 @@ pub struct Context {
2222
}
2323

2424
impl Context {
25-
/// try to determine all values in this context by probing them in the given `directory`, which
25+
/// try to determine all values in this context by probing them in the given `git_dir`, which
2626
/// should be on the file system the git repository is located on.
27+
/// `git_dir` is a typical git repository, expected to be populated with the typical files like `config`.
2728
///
2829
/// All errors are ignored and interpreted on top of the default for the platform the binary is compiled for.
29-
pub fn probe(directory: impl AsRef<std::path::Path>) -> Self {
30-
let root = directory.as_ref();
30+
pub fn probe(git_dir: impl AsRef<std::path::Path>) -> Self {
31+
let root = git_dir.as_ref();
3132
let ctx = Context::default();
3233
Context {
3334
symlink: Self::probe_symlink(root).unwrap_or(ctx.symlink),
35+
ignore_case: Self::probe_ignore_case(root).unwrap_or(ctx.ignore_case),
3436
..ctx
3537
}
3638
}
3739

40+
fn probe_ignore_case(git_dir: &Path) -> std::io::Result<bool> {
41+
std::fs::metadata(git_dir.join("cOnFiG")).map(|_| true).or_else(|err| {
42+
if err.kind() == std::io::ErrorKind::NotFound {
43+
Ok(false)
44+
} else {
45+
Err(err)
46+
}
47+
})
48+
}
49+
3850
fn probe_symlink(root: &Path) -> std::io::Result<bool> {
3951
let src_path = root.join("__link_src_file");
40-
std::fs::File::options().create_new(true).write(true).open(&src_path)?;
52+
std::fs::OpenOptions::new()
53+
.create_new(true)
54+
.write(true)
55+
.open(&src_path)?;
4156
let link_path = root.join("__file_link");
4257
if symlink::symlink_file(&src_path, &link_path).is_err() {
4358
std::fs::remove_file(&src_path)?;

git-worktree/tests/fs/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
#[test]
22
fn from_probing_cwd() {
33
let dir = tempfile::tempdir().unwrap();
4-
let _ctx = git_worktree::fs::Context::probe(dir.path());
4+
std::fs::File::create(dir.path().join("config")).unwrap();
5+
let ctx = git_worktree::fs::Context::probe(dir.path());
6+
dbg!(ctx);
57
let entries: Vec<_> = std::fs::read_dir(dir.path())
68
.unwrap()
79
.filter_map(Result::ok)
10+
.filter(|e| e.file_name().to_str() != Some("config"))
811
.map(|e| e.path().to_owned())
912
.collect();
1013
assert_eq!(

git-worktree/tests/index/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mod checkout {
1717
#[test]
1818
fn allow_symlinks() -> crate::Result {
1919
let opts = opts_with_symlink(true);
20-
if !git_worktree::fs::Context::probe(std::env::current_dir()?).symlink {
20+
if !git_worktree::fs::Context::probe(std::env::current_dir()?.join("..").join(".git")).symlink {
2121
eprintln!("IGNORING symlink test on file system without symlink support");
2222
// skip if symlinks aren't supported anyway.
2323
return Ok(());

0 commit comments

Comments
 (0)