Skip to content

Commit

Permalink
Do not allow folders without a .git directory to be added as project (
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Oct 11, 2024
1 parent 21b4639 commit 4420f5d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
11 changes: 10 additions & 1 deletion crates/gitbutler-project/src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,16 @@ impl Controller {
bail!("can only work in main worktrees");
};
}
Ok(_repo) => {}
Ok(repo) => {
match repo.work_dir() {
None => bail!("Cannot add non-bare repositories without a workdir"),
Some(wd) => {
if !wd.join(".git").is_dir() {
bail!("A git-repository without a `.git` directory cannot currently be added");
}
}
}
}
Err(err) => {
return Err(anyhow::Error::from(err))
.context(error::Context::new("must be a Git repository"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ git clone simple with-submodule
git submodule add ../submodule
git commit -m "add submodule"
)

git clone --bare simple non-bare-without-worktree
(cd non-bare-without-worktree
git config core.bare false
)
33 changes: 26 additions & 7 deletions crates/gitbutler-project/tests/projects/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,29 @@ mod add {

mod error {
use super::*;
use std::path::PathBuf;

#[test]
#[ignore = "Needs fix in gitoxide so it doesn't assume the parent-dir is the worktree (Git also doesn't do that)"]
fn non_bare_without_worktree() {
let (controller, _tmp) = new();
let root = repo_path_at("non-bare-without-worktree");
let err = controller.add(root).unwrap_err();
assert_eq!(
err.to_string(),
"Cannot add non-bare repositories without a workdir"
);
}

#[test]
fn submodule() {
let (controller, _tmp) = new();
let root = gitbutler_testsupport::gix_testtools::scripted_fixture_read_only(
"various-repositories.sh",
)
.unwrap()
.join("with-submodule")
.join("submodule");
let root = repo_path_at("with-submodule").join("submodule");
let err = controller.add(root).unwrap_err();
assert_eq!(err.to_string(), "TBD");
assert_eq!(
err.to_string(),
"A git-repository without a `.git` directory cannot currently be added"
);
}

#[test]
Expand Down Expand Up @@ -126,6 +137,14 @@ mod add {
)
.unwrap()
}

fn repo_path_at(name: &str) -> PathBuf {
gitbutler_testsupport::gix_testtools::scripted_fixture_read_only(
"various-repositories.sh",
)
.unwrap()
.join(name)
}
}
}

Expand Down

0 comments on commit 4420f5d

Please sign in to comment.