Skip to content

Commit

Permalink
fetchgit{,hub}: assert illegal tag + rev combinations
Browse files Browse the repository at this point in the history
It's quite a bit more complex due to this but this was asked for during review
  • Loading branch information
Atemu committed Dec 4, 2024
1 parent cb9f9a1 commit 5192fb7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
27 changes: 23 additions & 4 deletions pkgs/build-support/fetchgit/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 ? ""
Expand Down Expand Up @@ -62,12 +62,30 @@ lib.makeOverridable (lib.fetchers.withNormalizedHash { } (
assert deepClone -> leaveDotGit;
assert nonConeMode -> (sparseCheckout != []);

let
revWithTag =
let
warningMsg = "fetchgit requires either `rev` xor `tag`.";
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;

Expand All @@ -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}
Expand Down
11 changes: 7 additions & 4 deletions pkgs/build-support/fetchgithub/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 either `rev` xor `tag`.");

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 // {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 5192fb7

Please sign in to comment.