diff --git a/crates/gitbutler-project/src/controller.rs b/crates/gitbutler-project/src/controller.rs index 3a0350192..755d7f63a 100644 --- a/crates/gitbutler-project/src/controller.rs +++ b/crates/gitbutler-project/src/controller.rs @@ -72,7 +72,7 @@ impl Controller { let project = Project { id: ProjectId::generate(), title, - path: path.to_path_buf(), + path: gix::path::realpath(path)?, api: None, ..Default::default() }; diff --git a/crates/gitbutler-repo/src/commands.rs b/crates/gitbutler-repo/src/commands.rs index a1d613448..25741ef75 100644 --- a/crates/gitbutler-repo/src/commands.rs +++ b/crates/gitbutler-repo/src/commands.rs @@ -49,25 +49,24 @@ impl RepoCommands for Project { fn read_file_from_workspace(&self, relative_path: &Path) -> Result { let ctx = CommandContext::open(self)?; - if self - .path - .join(relative_path) - .canonicalize()? - .as_path() - .starts_with(self.path.clone()) - { - let tree = ctx.repository().head()?.peel_to_tree()?; - let entry = tree.get_path(relative_path)?; - let blob = ctx.repository().find_blob(entry.id())?; + let path_in_worktree = gix::path::realpath(self.path.join(relative_path))?; + if !path_in_worktree.starts_with(self.path.clone()) { + anyhow::bail!( + "Path to read from at '{}' isn't in the worktree directory '{}'", + relative_path.display(), + self.path.display() + ); + } - if !blob.is_binary() { - let content = std::str::from_utf8(blob.content())?; - Ok(content.to_string()) - } else { - anyhow::bail!("File is binary"); - } + let tree = ctx.repository().head()?.peel_to_tree()?; + let entry = tree.get_path(relative_path)?; + let blob = ctx.repository().find_blob(entry.id())?; + + if !blob.is_binary() { + let content = std::str::from_utf8(blob.content())?; + Ok(content.to_string()) } else { - anyhow::bail!("Invalid workspace file"); + anyhow::bail!("File is binary"); } } } diff --git a/crates/gitbutler-testsupport/src/testing_repository.rs b/crates/gitbutler-testsupport/src/testing_repository.rs index b7cadbb6f..5a9555718 100644 --- a/crates/gitbutler-testsupport/src/testing_repository.rs +++ b/crates/gitbutler-testsupport/src/testing_repository.rs @@ -1,5 +1,6 @@ use std::fs; +use crate::init_opts; use gitbutler_oxidize::git2_to_gix_object_id; use gix_testtools::bstr::ByteSlice as _; use tempfile::{tempdir, TempDir}; @@ -13,7 +14,7 @@ pub struct TestingRepository { impl TestingRepository { pub fn open() -> Self { let tempdir = tempdir().unwrap(); - let repository = git2::Repository::init(tempdir.path()).unwrap(); + let repository = git2::Repository::init_opts(tempdir.path(), &init_opts()).unwrap(); let config = repository.config().unwrap(); match config.open_level(git2::ConfigLevel::Local) {