Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 67 additions & 88 deletions src/commands/worktree/switch.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/git/ci_platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::git::{GitRemoteUrl, RefType, Repository};
/// A known forge.
///
/// This is the canonical identity shared by configuration, remote-host
/// classification, remote-ref providers, and CI dispatch. Unknown hosts stay
/// classification, remote-ref dispatch, and CI dispatch. Unknown hosts stay
/// outside the enum as `None`; callers that expose an explicit `unknown` value
/// add it only at that output boundary.
#[derive(Debug, Clone, Copy, PartialEq, Eq, strum::Display, strum::EnumString)]
Expand Down
45 changes: 6 additions & 39 deletions src/git/remote_ref/azure.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,16 @@
//! Azure DevOps PR provider.
//!
//! Implements `RemoteRefProvider` for Azure DevOps Pull Requests using the `az` CLI.
//! Requires the `azure-devops` extension (`az extension add --name azure-devops`).
//! Azure DevOps PR backend using the `az` CLI. Requires the `azure-devops`
//! extension (`az extension add --name azure-devops`).

use anyhow::{Context, bail};
use serde::Deserialize;

use super::{CliApiRequest, PlatformData, RemoteRefInfo, RemoteRefProvider, cli_api_error};
use super::{CliApiRequest, PlatformData, RemoteRefInfo, cli_api_error};
use crate::git::canonical_url_path_segment;
use crate::git::ci_platform::host_is_within;
use crate::git::url::{GitRemoteUrl, authority_host};
use crate::git::{ForgeKind, Repository};
use crate::shell_exec::Cmd;

/// Azure DevOps Pull Request provider.
#[derive(Debug, Clone, Copy)]
pub struct AzureDevOpsProvider;

impl RemoteRefProvider for AzureDevOpsProvider {
fn forge_kind(&self) -> ForgeKind {
ForgeKind::AzureDevOps
}

fn fetch_info(&self, number: u32, repo: &Repository) -> anyhow::Result<RemoteRefInfo> {
fetch_pr_info(number, repo)
}

fn ref_path(&self, number: u32) -> String {
format!("pull/{}/head", number)
}
}

/// Construct an Azure DevOps remote URL for a repo.
///
/// Emits `https://{host}/{org}/{project}/_git/{repo}` for `dev.azure.com` and
Expand Down Expand Up @@ -230,7 +210,7 @@ pub fn azure_devops_extension_installed(repo_root: &std::path::Path) -> bool {
.is_none_or(|extensions| extensions.iter().any(|e| e.name == "azure-devops"))
}

