Skip to content

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

refactor: unify CI platform detection with CiPlatform methods (#805)

Maximilian Roosd3bcb3ea7924
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c9156e424..c1f2223a5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -323,7 +323,7 @@
- **`wt step copy-ignored`**: Copy gitignored files listed in `.worktreeinclude` between worktrees. Useful for syncing `.env` files, IDE settings, and build caches to new worktrees via post-create hooks. Uses COW (reflink) copying for efficient handling of large directories. Matches Claude Code Desktop's worktree file syncing behavior.
- **`--foreground` flag**: Debug background hooks by running them in the foreground. Available on `wt hook post-start`, `wt hook post-switch`, and `wt remove`. Replaces the deprecated `--no-background` flag.
- **`--var` flag for hooks**: Override template variables when running hooks manually, e.g., `wt hook post-create --var target=main`.
-- **`[ci] platform` config**: Explicitly set CI platform (`github` or `gitlab`) for GitHub Enterprise or self-hosted GitLab where URL-based detection fails.
+- **`ci.platform` config**: Explicitly set CI platform (`github` or `gitlab`) for GitHub Enterprise or self-hosted GitLab where URL-based detection fails.
- **Upstream diff in `wt select`**: Tab 4 shows ahead/behind diff vs upstream tracking branch (remote⇅), matching the column in `wt list`.
- **`{{ base }}` and `{{ base_worktree_path }}` variables**: New template variables for creation hooks (post-create, post-start, post-switch) to access the base branch name and worktree path.
- **`-vv` diagnostic reports**: Double-verbose flag writes a diagnostic report to `.git/wt-logs/diagnostic.md` with environment info, configs, and logs for easy bug reporting.
diff --git a/src/commands/list/ci_status/mod.rs b/src/commands/list/ci_status/mod.rs
index 3c05f3f7c..fc9ff0a7c 100644
--- a/src/commands/list/ci_status/mod.rs
+++ b/src/commands/list/ci_status/mod.rs
@@ -306,8 +306,8 @@ impl PrStatus {
/// Detect CI status without caching (internal implementation)
///
/// Platform is determined by project config override or remote URL detection.
- /// For unknown platforms (e.g., GitHub Enterprise with custom domains), falls back
- /// to trying both platforms.
+ /// Returns `None` if the platform cannot be determined (user should set
+ /// `ci.platform` in project config for non-standard hostnames).
/// PR/MR detection always runs. Workflow/pipeline fallback only runs if `has_upstream`.
fn detect_uncached(
repo: &Repository,
@@ -323,56 +323,17 @@ impl PrStatus {
let platform = get_platform_for_repo(repo, platform_override);
match platform {
- Some(CiPlatform::GitHub) => {
- Self::detect_github_ci(repo, branch, local_head, has_upstream)
- }
- Some(CiPlatform::GitLab) => {
- Self::detect_gitlab_ci(repo, branch, local_head, has_upstream)
- }
+ Some(p) => p.detect_ci(repo, branch, local_head, has_upstream),
None => {
- // Unknown platform (e.g., GitHub Enterprise, self-hosted GitLab with custom domain)
- // Fall back to trying both platforms
- log::debug!("Could not determine CI platform, trying both");
- Self::detect_github_ci(repo, branch, local_head, has_upstream)
- .or_else(|| Self::detect_gitlab_ci(repo, branch, local_head, has_upstream))
+ // Unknown platform - user should set ci.platform in project config
+ log::debug!(
+ "Could not detect CI platform from remote URL; \
+ set ci.platform in .config/wt.toml for CI status"
+ );
+ None
}
}
}
-
- /// Detect GitHub CI status (PR first, then workflow if has_upstream)
- fn detect_github_ci(
- repo: &Repository,
- branch: &str,
- local_head: &str,
- has_upstream: bool,
- ) -> Option<Self> {
- if let Some(status) = github::detect_github(repo, branch, local_head) {
- return Some(status);
- }
- if has_upstream {
- return github::detect_github_commit_checks(repo, local_head);
- }
- None
- }
-
- /// Detect GitLab CI status (MR first, then pipeline if has_upstream)
- fn detect_gitlab_ci(
- repo: &Repository,
- branch: &str,
- local_head: &str,
- has_upstream: bool,
- ) -> Option<Self> {
- if !tool_available("glab", &["--version"]) {
- return None;
- }
- if let Some(status) = gitlab::detect_gitlab(repo, branch, local_head) {
- return Some(status);
- }
- if has_upstream {
- return gitlab::detect_gitlab_pipeline(branch, local_head);
- }
- None
- }
}
#[cfg(test)]
diff --git a/src/commands/list/ci_status/platform.rs b/src/commands/list/ci_status/platform.rs
index d8f1e2a90..892be5116 100644
--- a/src/commands/list/ci_status/platform.rs
+++ b/src/commands/list/ci_status/platform.rs
@@ -5,11 +5,13 @@
use worktrunk::git::{GitRemoteUrl, Repository};
+use super::{PrStatus, github, gitlab, tool_available};
+
/// CI platform detected from project config override or remote URL.
///
/// Platform is determined by:
-/// 1. Project config `[ci] platform = "github" | "gitlab"` (takes precedence)
-/// 2. Remote URL detection (searches for "github" or "gitlab" in URL)
+/// 1. Project config `ci.platform = "github"` or `"gitlab"` (takes precedence)
+/// 2. Remote URL detection (searches for "github" or "gitlab" in hostname)
#[derive(Debug, Clone, Copy, PartialEq, Eq, strum::Display, strum::EnumString)]
#[strum(serialize_all = "lowercase")]
pub enum CiPlatform {
@@ -17,6 +19,54 @@ pub enum CiPlatform {
GitLab,
}
+impl CiPlatform {
+ /// Check if the CLI tool for this platform is available.
+ fn is_tool_available(self) -> bool {
+ match self {
+ Self::GitHub => tool_available("gh", &["--version"]),
+ Self::GitLab => tool_available("glab", &["--version"]),
+ }
+ }
+
+ /// Detect CI status from PR/MR.
+ fn detect_pr_mr(self, repo: &Repository, branch: &str, local_head: &str) -> Option<PrStatus> {
+ match self {
+ Self::GitHub => github::detect_github(repo, branch, local_head),
+ Self::GitLab => gitlab::detect_gitlab(repo, branch, local_head),
+ }
+ }
+
+ /// Detect CI status from branch workflow/pipeline (fallback when no PR/MR).
+ fn detect_branch(self, repo: &Repository, branch: &str, local_head: &str) -> Option<PrStatus> {
+ match self {
+ Self::GitHub => github::detect_github_commit_checks(repo, local_head),
+ Self::GitLab => gitlab::detect_gitlab_pipeline(branch, local_head),
+ }
+ }
+
+ /// Detect CI status: PR/MR first, then branch workflow/pipeline if `has_upstream`.
+ ///
+ /// Returns `None` if the CLI tool is not available or no CI status found.
+ pub(super) fn detect_ci(
+ self,
+ repo: &Repository,
+ branch: &str,
+ local_head: &str,
+ has_upstream: bool,
+ ) -> Option<PrStatus> {
+ if !self.is_tool_available() {
+ return None;
+ }
+ if let Some(status) = self.detect_pr_mr(repo, branch, local_head) {
+ return Some(status);
+ }
+ if has_upstream {
+ return self.detect_branch(repo, branch, local_head);
+ }
+ None
+ }
+}
+
/// Detect the CI platform from a remote URL.
///
/// Uses [`GitRemoteUrl`] to parse the URL and check the host for "github" or "gitlab".
@@ -33,7 +83,7 @@ pub fn detect_platform_from_url(url: &str) -> Option<CiPlatform> {
/// Get the CI platform for a repository.
///
-/// If `platform_override` is provided (from project config `[ci] platform`),
+/// If `platform_override` is provided (from project config `ci.platform`),
/// uses that value directly. Otherwise, searches all remote URLs for a
/// supported platform (GitHub or GitLab).
pub fn get_platform_for_repo(