Do not allow folders without a .git directory to be added as project (#5099)

This commit is contained in:
Sebastian Thiel 2024-10-11 07:26:48 +02:00
parent 21b463982c
commit 4420f5d6be
No known key found for this signature in database
GPG Key ID: 9CB5EE7895E8268B
3 changed files with 41 additions and 8 deletions

View File

@ -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"));

View File

@ -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
)

View File

@ -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]
@ -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)
}
}
}