Skip to content

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

feat(switch): accept forge PR/MR URLs anywhere `pr:N` / `mr:N` works (#2898)

Worktrunk Bot456c8506eaff
diff --git a/docs/content/switch.md b/docs/content/switch.md
index ca23eeb05..b7e73c053 100644
--- a/docs/content/switch.md
+++ b/docs/content/switch.md
@@ -52,7 +52,7 @@ If the branch already has a worktree, `wt switch` changes directories to it. Oth
{{ terminal(cmd="wt switch - # Back to previous|||wt switch ^ # Default branch worktree|||wt switch --create fix --base=@ # Branch from current HEAD|||wt switch --create fix --base=pr:123 # Branch from PR #123's head|||wt switch pr:123 # PR #123's branch|||wt switch mr:101 # MR !101's branch") }}
-Shortcuts also apply to `--base`. For a fork PR/MR, the head commit is fetched and used as the base SHA without creating a tracking branch.
+Shortcuts also apply to `--base`. For a fork PR/MR, the head commit is fetched and used as the base SHA without creating a tracking branch. (Web URLs like `https://github.com/owner/repo/pull/N` or `https://gitlab.com/owner/repo/-/merge_requests/N` work in place of `pr:N` / `mr:N` anywhere a shortcut does.)
## Interactive picker
diff --git a/skills/worktrunk/reference/switch.md b/skills/worktrunk/reference/switch.md
index 7645c6709..190c70337 100644
--- a/skills/worktrunk/reference/switch.md
+++ b/skills/worktrunk/reference/switch.md
@@ -54,7 +54,7 @@ $ wt switch pr:123 # PR #123's branch
$ wt switch mr:101 # MR !101's branch
```
-Shortcuts also apply to `--base`. For a fork PR/MR, the head commit is fetched and used as the base SHA without creating a tracking branch.
+Shortcuts also apply to `--base`. For a fork PR/MR, the head commit is fetched and used as the base SHA without creating a tracking branch. (Web URLs like `https://github.com/owner/repo/pull/N` or `https://gitlab.com/owner/repo/-/merge_requests/N` work in place of `pr:N` / `mr:N` anywhere a shortcut does.)
## Interactive picker
diff --git a/src/cli/mod.rs b/src/cli/mod.rs
index 4d0b9449f..5c0b48ff3 100644
--- a/src/cli/mod.rs
+++ b/src/cli/mod.rs
@@ -602,7 +602,7 @@ $ wt switch pr:123 # PR #123's branch
$ wt switch mr:101 # MR !101's branch
```
-Shortcuts also apply to `--base`. For a fork PR/MR, the head commit is fetched and used as the base SHA without creating a tracking branch.
+Shortcuts also apply to `--base`. For a fork PR/MR, the head commit is fetched and used as the base SHA without creating a tracking branch. (Web URLs like `https://github.com/owner/repo/pull/N` or `https://gitlab.com/owner/repo/-/merge_requests/N` work in place of `pr:N` / `mr:N` anywhere a shortcut does.)
## Interactive picker
diff --git a/src/commands/worktree/switch.rs b/src/commands/worktree/switch.rs
index c3befc972..85542de6f 100644
--- a/src/commands/worktree/switch.rs
+++ b/src/commands/worktree/switch.rs
@@ -18,7 +18,7 @@ use worktrunk::config::{
};
use worktrunk::git::remote_ref::{
self, AzureDevOpsProvider, GitHubProvider, GitLabProvider, GiteaProvider, RemoteRefInfo,
- RemoteRefProvider,
+ RemoteRefProvider, parse_ref_url,
};
use worktrunk::git::{
GitError, GitRemoteUrl, RefContext, RefType, Repository, SwitchSuggestionCtx,
@@ -449,6 +449,12 @@ fn resolve_base_ref(
repo: &Repository,
base: &str,
) -> anyhow::Result<(String, Option<(String, String)>)> {
+ // Forge PR/MR web URLs (e.g. `https://github.com/owner/repo/pull/123`)
+ // normalise into the literal `pr:N` / `mr:N` shortcut so the parsing
+ // below handles both forms identically — no duplicate dispatch.
+ let normalised = parse_ref_url(base);
+ let base = normalised.as_deref().unwrap_or(base);
+
if let Some(suffix) = base.strip_prefix("pr:")
&& let Ok(number) = suffix.parse::<u32>()
{
@@ -527,6 +533,12 @@ fn resolve_switch_target(
create: bool,
base: Option<&str>,
) -> anyhow::Result<ResolvedTarget> {
+ // Forge PR/MR web URLs (e.g. `https://github.com/owner/repo/pull/123`)
+ // normalise into the literal `pr:N` / `mr:N` shortcut so the parsing
+ // below handles both forms identically — no duplicate dispatch.
+ let normalised = parse_ref_url(branch);
+ let branch = normalised.as_deref().unwrap_or(branch);
+
// Handle pr:<number> syntax — dispatches to GitHub, Gitea, or Azure DevOps based on remotes.
if let Some(suffix) = branch.strip_prefix("pr:")
&& let Ok(number) = suffix.parse::<u32>()
diff --git a/src/git/remote_ref/mod.rs b/src/git/remote_ref/mod.rs
index 8cc6b11f1..c3783b4b0 100644
--- a/src/git/remote_ref/mod.rs
+++ b/src/git/remote_ref/mod.rs
@@ -271,6 +271,56 @@ pub fn local_branch_name(info: &RemoteRefInfo) -> String {
info.source_branch.clone()
}
+/// Parse a forge PR/MR web URL into the equivalent `pr:N` / `mr:N` shortcut
+/// string.
+///
+/// Recognises the canonical PR/MR URLs across all four supported forges
+/// (GitHub including Enterprise, GitLab, Gitea, Azure DevOps). Callers
+/// substitute the returned shortcut for the original input, so URL handling
+/// flows through the same `pr:` / `mr:` parsing path as a literal shortcut —
+/// no duplicate dispatch.
+///
+/// Detection is shape-based, not host-based: the URL must use `http(s)://`
+/// and contain `/pull/N`, `/pulls/N`, `/-/merge_requests/N`, or
+/// `/pullrequest/N` in its path. Trailing path segments (e.g. `/files`,
+/// `/commits`), query strings, and fragments are ignored. Host is not
+/// inspected so self-hosted GitHub Enterprise / Gitea / GitLab instances
+/// work without a hostname allow-list.
+pub fn parse_ref_url(input: &str) -> Option<String> {
+ let trimmed = input.trim();
+ let rest = trimmed
+ .strip_prefix("https://")
+ .or_else(|| trimmed.strip_prefix("http://"))?;
+
+ // Drop query/fragment so trailing `?foo=bar` / `#discussion_r...` don't
+ // break the segment match.
+ let path = rest.split(['?', '#']).next()?;
+ let segments: Vec<&str> = path.split('/').filter(|s| !s.is_empty()).collect();
+
+ // Minimum shape: host / owner / repo / kind / N (5 segments). This
+ // rejects "pull/123" as a literal branch name (no protocol prefix passes
+ // the strip above already, but the length check covers e.g.
+ // `https://example.com/pull/1` which is too shallow to be a real URL).
+ if segments.len() < 5 {
+ return None;
+ }
+
+ for pair in segments.windows(2) {
+ let Ok(number) = pair[1].parse::<u32>() else {
+ continue;
+ };
+ match pair[0] {
+ // GitHub `pull`, Gitea `pulls`, Azure DevOps `pullrequest`.
+ "pull" | "pulls" | "pullrequest" => return Some(format!("pr:{number}")),
+ // GitLab `merge_requests` (always preceded by `/-/`).
+ "merge_requests" => return Some(format!("mr:{number}")),
+ _ => {}
+ }
+ }
+
+ None
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -289,4 +339,88 @@ mod tests {
assert_eq!(gl.ref_path(42), "merge-requests/42/head");
assert_eq!(gl.tracking_ref(42), "refs/merge-requests/42/head");
}
+
+ #[test]
+ fn parse_ref_url_github() {
+ assert_eq!(
+ parse_ref_url("https://github.com/owner/repo/pull/123").as_deref(),
+ Some("pr:123")
+ );
+ // GitHub Enterprise host.
+ assert_eq!(
+ parse_ref_url("https://github.acme.com/team/repo/pull/9").as_deref(),
+ Some("pr:9")
+ );
+ // Trailing segments (e.g. /files, /commits) and fragments.
+ assert_eq!(
+ parse_ref_url("https://github.com/owner/repo/pull/2895/files").as_deref(),
+ Some("pr:2895")
+ );
+ assert_eq!(
+ parse_ref_url("https://github.com/owner/repo/pull/77#discussion_r1").as_deref(),
+ Some("pr:77")
+ );
+ // http:// is accepted too.
+ assert_eq!(
+ parse_ref_url("http://github.com/owner/repo/pull/1").as_deref(),
+ Some("pr:1")
+ );
+ }
+
+ #[test]
+ fn parse_ref_url_gitlab() {
+ assert_eq!(
+ parse_ref_url("https://gitlab.com/group/repo/-/merge_requests/42").as_deref(),
+ Some("mr:42")
+ );
+ // Nested subgroups.
+ assert_eq!(
+ parse_ref_url("https://gitlab.com/group/sub/repo/-/merge_requests/7").as_deref(),
+ Some("mr:7")
+ );
+ // Self-hosted GitLab with trailing diff path.
+ assert_eq!(
+ parse_ref_url("https://gitlab.example.com/team/repo/-/merge_requests/12/diffs")
+ .as_deref(),
+ Some("mr:12")
+ );
+ }
+
+ #[test]
+ fn parse_ref_url_gitea() {
+ // Gitea / Codeberg use /pulls/N rather than GitHub's /pull/N.
+ assert_eq!(
+ parse_ref_url("https://codeberg.org/owner/repo/pulls/55").as_deref(),
+ Some("pr:55")
+ );
+ assert_eq!(
+ parse_ref_url("https://gitea.example.com/team/repo/pulls/3").as_deref(),
+ Some("pr:3")
+ );
+ }
+
+ #[test]
+ fn parse_ref_url_azure_devops() {
+ assert_eq!(
+ parse_ref_url("https://dev.azure.com/org/project/_git/repo/pullrequest/9").as_deref(),
+ Some("pr:9")
+ );
+ }
+
+ #[test]
+ fn parse_ref_url_rejects_non_urls() {
+ // Plain branch names — no protocol prefix.
+ assert_eq!(parse_ref_url("pr:123"), None);
+ assert_eq!(parse_ref_url("feature/pull/7"), None);
+ assert_eq!(parse_ref_url("pull/123"), None);
+ // Too shallow even with a protocol prefix.
+ assert_eq!(parse_ref_url("https://example.com/pull/1"), None);
+ // Wrong kind segment.
+ assert_eq!(parse_ref_url("https://github.com/o/r/issues/5"), None);
+ // Non-numeric tail (e.g. PR creation URL).
+ assert_eq!(parse_ref_url("https://github.com/o/r/pull/new"), None);
+ // Empty/whitespace.
+ assert_eq!(parse_ref_url(""), None);
+ assert_eq!(parse_ref_url(" "), None);
+ }
}
diff --git a/tests/snapshots/integration__integration_tests__help__help_switch_long.snap b/tests/snapshots/integration__integration_tests__help__help_switch_long.snap
index 1a6722dc0..cc884f5c7 100644
--- a/tests/snapshots/integration__integration_tests__help__help_switch_long.snap
+++ b/tests/snapshots/integration__integration_tests__help__help_switch_long.snap
@@ -168,7 +168,7 @@ If the branch already has a worktree, wt switch changes directories to i
  wt switch pr:123 # PR #123's branch
  wt switch mr:101 # MR !101's branch
-Shortcuts also apply to --base. For a fork PR/MR, the head commit is fetched and used as the base SHA without creating a tracking branch.
+Shortcuts also apply to --base. For a fork PR/MR, the head commit is fetched and used as the base SHA without creating a tracking branch. (Web URLs like https://github.com/owner/repo/pull/N or https://gitlab.com/owner/repo/-/merge_requests/N work in place of pr:N / mr:N anywhere a shortcut does.)
Interactive picker