on 19052d8 help sign in
haskell.nix/lib/clean-git.nix
201 lines 7.8 kB history raw
1# From https://github.com/NixOS/nix/issues/2944
2{ lib, runCommand, git, cleanSourceWith }:
3{ name ? null, src, subDir ? "", includeSiblings ? false, keepGitDir ? false }:
5# The function call
6#
7# gitSource ./toplevel subpath
8#
9# creates a Nix store path of ./toplevel/subpath that includes only those files
10# tracked by git. More precisely: mentioned in the git index (i.e. git add is enough
11# to get them to be included, you do not have to commit).
13# This is a whitelist-based alternative to manually listing files or using
14# nix-gitignore.
16# Internally, it works by calling git ls-files at evaluation time. To
17# avoid copying all of `.git` to the git store, it only copies the least amount
18# of files necessary for `git ls-files` to work; this is a bit fragile, but
19# very fast.
21with builtins;
23# We read the git index once, before getting the subdir parameter, so that it
24# is shared among multiple invocations of gitSource:
26let
27 remove_prefix = prefix: s:
28 builtins.substring
29 (builtins.stringLength prefix)
30 (builtins.stringLength s - builtins.stringLength prefix)
31 s;
33 lines = s: filter (x : x != [] && x != "") (split "\n" s);
35 origSrcSubDir = toString (src.origSrcSubDir or src);
36in
38if builtins.pathExists (origSrcSubDir + "/.git")
39then
40 let
41 hasIndex = builtins.pathExists (origSrcSubDir + "/.git/index");
42 isWorktree = (builtins.readDir origSrcSubDir).".git" == "regular";
44 # Identify the .git directory and filter just the files that we need.
45 gitDir = cleanSourceWith ({
46 caller = "cleanGit";
47 name = (if name == null then "" else name + "-") + "gitFiles";
48 filter = path: type:
49 type == "directory" ||
50 lib.any (i: (lib.hasSuffix i path)) [
51 "/config" "/index" "/HEAD" ] ||
52 (lib.strings.hasInfix "modules/" path &&
53 lib.any (i: (lib.hasSuffix i path)) [
54 "/config" "/index" "/HEAD" "/objects" "/refs" ]);
55 } // (
56 if hasIndex
57 then { inherit src; subDir = ".git"; }
58 else if !isWorktree
59 then abort "cleanGit: ${origSrcSubDir + "/.git"} has no index file"
60 else {
61 # likely a git worktree, so follow the indirection
62 src =
63 let
64 git_content = lines (readFile (origSrcSubDir + "/.git"));
65 first_line = head git_content;
66 prefix = "gitdir: ";
67 ok = length git_content == 1 && lib.hasPrefix prefix first_line;
68 in
69 if ok
70 then /. + remove_prefix prefix first_line
71 else abort "gitSource.nix: Cannot parse ${origSrcSubDir + "/.git"}";
72 }));
74 # Worktrees have a commondir pointing to the common `.git` dir. We need the
75 # config file from there to get the list of active submodules right.
76 commonConfig = if builtins.pathExists (toString gitDir.origSrc + "/commondir")
77 then
78 # The commondir file contains just the relative path of the
79 # common `.git` dir.
80 let
81 git_content = lines (readFile (toString gitDir.origSrc + "/commondir"));
82 first_line = head git_content;
83 in gitDir.origSrc + ("/" + first_line + "/config")
84 else gitDir + "/config";
86 # We need the .gitmodules file for submoules to work.
87 gitModulesStr = origSrcSubDir + "/.gitmodules";
88 gitModules = builtins.path { name = "gitmodules"; path = gitModulesStr; };
90 # Files from the git repository related knownSubmoduleDirs.
91 gitSubmoduleFiles = knownSubmoduleDirs:
92 let pathsNeeded = map (p: toString (src.origSrcSubDir or src) + "/${p}") (
93 lib.concatMap (x: all_paren_dirs (x + "/.git")) knownSubmoduleDirs);
94 in cleanSourceWith {
95 caller = "cleanGit";
96 name = (if name == null then "" else name + "-") + "gitSubmoduleFiles";
97 inherit src;
98 filter = path: type:
99 elem path pathsNeeded
100 ||
101 lib.any (i: (lib.hasSuffix i path)) [
102 ".gitmodules" ".git/config" ".git/index" ".git/HEAD" ".git/objects" ".git/refs" ]
103 ||
104 (lib.strings.hasInfix ".git/modules/" path &&
105 lib.any (i: (lib.hasSuffix i path)) [
106 "config" "index" "HEAD" "objects" "refs" ]);
107 };
109 hasSubmodules = !isWorktree && builtins.pathExists gitModulesStr;
111 # Make a temporary dir that looks enough like the real thing for
112 # `git ls-files --recurse-submodules` to give us an accurate list
113 # of all the files in the index.
114 whitelist_files = knownSubmoduleDirs:
115 runCommand "git-ls-files" {} ''
116 tmp=$(mktemp -d)
117 cd $tmp
118 ${ lib.optionalString hasSubmodules ''
119 cp -ra ${gitSubmoduleFiles knownSubmoduleDirs}/. $tmp
120 chmod +w -R $tmp
121 ''}
122 cp -r ${gitDir} .git
123 chmod +w -R .git
124 mkdir -p .git/objects .git/refs
125 mkdir -p .git-common/objects .git-common/refs
126 cp ${commonConfig} .git-common/config
127 echo ../.git-common > .git/commondir
128 ${ lib.optionalString (isWorktree && builtins.pathExists gitModulesStr) ''
129 cp ${gitModules} ./.gitmodules
130 ''}
131 mkdir $out
132 ${ lib.optionalString hasSubmodules ''
133 ${git}/bin/git submodule status --recursive | awk '{ print $2 }' > $out/submoduleDirs
134 ''}
135 ${git}/bin/git ls-files --recurse-submodules > $out/files
136 '';
138 # Find the submodules
139 whitelist_recursive = knownSubmoduleDirs:
140 let
141 # Get the new list of submoduleDirs and files
142 # (useing the submoduleDirs we already know about)
143 files = whitelist_files knownSubmoduleDirs;
144 new = {
145 submoduleDirs = lines (readFile (files + "/submoduleDirs"));
146 files = lines (readFile (files + "/files"));
147 };
148 in
149 # If we are not expecting submodules or if the new list does not
150 # contain any additional submodules we are done.
151 if !hasSubmodules || lib.all (x: elem x knownSubmoduleDirs) new.submoduleDirs
152 then new
153 else
154 # Look again using what we know now about the submodules
155 whitelist_recursive new.submoduleDirs;
157 # Use empty as a starting point for looking for submodules.
158 # We could allow a list to be passed into cleanGit, but when testing
159 # on the `ghcjs` repo (one with a lot of submodules) it did not
160 # make much of a difference to the speed of `cleanGit`.
161 whitelist = whitelist_recursive [];
163 all_paren_dirs = p:
164 if p == "." || p == "/"
165 then []
166 else [ p ] ++ all_paren_dirs (dirOf p);
168 # All the paths that we need to keep as a set (including parent dirs)
169 whitelist_set = listToAttrs (
170 concatMap (p:
171 # Using `origSrcSubDir` (if present) makes it possible to cleanGit src that
172 # has already been cleaned with cleanSrcWith.
173 let full_path = toString (src.origSrcSubDir or src) + "/${p}"; in
174 map (p': { name = p'; value = true; }) (all_paren_dirs full_path)
175 ) whitelist.files
176 );
178 # Identify files in the `.git` dir
179 isGitDirPath = path:
180 path == origSrcSubDir + "/.git"
181 || lib.hasPrefix (origSrcSubDir + "/.git/") path;
183 filter = path: type:
184 hasAttr (toString path) whitelist_set
185 || (keepGitDir && isGitDirPath path);
186 in
187 cleanSourceWith {
188 caller = "cleanGit";
189 inherit name src subDir includeSiblings filter;
190 } // {
191 # For testing
192 # inherit whitelist whitelist_set;
193 }
195else
196 trace "haskell-nix.haskellLib.cleanGit: ${origSrcSubDir} does not seem to be a git repository,\nassuming it is a clean checkout." (
197 cleanSourceWith {
198 caller = "cleanGit";
199 inherit name src subDir includeSiblings;
200 }
201 )