Skip to content

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

Refactor: Improve string handling with `into_owned` and `or_else`

Maximilian Roos595378ef5258
diff --git a/src/commands/worktree.rs b/src/commands/worktree.rs
index 4d005d805..42fcab2ea 100644
--- a/src/commands/worktree.rs
+++ b/src/commands/worktree.rs
@@ -73,8 +73,7 @@ pub fn handle_switch(
}
repo.run_command(&args)
- .map_err(|e| GitError::CommandFailed(format!("Failed to create worktree: {}", e)))
- .map(|_| ())?;
+ .map_err(|e| GitError::CommandFailed(format!("Failed to create worktree: {}", e)))?;
// Output success message
let success_msg = if create {
@@ -150,8 +149,7 @@ pub fn handle_remove(internal: bool) -> Result<(), GitError> {
repo.run_command(&["switch", &default_branch])
.map_err(|e| {
GitError::CommandFailed(format!("Failed to switch to '{}': {}", default_branch, e))
- })
- .map(|_| ())?;
+ })?;
if !internal {
println!("Switched to default branch '{}'", default_branch);
diff --git a/src/git.rs b/src/git.rs
index 4dfa50957..a228829ce 100644
--- a/src/git.rs
+++ b/src/git.rs
@@ -75,19 +75,21 @@ impl Repository {
/// Check if a git branch exists (local or remote).
pub fn branch_exists(&self, branch: &str) -> Result<bool, GitError> {
// Try local branch first
- let result =
- self.run_command(&["rev-parse", "--verify", &format!("refs/heads/{}", branch)]);
- if result.is_ok() {
+ if self
+ .run_command(&["rev-parse", "--verify", &format!("refs/heads/{}", branch)])
+ .is_ok()
+ {
return Ok(true);
}
// Try remote branch
- let result = self.run_command(&[
- "rev-parse",
- "--verify",
- &format!("refs/remotes/origin/{}", branch),
- ]);
- Ok(result.is_ok())
+ Ok(self
+ .run_command(&[
+ "rev-parse",
+ "--verify",
+ &format!("refs/remotes/origin/{}", branch),
+ ])
+ .is_ok())
}
/// Get the current branch name, or None if in detached HEAD state.
@@ -109,14 +111,12 @@ impl Repository {
/// 2. If not cached, query the remote and cache the result
pub fn default_branch(&self) -> Result<String, GitError> {
// Try local cache first (fast path)
- if let Ok(branch) = self.get_local_default_branch() {
- return Ok(branch);
- }
-
- // Query remote and cache it
- let branch = self.query_remote_default_branch()?;
- self.cache_default_branch(&branch)?;
- Ok(branch)
+ self.get_local_default_branch().or_else(|_| {
+ // Query remote and cache it
+ let branch = self.query_remote_default_branch()?;
+ self.cache_default_branch(&branch)?;
+ Ok(branch)
+ })
}
/// Get the git common directory (the actual .git directory for the repository).
@@ -411,10 +411,10 @@ impl Repository {
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
- return Err(GitError::CommandFailed(stderr.to_string()));
+ return Err(GitError::CommandFailed(stderr.into_owned()));
}
- Ok(String::from_utf8_lossy(&output.stdout).to_string())
+ Ok(String::from_utf8_lossy(&output.stdout).into_owned())
}
}
diff --git a/src/llm.rs b/src/llm.rs
index eea4f384f..734164e6d 100644
--- a/src/llm.rs
+++ b/src/llm.rs
@@ -57,7 +57,7 @@ fn try_generate_llm_message(
return Err(format!("LLM command failed: {}", stderr).into());
}
- let message = String::from_utf8_lossy(&output.stdout).trim().to_string();
+ let message = String::from_utf8_lossy(&output.stdout).trim().to_owned();
if message.is_empty() {
return Err("LLM returned empty message".into());