|
1 | 1 | #[cfg(any(feature = "blocking-client", feature = "async-client"))] |
2 | 2 | mod refs_impl { |
| 3 | + use super::by_name_or_url; |
| 4 | + use crate::OutputFormat; |
3 | 5 | use anyhow::bail; |
4 | 6 | use git_repository as git; |
5 | 7 | use git_repository::{ |
6 | 8 | protocol::fetch, |
7 | 9 | refspec::{match_group::validate::Fix, RefSpec}, |
8 | 10 | }; |
9 | 11 |
|
10 | | - use crate::OutputFormat; |
11 | | - |
12 | 12 | pub mod refs { |
13 | 13 | use git_repository::bstr::BString; |
14 | 14 |
|
@@ -46,15 +46,7 @@ mod refs_impl { |
46 | 46 | }: refs::Context, |
47 | 47 | ) -> anyhow::Result<()> { |
48 | 48 | use anyhow::Context; |
49 | | - let mut remote = match (name, url) { |
50 | | - (Some(name), None) => repo.find_remote(&name)?, |
51 | | - (None, None) => repo |
52 | | - .head()? |
53 | | - .into_remote(git::remote::Direction::Fetch) |
54 | | - .context("Cannot find a remote for unborn branch")??, |
55 | | - (None, Some(url)) => repo.remote_at(url)?, |
56 | | - (Some(_), Some(_)) => bail!("Must not set both the remote name and the url - they are mutually exclusive"), |
57 | | - }; |
| 49 | + let mut remote = by_name_or_url(&repo, name.as_deref(), url)?; |
58 | 50 | if let refs::Kind::Tracking { ref_specs, .. } = &kind { |
59 | 51 | if format != OutputFormat::Human { |
60 | 52 | bail!("JSON output isn't yet supported for listing ref-mappings."); |
@@ -258,3 +250,22 @@ mod refs_impl { |
258 | 250 | } |
259 | 251 | #[cfg(any(feature = "blocking-client", feature = "async-client"))] |
260 | 252 | pub use refs_impl::{refs, refs_fn as refs, JsonRef}; |
| 253 | + |
| 254 | +use git_repository as git; |
| 255 | + |
| 256 | +pub(crate) fn by_name_or_url<'repo>( |
| 257 | + repo: &'repo git::Repository, |
| 258 | + name: Option<&str>, |
| 259 | + url: Option<git::Url>, |
| 260 | +) -> anyhow::Result<git::Remote<'repo>> { |
| 261 | + use anyhow::{bail, Context}; |
| 262 | + Ok(match (name, url) { |
| 263 | + (Some(name), None) => repo.find_remote(&name)?, |
| 264 | + (None, None) => repo |
| 265 | + .head()? |
| 266 | + .into_remote(git::remote::Direction::Fetch) |
| 267 | + .context("Cannot find a remote for unborn branch")??, |
| 268 | + (None, Some(url)) => repo.remote_at(url)?, |
| 269 | + (Some(_), Some(_)) => bail!("Must not set both the remote name and the url - they are mutually exclusive"), |
| 270 | + }) |
| 271 | +} |
0 commit comments