Skip to content

Commit 17d93a0

Browse files
committed
more faithfully parse http.followRedirect
1 parent ee8ef45 commit 17d93a0

File tree

4 files changed

+42
-24
lines changed

4 files changed

+42
-24
lines changed

git-repository/src/repository/config/transport.rs

+20-19
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,7 @@ impl crate::Repository {
4848
use git_transport::client::http;
4949
use git_transport::client::http::options::ProxyAuthMethod;
5050

51-
use crate::{
52-
bstr::ByteVec,
53-
config::cache::util::{ApplyLeniency, ApplyLeniencyDefault},
54-
};
51+
use crate::{bstr::ByteVec, config::cache::util::ApplyLeniency};
5552
fn try_cow_to_string(
5653
v: Cow<'_, BStr>,
5754
lenient: bool,
@@ -185,24 +182,28 @@ impl crate::Repository {
185182
headers
186183
};
187184

188-
if let Some(follow_redirects) =
189-
config.string_filter("http", None, "followRedirects", &mut trusted_only)
185+
let redirects_key = "http.followRedirects";
186+
opts.follow_redirects = if config
187+
.string_filter_by_key(redirects_key, &mut trusted_only)
188+
.map_or(false, |v| v.as_ref() == "initial")
190189
{
191-
opts.follow_redirects = if follow_redirects.as_ref() == "initial" {
192-
http::options::FollowRedirects::Initial
193-
} else if git_config::Boolean::try_from(follow_redirects)
194-
.map_err(|err| crate::config::transport::Error::ConfigValue {
190+
http::options::FollowRedirects::Initial
191+
} else if let Some(val) = config
192+
.boolean_filter_by_key(redirects_key, &mut trusted_only)
193+
.map(|res| {
194+
res.map_err(|err| crate::config::transport::Error::ConfigValue {
195195
source: err,
196-
key: "http.followRedirects",
196+
key: redirects_key,
197197
})
198-
.with_lenient_default(lenient)?
199-
.0
200-
{
201-
http::options::FollowRedirects::All
202-
} else {
203-
http::options::FollowRedirects::None
204-
};
205-
}
198+
})
199+
.transpose()
200+
.with_leniency(lenient)?
201+
{
202+
val.then(|| http::options::FollowRedirects::All)
203+
.unwrap_or(http::options::FollowRedirects::None)
204+
} else {
205+
http::options::FollowRedirects::Initial
206+
};
206207

207208
opts.low_speed_time_seconds =
208209
integer(config, lenient, "http.lowSpeedTime", "u64", trusted_only, 0)?;
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:83f5270e487f0364e4a63f82cf4bfb5cb302bee744e9d3df521b7b3f850f3046
3-
size 10772
2+
oid sha256:3687c843a39942197ab8a31896b3a6c453295de7bb8d031642eb34b292a272da
3+
size 10816

git-repository/tests/fixtures/make_config_repos.sh

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ git init http-config
66
git config --add http.extraHeader ""
77
git config --add http.extraHeader "ExtraHeader: value2"
88
git config --add http.extraHeader "ExtraHeader: value3"
9-
git config http.followRedirects initial
9+
git config http.followRedirects true
1010
git config http.lowSpeedLimit 5k
1111
git config http.lowSpeedTime 10
1212
git config http.postBuffer 8k
@@ -19,6 +19,8 @@ git init http-config
1919
git clone --shared http-config http-remote-override
2020
(cd http-remote-override
2121

22+
git config http.followRedirects initial
23+
2224
git config http.proxy http://localhost:9090
2325
git config http.proxyAuthMethod basic
2426

@@ -28,6 +30,8 @@ git clone --shared http-config http-remote-override
2830

2931
git init http-proxy-empty
3032
(cd http-proxy-empty
33+
git config http.followRedirects false
34+
3135
git config http.proxy localhost:9090
3236
git config --add http.proxy "" # a value override disabling it later
3337
)
@@ -40,4 +44,8 @@ git init http-proxy-auto-prefix
4044
git init http-proxy-authenticated
4145
(cd http-proxy-authenticated
4246
git config http.proxy user@localhost:9090
47+
cat <<EOF >> .git/config
48+
[http]
49+
followRedirects
50+
EOF
4351
)

git-repository/tests/repository/config/transport_options.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ mod http {
2727
let git_transport::client::http::Options {
2828
proxy,
2929
proxy_auth_method,
30+
follow_redirects,
3031
..
3132
} = http_options(&repo, Some("origin"));
3233

3334
assert_eq!(proxy_auth_method, ProxyAuthMethod::Negotiate);
3435
assert_eq!(proxy.as_deref(), Some("http://overridden"));
36+
assert_eq!(follow_redirects, FollowRedirects::Initial);
3537
}
3638

3739
#[test]
@@ -54,7 +56,7 @@ mod http {
5456
&["ExtraHeader: value2", "ExtraHeader: value3"],
5557
"it respects empty values to clear prior values"
5658
);
57-
assert_eq!(follow_redirects, FollowRedirects::Initial);
59+
assert_eq!(follow_redirects, FollowRedirects::All);
5860
assert_eq!(low_speed_limit_bytes_per_second, 5120);
5961
assert_eq!(low_speed_time_seconds, 10);
6062
assert_eq!(proxy.as_deref(), Some("http://localhost:9090"),);
@@ -84,7 +86,12 @@ mod http {
8486
assert!(
8587
opts.proxy_authenticate.is_some(),
8688
"…and credential-helpers are used to do that. This could be overridden in remotes one day"
87-
)
89+
);
90+
assert_eq!(
91+
opts.follow_redirects,
92+
FollowRedirects::All,
93+
"an empty value is true, so we can't take shortcuts for these"
94+
);
8895
}
8996

9097
#[test]
@@ -97,6 +104,7 @@ mod http {
97104
Some(""),
98105
"empty strings indicate that the proxy is to be unset by the transport"
99106
);
107+
assert_eq!(opts.follow_redirects, FollowRedirects::None);
100108
}
101109

102110
#[test]
@@ -105,5 +113,6 @@ mod http {
105113

106114
let opts = http_options(&repo, None);
107115
assert_eq!(opts.proxy.as_deref(), Some("http://localhost:9090"));
116+
assert_eq!(opts.follow_redirects, FollowRedirects::Initial);
108117
}
109118
}

0 commit comments

Comments
 (0)