Skip to content

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

feat: Support gh CLI in non-Git directories

Maximilian Roos574c33838629
diff --git a/src/commands/list/ci_status.rs b/src/commands/list/ci_status.rs
index 05d63d54c..c82823192 100644
--- a/src/commands/list/ci_status.rs
+++ b/src/commands/list/ci_status.rs
@@ -31,13 +31,29 @@ impl PrStatus {
/// First tries to find PR/MR status, then falls back to workflow/pipeline runs
/// Returns None if no CI found or CLI tools unavailable
pub fn detect(branch: &str, local_head: &str) -> Option<Self> {
+ // Get GitHub repo for gh commands (from git remote)
+ let github_repo = Self::get_github_repo();
+
+ // Get git repo root directory for setting working directory
+ let repo_root = Self::get_repo_root();
+
// Try GitHub PR first
- if let Some(status) = Self::detect_github(branch, local_head) {
+ if let Some(status) = Self::detect_github(
+ branch,
+ local_head,
+ github_repo.as_deref(),
+ repo_root.as_deref(),
+ ) {
return Some(status);
}
// Try GitHub workflow runs (for branches without PRs)
- if let Some(status) = Self::detect_github_workflow(branch, local_head) {
+ if let Some(status) = Self::detect_github_workflow(
+ branch,
+ local_head,
+ github_repo.as_deref(),
+ repo_root.as_deref(),
+ ) {
return Some(status);
}
@@ -50,7 +66,67 @@ impl PrStatus {
Self::detect_gitlab_pipeline(branch, local_head)
}
- fn detect_github(branch: &str, local_head: &str) -> Option<Self> {
+ /// Get git repository root directory
+ fn get_repo_root() -> Option<String> {
+ let output = Command::new("git")
+ .args(["rev-parse", "--show-toplevel"])
+ .output()
+ .ok()?;
+
+ if !output.status.success() {
+ return None;
+ }
+
+ Some(String::from_utf8(output.stdout).ok()?.trim().to_string())
+ }
+
+ /// Extract GitHub repository owner/name from git remote URL
+ /// Returns None if not a GitHub repo or if git command fails
+ fn get_github_repo() -> Option<String> {
+ let output = Command::new("git")
+ .args(["remote", "get-url", "origin"])
+ .output()
+ .ok()?;
+
+ if !output.status.success() {
+ return None;
+ }
+
+ let url = String::from_utf8(output.stdout).ok()?.trim().to_string();
+
+ // Parse GitHub URL - handles both HTTPS and SSH formats
+ // HTTPS: https://github.com/owner/repo.git
+ // SSH: [email protected]:owner/repo.git
+ if !url.contains("github.com") {
+ return None;
+ }
+
+ // Find the part after "github.com:" or "github.com/"
+ let after_github = if let Some(pos) = url.find("github.com:") {
+ &url[pos + "github.com:".len()..]
+ } else if let Some(pos) = url.find("github.com/") {
+ &url[pos + "github.com/".len()..]
+ } else {
+ return None;
+ };
+
+ // Remove .git suffix if present
+ let repo_part = after_github.strip_suffix(".git").unwrap_or(after_github);
+
+ // Extract owner/repo (should be of form "owner/repo")
+ if repo_part.split('/').count() >= 2 {
+ Some(repo_part.to_string())
+ } else {
+ None
+ }
+ }
+
+ fn detect_github(
+ branch: &str,
+ local_head: &str,
+ repo: Option<&str>,
+ repo_root: Option<&str>,
+ ) -> Option<Self> {
// Check if gh is available and authenticated
if !Command::new("gh")
.args(["auth", "status"])
@@ -62,16 +138,30 @@ impl PrStatus {
}
// Get PR info for the branch
- let output = Command::new("gh")
- .args([
- "pr",
- "view",
- branch,
- "--json",
- "state,headRefOid,mergeStateStatus,statusCheckRollup",
- ])
- .output()
- .ok()?;
+ let mut cmd = Command::new("gh");
+ cmd.args([
+ "pr",
+ "view",
+ branch,
+ "--json",
+ "state,headRefOid,mergeStateStatus,statusCheckRollup",
+ ]);
+
+ // Remove environment variables that force color output
+ cmd.env_remove("CLICOLOR_FORCE");
+ cmd.env_remove("GH_FORCE_TTY");
+ cmd.env("NO_COLOR", "1");
+ cmd.env("CLICOLOR", "0");
+
+ if let Some(r) = repo {
+ cmd.args(["--repo", r]);
+ }
+
+ if let Some(root) = repo_root {
+ cmd.current_dir(root);
+ }
+
+ let output = cmd.output().ok()?;
if !output.status.success() {
return None;
@@ -201,7 +291,12 @@ impl PrStatus {
}
}
- fn detect_github_workflow(branch: &str, _local_head: &str) -> Option<Self> {
+ fn detect_github_workflow(
+ branch: &str,
+ _local_head: &str,
+ repo: Option<&str>,
+ repo_root: Option<&str>,
+ ) -> Option<Self> {
// Check if gh is available and authenticated
if !Command::new("gh")
.args(["auth", "status"])
@@ -213,19 +308,35 @@ impl PrStatus {
}
// Get most recent workflow run for the branch
- let output = Command::new("gh")
- .args([
- "run",
- "list",
- "--branch",
- branch,
- "--limit",
- "1",
- "--json",
- "status,conclusion",
- ])
- .output()
- .ok()?;
+ let mut cmd = Command::new("gh");
+ cmd.args([
+ "run",
+ "list",
+ "--branch",
+ branch,
+ "--limit",
+ "1",
+ "--json",
+ "status,conclusion",
+ ]);
+
+ // Remove environment variables that force color output even when piped
+ // CLICOLOR_FORCE and GH_FORCE_TTY override NO_COLOR and TTY detection
+ cmd.env_remove("CLICOLOR_FORCE");
+ cmd.env_remove("GH_FORCE_TTY");
+ // Request no color output
+ cmd.env("NO_COLOR", "1");
+ cmd.env("CLICOLOR", "0");
+
+ if let Some(r) = repo {
+ cmd.args(["--repo", r]);
+ }
+
+ if let Some(root) = repo_root {
+ cmd.current_dir(root);
+ }
+
+ let output = cmd.output().ok()?;
if !output.status.success() {
return None;