Skip to content

Commit 51f998f

Browse files
authored
Merge pull request #2173 from metlos/remote-with-url
feat: ability to change the fetch url of a remote
2 parents 81c0c16 + 620d275 commit 51f998f

File tree

3 files changed

+108
-4
lines changed

3 files changed

+108
-4
lines changed

gix/src/remote/build.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,47 @@ use crate::{bstr::BStr, remote, Remote};
22

33
/// Builder methods
44
impl Remote<'_> {
5+
/// Override the `url` to be used when fetching data from a remote.
6+
///
7+
/// Note that this URL is typically set during instantiation with [`crate::Repository::remote_at()`].
8+
pub fn with_url<Url, E>(self, url: Url) -> Result<Self, remote::init::Error>
9+
where
10+
Url: TryInto<gix_url::Url, Error = E>,
11+
gix_url::parse::Error: From<E>,
12+
{
13+
self.url_inner(
14+
url.try_into().map_err(|err| remote::init::Error::Url(err.into()))?,
15+
true,
16+
)
17+
}
18+
19+
/// Set the `url` to be used when fetching data from a remote, without applying rewrite rules in case these could be faulty,
20+
/// eliminating one failure mode.
21+
///
22+
/// Note that this URL is typically set during instantiation with [`crate::Repository::remote_at_without_url_rewrite()`].
23+
pub fn with_url_without_url_rewrite<Url, E>(self, url: Url) -> Result<Self, remote::init::Error>
24+
where
25+
Url: TryInto<gix_url::Url, Error = E>,
26+
gix_url::parse::Error: From<E>,
27+
{
28+
self.url_inner(
29+
url.try_into().map_err(|err| remote::init::Error::Url(err.into()))?,
30+
false,
31+
)
32+
}
33+
534
/// Set the `url` to be used when pushing data to a remote.
35+
#[deprecated = "Use `with_push_url()` instead"]
636
pub fn push_url<Url, E>(self, url: Url) -> Result<Self, remote::init::Error>
37+
where
38+
Url: TryInto<gix_url::Url, Error = E>,
39+
gix_url::parse::Error: From<E>,
40+
{
41+
self.with_push_url(url)
42+
}
43+
44+
/// Set the `url` to be used when pushing data to a remote.
45+
pub fn with_push_url<Url, E>(self, url: Url) -> Result<Self, remote::init::Error>
746
where
847
Url: TryInto<gix_url::Url, Error = E>,
948
gix_url::parse::Error: From<E>,
@@ -16,7 +55,18 @@ impl Remote<'_> {
1655

1756
/// Set the `url` to be used when pushing data to a remote, without applying rewrite rules in case these could be faulty,
1857
/// eliminating one failure mode.
58+
#[deprecated = "Use `with_push_url_without_rewrite()` instead"]
1959
pub fn push_url_without_url_rewrite<Url, E>(self, url: Url) -> Result<Self, remote::init::Error>
60+
where
61+
Url: TryInto<gix_url::Url, Error = E>,
62+
gix_url::parse::Error: From<E>,
63+
{
64+
self.with_push_url_without_url_rewrite(url)
65+
}
66+
67+
/// Set the `url` to be used when pushing data to a remote, without applying rewrite rules in case these could be faulty,
68+
/// eliminating one failure mode.
69+
pub fn with_push_url_without_url_rewrite<Url, E>(self, url: Url) -> Result<Self, remote::init::Error>
2070
where
2171
Url: TryInto<gix_url::Url, Error = E>,
2272
gix_url::parse::Error: From<E>,
@@ -50,6 +100,19 @@ impl Remote<'_> {
50100
Ok(self)
51101
}
52102

