Skip to content

Commit 3b44a46

Browse files
authored
Normalize source URLs (#274)
* Normalize source URLs * Fix URL normalisation
1 parent 33a4a8c commit 3b44a46

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

src/sources/cfg.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::OrgType;
1+
use super::{normalize_url, OrgType};
22
use crate::{cfg, diag::FileId, LintLevel, Spanned};
33
use serde::Deserialize;
44

@@ -113,7 +113,8 @@ impl cfg::UnvalidatedConfig for Config {
113113
.chain(self.allow_git.into_iter())
114114
{
115115
match url::Url::parse(aurl.as_ref()) {
116-
Ok(url) => {
116+
Ok(mut url) => {
117+
normalize_url(&mut url);
117118
allowed_sources.push(UrlSpan {
118119
value: url,
119120
span: aurl.span,

src/sources/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::{
55
diag::{Check, Diagnostic, Label, Pack, Severity},
66
LintLevel,
77
};
8+
use url::Url;
89

910
pub fn check(ctx: crate::CheckCtx<'_, ValidConfig>, sender: crossbeam::channel::Sender<Pack>) {
1011
use bitvec::prelude::*;
@@ -34,6 +35,7 @@ pub fn check(ctx: crate::CheckCtx<'_, ValidConfig>, sender: crossbeam::channel::
3435
let mut url = source.url().clone();
3536
url.set_query(None);
3637
url.set_fragment(None);
38+
normalize_url(&mut url);
3739
url
3840
};
3941

@@ -219,6 +221,23 @@ pub fn check(ctx: crate::CheckCtx<'_, ValidConfig>, sender: crossbeam::channel::
219221
}
220222
}
221223

224+
fn normalize_url(url: &mut Url) {
225+
// Normalizes the URL so that different represatations can be compared to each other.
226+
// At the moment we just remove a tailing `.git` but there are more possible optimisations.
227+
// See https://github.com/rust-lang/cargo/blob/1f6c6bd5e7bbdf596f7e88e6db347af5268ab113/src/cargo/util/canonical_url.rs#L31-L57
228+
// for what cargo does
229+
230+
let git_extension = ".git";
231+
let needs_chopping = url.path().ends_with(&git_extension);
232+
if needs_chopping {
233+
let last = {
234+
let last = url.path_segments().unwrap().last().unwrap();
235+
last[..last.len() - git_extension.len()].to_owned()
236+
};
237+
url.path_segments_mut().unwrap().pop().push(&last);
238+
}
239+
}
240+
222241
#[derive(PartialEq, Debug)]
223242
pub enum OrgType {
224243
Github,

tests/cfg/sources.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ allow-registry = [
66
"https://sekretz.com/registry/index",
77
]
88
allow-git = [
9-
"https://notgithub.com/orgname/reponame",
9+
"https://notgithub.com/orgname/reponame.git",
1010
]
1111
[sources.allow-org]
1212
github = [

0 commit comments

Comments
 (0)