1
1
mirror of https://github.com/oxalica/nil.git synced 2024-11-22 02:55:39 +03:00

Only strip query parameters and fragment for flake ref URLs (#141)

This commit is contained in:
uncenter 2024-08-05 14:30:17 -04:00 committed by GitHub
parent 01a62046a7
commit ff9e2f0296
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -76,22 +76,15 @@ fn try_resolve_link_uri(uri: &str) -> Option<Url> {
// 1. Efficiency.
// 2. Less false-positives.
// 3. Reject not-really-absolute `mirror:` URIs.
const FILTERS: &[&str] = &[
// Common URLs.
"https:",
"http:",
"ftp:",
"file:",
// Flake-refs.
// https://nixos.org/manual/nix/unstable/command-ref/new-cli/nix3-flake.html#types
"path:",
"git+",
"tarball+",
"github:",
"sourcehut:",
];
if !FILTERS.iter().any(|prefix| uri.starts_with(prefix)) {
// https://nixos.org/manual/nix/unstable/command-ref/new-cli/nix3-flake.html#types
const FLAKE_REFS: &[&str] = &["path:", "git+", "tarball+", "github:", "sourcehut:"];
// Common URLs.
const FILTERS: &[&str] = &["https:", "http:", "ftp:", "file:"];
let is_flake_ref = FLAKE_REFS.iter().any(|prefix| uri.starts_with(prefix));
if !is_flake_ref && !FILTERS.iter().any(|prefix| uri.starts_with(prefix)) {
return None;
}
@ -132,8 +125,10 @@ fn try_resolve_link_uri(uri: &str) -> Option<Url> {
// Trim `?` and `#` parts, which are special for flake-ref.
// Not a part of original URI.
uri.set_query(None);
uri.set_fragment(None);
if is_flake_ref {
uri.set_query(None);
uri.set_fragment(None);
}
Some(uri)
}
@ -196,7 +191,7 @@ mod tests {
github:NixOS/nixpkgs -> https://github.com/NixOS/nixpkgs: https://github.com/NixOS/nixpkgs
"github:NixOS/nixpkgs/nixos-22.05" -> https://github.com/NixOS/nixpkgs: https://github.com/NixOS/nixpkgs
"github:NixOS/nixpkgs/pull/190594/head" -> https://github.com/NixOS/nixpkgs: https://github.com/NixOS/nixpkgs
"https://example.com?foo=1#bar" -> https://example.com/: https://example.com/
"https://example.com?foo=1#bar" -> https://example.com/?foo=1#bar: https://example.com/?foo=1#bar
"#]],
);
}