Skip to content

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

Refactor: Centralize clean working tree check and output logic

Maximilian Roos0a096cc25a0f
diff --git a/src/commands/merge.rs b/src/commands/merge.rs
index 84725f36d..b51450f89 100644
--- a/src/commands/merge.rs
+++ b/src/commands/merge.rs
@@ -23,11 +23,7 @@ pub fn handle_merge(target: Option<&str>, squash: bool, keep: bool) -> Result<()
}
// Check for uncommitted changes
- if repo.is_dirty()? {
- return Err(GitError::CommandFailed(format_error(
- "Working tree has uncommitted changes. Commit or stash them first.",
- )));
- }
+ repo.ensure_clean_working_tree()?;
// Squash commits if requested
if squash {
diff --git a/src/commands/worktree.rs b/src/commands/worktree.rs
index 11e373083..11e2cbc12 100644
--- a/src/commands/worktree.rs
+++ b/src/commands/worktree.rs
@@ -76,18 +76,20 @@ pub fn handle_switch(
// Output success message
let success_msg = if create {
- format!("Created new branch and worktree for '{}'", branch)
+ format!(
+ "Created new branch and worktree for '{}' at {}",
+ branch,
+ worktree_path.display()
+ )
} else {
- format!("Added worktree for existing branch '{}'", branch)
+ format!(
+ "Added worktree for existing branch '{}' at {}",
+ branch,
+ worktree_path.display()
+ )
};
- if internal {
- println!("__WORKTRUNK_CD__{}", worktree_path.display());
- println!("{} at {}", success_msg, worktree_path.display());
- } else {
- println!("{}", success_msg);
- print_worktree_info(&worktree_path, "switch");
- }
+ output_with_cd(internal, &worktree_path, &success_msg, "switch");
Ok(())
}
@@ -96,11 +98,7 @@ pub fn handle_remove(internal: bool) -> Result<(), GitError> {
let repo = Repository::current();
// Check for uncommitted changes
- if repo.is_dirty()? {
- return Err(GitError::CommandFailed(format_error(
- "Working tree has uncommitted changes. Commit or stash them first.",
- )));
- }
+ repo.ensure_clean_working_tree()?;
// Get current state
let current_branch = repo.current_branch()?;
@@ -295,3 +293,20 @@ fn print_worktree_info(path: &std::path::Path, command: &str) {
fn format_warning(msg: &str) -> String {
worktrunk::error_format::format_warning(msg)
}
+
+/// Output message with optional CD directive for internal mode.
+///
+/// In internal mode: prints CD directive and message
+/// In normal mode: prints message and shell integration note
+fn output_with_cd(internal: bool, path: &std::path::Path, message: &str, command: &str) {
+ if internal {
+ println!("__WORKTRUNK_CD__{}", path.display());
+ println!("{}", message);
+ } else {
+ println!("{}", message);
+ println!(
+ "Note: Use 'wt {}' (with shell integration) for automatic cd",
+ command
+ );
+ }
+}
diff --git a/src/git.rs b/src/git.rs
index eeb215dfe..07a334886 100644
--- a/src/git.rs
+++ b/src/git.rs
@@ -178,6 +178,18 @@ impl Repository {
Ok(!stdout.trim().is_empty())
}
+ /// Ensure the working tree is clean (no uncommitted changes).
+ ///
+ /// Returns an error if there are uncommitted changes.
+ pub fn ensure_clean_working_tree(&self) -> Result<(), GitError> {
+ if self.is_dirty()? {
+ return Err(GitError::CommandFailed(crate::error_format::format_error(
+ "Working tree has uncommitted changes. Commit or stash them first.",
+ )));
+ }
+ Ok(())
+ }
+
/// Get the worktree root directory (top-level of the working tree).
pub fn worktree_root(&self) -> Result<PathBuf, GitError> {
let stdout = self.run_command(&["rev-parse", "--show-toplevel"])?;
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_create_new.snap b/tests/snapshots/integration__integration_tests__switch__switch_create_new.snap
index a2178c2dc..0ef76e70f 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_create_new.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_create_new.snap
@@ -7,6 +7,7 @@ info:
- "--create"
- feature-x
env:
+ CLICOLOR_FORCE: "1"
GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
GIT_CONFIG_GLOBAL: /dev/null
@@ -19,8 +20,7 @@ info:
success: true
exit_code: 0
----- stdout -----
-Created new branch and worktree for 'feature-x'
-Path: [REPO]/../main.feature-x
+Created new branch and worktree for 'feature-x' at [REPO]/../main.feature-x
Note: Use 'wt switch' (with shell integration) for automatic cd
----- stderr -----
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_with_base.snap b/tests/snapshots/integration__integration_tests__switch__switch_with_base.snap
index b84b458eb..c9e52d6a8 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_with_base.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_with_base.snap
@@ -9,6 +9,7 @@ info:
- main
- feature-with-base
env:
+ CLICOLOR_FORCE: "1"
GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
GIT_CONFIG_GLOBAL: /dev/null
@@ -21,8 +22,7 @@ info:
success: true
exit_code: 0
----- stdout -----
-Created new branch and worktree for 'feature-with-base'
-Path: [REPO]/../main.feature-with-base
+Created new branch and worktree for 'feature-with-base' at [REPO]/../main.feature-with-base
Note: Use 'wt switch' (with shell integration) for automatic cd
----- stderr -----