From efafc085f3ec40de1b5b649a87d8a152b2ccb152 Mon Sep 17 00:00:00 2001 From: Joscha Date: Sat, 3 May 2025 00:41:30 +0200 Subject: [PATCH] Add repo remove command --- gdn-cli/src/commands/repo.rs | 3 +++ gdn-cli/src/commands/repo/remove.rs | 23 +++++++++++++++++++++++ gdn/src/data.rs | 2 +- gdn/src/data/v1.rs | 3 ++- 4 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 gdn-cli/src/commands/repo/remove.rs diff --git a/gdn-cli/src/commands/repo.rs b/gdn-cli/src/commands/repo.rs index fb05c5f..e8bf2fc 100644 --- a/gdn-cli/src/commands/repo.rs +++ b/gdn-cli/src/commands/repo.rs @@ -1,4 +1,5 @@ mod add; +mod remove; mod show; use clap::Parser; @@ -10,6 +11,7 @@ use crate::Environment; pub enum Command { Show(show::Command), Add(add::Command), + Remove(remove::Command), } impl Command { @@ -17,6 +19,7 @@ impl Command { match self { Self::Show(command) => command.run(env), Self::Add(command) => command.run(env), + Self::Remove(command) => command.run(env), } } } diff --git a/gdn-cli/src/commands/repo/remove.rs b/gdn-cli/src/commands/repo/remove.rs new file mode 100644 index 0000000..1c76425 --- /dev/null +++ b/gdn-cli/src/commands/repo/remove.rs @@ -0,0 +1,23 @@ +use clap::Parser; + +use crate::Environment; + +/// Remove an existing repository. +#[derive(Debug, Parser)] +pub struct Command { + repo: String, +} + +impl Command { + pub fn run(self, env: &Environment) -> anyhow::Result<()> { + let data = gdn::data::open_and_migrate(env.data_dir.clone())?; + let state = gdn::data::load_state(&data)?; + let Some(id) = state.resolve_repo_identifier(&self.repo) else { + println!("No repo found for identifier {}.", self.repo); + return Ok(()); + }; + gdn::data::remove_repo(&data, id)?; + println!("Removed repo {} ({id}).", self.repo); + Ok(()) + } +} diff --git a/gdn/src/data.rs b/gdn/src/data.rs index 296204f..7ea2714 100644 --- a/gdn/src/data.rs +++ b/gdn/src/data.rs @@ -12,7 +12,7 @@ pub use crate::repo::VERSION as REPO_VERSION; pub use self::{ datadir::{LockedDataDir, UnlockedDataDir}, - v1::{State, VERSION, add_repo, load_repo, load_repo_version, load_state, tidy}, + v1::{State, VERSION, add_repo, load_repo, load_repo_version, load_state, remove_repo, tidy}, }; fn migrate(dir: &LockedDataDir) -> anyhow::Result<()> { diff --git a/gdn/src/data/v1.rs b/gdn/src/data/v1.rs index 402205f..7f80253 100644 --- a/gdn/src/data/v1.rs +++ b/gdn/src/data/v1.rs @@ -91,7 +91,8 @@ pub fn remove_repo(dir: &LockedDataDir, id: RepoId) -> anyhow::Result<()> { state.repos.remove(&id); save_state(dir, state)?; - // TODO Check if this works with read-only files + // This seems to work even with read-only files, so it should work fine for + // bare git repos. I don't expect to encounter read-only directories. fs::remove_dir_all(repo_dir(dir, id))?; Ok(())