From e528c40a07f1a7d1ecfd4808b5a7ef8e5fb8bb0c Mon Sep 17 00:00:00 2001 From: Lorenz Leutgeb Date: Fri, 12 Sep 2025 10:25:07 +0200 Subject: [PATCH] radicle-cli: Test initializing from bare repos --- .../examples/git/git-is-bare-repository.md | 4 ++ .../examples/rad-init-existing-bare.md | 40 +++++++++++++++++++ crates/radicle-cli/tests/commands.rs | 31 ++++++++++++++ crates/radicle/src/test/fixtures.rs | 22 ++++++++-- 4 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 crates/radicle-cli/examples/git/git-is-bare-repository.md create mode 100644 crates/radicle-cli/examples/rad-init-existing-bare.md diff --git a/crates/radicle-cli/examples/git/git-is-bare-repository.md b/crates/radicle-cli/examples/git/git-is-bare-repository.md new file mode 100644 index 00000000..e0b38721 --- /dev/null +++ b/crates/radicle-cli/examples/git/git-is-bare-repository.md @@ -0,0 +1,4 @@ +``` +$ git rev-parse --is-bare-repository +true +``` \ No newline at end of file diff --git a/crates/radicle-cli/examples/rad-init-existing-bare.md b/crates/radicle-cli/examples/rad-init-existing-bare.md new file mode 100644 index 00000000..cfbefd4f --- /dev/null +++ b/crates/radicle-cli/examples/rad-init-existing-bare.md @@ -0,0 +1,40 @@ +Let's clone a regular repository via plain Git: +``` +$ git clone --bare $URL heartwood +$ cd heartwood +$ git rev-parse HEAD +f2de534b5e81d7c6e2dcaf58c3dd91573c0a0354 +``` + +We can see it's not a Radicle working copy: +``` (fail) +$ rad . +✗ Error: Current directory is not a Radicle repository +``` + +Let's pick an existing repository: +``` +$ rad inspect rad:z42hL2jL4XNk6K8oHQaSWfMgCL7ji +rad:z42hL2jL4XNk6K8oHQaSWfMgCL7ji +``` + +And initialize this working copy as that existing repository: +``` +$ rad init --existing rad:z42hL2jL4XNk6K8oHQaSWfMgCL7ji +✓ Initialized existing repository rad:z42hL2jL4XNk6K8oHQaSWfMgCL7ji in [..]/heartwood/.. +``` + +We can confirm that the working copy is initialized: +``` +$ rad . +rad:z42hL2jL4XNk6K8oHQaSWfMgCL7ji +$ git remote show rad +* remote rad + Fetch URL: rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji + Push URL: rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi + HEAD branch: (unknown) + Remote branch: + master new (next fetch will store in remotes/rad) + Local ref configured for 'git push': + master pushes to master (up to date) +``` diff --git a/crates/radicle-cli/tests/commands.rs b/crates/radicle-cli/tests/commands.rs index 4529e615..c9ae4ef8 100644 --- a/crates/radicle-cli/tests/commands.rs +++ b/crates/radicle-cli/tests/commands.rs @@ -176,6 +176,15 @@ fn rad_init() { Environment::alice(["rad-init"]); } +#[test] +fn rad_init_bare() { + let mut env = Environment::new(); + let alice = env.profile("alice"); + radicle::test::fixtures::bare_repository(env.work(&alice).as_path()); + env.tests(["git/git-is-bare-repository", "rad-init"], &alice) + .unwrap(); +} + #[test] fn rad_init_existing() { let mut environment = Environment::new(); @@ -198,6 +207,28 @@ fn rad_init_existing() { .unwrap(); } +#[test] +fn rad_init_existing_bare() { + let mut environment = Environment::new(); + let mut profile = environment.node("alice"); + let working = tempfile::tempdir().unwrap(); + let rid = profile.project("heartwood", "Radicle Heartwood Protocol & Stack"); + + test( + "examples/rad-init-existing-bare.md", + working.path(), + Some(&profile.home), + [( + "URL", + git::url::File::new(profile.storage.path()) + .rid(rid) + .to_string() + .as_str(), + )], + ) + .unwrap(); +} + #[test] fn rad_init_no_seed() { Environment::alice(["rad-init-no-seed"]); diff --git a/crates/radicle/src/test/fixtures.rs b/crates/radicle/src/test/fixtures.rs index e6c82de2..67da9412 100644 --- a/crates/radicle/src/test/fixtures.rs +++ b/crates/radicle/src/test/fixtures.rs @@ -95,11 +95,28 @@ where /// Creates a regular repository at the given path with a couple of commits. pub fn repository>(path: P) -> (git2::Repository, git2::Oid) { - let repo = git2::Repository::init_opts( + let (repo, oid) = repository_with( path, git2::RepositoryInitOptions::new().external_template(false), + ); + repo.checkout_head(None).unwrap(); + (repo, oid) +} + +pub fn bare_repository>(path: P) -> (git2::Repository, git2::Oid) { + repository_with( + path, + git2::RepositoryInitOptions::new() + .external_template(false) + .bare(true), ) - .unwrap(); +} + +fn repository_with>( + path: P, + opts: &mut git2::RepositoryInitOptions, +) -> (git2::Repository, git2::Oid) { + let repo = git2::Repository::init_opts(path, opts).unwrap(); { let mut config = repo.config().unwrap(); @@ -124,7 +141,6 @@ pub fn repository>(path: P) -> (git2::Repository, git2::Oid) { commit.id() }; repo.set_head("refs/heads/master").unwrap(); - repo.checkout_head(None).unwrap(); drop(tree); drop(head);