From f1f814eaff27bb8b129347242cf694a6302bc5a3 Mon Sep 17 00:00:00 2001 From: figsoda Date: Wed, 1 Feb 2023 14:39:50 -0500 Subject: [PATCH] add --submodules option to fetch submodules/subrepos --- src/cli.rs | 4 ++ src/fetcher/builtin_git.rs | 13 +++++ src/fetcher/git.rs | 7 ++- src/fetcher/gitea.rs | 33 +++++++++-- src/fetcher/github.rs | 12 +++- src/fetcher/gitlab.rs | 19 ++++++- src/fetcher/hg.rs | 6 +- src/fetcher/mod.rs | 18 ++++-- src/fetcher/sourcehut.rs | 12 +++- src/main.rs | 4 +- src/prefetch.rs | 9 +-- src/simple.rs | 74 +++++++++++++++++++++++-- tests/cmd/submodules/builtin_git.stdout | 5 ++ tests/cmd/submodules/builtin_git.toml | 7 +++ tests/cmd/submodules/false.stdout | 6 ++ tests/cmd/submodules/false.toml | 7 +++ tests/cmd/submodules/git.stdout | 5 ++ tests/cmd/submodules/git.toml | 7 +++ tests/cmd/submodules/gitea.stdout | 8 +++ tests/cmd/submodules/gitea.toml | 7 +++ tests/cmd/submodules/github.toml | 1 + tests/cmd/submodules/gitlab.stdout | 8 +++ tests/cmd/submodules/gitlab.toml | 5 ++ tests/cmd/submodules/hg.stdout | 6 ++ tests/cmd/submodules/hg.toml | 7 +++ tests/cmd/submodules/json.stdout | 1 + tests/cmd/submodules/json.toml | 6 ++ tests/cmd/submodules/sourcehut.stdout | 7 +++ tests/cmd/submodules/sourcehut.toml | 1 + tests/cmd/submodules/true.stdout | 7 +++ tests/cmd/submodules/true.toml | 1 + tests/cmd/submodules/unsupported.stdout | 5 ++ tests/cmd/submodules/unsupported.toml | 1 + 33 files changed, 290 insertions(+), 29 deletions(-) create mode 100644 tests/cmd/submodules/builtin_git.stdout create mode 100644 tests/cmd/submodules/builtin_git.toml create mode 100644 tests/cmd/submodules/false.stdout create mode 100644 tests/cmd/submodules/false.toml create mode 100644 tests/cmd/submodules/git.stdout create mode 100644 tests/cmd/submodules/git.toml create mode 100644 tests/cmd/submodules/gitea.stdout create mode 100644 tests/cmd/submodules/gitea.toml create mode 100644 tests/cmd/submodules/github.toml create mode 100644 tests/cmd/submodules/gitlab.stdout create mode 100644 tests/cmd/submodules/gitlab.toml create mode 100644 tests/cmd/submodules/hg.stdout create mode 100644 tests/cmd/submodules/hg.toml create mode 100644 tests/cmd/submodules/json.stdout create mode 100644 tests/cmd/submodules/json.toml create mode 100644 tests/cmd/submodules/sourcehut.stdout create mode 100644 tests/cmd/submodules/sourcehut.toml create mode 100644 tests/cmd/submodules/true.stdout create mode 100644 tests/cmd/submodules/true.toml create mode 100644 tests/cmd/submodules/unsupported.stdout create mode 100644 tests/cmd/submodules/unsupported.toml diff --git a/src/cli.rs b/src/cli.rs index 3e2460c..1195c4b 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -16,6 +16,10 @@ pub struct Opts { /// The revision or reference to be fetched pub rev: Option, + /// Fetch submodules instead of using the fetcher's default + #[arg(short = 'S', long, num_args=0..=1, require_equals = true, default_missing_value = "true")] + pub submodules: Option, + /// Specify the fetcher function instead of inferring from the URL #[arg(short, long)] pub fetcher: Option, diff --git a/src/fetcher/builtin_git.rs b/src/fetcher/builtin_git.rs index 0a153c0..1a04acf 100644 --- a/src/fetcher/builtin_git.rs +++ b/src/fetcher/builtin_git.rs @@ -14,6 +14,7 @@ impl<'a> Fetcher<'a> for BuiltinsFetchGit { out: &mut impl Write, url: &'a Url, rev: Option, + submodules: Option, args: Vec<(String, String)>, args_str: Vec<(String, String)>, overwrites: FxHashMap, @@ -37,6 +38,12 @@ impl<'a> Fetcher<'a> for BuiltinsFetchGit { writeln!(out, r#"{indent} {rev_type} = "{rev}";"#)?; } + if let Some(submodules) = overwrites.remove("submodules") { + writeln!(out, "{indent} submodules = {submodules};")?; + } else if matches!(submodules, Some(true)) { + writeln!(out, "{indent} submodules = true;")?; + } + for (key, value) in args { let value = overwrites.remove(&key).unwrap_or(value); writeln!(out, "{indent} {key} = {value};")?; @@ -63,6 +70,7 @@ impl<'a> Fetcher<'a> for BuiltinsFetchGit { _: &mut impl Write, _: &'a Url, _: Option, + _: Option, _: Vec<(String, String)>, _: Vec<(String, String)>, ) -> Result<()> { @@ -74,6 +82,7 @@ impl<'a> Fetcher<'a> for BuiltinsFetchGit { out: &mut impl Write, url: &'a Url, rev: Option, + submodules: Option, args: Vec<(String, String)>, args_str: Vec<(String, String)>, overwrites: Vec<(String, String)>, @@ -87,6 +96,10 @@ impl<'a> Fetcher<'a> for BuiltinsFetchGit { rev_type: rev, }); + if matches!(submodules, Some(true)) { + fetcher_args["submodules"] = json!(true); + } + for (key, value) in args { fetcher_args[key] = json!({ "type": "nix", diff --git a/src/fetcher/git.rs b/src/fetcher/git.rs index d5d9636..c39065d 100644 --- a/src/fetcher/git.rs +++ b/src/fetcher/git.rs @@ -8,6 +8,8 @@ impl_fetcher!(Fetchgit); impl<'a> SimpleFetcher<'a, 1> for Fetchgit { const KEYS: [&'static str; 1] = ["url"]; const NAME: &'static str = "fetchgit"; + const SUBMODULES_DEFAULT: bool = true; + const SUBMODULES_KEY: Option<&'static str> = Some("fetchSubmodules"); fn get_values(&self, url: &'a Url) -> Option<[&'a str; 1]> { Some([if matches!(self.0, GitScheme::Plus) { @@ -23,13 +25,14 @@ impl<'a> Fetchgit { &'a self, values @ [url]: &[&str; 1], rev: &str, + submodules: bool, args: &[(String, String)], args_str: &[(String, String)], ) -> Result { if args.is_empty() && args_str.is_empty() { - git_prefetch(matches!(self.0, GitScheme::Yes), url, rev) + git_prefetch(matches!(self.0, GitScheme::Yes), url, rev, !submodules) } else { - self.fetch_fod(values, rev, args, args_str) + self.fetch_fod(values, rev, submodules, args, args_str) } } } diff --git a/src/fetcher/gitea.rs b/src/fetcher/gitea.rs index f6de7d7..26b492f 100644 --- a/src/fetcher/gitea.rs +++ b/src/fetcher/gitea.rs @@ -3,7 +3,8 @@ use serde::Deserialize; use crate::{ impl_fetcher, - simple::{SimpleFetcher, SimpleUrlFetcher}, + prefetch::{git_prefetch, url_prefetch}, + simple::SimpleFetcher, }; pub struct FetchFromGitea<'a>(pub &'a str); @@ -17,6 +18,7 @@ struct Commit { impl SimpleFetcher<'_, 2> for FetchFromGitea<'_> { const KEYS: [&'static str; 2] = ["owner", "repo"]; const NAME: &'static str = "fetchFromGitea"; + const SUBMODULES_KEY: Option<&'static str> = Some("fetchSubmodules"); fn host(&self) -> Option<&str> { Some(self.0) @@ -37,8 +39,31 @@ impl SimpleFetcher<'_, 2> for FetchFromGitea<'_> { } } -impl<'a> SimpleUrlFetcher<'a, 2> for FetchFromGitea<'a> { - fn get_url(&self, [owner, repo]: &[&str; 2], rev: &str) -> String { - format!("https://{}/{owner}/{repo}/archive/{rev}.tar.gz", self.0) +impl FetchFromGitea<'_> { + fn fetch( + &self, + values @ [owner, repo]: &[&str; 2], + rev: &str, + submodules: bool, + args: &[(String, String)], + args_str: &[(String, String)], + ) -> Result { + if args.is_empty() && args_str.is_empty() { + if submodules { + git_prefetch( + true, + &format!("git+https://{}/{owner}/{repo}", self.0), + rev, + true, + ) + } else { + url_prefetch( + format!("https://{}/{owner}/{repo}/archive/{rev}.tar.gz", self.0), + true, + ) + } + } else { + self.fetch_fod(values, rev, submodules, args, args_str) + } } } diff --git a/src/fetcher/github.rs b/src/fetcher/github.rs index c3ce31c..9c596c3 100644 --- a/src/fetcher/github.rs +++ b/src/fetcher/github.rs @@ -3,7 +3,7 @@ use serde::Deserialize; use crate::{ impl_fetcher, - simple::{SimpleFetcher, SimpleFlakeFetcher}, + simple::{SimpleFetcher, SimpleGitFetcher}, }; pub struct FetchFromGitHub<'a>(pub Option<&'a str>); @@ -18,6 +18,7 @@ impl SimpleFetcher<'_, 2> for FetchFromGitHub<'_> { const HOST_KEY: &'static str = "githubBase"; const KEYS: [&'static str; 2] = ["owner", "repo"]; const NAME: &'static str = "fetchFromGitHub"; + const SUBMODULES_KEY: Option<&'static str> = Some("fetchSubmodules"); fn host(&self) -> Option<&str> { self.0 @@ -36,7 +37,7 @@ impl SimpleFetcher<'_, 2> for FetchFromGitHub<'_> { } } -impl<'a> SimpleFlakeFetcher<'a, 2> for FetchFromGitHub<'a> { +impl<'a> SimpleGitFetcher<'a, 2> for FetchFromGitHub<'a> { fn get_flake_ref(&self, [owner, repo]: &[&str; 2], rev: &str) -> String { if let Some(host) = self.0 { format!("github:{owner}/{repo}/{rev}?host={host}") @@ -44,4 +45,11 @@ impl<'a> SimpleFlakeFetcher<'a, 2> for FetchFromGitHub<'a> { format!("github:{owner}/{repo}/{rev}") } } + + fn get_repo_url(&self, [owner, repo]: &[&str; 2]) -> String { + format!( + "git+https://{}/{owner}/{repo}", + self.0.unwrap_or("github.com"), + ) + } } diff --git a/src/fetcher/gitlab.rs b/src/fetcher/gitlab.rs index b2b02ee..a848cc7 100644 --- a/src/fetcher/gitlab.rs +++ b/src/fetcher/gitlab.rs @@ -6,7 +6,7 @@ use std::fmt::Write; use crate::{ impl_fetcher, - simple::{SimpleFetcher, SimpleFlakeFetcher}, + simple::{SimpleFetcher, SimpleGitFetcher}, Url, }; @@ -33,6 +33,7 @@ struct Commit { impl<'a> SimpleFetcher<'a, 2> for FetchFromGitLab<'a> { const KEYS: [&'static str; 2] = ["owner", "repo"]; const NAME: &'static str = "fetchFromGitLab"; + const SUBMODULES_KEY: Option<&'static str> = Some("fetchSubmodules"); fn host(&self) -> Option<&str> { self.host @@ -84,7 +85,7 @@ impl<'a> SimpleFetcher<'a, 2> for FetchFromGitLab<'a> { } } -impl<'a> SimpleFlakeFetcher<'a, 2> for FetchFromGitLab<'a> { +impl<'a> SimpleGitFetcher<'a, 2> for FetchFromGitLab<'a> { fn get_flake_ref(&self, [owner, repo]: &[&str; 2], rev: &str) -> String { let mut flake_ref = String::from("gitlab:"); if let Some(group) = self.group.get() { @@ -102,4 +103,18 @@ impl<'a> SimpleFlakeFetcher<'a, 2> for FetchFromGitLab<'a> { } flake_ref } + + fn get_repo_url(&self, [owner, repo]: &[&str; 2]) -> String { + let mut flake_ref = String::from("git+https://"); + flake_ref.push_str(self.host.unwrap_or("gitlab.com")); + flake_ref.push('/'); + if let Some(group) = self.group.get() { + flake_ref.push_str(group); + flake_ref.push('/'); + } + flake_ref.push_str(owner); + flake_ref.push('/'); + flake_ref.push_str(repo); + flake_ref + } } diff --git a/src/fetcher/hg.rs b/src/fetcher/hg.rs index cc5ea5c..243eb62 100644 --- a/src/fetcher/hg.rs +++ b/src/fetcher/hg.rs @@ -11,6 +11,7 @@ impl<'a> SimpleFetcher<'a, 1> for Fetchhg { const HASH_KEY: &'static str = "sha256"; const KEYS: [&'static str; 1] = ["url"]; const NAME: &'static str = "fetchhg"; + const SUBMODULES_KEY: Option<&'static str> = Some("fetchSubrepos"); fn get_values(&self, url: &'a Url) -> Option<[&'a str; 1]> { Some([if self.0 { @@ -22,10 +23,11 @@ impl<'a> SimpleFetcher<'a, 1> for Fetchhg { } impl SimpleFlakeFetcher<'_, 1> for Fetchhg { - fn get_flake_ref(&self, [url]: &[&str; 1], rev: &str) -> String { + fn get_flake_ref(&self, [url]: &[&str; 1], rev: &str, submodules: bool) -> String { format!( - "hg+{url}?{}={rev}", + "hg+{url}?{}={rev}{}", if rev.len() == 40 { "rev" } else { "ref" }, + if submodules { "&submodules=1" } else { "" }, ) } } diff --git a/src/fetcher/mod.rs b/src/fetcher/mod.rs index 7b3aa9f..dc5febc 100644 --- a/src/fetcher/mod.rs +++ b/src/fetcher/mod.rs @@ -42,6 +42,7 @@ pub trait Fetcher<'a> { out: &mut impl Write, url: &'a Url, rev: Option, + submodules: Option, args: Vec<(String, String)>, args_str: Vec<(String, String)>, overwrites: FxHashMap, @@ -53,6 +54,7 @@ pub trait Fetcher<'a> { out: &mut impl Write, url: &'a Url, rev: Option, + submodules: Option, args: Vec<(String, String)>, args_str: Vec<(String, String)>, ) -> Result<()>; @@ -62,6 +64,7 @@ pub trait Fetcher<'a> { out: &mut impl Write, url: &'a Url, rev: Option, + submodules: Option, args: Vec<(String, String)>, args_str: Vec<(String, String)>, overwrites: Vec<(String, String)>, @@ -98,6 +101,7 @@ macro_rules! impl_fetcher { out: &mut impl ::std::io::Write, url: &'a $crate::Url, rev: Option, + submodules: Option, args: Vec<(String, String)>, args_str: Vec<(String, String)>, overwrites: ::rustc_hash::FxHashMap, @@ -114,9 +118,10 @@ macro_rules! impl_fetcher { None => self.fetch_rev(values)?, }; - let hash = self.fetch(values, &rev, &args, &args_str)?; + let submodules = self.resolve_submodules(submodules); + let hash = self.fetch(values, &rev, submodules, &args, &args_str)?; - self.write_nix(out, values, rev, hash, args, args_str, overwrites, indent) + self.write_nix(out, values, rev, hash, submodules, args, args_str, overwrites, indent) } fn fetch_hash( @@ -124,6 +129,7 @@ macro_rules! impl_fetcher { out: &mut impl ::std::io::Write, url: &'a $crate::Url, rev: Option, + submodules: Option, args: Vec<(String, String)>, args_str: Vec<(String, String)>, ) -> ::anyhow::Result<()> { @@ -138,7 +144,8 @@ macro_rules! impl_fetcher { None => self.fetch_rev(values)?, }; - let hash = self.fetch(values, &rev, &args, &args_str)?; + let submodules = self.resolve_submodules(submodules); + let hash = self.fetch(values, &rev, submodules, &args, &args_str)?; write!(out, "{}", hash)?; Ok(()) @@ -149,6 +156,7 @@ macro_rules! impl_fetcher { out: &mut impl ::std::io::Write, url: &'a $crate::Url, rev: Option, + submodules: Option, args: Vec<(String, String)>, args_str: Vec<(String, String)>, overwrites: Vec<(String, String)>, @@ -165,13 +173,15 @@ macro_rules! impl_fetcher { None => self.fetch_rev(values)?, }; - let hash = self.fetch(values, &rev, &args, &args_str)?; + let submodules = self.resolve_submodules(submodules); + let hash = self.fetch(values, &rev, submodules, &args, &args_str)?; self.write_json( out, values, rev, hash, + submodules, args, args_str, overwrites, diff --git a/src/fetcher/sourcehut.rs b/src/fetcher/sourcehut.rs index 582c0fa..82a2c01 100644 --- a/src/fetcher/sourcehut.rs +++ b/src/fetcher/sourcehut.rs @@ -1,6 +1,6 @@ use crate::{ impl_fetcher, - simple::{SimpleFetcher, SimpleFlakeFetcher}, + simple::{SimpleFetcher, SimpleGitFetcher}, }; pub struct FetchFromSourcehut<'a>(pub Option<&'a str>); @@ -9,13 +9,14 @@ impl_fetcher!(FetchFromSourcehut<'a>); impl<'a> SimpleFetcher<'a, 2> for FetchFromSourcehut<'a> { const KEYS: [&'static str; 2] = ["owner", "repo"]; const NAME: &'static str = "fetchFromSourcehut"; + const SUBMODULES_KEY: Option<&'static str> = Some("fetchSubmodules"); fn host(&self) -> Option<&str> { self.0 } } -impl<'a> SimpleFlakeFetcher<'a, 2> for FetchFromSourcehut<'a> { +impl<'a> SimpleGitFetcher<'a, 2> for FetchFromSourcehut<'a> { fn get_flake_ref(&self, [owner, repo]: &[&str; 2], rev: &str) -> String { if let Some(host) = self.0 { format!("sourcehut:{owner}/{repo}/{rev}?host={host}") @@ -23,4 +24,11 @@ impl<'a> SimpleFlakeFetcher<'a, 2> for FetchFromSourcehut<'a> { format!("sourcehut:{owner}/{repo}/{rev}") } } + + fn get_repo_url(&self, [owner, repo]: &[&str; 2]) -> String { + format!( + "git+https://{}/{owner}/{repo}", + self.0.unwrap_or("git.sr.ht"), + ) + } } diff --git a/src/main.rs b/src/main.rs index 1b25187..7464a49 100644 --- a/src/main.rs +++ b/src/main.rs @@ -229,12 +229,13 @@ fn main() -> Result<()> { let args = opts.args.into_iter().tuples().collect(); let args_str = opts.args_str.into_iter().tuples().collect(); if opts.hash { - fetcher.fetch_hash(out, &url, opts.rev, args, args_str) + fetcher.fetch_hash(out, &url, opts.rev, opts.submodules, args, args_str) } else if opts.json { fetcher.fetch_json( out, &url, opts.rev, + opts.submodules, args, args_str, opts.overwrites.into_iter().tuples().collect(), @@ -253,6 +254,7 @@ fn main() -> Result<()> { out, &url, opts.rev, + opts.submodules, args, args_str, overwrites, diff --git a/src/prefetch.rs b/src/prefetch.rs index a0e19fe..4c5ceac 100644 --- a/src/prefetch.rs +++ b/src/prefetch.rs @@ -52,20 +52,21 @@ pub fn flake_prefetch(flake_ref: String) -> Result { } // work around for https://github.com/NixOS/nix/issues/5291 -pub fn git_prefetch(git_scheme: bool, url: &str, rev: &str) -> Result { +pub fn git_prefetch(git_scheme: bool, url: &str, rev: &str, submodules: bool) -> Result { let prefix = if git_scheme { "" } else { "git+" }; + let submodules = if submodules { "&submodules=1" } else { "" }; if rev.len() == 40 { - flake_prefetch(format!("{prefix}{url}?rev={rev}&submodules=1")) + flake_prefetch(format!("{prefix}{url}?rev={rev}{submodules}")) } else { if !rev.starts_with("refs/") { if let hash @ Ok(_) = - flake_prefetch(format!("{prefix}{url}?ref=refs/tags/{rev}&submodules=1")) + flake_prefetch(format!("{prefix}{url}?ref=refs/tags/{rev}{submodules}")) { return hash; } } - flake_prefetch(format!("{prefix}{url}?ref={rev}&submodules=1")) + flake_prefetch(format!("{prefix}{url}?ref={rev}{submodules}")) } } diff --git a/src/simple.rs b/src/simple.rs index 3fd3ade..a8e47e2 100644 --- a/src/simple.rs +++ b/src/simple.rs @@ -1,4 +1,4 @@ -use crate::Url; +use crate::{prefetch::git_prefetch, Url}; use anyhow::{bail, Result}; use itertools::Itertools; use rustc_hash::FxHashMap; @@ -14,6 +14,8 @@ pub trait SimpleFetcher<'a, const N: usize> { const KEYS: [&'static str; N]; const NAME: &'static str; const REV_KEY: &'static str = "rev"; + const SUBMODULES_DEFAULT: bool = false; + const SUBMODULES_KEY: Option<&'static str> = None; fn host(&self) -> Option<&str> { None @@ -36,6 +38,10 @@ pub trait SimpleFetcher<'a, const N: usize> { Some(xs) } + fn resolve_submodules(&self, submodules: Option) -> bool { + submodules.map_or(false, |submodules| submodules ^ Self::SUBMODULES_DEFAULT) + } + fn fetch_rev(&self, _: &[&str; N]) -> Result { bail!( "{} does not support fetching the latest revision", @@ -47,6 +53,7 @@ pub trait SimpleFetcher<'a, const N: usize> { &self, values: &[&str; N], rev: &str, + submodules: bool, args: &[(String, String)], args_str: &[(String, String)], ) -> Result { @@ -71,6 +78,12 @@ pub trait SimpleFetcher<'a, const N: usize> { Self::HASH_KEY, )?; + if submodules { + if let Some(key) = Self::SUBMODULES_KEY { + write!(expr, "{key}={};", !Self::SUBMODULES_DEFAULT)?; + } + } + for (key, value) in args { write!(expr, "{key}={value};")?; } @@ -89,6 +102,7 @@ pub trait SimpleFetcher<'a, const N: usize> { values: &[&str; N], rev: String, hash: String, + submodules: bool, args: Vec<(String, String)>, args_str: Vec<(String, String)>, overwrites: FxHashMap, @@ -129,6 +143,14 @@ pub trait SimpleFetcher<'a, const N: usize> { writeln!(out, r#"{indent} {} = "{hash}";"#, Self::HASH_KEY)?; } + if let Some(key) = Self::SUBMODULES_KEY { + if let Some(submodules) = overwrites.remove(key) { + writeln!(out, "{indent} {key} = {submodules};")?; + } else if submodules { + writeln!(out, "{indent} {key} = {};", !Self::SUBMODULES_DEFAULT)?; + } + } + for (key, value) in args { let value = overwrites.remove(&key).unwrap_or(value); writeln!(out, "{indent} {key} = {value};")?; @@ -156,6 +178,7 @@ pub trait SimpleFetcher<'a, const N: usize> { values: &[&str; N], rev: String, hash: String, + submodules: bool, args: Vec<(String, String)>, args_str: Vec<(String, String)>, overwrites: Vec<(String, String)>, @@ -174,6 +197,12 @@ pub trait SimpleFetcher<'a, const N: usize> { fetcher_args["group"] = json!(group); } + if submodules { + if let Some(key) = Self::SUBMODULES_KEY { + fetcher_args[key] = json!(!Self::SUBMODULES_DEFAULT); + } + } + for (key, value) in args { fetcher_args[key] = json!({ "type": "nix", @@ -211,27 +240,59 @@ pub trait SimpleFodFetcher<'a, const N: usize>: SimpleFetcher<'a, N> { &self, values: &[&str; N], rev: &str, + submodules: bool, args: &[(String, String)], args_str: &[(String, String)], ) -> Result { - self.fetch_fod(values, rev, args, args_str) + self.fetch_fod(values, rev, submodules, args, args_str) } } pub trait SimpleFlakeFetcher<'a, const N: usize>: SimpleFetcher<'a, N> { - fn get_flake_ref(&self, values: &[&str; N], rev: &str) -> String; + fn get_flake_ref(&self, values: &[&str; N], rev: &str, submodules: bool) -> String; fn fetch( &self, values: &[&str; N], rev: &str, + submodules: bool, args: &[(String, String)], args_str: &[(String, String)], ) -> Result { if args.is_empty() && args_str.is_empty() { - flake_prefetch(self.get_flake_ref(values, rev)) + flake_prefetch(self.get_flake_ref(values, rev, submodules)) } else { - self.fetch_fod(values, rev, args, args_str) + self.fetch_fod(values, rev, submodules, args, args_str) + } + } +} + +pub trait SimpleGitFetcher<'a, const N: usize>: SimpleFetcher<'a, N> { + fn get_flake_ref(&self, values: &[&str; N], rev: &str) -> String; + + fn get_repo_url(&self, values: &[&str; N]) -> String; + + fn fetch( + &self, + values: &[&str; N], + rev: &str, + submodules: bool, + args: &[(String, String)], + args_str: &[(String, String)], + ) -> Result { + if args.is_empty() && args_str.is_empty() { + if submodules { + git_prefetch( + true, + &self.get_repo_url(values), + rev, + !Self::SUBMODULES_DEFAULT, + ) + } else { + flake_prefetch(self.get_flake_ref(values, rev)) + } + } else { + self.fetch_fod(values, rev, submodules, args, args_str) } } } @@ -245,13 +306,14 @@ pub trait SimpleUrlFetcher<'a, const N: usize>: SimpleFetcher<'a, N> { &self, values: &[&str; N], rev: &str, + submodules: bool, args: &[(String, String)], args_str: &[(String, String)], ) -> Result { if args.is_empty() && args_str.is_empty() { url_prefetch(self.get_url(values, rev), Self::UNPACK) } else { - self.fetch_fod(values, rev, args, args_str) + self.fetch_fod(values, rev, submodules, args, args_str) } } } diff --git a/tests/cmd/submodules/builtin_git.stdout b/tests/cmd/submodules/builtin_git.stdout new file mode 100644 index 0000000..b39a35f --- /dev/null +++ b/tests/cmd/submodules/builtin_git.stdout @@ -0,0 +1,5 @@ +builtins.fetchGit { + url = "https://github.com/Koihik/LuaFormatter"; + ref = "refs/tags/1.3.0"; + submodules = true; +} \ No newline at end of file diff --git a/tests/cmd/submodules/builtin_git.toml b/tests/cmd/submodules/builtin_git.toml new file mode 100644 index 0000000..353b19f --- /dev/null +++ b/tests/cmd/submodules/builtin_git.toml @@ -0,0 +1,7 @@ +args = [ + "https://github.com/Koihik/LuaFormatter", + "refs/tags/1.3.0", + "--submodules", + "--fetcher", + "builtins.fetchGit", +] diff --git a/tests/cmd/submodules/false.stdout b/tests/cmd/submodules/false.stdout new file mode 100644 index 0000000..bd4ebc6 --- /dev/null +++ b/tests/cmd/submodules/false.stdout @@ -0,0 +1,6 @@ +fetchgit { + url = "https://github.com/Koihik/LuaFormatter"; + rev = "1.3.0"; + hash = "sha256-UrADnTFH8y1rqA2LytSK9HkIQdxOIrY4CwqfL139Oag="; + fetchSubmodules = false; +} \ No newline at end of file diff --git a/tests/cmd/submodules/false.toml b/tests/cmd/submodules/false.toml new file mode 100644 index 0000000..65a97bf --- /dev/null +++ b/tests/cmd/submodules/false.toml @@ -0,0 +1,7 @@ +args = [ + "https://github.com/Koihik/LuaFormatter", + "1.3.0", + "--submodules=false", + "--fetcher", + "fetchgit", +] diff --git a/tests/cmd/submodules/git.stdout b/tests/cmd/submodules/git.stdout new file mode 100644 index 0000000..a9fa314 --- /dev/null +++ b/tests/cmd/submodules/git.stdout @@ -0,0 +1,5 @@ +fetchgit { + url = "https://github.com/Koihik/LuaFormatter"; + rev = "1.3.0"; + hash = "sha256-O42sNIFDi2Dv6KWkBynrR60RABCAitSVTp42W6w0tcg="; +} \ No newline at end of file diff --git a/tests/cmd/submodules/git.toml b/tests/cmd/submodules/git.toml new file mode 100644 index 0000000..fb72669 --- /dev/null +++ b/tests/cmd/submodules/git.toml @@ -0,0 +1,7 @@ +args = [ + "https://github.com/Koihik/LuaFormatter", + "1.3.0", + "--submodules", + "--fetcher", + "fetchgit", +] diff --git a/tests/cmd/submodules/gitea.stdout b/tests/cmd/submodules/gitea.stdout new file mode 100644 index 0000000..dfbcab6 --- /dev/null +++ b/tests/cmd/submodules/gitea.stdout @@ -0,0 +1,8 @@ +fetchFromGitea { + domain = "repo.palemoon.org"; + owner = "MoonchildProductions"; + repo = "Pale-Moon"; + rev = "31.0.0_Release"; + hash = "sha256-fIQAQCtjA/9Otft3e9Z4xWgE09sqsdArYQtZqmEgfTc="; + fetchSubmodules = true; +} \ No newline at end of file diff --git a/tests/cmd/submodules/gitea.toml b/tests/cmd/submodules/gitea.toml new file mode 100644 index 0000000..adf35a1 --- /dev/null +++ b/tests/cmd/submodules/gitea.toml @@ -0,0 +1,7 @@ +args = [ + "https://repo.palemoon.org/MoonchildProductions/Pale-Moon", + "31.0.0_Release", + "--submodules", + "--fetcher", + "fetchFromGitea", +] diff --git a/tests/cmd/submodules/github.toml b/tests/cmd/submodules/github.toml new file mode 100644 index 0000000..babecbf --- /dev/null +++ b/tests/cmd/submodules/github.toml @@ -0,0 +1 @@ +args = ["https://github.com/Koihik/LuaFormatter", "1.3.0", "--submodules"] diff --git a/tests/cmd/submodules/gitlab.stdout b/tests/cmd/submodules/gitlab.stdout new file mode 100644 index 0000000..293abc5 --- /dev/null +++ b/tests/cmd/submodules/gitlab.stdout @@ -0,0 +1,8 @@ +fetchFromGitLab { + group = "librewolf-community"; + owner = "browser"; + repo = "source"; + rev = "100.0-1"; + hash = "sha256-ijqyZAEKyL7V/NFtI3ErHcSnj7VbBFvgCgK9NJren/U="; + fetchSubmodules = true; +} \ No newline at end of file diff --git a/tests/cmd/submodules/gitlab.toml b/tests/cmd/submodules/gitlab.toml new file mode 100644 index 0000000..da7fa94 --- /dev/null +++ b/tests/cmd/submodules/gitlab.toml @@ -0,0 +1,5 @@ +args = [ + "https://gitlab.com/librewolf-community/browser/source", + "100.0-1", + "--submodules", +] diff --git a/tests/cmd/submodules/hg.stdout b/tests/cmd/submodules/hg.stdout new file mode 100644 index 0000000..e32addf --- /dev/null +++ b/tests/cmd/submodules/hg.stdout @@ -0,0 +1,6 @@ +fetchhg { + url = "http://www.octave.org/hg/octave"; + rev = "release-7-1-0"; + sha256 = "sha256-zNpgpPPGAoIpSIRx39wJpEyLbxjJBsnFjUy47hGiZLs="; + fetchSubrepos = true; +} \ No newline at end of file diff --git a/tests/cmd/submodules/hg.toml b/tests/cmd/submodules/hg.toml new file mode 100644 index 0000000..e148a9d --- /dev/null +++ b/tests/cmd/submodules/hg.toml @@ -0,0 +1,7 @@ +args = [ + "http://www.octave.org/hg/octave", + "release-7-1-0", + "--submodules", + "--fetcher", + "fetchhg", +] diff --git a/tests/cmd/submodules/json.stdout b/tests/cmd/submodules/json.stdout new file mode 100644 index 0000000..223077e --- /dev/null +++ b/tests/cmd/submodules/json.stdout @@ -0,0 +1 @@ +{"args":{"fetchSubmodules":true,"hash":"sha256-O42sNIFDi2Dv6KWkBynrR60RABCAitSVTp42W6w0tcg=","owner":"Koihik","repo":"LuaFormatter","rev":"1.3.0"},"fetcher":"fetchFromGitHub"} \ No newline at end of file diff --git a/tests/cmd/submodules/json.toml b/tests/cmd/submodules/json.toml new file mode 100644 index 0000000..cd2dc52 --- /dev/null +++ b/tests/cmd/submodules/json.toml @@ -0,0 +1,6 @@ +args = [ + "https://github.com/Koihik/LuaFormatter", + "1.3.0", + "--submodules", + "--json", +] diff --git a/tests/cmd/submodules/sourcehut.stdout b/tests/cmd/submodules/sourcehut.stdout new file mode 100644 index 0000000..ce80574 --- /dev/null +++ b/tests/cmd/submodules/sourcehut.stdout @@ -0,0 +1,7 @@ +fetchFromSourcehut { + owner = "~cnx"; + repo = "blackshades"; + rev = "2.4.0"; + hash = "sha256-owsP50zYFV1Z+c0bSUaHmnvH+BywfJSp7oSk3ys/+GM="; + fetchSubmodules = true; +} \ No newline at end of file diff --git a/tests/cmd/submodules/sourcehut.toml b/tests/cmd/submodules/sourcehut.toml new file mode 100644 index 0000000..50feb33 --- /dev/null +++ b/tests/cmd/submodules/sourcehut.toml @@ -0,0 +1 @@ +args = ["https://git.sr.ht/~cnx/blackshades", "2.4.0", "--submodules"] diff --git a/tests/cmd/submodules/true.stdout b/tests/cmd/submodules/true.stdout new file mode 100644 index 0000000..653eaec --- /dev/null +++ b/tests/cmd/submodules/true.stdout @@ -0,0 +1,7 @@ +fetchFromGitHub { + owner = "Koihik"; + repo = "LuaFormatter"; + rev = "1.3.0"; + hash = "sha256-O42sNIFDi2Dv6KWkBynrR60RABCAitSVTp42W6w0tcg="; + fetchSubmodules = true; +} \ No newline at end of file diff --git a/tests/cmd/submodules/true.toml b/tests/cmd/submodules/true.toml new file mode 100644 index 0000000..6df403b --- /dev/null +++ b/tests/cmd/submodules/true.toml @@ -0,0 +1 @@ +args = ["https://github.com/Koihik/LuaFormatter", "1.3.0", "--submodules=true"] diff --git a/tests/cmd/submodules/unsupported.stdout b/tests/cmd/submodules/unsupported.stdout new file mode 100644 index 0000000..5c66182 --- /dev/null +++ b/tests/cmd/submodules/unsupported.stdout @@ -0,0 +1,5 @@ +fetchCrate { + pname = "nurl"; + version = "0.3.0"; + hash = "sha256-B6T4DEhE2Jq3YSL+b//27gRkQlvqhynSMBCGdYD5Gog="; +} \ No newline at end of file diff --git a/tests/cmd/submodules/unsupported.toml b/tests/cmd/submodules/unsupported.toml new file mode 100644 index 0000000..bdbb1ba --- /dev/null +++ b/tests/cmd/submodules/unsupported.toml @@ -0,0 +1 @@ +args = ["https://crates.io/crates/nurl", "0.3.0", "--submodules"]