Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

Refactor `if let` statements to `match` for clarity

Maximilian Roos50f6a92985e3
diff --git a/src/commands/list/mod.rs b/src/commands/list/mod.rs
index 31b4b94c3..b73dc2329 100644
--- a/src/commands/list/mod.rs
+++ b/src/commands/list/mod.rs
@@ -79,9 +79,12 @@ pub fn handle_list() -> Result<(), GitError> {
};
// Get upstream tracking info
- let (upstream_remote, upstream_ahead, upstream_behind) = if let Some(ref branch) = wt.branch
+ let (upstream_remote, upstream_ahead, upstream_behind) = match wt
+ .branch
+ .as_ref()
+ .and_then(|b| wt_repo.upstream_branch(b).ok().flatten())
{
- if let Ok(Some(upstream_branch)) = wt_repo.upstream_branch(branch) {
+ Some(upstream_branch) => {
// Extract remote name from "origin/main" -> "origin"
let remote = upstream_branch
.split_once('/')
@@ -92,11 +95,8 @@ pub fn handle_list() -> Result<(), GitError> {
.ahead_behind(&upstream_branch, &wt.head)
.unwrap_or((0, 0));
(Some(remote), ahead, behind)
- } else {
- (None, 0, 0)
}
- } else {
- (None, 0, 0)
+ None => (None, 0, 0),
};
// Get worktree state (merge/rebase/etc)
diff --git a/src/commands/worktree.rs b/src/commands/worktree.rs
index 084c97dcf..4d005d805 100644
--- a/src/commands/worktree.rs
+++ b/src/commands/worktree.rs
@@ -30,19 +30,21 @@ pub fn handle_switch(
}
// Check if worktree already exists for this branch
- if let Some(existing_path) = repo.worktree_for_branch(branch)? {
- if existing_path.exists() {
+ match repo.worktree_for_branch(branch)? {
+ Some(existing_path) if existing_path.exists() => {
if internal {
println!("__WORKTRUNK_CD__{}", existing_path.display());
}
return Ok(());
- } else {
+ }
+ Some(_) => {
return Err(GitError::CommandFailed(format_error_with_bold(
"Worktree directory missing for '",
branch,
"'. Run 'git worktree prune' to clean up.",
)));
}
+ None => {}
}
// No existing worktree, create one
diff --git a/src/display.rs b/src/display.rs
index c52ca4bbe..496422a91 100644
--- a/src/display.rs
+++ b/src/display.rs
@@ -149,14 +149,10 @@ pub fn find_common_prefix(paths: &[PathBuf]) -> PathBuf {
/// Shorten a path relative to a common prefix
pub fn shorten_path(path: &Path, prefix: &Path) -> String {
- if let Ok(relative) = path.strip_prefix(prefix) {
- if relative.as_os_str().is_empty() {
- ".".to_string()
- } else {
- format!("./{}", relative.display())
- }
- } else {
- path.display().to_string()
+ match path.strip_prefix(prefix) {
+ Ok(rel) if rel.as_os_str().is_empty() => ".".to_string(),
+ Ok(rel) => format!("./{}", rel.display()),
+ Err(_) => path.display().to_string(),
}
}
diff --git a/src/git.rs b/src/git.rs
index dc885efc4..9985fe745 100644
--- a/src/git.rs
+++ b/src/git.rs
@@ -523,21 +523,18 @@ fn parse_local_default_branch(output: &str) -> Result<String, GitError> {
}
fn parse_remote_default_branch(output: &str) -> Result<String, GitError> {
- for line in output.lines() {
- if let Some(symref) = line.strip_prefix("ref: ") {
- // Parse "refs/heads/main\tHEAD"
- if let Some((ref_path, _)) = symref.split_once('\t') {
- // Strip "refs/heads/" prefix
- if let Some(branch) = ref_path.strip_prefix("refs/heads/") {
- return Ok(branch.to_string());
- }
- }
- }
- }
-
- Err(GitError::ParseError(
- "Could not find symbolic ref in ls-remote output".to_string(),
- ))
+ output
+ .lines()
+ .find_map(|line| {
+ line.strip_prefix("ref: ")
+ .and_then(|symref| symref.split_once('\t'))
+ .map(|(ref_path, _)| ref_path)
+ .and_then(|ref_path| ref_path.strip_prefix("refs/heads/"))
+ .map(|branch| branch.to_string())
+ })
+ .ok_or_else(|| {
+ GitError::ParseError("Could not find symbolic ref in ls-remote output".to_string())
+ })
}
fn parse_numstat(output: &str) -> Result<(usize, usize), GitError> {