Skip to content

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

Refactor integration reason computation and switch message

Maximilian Roosb4536793756a
diff --git a/src/commands/list/ci_status.rs b/src/commands/list/ci_status.rs
index cca9461f8..82fd1d94d 100644
--- a/src/commands/list/ci_status.rs
+++ b/src/commands/list/ci_status.rs
@@ -676,6 +676,7 @@ mod tests {
head_pipeline: None,
pipeline: None,
source_project_id: None,
+ web_url: None,
};
assert_eq!(mr.ci_status(), CiStatus::NoCI);
@@ -687,12 +688,15 @@ mod tests {
head_pipeline: Some(GitLabPipeline {
status: Some("success".into()),
sha: None,
+ web_url: None,
}),
pipeline: Some(GitLabPipeline {
status: Some("failed".into()),
sha: None,
+ web_url: None,
}),
source_project_id: None,
+ web_url: None,
};
assert_eq!(mr.ci_status(), CiStatus::Passed);
@@ -705,8 +709,10 @@ mod tests {
pipeline: Some(GitLabPipeline {
status: Some("running".into()),
sha: None,
+ web_url: None,
}),
source_project_id: None,
+ web_url: None,
};
assert_eq!(mr.ci_status(), CiStatus::Running);
}
@@ -1628,9 +1634,7 @@ impl PrStatus {
ci_status,
source: CiSource::PullRequest,
is_stale,
- // TODO: Fetch GitLab MR URL from glab output to enable clickable links
- // Currently only GitHub PRs have clickable underlined indicators
- url: None,
+ url: mr_info.web_url.clone(),
})
}
@@ -1740,8 +1744,7 @@ impl PrStatus {
ci_status,
source: CiSource::Branch,
is_stale,
- // TODO: Fetch GitLab pipeline URL to enable clickable links
- url: None,
+ url: pipeline.web_url.clone(),
})
}
}
@@ -1882,6 +1885,8 @@ struct GitLabMrInfo {
/// The source project ID (the project the MR's branch comes from).
/// Used to filter MRs by source project.
source_project_id: Option<u64>,
+ /// URL to the MR page for clickable links
+ web_url: Option<String>,
}
impl GitLabMrInfo {
@@ -1900,6 +1905,9 @@ struct GitLabPipeline {
/// Only present in `glab ci list` output, not in MR view embedded pipeline
#[serde(default)]
sha: Option<String>,
+ /// URL to the pipeline page for clickable links
+ #[serde(default)]
+ web_url: Option<String>,
}
fn parse_gitlab_status(status: Option<&str>) -> CiStatus {
diff --git a/src/commands/repository_ext.rs b/src/commands/repository_ext.rs
index ed32b2aef..f96c7ef9b 100644
--- a/src/commands/repository_ext.rs
+++ b/src/commands/repository_ext.rs
@@ -6,7 +6,7 @@ use super::worktree::{BranchDeletionMode, RemoveResult};
use anyhow::Context;
use color_print::cformat;
use worktrunk::config::ProjectConfig;
-use worktrunk::git::{GitError, Repository};
+use worktrunk::git::{GitError, IntegrationReason, Repository};
use worktrunk::path::format_path_for_display;
use worktrunk::styling::{format_with_gutter, progress_message, warning_message};
@@ -150,19 +150,12 @@ impl RepositoryCliExt for Repository {
// Pre-compute integration reason to avoid race conditions when removing
// multiple worktrees in background mode. Background git processes can hold
// locks that cause subsequent integration checks to fail.
- let integration_reason = if deletion_mode.should_keep() || deletion_mode.is_force() {
- // Skip integration check if user explicitly controls branch deletion
- None
- } else if let Some(ref target) = target_branch {
- // Use main_path repo for integration check (may be different from current repo)
- let main_repo = Repository::at(&main_path);
- let effective_target = main_repo.effective_integration_target(target);
- let mut provider =
- worktrunk::git::LazyGitIntegration::new(&main_repo, branch_name, &effective_target);
- worktrunk::git::check_integration(&mut provider)
- } else {
- None
- };
+ let integration_reason = compute_integration_reason(
+ &main_path,
+ Some(branch_name),
+ target_branch.as_deref(),
+ deletion_mode,
+ );
Ok(RemoveResult::RemovedWorktree {
main_path,
@@ -212,18 +205,12 @@ impl RepositoryCliExt for Repository {
};
// Pre-compute integration reason (same logic as remove_worktree_by_name)
- let integration_reason =
- if deletion_mode.should_keep() || deletion_mode.is_force() || branch_name.is_none() {
- None
- } else if let (Some(target), Some(bn)) = (&target_branch, &branch_name) {
- let main_repo = Repository::at(&main_path);
- let effective_target = main_repo.effective_integration_target(target);
- let mut provider =
- worktrunk::git::LazyGitIntegration::new(&main_repo, bn, &effective_target);
- worktrunk::git::check_integration(&mut provider)
- } else {
- None
- };
+ let integration_reason = compute_integration_reason(
+ &main_path,
+ branch_name.as_deref(),
+ target_branch.as_deref(),
+ deletion_mode,
+ );
Ok(RemoveResult::RemovedWorktree {
main_path,
@@ -341,6 +328,30 @@ fn load_project_config_at(repo_root: &Path) -> anyhow::Result<Option<ProjectConf
ProjectConfig::load(repo_root).context("Failed to load project config")
}
+/// Compute integration reason for branch deletion.
+///
+/// Returns `None` if:
+/// - `deletion_mode` is `Keep` or `ForceDelete` (skip integration check)
+/// - `branch_name` is `None` (detached HEAD)
+/// - `target_branch` is `None` (no target to check against)
+/// - Branch is not integrated into target (safe deletion not confirmed)
+fn compute_integration_reason(
+ main_path: &Path,
+ branch_name: Option<&str>,
+ target_branch: Option<&str>,
+ deletion_mode: BranchDeletionMode,
+) -> Option<IntegrationReason> {
+ if deletion_mode.should_keep() || deletion_mode.is_force() {
+ return None;
+ }
+ let (branch, target) = branch_name.zip(target_branch)?;
+ let main_repo = Repository::at(main_path);
+ let effective_target = main_repo.effective_integration_target(target);
+ let mut provider =
+ worktrunk::git::LazyGitIntegration::new(&main_repo, branch, &effective_target);
+ worktrunk::git::check_integration(&mut provider)
+}
+
/// Parse `git status --porcelain -z` output into a list of affected filenames.
///
/// The -z format uses NUL separators and handles renames specially:
diff --git a/src/output/handlers.rs b/src/output/handlers.rs
index a02542ce6..086a6b8cc 100644
--- a/src/output/handlers.rs
+++ b/src/output/handlers.rs
@@ -338,6 +338,20 @@ fn shell_integration_hint() -> String {
cformat!("Run <bright-black>wt config shell install</> to enable automatic cd")
}
+/// Show switch message when changing directory after worktree removal
+fn print_switch_message_if_changed(
+ changed_directory: bool,
+ main_path: &Path,
+) -> anyhow::Result<()> {
+ if changed_directory && let Ok(Some(dest_branch)) = Repository::at(main_path).current_branch() {
+ let path_display = format_path_for_display(main_path);
+ super::print(info_message(cformat!(
+ "Switched to worktree for <bold>{dest_branch}</> @ <bold>{path_display}</>"
+ )))?;
+ }
+ Ok(())
+}
+
/// Handle output for a switch operation
///
/// When shell integration is not active and no execute command is provided,
@@ -689,15 +703,7 @@ fn handle_removed_worktree_output(
)))?;
}
- // Show switch message when changing directory
- if changed_directory
- && let Ok(Some(dest_branch)) = Repository::at(main_path).current_branch()
- {
- let path_display = format_path_for_display(main_path);
- super::print(info_message(cformat!(
- "Switched to worktree for <bold>{dest_branch}</> @ <bold>{path_display}</>"
- )))?;
- }
+ print_switch_message_if_changed(changed_directory, main_path)?;
// Build command with the decision we already made
let remove_command =
@@ -773,15 +779,7 @@ fn handle_removed_worktree_output(
)))?;
}
- // Show switch message when changing directory
- if changed_directory
- && let Ok(Some(dest_branch)) = Repository::at(main_path).current_branch()
- {
- let path_display = format_path_for_display(main_path);
- super::print(info_message(cformat!(
- "Switched to worktree for <bold>{dest_branch}</> @ <bold>{path_display}</>"
- )))?;
- }
+ print_switch_message_if_changed(changed_directory, main_path)?;
super::flush()?;
Ok(())