Skip to content

Commit

Permalink
git: allow fetching tags without prefixing refs/tags/
Browse files Browse the repository at this point in the history
work around for NixOS/nix#5291
  • Loading branch information
figsoda committed Jan 30, 2023
1 parent d333520 commit 2b47f46
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 15 deletions.
25 changes: 14 additions & 11 deletions src/fetcher/git.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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)
}
}
}
18 changes: 18 additions & 0 deletions src/prefetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion tests/cmd/fetcher/git/basic.stdout
Original file line number Diff line number Diff line change
@@ -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=";
}
2 changes: 1 addition & 1 deletion tests/cmd/fetcher/git/basic.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
args = [
"https://github.com/nix-community/nurl",
"refs/tags/v0.3.0",
"v0.3.0",
"--fetcher",
"fetchgit",
]
2 changes: 1 addition & 1 deletion tests/cmd/fetcher/git/scheme.stdout
Original file line number Diff line number Diff line change
@@ -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=";
}
2 changes: 1 addition & 1 deletion tests/cmd/fetcher/git/scheme.toml
Original file line number Diff line number Diff line change
@@ -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",
]

0 comments on commit 2b47f46

Please sign in to comment.