Skip to content

Commit

Permalink
symlink probing (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Feb 28, 2022
1 parent 3556af4 commit a3714ef
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions git-worktree/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ quick-error = "2.0.1"
bstr = { version = "0.2.13", default-features = false }

document-features = { version = "0.2.0", optional = true }
symlink = "0.1.0"

[dev-dependencies]
git-testtools = { path = "../tests/tools" }
Expand Down
43 changes: 43 additions & 0 deletions git-worktree/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,49 @@ pub struct Context {
pub symlink: bool,
}

impl Context {
/// try to determine all values in this context by probing them in the current working directory, which should be on the file system
/// the git repository is located on.
///
/// All errors are ignored and interpreted on top of the default for the platform the binary is compiled for.
pub fn from_probing_cwd() -> Self {
let ctx = Context::default();
Context {
symlink: Self::probe_symlink().unwrap_or(ctx.symlink),
..ctx
}
}

fn probe_symlink() -> Option<bool> {
let src_path = "__link_src_file";
std::fs::File::options()
.create_new(true)
.write(true)
.open(src_path)
.ok()?;
let link_path = "__file_link";
if symlink::symlink_file(src_path, link_path).is_err() {
std::fs::remove_file(src_path).ok();
return None;
}
let cleanup_all = || {
std::fs::remove_file(src_path).ok();
symlink::remove_symlink_file(link_path)
.or_else(|_| std::fs::remove_file(link_path))
.ok();
};

let res = Some(
std::fs::symlink_metadata(link_path)
.map_err(|_| cleanup_all())
.ok()?
.is_symlink(),
);
cleanup_all();
res
}
}

#[cfg(windows)]
impl Default for Context {
fn default() -> Self {
Expand Down
5 changes: 5 additions & 0 deletions git-worktree/tests/index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ mod checkout {
#[test]
fn allow_symlinks() -> crate::Result {
let opts = opts_with_symlink(true);
if !git_worktree::fs::Context::from_probing_cwd().symlink {
eprintln!("IGNORING symlink test on file system without symlink support");
// skip if symlinks aren't supported anyway.
return Ok(());
};
let (source_tree, destination) = setup_fixture_with_options(opts, "make_mixed_without_submodules")?;

assert_equality(&source_tree, &destination, opts.fs.symlink)?;
Expand Down

0 comments on commit a3714ef

Please sign in to comment.