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 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<Vec<String>, String> {
pub fn get_worktrees(&self) -> Result<Vec<Worktree>, 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);
}
}

View File

@@ -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();