From fcbad5a3eb1b8c254728a01f153ae4b2001a06f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20K=C3=B6rber?= Date: Wed, 29 Dec 2021 18:42:57 +0100 Subject: [PATCH] Refactor worktree into own struct --- src/repo.rs | 40 ++++++++++++++++++++++++++++++---------- src/table.rs | 16 ++++++++++------ 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/src/repo.rs b/src/repo.rs index 2daba33..d66f8e7 100644 --- a/src/repo.rs +++ b/src/repo.rs @@ -166,6 +166,22 @@ pub struct RepoStatus { pub branches: Vec<(String, Option<(String, RemoteTrackingStatus)>)>, } +pub struct Worktree { + name: String, +} + +impl Worktree { + pub fn new(name: &str) -> Self { + Self { + name: name.to_string(), + } + } + + pub fn name(&self) -> &str { + &self.name + } +} + #[cfg(test)] mod tests { use super::*; @@ -746,14 +762,14 @@ impl Repo { ))) } - pub fn get_worktrees(&self) -> Result, String> { + pub fn get_worktrees(&self) -> Result, String> { Ok(self .0 .worktrees() .map_err(convert_libgit2_error)? .iter() .map(|name| name.expect("Worktree name is invalid utf-8")) - .map(|name| name.to_string()) + .map(Worktree::new) .collect()) } @@ -905,24 +921,25 @@ impl Repo { for worktree in worktrees .iter() - .filter(|worktree| *worktree != &default_branch_name) + .filter(|worktree| worktree.name() != default_branch_name) .filter(|worktree| match &config { None => true, Some(config) => match &config.persistent_branches { None => true, - Some(branches) => !branches.contains(worktree), + Some(branches) => !branches.iter().any(|branch| branch == worktree.name()), }, }) { - let repo_dir = &directory.join(&worktree); + let repo_dir = &directory.join(&worktree.name()); if repo_dir.exists() { - match self.remove_worktree(worktree, repo_dir, false, &config) { - Ok(_) => print_success(&format!("Worktree {} deleted", &worktree)), + match self.remove_worktree(worktree.name(), repo_dir, false, &config) { + Ok(_) => print_success(&format!("Worktree {} deleted", &worktree.name())), Err(error) => match error { WorktreeRemoveFailureReason::Changes(changes) => { warnings.push(format!( "Changes found in {}: {}, skipping", - &worktree, &changes + &worktree.name(), + &changes )); continue; } @@ -936,7 +953,10 @@ impl Repo { }, } } else { - warnings.push(format!("Worktree {} does not have a directory", &worktree)); + warnings.push(format!( + "Worktree {} does not have a directory", + &worktree.name() + )); } } Ok(warnings) @@ -991,7 +1011,7 @@ impl Repo { if dirname == default_branch_name { continue; } - if !&worktrees.contains(&dirname) { + if !&worktrees.iter().any(|worktree| worktree.name() == dirname) { unmanaged_worktrees.push(dirname); } } diff --git a/src/table.rs b/src/table.rs index f48ce5d..6fa7ed2 100644 --- a/src/table.rs +++ b/src/table.rs @@ -104,14 +104,15 @@ pub fn get_worktree_status_table( add_worktree_table_header(&mut table); for worktree in &worktrees { - let worktree_dir = &directory.join(&worktree); + let worktree_dir = &directory.join(&worktree.name()); if worktree_dir.exists() { let repo = match crate::Repo::open(worktree_dir, false) { Ok(repo) => repo, Err(error) => { errors.push(format!( "Failed opening repo of worktree {}: {}", - &worktree, &error + &worktree.name(), + &error )); continue; } @@ -120,7 +121,10 @@ pub fn get_worktree_status_table( errors.push(error); } } else { - errors.push(format!("Worktree {} does not have a directory", &worktree)); + errors.push(format!( + "Worktree {} does not have a directory", + &worktree.name() + )); } } for entry in std::fs::read_dir(&directory).map_err(|error| error.to_string())? { @@ -136,7 +140,7 @@ pub fn get_worktree_status_table( if dirname == crate::GIT_MAIN_WORKTREE_DIRECTORY { continue; } - if !&worktrees.contains(&dirname) { + if !&worktrees.iter().any(|worktree| worktree.name() == dirname) { errors.push(format!( "Found {}, which is not a valid worktree directory!", &dirname @@ -211,7 +215,7 @@ fn add_worktree_table_header(table: &mut Table) { fn add_worktree_status( table: &mut Table, - worktree_name: &str, + worktree: &crate::repo::Worktree, repo: &crate::Repo, ) -> Result<(), String> { let repo_status = repo.status(false)?; @@ -245,7 +249,7 @@ fn add_worktree_status( }; table.add_row(vec![ - worktree_name, + worktree.name(), &match repo_status.changes { Some(changes) => { let mut out = Vec::new();