fn fetch_pr_info(pr_number: u32, repo: &Repository) -> anyhow::Result<RemoteRefInfo> {
pub(super) fn fetch_pr_info(pr_number: u32, repo: &Repository) -> anyhow::Result<RemoteRefInfo> {
let repo_root = repo.repo_path()?;
let pr_id = pr_number.to_string();

Expand Down Expand Up @@ -297,8 +277,8 @@ fn fetch_pr_info(pr_number: u32, repo: &Repository) -> anyhow::Result<RemoteRefI
.unwrap_or(&response.source_ref_name)
.to_string();

// Validate at the provider boundary — same as the GitHub/GitLab/Gitea
// providers — so an empty or `refs/heads/`-only sourceRefName produces a
// Validate at the forge backend boundary — same as the GitHub/GitLab/Gitea
// backends — so an empty or `refs/heads/`-only sourceRefName produces a
// clear diagnostic rather than confusing downstream git/path errors.
if source_branch.is_empty() {
bail!(
Expand Down Expand Up @@ -358,19 +338,6 @@ fn fetch_pr_info(pr_number: u32, repo: &Repository) -> anyhow::Result<RemoteRefI
mod tests {
use super::*;

#[test]
fn test_ref_path() {
let provider = AzureDevOpsProvider;
assert_eq!(provider.ref_path(550), "pull/550/head");
assert_eq!(provider.tracking_ref(550), "refs/pull/550/head");
}

#[test]
fn test_ref_type() {
let provider = AzureDevOpsProvider;
assert_eq!(provider.ref_type(), crate::git::RefType::Pr);
}

#[test]
fn test_parse_web_url_dev_azure() {
let parsed = parse_web_url(Some("https://dev.azure.com/myorg/myproject/_git/myrepo"));
Expand Down
45 changes: 6 additions & 39 deletions src/git/remote_ref/gitea.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! Gitea PR provider.
//!
//! Implements `RemoteRefProvider` for Gitea Pull Requests using the `tea` CLI,
//! and hosts the `tea`-facing helpers other modules share: [`api_status`] (how
//! Gitea PR backend using the `tea` CLI. It also hosts the `tea`-facing helpers
//! other modules share: [`api_status`] (how
//! every caller separates a failed request from a resource, and decides whether
//! a retry could help), [`is_authed_for`], and [`has_any_login`] — read by the
//! switch dispatcher and the CI-status backend, so a change to one of them is
Expand Down Expand Up @@ -37,29 +35,11 @@ use anyhow::{Context, bail};
use serde::Deserialize;

use super::{
CliApiRequest, PlatformData, RemoteRefInfo, RemoteRefProvider, cli_api_error,
extract_host_from_html_url, run_cli_api,
CliApiRequest, PlatformData, RemoteRefInfo, cli_api_error, extract_host_from_html_url,
run_cli_api,
};
use crate::git::{ForgeKind, Repository};

/// Gitea Pull Request provider.
#[derive(Debug, Clone, Copy)]
pub struct GiteaProvider;

impl RemoteRefProvider for GiteaProvider {
fn forge_kind(&self) -> ForgeKind {
ForgeKind::Gitea
}

fn fetch_info(&self, number: u32, repo: &Repository) -> anyhow::Result<RemoteRefInfo> {
fetch_pr_info(number, repo)
}

fn ref_path(&self, number: u32) -> String {
format!("pull/{}/head", number)
}
}

/// Raw JSON response from `tea api repos/{owner}/{repo}/pulls/{number}`.
#[derive(Debug, Deserialize)]
struct TeaApiPrResponse {
Expand Down Expand Up @@ -150,7 +130,7 @@ struct TeaOwner {
}

/// Fetch PR information from Gitea using the `tea` CLI.
fn fetch_pr_info(pr_number: u32, repo: &Repository) -> anyhow::Result<RemoteRefInfo> {
pub(super) fn fetch_pr_info(pr_number: u32, repo: &Repository) -> anyhow::Result<RemoteRefInfo> {
let repo_root = repo.repo_path()?;

// Resolve owner/repo from the Gitea remote — which may be non-primary in
Expand Down Expand Up @@ -334,7 +314,7 @@ pub fn fork_remote_url(host: &str, owner: &str, repo: &str) -> String {

/// Whether `tea` has a login configured for `host`.
///
/// Used by the switch dispatcher to decide which provider to try when the
/// Used by the switch dispatcher to decide which forge CLI to try when the
/// remote URL doesn't unambiguously identify the forge. Reads tea's config
/// file directly — `$XDG_CONFIG_HOME/tea/config.yml` (default
/// `~/.config/tea/config.yml`) with legacy fallback `~/.tea/tea.yml` — and
Expand Down Expand Up @@ -416,19 +396,6 @@ fn read_tea_config() -> Option<String> {
mod tests {
use super::*;

#[test]
fn test_ref_path() {
let provider = GiteaProvider;
assert_eq!(provider.ref_path(7), "pull/7/head");
assert_eq!(provider.tracking_ref(7), "refs/pull/7/head");
}

#[test]
fn test_ref_type() {
let provider = GiteaProvider;
assert_eq!(provider.ref_type(), crate::git::RefType::Pr);
}

/// The status line is the discriminator, and it is the second token of the
/// first `HTTP/` line — reachable past whatever `tea` wrote before it, and
/// past a status with no reason phrase.
Expand Down
41 changes: 4 additions & 37 deletions src/git/remote_ref/github.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,17 @@
//! GitHub PR provider.
//!
//! Implements `RemoteRefProvider` for GitHub Pull Requests using the `gh` CLI.
//! GitHub PR backend using the `gh` CLI.

use std::path::Path;

use anyhow::{Context, bail};
use serde::Deserialize;

use super::{
CliApiRequest, PlatformData, RemoteRefInfo, RemoteRefProvider, cli_api_error, cli_config_value,
CliApiRequest, PlatformData, RemoteRefInfo, cli_api_error, cli_config_value,
extract_host_from_html_url, run_cli_api,
};
use crate::git::{ForgeKind, Repository};
use crate::shell_exec::Cmd;

/// GitHub Pull Request provider.
#[derive(Debug, Clone, Copy)]
pub struct GitHubProvider;

impl RemoteRefProvider for GitHubProvider {
fn forge_kind(&self) -> ForgeKind {
ForgeKind::GitHub
}

fn fetch_info(&self, number: u32, repo: &Repository) -> anyhow::Result<RemoteRefInfo> {
fetch_pr_info(number, repo)
}

fn ref_path(&self, number: u32) -> String {
format!("pull/{}/head", number)
}
}

/// Raw JSON response from `gh api repos/{owner}/{repo}/pulls/{number}`.
#[derive(Debug, Deserialize)]
struct GhApiPrResponse {
Expand Down Expand Up @@ -100,7 +80,7 @@ fn gh_default_repo(repo_root: &Path) -> Option<(String, String)> {
}

/// Fetch PR information from GitHub using the `gh` CLI.
fn fetch_pr_info(pr_number: u32, repo: &Repository) -> anyhow::Result<RemoteRefInfo> {
pub(super) fn fetch_pr_info(pr_number: u32, repo: &Repository) -> anyhow::Result<RemoteRefInfo> {
let repo_root = repo.repo_path()?;

// Determine which owner/repo to query. Prefer gh's default repo
Expand Down Expand Up @@ -239,7 +219,7 @@ fn use_ssh_protocol() -> bool {

/// Whether `gh` has an authentication token configured for `host`.
///
/// Used by the switch dispatcher to decide which provider to try when the
/// Used by the switch dispatcher to decide which forge CLI to try when the
/// remote URL doesn't unambiguously identify the forge (e.g. self-hosted on
/// `git.example.com`). `gh auth token --hostname <host>` reads from
/// `~/.config/gh/hosts.yml` and the OS keyring — no network. Returns `false`
Expand All @@ -266,19 +246,6 @@ pub fn fork_remote_url(host: &str, owner: &str, repo: &str) -> String {
mod tests {
use super::*;

#[test]
fn test_ref_path() {
let provider = GitHubProvider;
assert_eq!(provider.ref_path(123), "pull/123/head");
assert_eq!(provider.tracking_ref(123), "refs/pull/123/head");
}

#[test]
fn test_ref_type() {
let provider = GitHubProvider;
assert_eq!(provider.ref_type(), crate::git::RefType::Pr);
}

#[test]
fn test_fork_remote_url_formats() {
// Protocol depends on `gh config get git_protocol`, so just check format
Expand Down
42 changes: 4 additions & 38 deletions src/git/remote_ref/gitlab.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//! GitLab MR provider.
//!
//! Implements `RemoteRefProvider` for GitLab Merge Requests using the `glab` CLI.
//! GitLab MR backend using the `glab` CLI.
//!
//! # API Differences from GitHub
//!
Expand All @@ -27,30 +25,10 @@ use anyhow::{Context, bail};
use serde::Deserialize;

use super::{
CliApiRequest, PlatformData, RemoteRefInfo, RemoteRefProvider, cli_api_error, cli_config_value,
run_cli_api,
CliApiRequest, PlatformData, RemoteRefInfo, cli_api_error, cli_config_value, run_cli_api,
};
use crate::git::{ForgeKind, Repository};

/// GitLab Merge Request provider.
#[derive(Debug, Clone, Copy)]
pub struct GitLabProvider;

impl RemoteRefProvider for GitLabProvider {
fn forge_kind(&self) -> ForgeKind {
ForgeKind::GitLab
}

fn fetch_info(&self, number: u32, repo: &Repository) -> anyhow::Result<RemoteRefInfo> {
let repo_root = repo.repo_path()?;
fetch_mr_info(number, repo_root)
}

fn ref_path(&self, number: u32) -> String {
format!("merge-requests/{}/head", number)
}
}

/// Raw JSON response from `glab api projects/:id/merge_requests/<number>`.
#[derive(Debug, Deserialize)]
struct GlabMrResponse {
Expand All @@ -77,7 +55,8 @@ struct GlabProject {
}

/// Fetch MR information from GitLab using the `glab` CLI.
fn fetch_mr_info(mr_number: u32, repo_root: &Path) -> anyhow::Result<RemoteRefInfo> {
pub(super) fn fetch_mr_info(mr_number: u32, repo: &Repository) -> anyhow::Result<RemoteRefInfo> {
let repo_root = repo.repo_path()?;
let api_path = format!("projects/:id/merge_requests/{}", mr_number);
let args = ["api", api_path.as_str()];
let output = run_cli_api(CliApiRequest {
Expand Down Expand Up @@ -284,19 +263,6 @@ mod tests {
use super::*;
use crate::git::remote_ref::RemoteRefInfo;

#[test]
fn test_ref_path() {
let provider = GitLabProvider;
assert_eq!(provider.ref_path(42), "merge-requests/42/head");
assert_eq!(provider.tracking_ref(42), "refs/merge-requests/42/head");
}

#[test]
fn test_ref_type() {
let provider = GitLabProvider;
assert_eq!(provider.ref_type(), crate::git::RefType::Mr);
}

#[test]
fn test_fork_remote_url_formats() {
// Protocol depends on `glab config get git_protocol`, so just check format
Expand Down
Loading
Loading