Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion gix/src/clone/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ impl PrepareFetch {
mut create_opts: crate::create::Options,
open_opts: crate::open::Options,
) -> Result<Self, Error> {
create_opts.destination_must_be_empty = true;
if create_opts.destination_must_be_empty.is_none() {
create_opts.destination_must_be_empty = Some(true);
}

let mut repo = crate::ThreadSafeRepository::init_opts(path, kind, create_opts, open_opts)?.to_thread_local();
url.canonicalize(repo.options.current_dir_or_empty())
.map_err(|err| Error::CanonicalizeUrl {
Expand Down
4 changes: 2 additions & 2 deletions gix/src/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pub struct Options {
/// If true, and the kind of repository to create has a worktree, then the destination directory must be empty.
///
/// By default repos with worktree can be initialized into a non-empty repository as long as there is no `.git` directory.
pub destination_must_be_empty: bool,
pub destination_must_be_empty: Option<bool>,
/// If set, use these filesystem capabilities to populate the respective git-config fields.
/// If `None`, the directory will be probed.
pub fs_capabilities: Option<gix_fs::Capabilities>,
Expand All @@ -135,7 +135,7 @@ pub fn into(
let mut dot_git = directory.into();
let bare = matches!(kind, Kind::Bare);

if bare || destination_must_be_empty {
if bare || destination_must_be_empty.unwrap_or(false) {
let num_entries_in_dot_git = fs::read_dir(&dot_git)
.or_else(|err| {
if err.kind() == std::io::ErrorKind::NotFound {
Expand Down
49 changes: 49 additions & 0 deletions gix/tests/gix/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,36 @@ mod blocking_io {
assure_index_entries_on_disk(&index, repo.workdir().expect("non-bare"));
Ok(())
}

#[test]
fn fetch_and_checkout_into_non_empty_directory() -> crate::Result {
let tmp = gix_testtools::tempfile::TempDir::new()?;
let existing_path = tmp.path().join("existing.txt");
let existing_content = b"I was here before you";
std::fs::write(&existing_path, existing_content)?;

let mut prepare = gix::clone::PrepareFetch::new(
remote::repo("base").path(),
tmp.path(),
gix::create::Kind::WithWorktree,
gix::create::Options {
destination_must_be_empty: Some(false),
..Default::default()
},
restricted(),
)?;
let (mut checkout, _out) =
prepare.fetch_then_checkout(gix::progress::Discard, &std::sync::atomic::AtomicBool::default())?;
let (repo, _) = checkout.main_worktree(gix::progress::Discard, &std::sync::atomic::AtomicBool::default())?;

let index = repo.index()?;
assert_eq!(index.entries().len(), 1, "All entries are known as per HEAD tree");
assure_index_entries_on_disk(&index, repo.workdir().expect("non-bare"));

assert_eq!(std::fs::read(&existing_path)?, existing_content);
Ok(())
}

#[test]
fn fetch_and_checkout_specific_ref() -> crate::Result {
let tmp = gix_testtools::tempfile::TempDir::new()?;
Expand Down Expand Up @@ -800,6 +830,25 @@ fn clone_and_destination_must_be_empty() -> crate::Result {
Ok(())
}

#[test]
fn clone_with_worktree_and_destination_must_be_empty() -> crate::Result {
let tmp = gix_testtools::tempfile::TempDir::new()?;
std::fs::write(tmp.path().join("file"), b"hello")?;
match gix::clone::PrepareFetch::new(
remote::repo("base").path(),
tmp.path(),
gix::create::Kind::WithWorktree,
Default::default(),
restricted(),
) {
Ok(_) => unreachable!("this should fail as the directory isn't empty"),
Err(err) => assert!(err
.to_string()
.starts_with("Refusing to initialize the non-empty directory as ")),
}
Ok(())
}

#[test]
fn clone_bare_into_empty_directory_and_early_drop() -> crate::Result {
let tmp = gix_testtools::tempfile::TempDir::new()?;
Expand Down
2 changes: 1 addition & 1 deletion gix/tests/gix/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ mod non_bare {
tmp.path(),
gix::create::Kind::WithWorktree,
gix::create::Options {
destination_must_be_empty: true,
destination_must_be_empty: Some(true),
..Default::default()
},
gix::open::Options::isolated(),
Expand Down
Loading