Refactor worktree into own struct

This commit is contained in:
2021-12-29 18:42:57 +01:00
parent cbc9792755
commit fcbad5a3eb
2 changed files with 40 additions and 16 deletions

View File

@@ -166,6 +166,22 @@ pub struct RepoStatus {
pub branches: Vec<(String, Option<(String, RemoteTrackingStatus)>)>, 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)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@@ -746,14 +762,14 @@ impl Repo {
))) )))
} }
pub fn get_worktrees(&self) -> Result<Vec<String>, String> { pub fn get_worktrees(&self) -> Result<Vec<Worktree>, String> {
Ok(self Ok(self
.0 .0
.worktrees() .worktrees()
.map_err(convert_libgit2_error)? .map_err(convert_libgit2_error)?
.iter() .iter()
.map(|name| name.expect("Worktree name is invalid utf-8")) .map(|name| name.expect("Worktree name is invalid utf-8"))
.map(|name| name.to_string()) .map(Worktree::new)
.collect()) .collect())
} }
@@ -905,24 +921,25 @@ impl Repo {
for worktree in worktrees for worktree in worktrees
.iter() .iter()
.filter(|worktree| *worktree != &default_branch_name) .filter(|worktree| worktree.name() != default_branch_name)
.filter(|worktree| match &config { .filter(|worktree| match &config {
None => true, None => true,
Some(config) => match &config.persistent_branches { Some(config) => match &config.persistent_branches {
None => true, 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() { if repo_dir.exists() {
match self.remove_worktree(worktree, repo_dir, false, &config) { match self.remove_worktree(worktree.name(), repo_dir, false, &config) {
Ok(_) => print_success(&format!("Worktree {} deleted", &worktree)), Ok(_) => print_success(&format!("Worktree {} deleted", &worktree.name())),
Err(error) => match error { Err(error) => match error {
WorktreeRemoveFailureReason::Changes(changes) => { WorktreeRemoveFailureReason::Changes(changes) => {
warnings.push(format!( warnings.push(format!(
"Changes found in {}: {}, skipping", "Changes found in {}: {}, skipping",
&worktree, &changes &worktree.name(),
&changes
)); ));
continue; continue;
} }
@@ -936,7 +953,10 @@ impl Repo {
}, },
} }
} else { } else {
warnings.push(format!("Worktree {} does not have a directory", &worktree)); warnings.push(format!(
"Worktree {} does not have a directory",
&worktree.name()
));
} }
} }
Ok(warnings) Ok(warnings)
@@ -991,7 +1011,7 @@ impl Repo {
if dirname == default_branch_name { if dirname == default_branch_name {
continue; continue;
} }
if !&worktrees.contains(&dirname) { if !&worktrees.iter().any(|worktree| worktree.name() == dirname) {
unmanaged_worktrees.push(dirname); unmanaged_worktrees.push(dirname);
} }
} }

View File

@@ -104,14 +104,15 @@ pub fn get_worktree_status_table(
add_worktree_table_header(&mut table); add_worktree_table_header(&mut table);
for worktree in &worktrees { for worktree in &worktrees {
let worktree_dir = &directory.join(&worktree); let worktree_dir = &directory.join(&worktree.name());
if worktree_dir.exists() { if worktree_dir.exists() {
let repo = match crate::Repo::open(worktree_dir, false) { let repo = match crate::Repo::open(worktree_dir, false) {
Ok(repo) => repo, Ok(repo) => repo,
Err(error) => { Err(error) => {
errors.push(format!( errors.push(format!(
"Failed opening repo of worktree {}: {}", "Failed opening repo of worktree {}: {}",
&worktree, &error &worktree.name(),
&error
)); ));
continue; continue;
} }
@@ -120,7 +121,10 @@ pub fn get_worktree_status_table(
errors.push(error); errors.push(error);
} }
} else { } 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())? { 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 { if dirname == crate::GIT_MAIN_WORKTREE_DIRECTORY {
continue; continue;
} }
if !&worktrees.contains(&dirname) { if !&worktrees.iter().any(|worktree| worktree.name() == dirname) {
errors.push(format!( errors.push(format!(
"Found {}, which is not a valid worktree directory!", "Found {}, which is not a valid worktree directory!",
&dirname &dirname
@@ -211,7 +215,7 @@ fn add_worktree_table_header(table: &mut Table) {
fn add_worktree_status( fn add_worktree_status(
table: &mut Table, table: &mut Table,
worktree_name: &str, worktree: &crate::repo::Worktree,
repo: &crate::Repo, repo: &crate::Repo,
) -> Result<(), String> { ) -> Result<(), String> {
let repo_status = repo.status(false)?; let repo_status = repo.status(false)?;
@@ -245,7 +249,7 @@ fn add_worktree_status(
}; };
table.add_row(vec![ table.add_row(vec![
worktree_name, worktree.name(),
&match repo_status.changes { &match repo_status.changes {
Some(changes) => { Some(changes) => {
let mut out = Vec::new(); let mut out = Vec::new();