fetchgit{,hub}: assert illegal tag + rev combinations

It's quite a bit more complex due to this but this was asked for during review
This commit is contained in:
Atemu
2024-12-05 16:29:11 +01:00
parent cb9f9a1e5a
commit e9a31f5146
2 changed files with 30 additions and 8 deletions
+23 -4
View File
@@ -15,14 +15,14 @@ lib.makeOverridable (lib.fetchers.withNormalizedHash { } (
# doc/build-helpers/fetchers.chapter.md
{ url
, tag ? null
, rev ? if tag != null then "refs/tags/${tag}" else "HEAD" # FIXME fetching HEAD by default is problematic at best
, rev ? null
, leaveDotGit ? deepClone
, outputHash ? lib.fakeHash, outputHashAlgo ? null
, fetchSubmodules ? true, deepClone ? false
, branchName ? null
, sparseCheckout ? []
, nonConeMode ? false
, name ? urlToName url rev
, name ? null
, # Shell code executed after the file has been fetched
# successfully. This can do things like check or transform the file.
postFetch ? ""
@@ -62,12 +62,30 @@ lib.makeOverridable (lib.fetchers.withNormalizedHash { } (
assert deepClone -> leaveDotGit;
assert nonConeMode -> (sparseCheckout != []);
let
revWithTag =
let
warningMsg = "fetchgit requires one of either `rev` or `tag` to be provided (not both).";
otherIsNull = other: lib.assertMsg (other == null) warningMsg;
in
if tag != null then
assert (otherIsNull rev);
"refs/tags/${tag}"
else if rev != null then
assert (otherIsNull tag);
rev
else
# FIXME fetching HEAD if no rev or tag is provided is problematic at best
"HEAD";
in
if builtins.isString sparseCheckout then
# Changed to throw on 2023-06-04
throw "Please provide directories/patterns for sparse checkout as a list of strings. Passing a (multi-line) string is not supported any more."
else
stdenvNoCC.mkDerivation {
inherit name;
name = if name != null then name else urlToName url revWithTag;
builder = ./builder.sh;
fetcher = ./nix-prefetch-git;
@@ -82,7 +100,8 @@ stdenvNoCC.mkDerivation {
# > from standard in as a newline-delimited list instead of from the arguments.
sparseCheckout = builtins.concatStringsSep "\n" sparseCheckout;
inherit url rev leaveDotGit fetchLFS fetchSubmodules deepClone branchName nonConeMode postFetch;
inherit url leaveDotGit fetchLFS fetchSubmodules deepClone branchName nonConeMode postFetch;
rev = revWithTag;
postHook = if netrcPhase == null then null else ''
${netrcPhase}
+7 -4
View File
@@ -3,7 +3,7 @@
lib.makeOverridable (
{ owner, repo
, tag ? null
, rev ? if tag != null then "refs/tags/${tag}" else null
, rev ? null
, name ? "source"
, fetchSubmodules ? false, leaveDotGit ? null
, deepClone ? false, private ? false, forceFetchGit ? false
@@ -14,13 +14,16 @@ lib.makeOverridable (
, ... # For hash agility
}@args:
assert (lib.assertMsg (rev != null) "You must provide `fetchFromGitHub with a `rev` or `tag`.");
assert (lib.assertMsg (lib.xor (tag == null) (rev == null)) "fetchFromGitHub requires one of either `rev` or `tag` to be provided (not both).");
let
position = (if args.meta.description or null != null
then builtins.unsafeGetAttrPos "description" args.meta
else builtins.unsafeGetAttrPos "rev" args
else if tag != null then
builtins.unsafeGetAttrPos "tag" args
else
builtins.unsafeGetAttrPos "rev" args
);
baseUrl = "https://${githubBase}/${owner}/${repo}";
newMeta = meta // {
@@ -61,7 +64,7 @@ let
inherit tag rev deepClone fetchSubmodules sparseCheckout fetchLFS; url = gitRepoUrl;
} // lib.optionalAttrs (leaveDotGit != null) { inherit leaveDotGit; }
else {
url = "${baseUrl}/archive/${rev}.tar.gz";
url = "${baseUrl}/archive/${if tag != null then "refs/tags/${tag}" else rev}.tar.gz";
passthru = {
inherit gitRepoUrl;