1 {lib, stdenvNoCC, git, git-lfs, cacert}: let
2 urlToName = url: rev: let
3 inherit (lib) removeSuffix splitString last;
4 base = last (splitString ":" (baseNameOf (removeSuffix "/" url)));
6 matched = builtins.match "(.*)\\.git" base;
8 short = builtins.substring 0 7 rev;
10 appendShort = lib.optionalString ((builtins.match "[a-f0-9]*" rev) != null) "-${short}";
11 in "${if matched == null then base else builtins.head matched}${appendShort}";
13 lib.makeOverridable (lib.fetchers.withNormalizedHash { } (
14 { url, rev ? "HEAD", leaveDotGit ? deepClone
15 , outputHash ? lib.fakeHash, outputHashAlgo ? null
16 , fetchSubmodules ? true, deepClone ? false
20 , name ? urlToName url rev
21 , # Shell code executed after the file has been fetched
22 # successfully. This can do things like check or transform the file.
24 , preferLocalBuild ? true
26 , # Shell code to build a netrc file for BASIC auth
28 , # Impure env vars (https://nixos.org/nix/manual/#sec-advanced-attributes)
29 # needed for netrcPhase
30 netrcImpureEnvVars ? []
32 , allowedRequisites ? null
36 fetchgit has one problem: git fetch only works for refs.
37 This is because fetching arbitrary (maybe dangling) commits creates garbage collection risks
38 and checking whether a commit belongs to a ref is expensive. This may
39 change in the future when some caching is added to git (?)
40 Usually refs are either tags (refs/tags/*) or branches (refs/heads/*)
41 Cloning branches will make the hash check fail when there is an update.
42 But not all patches we want can be accessed by tags.
44 The workaround is getting the last n commits so that it's likely that they
45 still contain the hash we want.
47 for now : increase depth iteratively (TODO)
49 real fix: ask git folks to add a
50 git fetch $HASH contained in $BRANCH
51 facility because checking that $HASH is contained in $BRANCH is less
52 expensive than fetching --depth $N.
53 Even if git folks implemented this feature soon it may take years until
54 server admins start using the new version?
57 assert deepClone -> leaveDotGit;
58 assert nonConeMode -> (sparseCheckout != []);
60 if builtins.isString sparseCheckout then
61 # Changed to throw on 2023-06-04
62 throw "Please provide directories/patterns for sparse checkout as a list of strings. Passing a (multi-line) string is not supported any more."
64 stdenvNoCC.mkDerivation {
66 builder = ./builder.sh;
67 fetcher = ./nix-prefetch-git;
69 nativeBuildInputs = [ git cacert ]
70 ++ lib.optionals fetchLFS [ git-lfs ];
72 inherit outputHash outputHashAlgo;
73 outputHashMode = "recursive";
75 # git-sparse-checkout(1) says:
76 # > When the --stdin option is provided, the directories or patterns are read
77 # > from standard in as a newline-delimited list instead of from the arguments.
78 sparseCheckout = builtins.concatStringsSep "\n" sparseCheckout;
80 inherit url rev leaveDotGit fetchLFS fetchSubmodules deepClone branchName nonConeMode postFetch;
82 postHook = if netrcPhase == null then null else ''
84 # required that git uses the netrc file
86 export NETRC=$PWD/.netrc
90 impureEnvVars = lib.fetchers.proxyImpureEnvVars ++ netrcImpureEnvVars ++ [
91 "GIT_PROXY_COMMAND" "NIX_GIT_SSL_CAINFO" "SOCKS_SERVER"
95 inherit preferLocalBuild meta allowedRequisites;