git: allow fetching tags without prefixing refs/tags/

work around for https://github.com/NixOS/nix/issues/5291
This commit is contained in:
figsoda 2023-01-29 22:50:09 -05:00
parent d333520948
commit 2b47f4624a
6 changed files with 36 additions and 15 deletions

View File

@ -1,8 +1,6 @@
use crate::{
impl_fetcher,
simple::{SimpleFetcher, SimpleFlakeFetcher},
GitScheme, Url,
};
use anyhow::Result;
use crate::{impl_fetcher, prefetch::git_prefetch, simple::SimpleFetcher, GitScheme, Url};
pub struct Fetchgit(pub GitScheme);
impl_fetcher!(Fetchgit);
@ -20,13 +18,18 @@ impl<'a> SimpleFetcher<'a, 1> for Fetchgit {
}
}
impl<'a> SimpleFlakeFetcher<'a, 1> for Fetchgit {
fn get_flake_ref(&self, [url]: &[&str; 1], rev: &str) -> String {
let rev_type = if rev.len() == 40 { "rev" } else { "ref" };
if matches!(self.0, GitScheme::Yes) {
format!("{url}?{rev_type}={rev}&submodules=1")
impl<'a> Fetchgit {
fn fetch(
&'a self,
values @ [url]: &[&str; 1],
rev: &str,
args: &[(String, String)],
args_str: &[(String, String)],
) -> Result<String> {
if args.is_empty() && args_str.is_empty() {
git_prefetch(matches!(self.0, GitScheme::Yes), url, rev)
} else {
format!("git+{url}?{rev_type}={rev}&submodules=1")
self.fetch_fod(values, rev, args, args_str)
}
}
}

View File

@ -51,6 +51,24 @@ pub fn flake_prefetch(flake_ref: String) -> Result<String> {
.hash)
}
// work around for https://github.com/NixOS/nix/issues/5291
pub fn git_prefetch(git_scheme: bool, url: &str, rev: &str) -> Result<String> {
let prefix = if git_scheme { "" } else { "git+" };
if rev.len() == 40 {
flake_prefetch(format!("{prefix}{url}?rev={rev}&submodules=1"))
} else {
if !rev.starts_with("refs/") {
if let hash @ Ok(_) =
flake_prefetch(format!("{prefix}{url}?ref=refs/tags/{rev}&submodules=1"))
{
return hash;
}
}
flake_prefetch(format!("{prefix}{url}?ref={rev}&submodules=1"))
}
}
pub fn url_prefetch(url: String, unpack: bool) -> Result<String> {
let mut cmd = Command::new("nix-prefetch-url");
if unpack {

View File

@ -1,5 +1,5 @@
fetchgit {
url = "https://github.com/nix-community/nurl";
rev = "refs/tags/v0.3.0";
rev = "v0.3.0";
hash = "sha256-jZ+cCp1THDhfHH5yMmRPjGuthOqsgcF/3OjZ61FMdA4=";
}

View File

@ -1,6 +1,6 @@
args = [
"https://github.com/nix-community/nurl",
"refs/tags/v0.3.0",
"v0.3.0",
"--fetcher",
"fetchgit",
]

View File

@ -1,5 +1,5 @@
fetchgit {
url = "git://git.kernel.org/pub/scm/linux/kernel/git/jejb/efitools.git";
rev = "refs/tags/v1.9.0";
rev = "v1.9.0";
hash = "sha256-W38CWoPnkt0+VkVp+uW+sjWjo6MXYq2eTXRxWKyQph8=";
}

View File

@ -1,6 +1,6 @@
args = [
"git://git.kernel.org/pub/scm/linux/kernel/git/jejb/efitools.git",
"refs/tags/v1.9.0",
"v1.9.0",
"--fetcher",
"fetchgit",
]