Skip to content

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

feat: show branch and fork info in PR/MR description (#808)

Maximilian Roos7ee2fb7430ac
diff --git a/src/commands/worktree/switch.rs b/src/commands/worktree/switch.rs
index 3cd53fd82..5ab8f3fbf 100644
--- a/src/commands/worktree/switch.rs
+++ b/src/commands/worktree/switch.rs
@@ -33,13 +33,14 @@ struct ResolvedTarget {
/// Returns two lines for gutter formatting:
/// ```text
/// ┃ Fix authentication bug in login flow (#101)
-/// ┃ by @alice · open · https://github.com/owner/repo/pull/101
+/// ┃ by @alice · open · feature-auth · https://github.com/owner/repo/pull/101
/// ```
fn format_ref_context(ctx: &impl RefContext) -> String {
let mut status_parts = vec![format!("by @{}", ctx.author()), ctx.state().to_string()];
if ctx.draft() {
status_parts.push("draft".to_string());
}
+ status_parts.push(ctx.source_ref());
let status_line = status_parts.join(" · ");
cformat!(
diff --git a/src/git/error.rs b/src/git/error.rs
index b4fb6e0fb..0512e40b7 100644
--- a/src/git/error.rs
+++ b/src/git/error.rs
@@ -85,6 +85,12 @@ pub trait RefContext {
fn state(&self) -> &str;
fn draft(&self) -> bool;
fn url(&self) -> &str;
+
+ /// The source branch reference for display.
+ ///
+ /// For same-repo PRs/MRs: just the branch name (e.g., `feature-auth`)
+ /// For fork PRs/MRs: `owner:branch` format (e.g., `contributor:feature-fix`)
+ fn source_ref(&self) -> String;
}
/// Domain errors for git and worktree operations.
diff --git a/src/git/mr_ref.rs b/src/git/mr_ref.rs
index 434863d01..7021576e5 100644
--- a/src/git/mr_ref.rs
+++ b/src/git/mr_ref.rs
@@ -82,6 +82,43 @@ impl super::RefContext for MrInfo {
fn url(&self) -> &str {
&self.url
}
+ fn source_ref(&self) -> String {
+ if self.is_cross_project {
+ // Try to extract owner from source project URL
+ let owner = self
+ .source_project_http_url
+ .as_ref()
+ .or(self.source_project_ssh_url.as_ref())
+ .and_then(|url| extract_owner_from_url(url));
+ match owner {
+ Some(owner) => format!("{}:{}", owner, self.source_branch),
+ None => self.source_branch.clone(),
+ }
+ } else {
+ self.source_branch.clone()
+ }
+ }
+}
+
+/// Extract owner from a git URL.
+///
+/// Handles both SSH (`[email protected]:owner/repo.git`) and HTTPS
+/// (`https://gitlab.com/owner/repo.git`) formats.
+fn extract_owner_from_url(url: &str) -> Option<String> {
+ // SSH format: [email protected]:owner/repo.git
+ if let Some(path) = url.strip_prefix("git@").and_then(|s| s.split(':').nth(1)) {
+ return path.split('/').next().map(|s| s.to_string());
+ }
+ // HTTPS format: https://gitlab.com/owner/repo.git
+ // After stripping prefix: "gitlab.com/owner/repo.git"
+ // nth(1) skips the host and returns the owner
+ if let Some(path) = url
+ .strip_prefix("https://")
+ .or_else(|| url.strip_prefix("http://"))
+ {
+ return path.split('/').nth(1).map(|s| s.to_string());
+ }
+ None
}
/// Raw JSON response from `glab api projects/:id/merge_requests/<number>`.
@@ -578,4 +615,101 @@ mod tests {
let url = target_remote_url(&mr);
assert_eq!(url, None);
}
+
+ #[test]
+ fn test_extract_owner_from_url_ssh() {
+ assert_eq!(
+ extract_owner_from_url("[email protected]:owner/repo.git"),
+ Some("owner".to_string())
+ );
+ assert_eq!(
+ extract_owner_from_url("[email protected]:org/repo.git"),
+ Some("org".to_string())
+ );
+ }
+
+ #[test]
+ fn test_extract_owner_from_url_https() {
+ assert_eq!(
+ extract_owner_from_url("https://gitlab.com/owner/repo.git"),
+ Some("owner".to_string())
+ );
+ assert_eq!(
+ extract_owner_from_url("http://gitlab.com/owner/repo.git"),
+ Some("owner".to_string())
+ );
+ }
+
+ #[test]
+ fn test_extract_owner_from_url_invalid() {
+ assert_eq!(extract_owner_from_url("invalid-url"), None);
+ assert_eq!(extract_owner_from_url(""), None);
+ }
+
+ #[test]
+ fn test_source_ref_same_project() {
+ let mr = MrInfo {
+ number: 101,
+ title: "Fix bug".to_string(),
+ author: "alice".to_string(),
+ state: "opened".to_string(),
+ draft: false,
+ source_branch: "feature-auth".to_string(),
+ source_project_id: 123,
+ target_project_id: 123,
+ source_project_ssh_url: None,
+ source_project_http_url: None,
+ target_project_ssh_url: None,
+ target_project_http_url: None,
+ is_cross_project: false,
+ url: "https://gitlab.com/owner/repo/-/merge_requests/101".to_string(),
+ };
+ use crate::git::RefContext;
+ assert_eq!(mr.source_ref(), "feature-auth");
+ }
+
+ #[test]
+ fn test_source_ref_cross_project() {
+ let mr = MrInfo {
+ number: 101,
+ title: "Fix bug".to_string(),
+ author: "contributor".to_string(),
+ state: "opened".to_string(),
+ draft: false,
+ source_branch: "feature-fix".to_string(),
+ source_project_id: 456,
+ target_project_id: 123,
+ source_project_ssh_url: Some("[email protected]:contributor/repo.git".to_string()),
+ source_project_http_url: Some("https://gitlab.com/contributor/repo.git".to_string()),
+ target_project_ssh_url: None,
+ target_project_http_url: None,
+ is_cross_project: true,
+ url: "https://gitlab.com/owner/repo/-/merge_requests/101".to_string(),
+ };
+ use crate::git::RefContext;
+ assert_eq!(mr.source_ref(), "contributor:feature-fix");
+ }
+
+ #[test]
+ fn test_source_ref_cross_project_no_url() {
+ // When URL parsing fails, falls back to just the branch name
+ let mr = MrInfo {
+ number: 101,
+ title: "Fix bug".to_string(),
+ author: "contributor".to_string(),
+ state: "opened".to_string(),
+ draft: false,
+ source_branch: "feature-fix".to_string(),
+ source_project_id: 456,
+ target_project_id: 123,
+ source_project_ssh_url: None,
+ source_project_http_url: None,
+ target_project_ssh_url: None,
+ target_project_http_url: None,
+ is_cross_project: true,
+ url: "https://gitlab.com/owner/repo/-/merge_requests/101".to_string(),
+ };
+ use crate::git::RefContext;
+ assert_eq!(mr.source_ref(), "feature-fix");
+ }
}
diff --git a/src/git/pr_ref.rs b/src/git/pr_ref.rs
index ebdc36584..6d1dfaf33 100644
--- a/src/git/pr_ref.rs
+++ b/src/git/pr_ref.rs
@@ -89,6 +89,13 @@ impl super::RefContext for PrInfo {
fn url(&self) -> &str {
&self.url
}
+ fn source_ref(&self) -> String {
+ if self.is_cross_repository {
+ format!("{}:{}", self.head_owner, self.head_ref_name)
+ } else {
+ self.head_ref_name.clone()
+ }
+ }
}
/// Raw JSON response from `gh api repos/{owner}/{repo}/pulls/{number}`.
@@ -467,4 +474,46 @@ mod tests {
"https://github.example.com/org/project.git"
);
}
+
+ #[test]
+ fn test_source_ref_same_repo() {
+ let pr = PrInfo {
+ number: 101,
+ title: "Fix bug".to_string(),
+ author: "alice".to_string(),
+ state: "open".to_string(),
+ draft: false,
+ head_ref_name: "feature-auth".to_string(),
+ head_owner: "owner".to_string(),
+ head_repo: "repo".to_string(),
+ base_owner: "owner".to_string(),
+ base_repo: "repo".to_string(),
+ is_cross_repository: false,
+ host: "github.com".to_string(),
+ url: "https://github.com/owner/repo/pull/101".to_string(),
+ };
+ use crate::git::RefContext;
+ assert_eq!(pr.source_ref(), "feature-auth");
+ }
+
+ #[test]
+ fn test_source_ref_fork() {
+ let pr = PrInfo {
+ number: 42,
+ title: "Add feature".to_string(),
+ author: "contributor".to_string(),
+ state: "open".to_string(),
+ draft: false,
+ head_ref_name: "feature-fix".to_string(),
+ head_owner: "contributor".to_string(),
+ head_repo: "repo".to_string(),
+ base_owner: "owner".to_string(),
+ base_repo: "repo".to_string(),
+ is_cross_repository: true,
+ host: "github.com".to_string(),
+ url: "https://github.com/owner/repo/pull/42".to_string(),
+ };
+ use crate::git::RefContext;
+ assert_eq!(pr.source_ref(), "contributor:feature-fix");
+ }
}
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_mr_create_conflict.snap b/tests/snapshots/integration__integration_tests__switch__switch_mr_create_conflict.snap
index a491d74fb..223730a47 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_mr_create_conflict.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_mr_create_conflict.snap
@@ -38,6 +38,6 @@ exit_code: 1
----- stderr -----
◎ Fetching MR !101...
  Fix authentication bug in login flow (!101)
-  by @alice · opened · https://gitlab.com/owner/test-repo/-/merge_requests/101
+  by @alice · opened · feature-auth · https://gitlab.com/owner/test-repo/-/merge_requests/101
✗ Cannot create branch for mr:101 — MR already has branch feature-auth
↳ To switch to it: wt switch mr:101
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_mr_fork.snap b/tests/snapshots/integration__integration_tests__switch__switch_mr_fork.snap
index 12354bc19..49531193c 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_mr_fork.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_mr_fork.snap
@@ -37,7 +37,7 @@ exit_code: 0
----- stderr -----
◎ Fetching MR !42...
  Add feature fix for edge case (!42)
-  by @contributor · opened · https://gitlab.com/owner/test-repo/-/merge_requests/42
+  by @contributor · opened · contributor:feature-fix · https://gitlab.com/owner/test-repo/-/merge_requests/42
○ Push configured to fork: https://gitlab.com/contributor/test-repo.git
✓ Created branch feature-fix (tracking MR !42) and worktree @ _REPO_.feature-fix
▲ Cannot change directory — shell integration not installed
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_mr_fork_existing_branch_tracks_different.snap b/tests/snapshots/integration__integration_tests__switch__switch_mr_fork_existing_branch_tracks_different.snap
index ff7799e69..dc2b3f31c 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_mr_fork_existing_branch_tracks_different.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_mr_fork_existing_branch_tracks_different.snap
@@ -37,6 +37,6 @@ exit_code: 1
----- stderr -----
◎ Fetching MR !42...
  Add feature fix for edge case (!42)
-  by @contributor · opened · https://gitlab.com/owner/test-repo/-/merge_requests/42
+  by @contributor · opened · feature-fix · https://gitlab.com/owner/test-repo/-/merge_requests/42
✗ Branch feature-fix exists but doesn't track MR !42
↳ To free the name, run git branch -m -- feature-fix feature-fix-old
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_mr_fork_existing_branch_tracks_mr.snap b/tests/snapshots/integration__integration_tests__switch__switch_mr_fork_existing_branch_tracks_mr.snap
index 5c1573e4d..3fca8cbc9 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_mr_fork_existing_branch_tracks_mr.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_mr_fork_existing_branch_tracks_mr.snap
@@ -37,7 +37,7 @@ exit_code: 0
----- stderr -----
◎ Fetching MR !42...
  Add feature fix for edge case (!42)
-  by @contributor · opened · https://gitlab.com/owner/test-repo/-/merge_requests/42
+  by @contributor · opened · feature-fix · https://gitlab.com/owner/test-repo/-/merge_requests/42
○ Branch feature-fix already configured for MR !42
✓ Created worktree for feature-fix @ _REPO_.feature-fix
▲ Cannot change directory — shell integration not installed
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_mr_fork_existing_no_tracking.snap b/tests/snapshots/integration__integration_tests__switch__switch_mr_fork_existing_no_tracking.snap
index ff7799e69..dc2b3f31c 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_mr_fork_existing_no_tracking.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_mr_fork_existing_no_tracking.snap
@@ -37,6 +37,6 @@ exit_code: 1
----- stderr -----
◎ Fetching MR !42...
  Add feature fix for edge case (!42)
-  by @contributor · opened · https://gitlab.com/owner/test-repo/-/merge_requests/42
+  by @contributor · opened · feature-fix · https://gitlab.com/owner/test-repo/-/merge_requests/42
✗ Branch feature-fix exists but doesn't track MR !42
↳ To free the name, run git branch -m -- feature-fix feature-fix-old
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_mr_same_repo.snap b/tests/snapshots/integration__integration_tests__switch__switch_mr_same_repo.snap
index ceb53e4a3..afe3f08f2 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_mr_same_repo.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_mr_same_repo.snap
@@ -37,6 +37,6 @@ exit_code: 0
----- stderr -----
◎ Fetching MR !101...
  Fix authentication bug in login flow (!101)
-  by @alice · opened · https://gitlab.com/owner/test-repo/-/merge_requests/101
+  by @alice · opened · feature-auth · https://gitlab.com/owner/test-repo/-/merge_requests/101
▲ Worktree for feature-auth @ _REPO_.feature-auth, but cannot change directory — shell integration not installed
↳ To enable automatic cd, run wt config shell install
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_pr_create_conflict.snap b/tests/snapshots/integration__integration_tests__switch__switch_pr_create_conflict.snap
index 2352adc09..6910a34c6 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_pr_create_conflict.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_pr_create_conflict.snap
@@ -1,6 +1,5 @@
---
source: tests/integration_tests/switch.rs
-assertion_line: 1451
info:
program: wt
args:
@@ -39,6 +38,6 @@ exit_code: 1
----- stderr -----
◎ Fetching PR #101...
  Fix authentication bug in login flow (#101)
-  by @alice · open · https://github.com/owner/test-repo/pull/101
+  by @alice · open · feature-auth · https://github.com/owner/test-repo/pull/101
✗ Cannot create branch for pr:101 — PR already has branch feature-auth
↳ To switch to it: wt switch pr:101
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_pr_fork.snap b/tests/snapshots/integration__integration_tests__switch__switch_pr_fork.snap
index 30a112bc1..1945fa530 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_pr_fork.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_pr_fork.snap
@@ -37,7 +37,7 @@ exit_code: 0
----- stderr -----
◎ Fetching PR #42...
  Add feature fix for edge case (#42)
-  by @contributor · open · https://github.com/owner/test-repo/pull/42
+  by @contributor · open · contributor:feature-fix · https://github.com/owner/test-repo/pull/42
○ Push configured to fork: https://github.com/contributor/test-repo.git
✓ Created branch feature-fix (tracking PR #42) and worktree @ _REPO_.feature-fix
▲ Cannot change directory — shell integration not installed
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_pr_fork_existing_different_pr.snap b/tests/snapshots/integration__integration_tests__switch__switch_pr_fork_existing_different_pr.snap
index 798ded677..61ba992e6 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_pr_fork_existing_different_pr.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_pr_fork_existing_different_pr.snap
@@ -37,7 +37,7 @@ exit_code: 0
----- stderr -----
◎ Fetching PR #42...
  Add feature fix for edge case (#42)
-  by @contributor · open · https://github.com/owner/test-repo/pull/42
+  by @contributor · open · contributor:feature-fix · https://github.com/owner/test-repo/pull/42
▲ Using prefixed branch name contributor/feature-fix due to name conflict
↳ Push to fork is not supported with prefixed branches; feedback welcome at https://github.com/max-sixty/worktrunk/issues/714
✓ Created branch contributor/feature-fix (tracking PR #42) and worktree @ _REPO_.contributor-feature-fix
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_pr_fork_existing_no_tracking.snap b/tests/snapshots/integration__integration_tests__switch__switch_pr_fork_existing_no_tracking.snap
index 798ded677..61ba992e6 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_pr_fork_existing_no_tracking.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_pr_fork_existing_no_tracking.snap
@@ -37,7 +37,7 @@ exit_code: 0
----- stderr -----
◎ Fetching PR #42...
  Add feature fix for edge case (#42)
-  by @contributor · open · https://github.com/owner/test-repo/pull/42
+  by @contributor · open · contributor:feature-fix · https://github.com/owner/test-repo/pull/42
▲ Using prefixed branch name contributor/feature-fix due to name conflict
↳ Push to fork is not supported with prefixed branches; feedback welcome at https://github.com/max-sixty/worktrunk/issues/714
✓ Created branch contributor/feature-fix (tracking PR #42) and worktree @ _REPO_.contributor-feature-fix
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_pr_fork_existing_same_pr.snap b/tests/snapshots/integration__integration_tests__switch__switch_pr_fork_existing_same_pr.snap
index bd7588022..35b89eb24 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_pr_fork_existing_same_pr.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_pr_fork_existing_same_pr.snap
@@ -37,7 +37,7 @@ exit_code: 0
----- stderr -----
◎ Fetching PR #42...
  Add feature fix for edge case (#42)
-  by @contributor · open · https://github.com/owner/test-repo/pull/42
+  by @contributor · open · contributor:feature-fix · https://github.com/owner/test-repo/pull/42
○ Branch feature-fix already configured for PR #42
✓ Created worktree for feature-fix @ _REPO_.feature-fix
▲ Cannot change directory — shell integration not installed
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_pr_fork_no_upstream.snap b/tests/snapshots/integration__integration_tests__switch__switch_pr_fork_no_upstream.snap
index df718c37d..2dc180c83 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_pr_fork_no_upstream.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_pr_fork_no_upstream.snap
@@ -37,6 +37,6 @@ exit_code: 1
----- stderr -----
◎ Fetching PR #42...
  Add feature fix for edge case (#42)
-  by @contributor · open · https://github.com/owner/test-repo/pull/42
+  by @contributor · open · contributor:feature-fix · https://github.com/owner/test-repo/pull/42
✗ No remote found for owner/test-repo
↳ Add the remote: git remote add upstream https://github.com/owner/test-repo.git
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_pr_fork_prefixed_exists_different_pr.snap b/tests/snapshots/integration__integration_tests__switch__switch_pr_fork_prefixed_exists_different_pr.snap
index 889c12ebf..80eff1bb3 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_pr_fork_prefixed_exists_different_pr.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_pr_fork_prefixed_exists_different_pr.snap
@@ -37,6 +37,6 @@ exit_code: 1
----- stderr -----
◎ Fetching PR #42...
  Add feature fix for edge case (#42)
-  by @contributor · open · https://github.com/owner/test-repo/pull/42
+  by @contributor · open · contributor:feature-fix · https://github.com/owner/test-repo/pull/42
✗ Branch contributor/feature-fix exists but doesn't track PR #42
↳ To free the name, run git branch -m -- contributor/feature-fix contributor/feature-fix-old
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_pr_fork_prefixed_exists_same_pr.snap b/tests/snapshots/integration__integration_tests__switch__switch_pr_fork_prefixed_exists_same_pr.snap
index 21dfb4528..749fa4c68 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_pr_fork_prefixed_exists_same_pr.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_pr_fork_prefixed_exists_same_pr.snap
@@ -37,7 +37,7 @@ exit_code: 0
----- stderr -----
◎ Fetching PR #42...
  Add feature fix for edge case (#42)
-  by @contributor · open · https://github.com/owner/test-repo/pull/42
+  by @contributor · open · contributor:feature-fix · https://github.com/owner/test-repo/pull/42
○ Branch contributor/feature-fix already configured for PR #42
▲ Worktree for contributor/feature-fix @ _REPO_.contributor-feature-fix, but cannot change directory — shell integration not installed
↳ To enable automatic cd, run wt config shell install
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_pr_same_repo.snap b/tests/snapshots/integration__integration_tests__switch__switch_pr_same_repo.snap
index f8af0bdf4..dd423190e 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_pr_same_repo.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_pr_same_repo.snap
@@ -37,7 +37,7 @@ exit_code: 0
----- stderr -----
◎ Fetching PR #101...
  Fix authentication bug in login flow (#101)
-  by @alice · open · https://github.com/owner/test-repo/pull/101
+  by @alice · open · feature-auth · https://github.com/owner/test-repo/pull/101
◎ Fetching feature-auth from origin...
▲ Worktree for feature-auth @ _REPO_.feature-auth, but cannot change directory — shell integration not installed
↳ To enable automatic cd, run wt config shell install
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_pr_same_repo_no_remote.snap b/tests/snapshots/integration__integration_tests__switch__switch_pr_same_repo_no_remote.snap
index 63ec38371..5e8d79449 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_pr_same_repo_no_remote.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_pr_same_repo_no_remote.snap
@@ -37,6 +37,6 @@ exit_code: 1
----- stderr -----
◎ Fetching PR #101...
  Fix authentication bug in login flow (#101)
-  by @alice · open · https://github.com/owner/test-repo/pull/101
+  by @alice · open · feature-auth · https://github.com/owner/test-repo/pull/101
✗ No remote found for owner/test-repo
↳ Add the remote: git remote add upstream https://github.com/owner/test-repo.git