103+
fn url_inner(mut self, url: gix_url::Url, should_rewrite_urls: bool) -> Result<Self, remote::init::Error> {
104+
self.url = url.into();
105+
106+
let (fetch_url_alias, _) = if should_rewrite_urls {
107+
remote::init::rewrite_urls(&self.repo.config, self.url.as_ref(), None)
108+
} else {
109+
Ok((None, None))
110+
}?;
111+
self.url_alias = fetch_url_alias;
112+
113+
Ok(self)
114+
}
115+
53116
/// Add `specs` as refspecs for `direction` to our list if they are unique, or ignore them otherwise.
54117
pub fn with_refspecs<Spec>(
55118
mut self,

gix/tests/gix/remote/save.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ mod save_as_to {
6363
let repo = basic_repo()?;
6464
let mut remote = repo
6565
.remote_at("https://example.com/path")?
66-
.push_url("https://ein.hub/path")?
66+
.with_push_url("https://ein.hub/path")?
6767
.with_fetch_tags(gix::remote::fetch::Tags::All)
6868
.with_refspecs(
6969
[

gix/tests/gix/repository/remote.rs

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@ mod remote_at {
1313
assert_eq!(remote.url(Direction::Fetch).unwrap().to_bstring(), fetch_url);
1414
assert_eq!(remote.url(Direction::Push).unwrap().to_bstring(), fetch_url);
1515

16-
let mut remote = remote.push_url("[email protected]:./relative")?;
16+
let mut remote = remote.with_push_url("[email protected]:./relative")?;
1717
assert_eq!(
1818
remote.url(Direction::Push).unwrap().to_bstring(),
1919
"[email protected]:./relative"
2020
);
2121
assert_eq!(remote.url(Direction::Fetch).unwrap().to_bstring(), fetch_url);
2222

23+
let new_fetch_url = "https://host.xz/byron/gitoxide";
24+
remote = remote.with_url(new_fetch_url)?;
25+
assert_eq!(remote.url(Direction::Fetch).unwrap().to_bstring(), new_fetch_url);
26+
2327
for (spec, direction) in [
2428
("refs/heads/push", Direction::Push),
2529
("refs/heads/fetch", Direction::Fetch),
@@ -57,9 +61,21 @@ mod remote_at {
5761
"push is the same as fetch was rewritten"
5862
);
5963

64+
let remote = remote.with_url("https://github.com/foobar/gitoxide")?;
65+
assert_eq!(
66+
remote.url(Direction::Fetch).unwrap().to_bstring(),
67+
rewritten_fetch_url,
68+
"fetch was rewritten"
69+
);
70+
assert_eq!(
71+
remote.url(Direction::Push).unwrap().to_bstring(),
72+
rewritten_fetch_url,
73+
"push is the same as fetch was rewritten"
74+
);
75+
6076
let remote = repo
6177
.remote_at("https://github.com/foobar/gitoxide".to_owned())?
62-
.push_url("file://dev/null".to_owned())?;
78+
.with_push_url("file://dev/null".to_owned())?;
6379
assert_eq!(remote.url(Direction::Fetch).unwrap().to_bstring(), rewritten_fetch_url);
6480
assert_eq!(
6581
remote.url(Direction::Push).unwrap().to_bstring(),
@@ -87,15 +103,40 @@ mod remote_at {
87103
"push is the same as fetch was rewritten"
88104
);
89105

106+
let remote = remote.with_url_without_url_rewrite("https://github.com/foobaz/gitoxide")?;
107+
assert_eq!(
108+
remote.url(Direction::Fetch).unwrap().to_bstring(),
109+
"https://github.com/foobaz/gitoxide",
110+
"fetch was rewritten"
111+
);
112+
assert_eq!(
113+
remote.url(Direction::Push).unwrap().to_bstring(),
114+
"https://github.com/foobaz/gitoxide",
115+
"push is the same as fetch was rewritten"
116+
);
117+
90118
let remote = repo
91119
.remote_at_without_url_rewrite("https://github.com/foobar/gitoxide".to_owned())?
92-
.push_url_without_url_rewrite("file://dev/null".to_owned())?;
120+
.with_push_url_without_url_rewrite("file://dev/null".to_owned())?;
93121
assert_eq!(remote.url(Direction::Fetch).unwrap().to_bstring(), fetch_url);
94122
assert_eq!(
95123
remote.url(Direction::Push).unwrap().to_bstring(),
96124
"file://dev/null",
97125
"push-url rewrite rules are not applied"
98126
);
127+
128+
let remote = remote
129+
.with_url_without_url_rewrite("https://github.com/foobaz/gitoxide".to_owned())?
130+
.with_push_url_without_url_rewrite("file://dev/null".to_owned())?;
131+
assert_eq!(
132+
remote.url(Direction::Fetch).unwrap().to_bstring(),
133+
"https://github.com/foobaz/gitoxide"
134+
);
135+
assert_eq!(
136+
remote.url(Direction::Push).unwrap().to_bstring(),
137+
"file://dev/null",
138+
"push-url rewrite rules are not applied"
139+
);
99140
Ok(())
100141
}
101142
}

0 commit comments

Comments
 (0)