From 755e28d044aaff9a8aa0fa4700105564726ec33d Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Fri, 14 Jul 2023 13:13:11 +0200 Subject: Add a housekeeping task, which does git gc --- src/git.rs | 14 ++++++++++++++ src/lib.rs | 19 +++++++++++++++++++ src/parser.rs | 9 +++++++++ 3 files changed, 42 insertions(+) (limited to 'src') diff --git a/src/git.rs b/src/git.rs index d92b5a0..76ff326 100644 --- a/src/git.rs +++ b/src/git.rs @@ -234,6 +234,20 @@ pub fn set_branch(directory: &Path, branch: &str) -> Result<(), ShackleError> { } } +pub fn housekeeping(directory: &Path) -> Result<(), ShackleError> { + if !is_valid_git_repo_path(directory)? { + return Err(ShackleError::InvalidDirectory); + } + + Command::new("git") + .arg("gc") + .current_dir(directory) + .spawn()? + .wait()?; + + Ok(()) +} + pub fn delete(directory: &Path) -> Result<(), ShackleError> { if !is_valid_git_repo_path(directory)? { return Err(ShackleError::InvalidDirectory); diff --git a/src/lib.rs b/src/lib.rs index 809c62d..163e41b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -63,6 +63,25 @@ pub fn run_command(user_input: &str) -> Result, ShackleError git::delete(&directory)?; println!("Successfully deleted \"{}\"", directory.display()); } + Ok(ShackleCommand::Housekeeping(HousekeepingArgs { directory })) => match directory { + Some(directory) => { + git::housekeeping(&directory)?; + println!( + "Successfully did housekeeping on \"{}\"", + directory.display() + ); + } + None => { + let list = git::list()?; + for repo in list { + git::housekeeping(&repo.path)?; + println!( + "Successfully did housekeeping on \"{}\"", + repo.path.display() + ); + } + } + }, Ok(ShackleCommand::GitUploadPack(upload_pack_args)) => { git::upload_pack(&upload_pack_args)?; } diff --git a/src/parser.rs b/src/parser.rs index b429572..5806cec 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -18,6 +18,8 @@ pub enum ShackleCommand { SetBranch(SetBranchArgs), /// Deletes a repository Delete(DeleteArgs), + /// Does any housekeeping, like deleting unreachable objects and repacking more efficiently + Housekeeping(HousekeepingArgs), /// Quit the shell Exit, /// Server side command required to git fetch from the server @@ -73,6 +75,13 @@ pub struct DeleteArgs { pub directory: PathBuf, } +#[derive(Parser, Clone, Debug, PartialEq, Eq)] +pub struct HousekeepingArgs { + /// The full relative path of the repository, for example + /// git/shuckie/repo.git. If omitted, all repos will be checked. + pub directory: Option, +} + #[derive(Parser, Clone, Debug, PartialEq, Eq)] pub struct GitUploadPackArgs { /// Do not try /.git/ if is no Git directory -- cgit v1.2.3