1 # https://github.com/siers/nix-gitignore/
5 # An interesting bit from the gitignore(5):
6 # - A slash followed by two consecutive asterisks then a slash matches
7 # - zero or more directories. For example, "a/**/b" matches "a/b",
8 # - "a/x/b", "a/x/y/b" and so on.
11 inherit (builtins) filterSource;
33 inherit (lib.strings) match split typeOf;
36 last = l: elemAt l ((length l) - 1);
39 # [["good/relative/source/file" true] ["bad.tmpfile" false]] -> root -> path
45 relPath = removePrefix ((toString root) + "/") name;
46 matches = pair: (match (head pair) relPath) != null;
47 matched = map (pair: [
60 ++ (filter head matched)
65 # string -> [[regex bool]]
70 isComment = i: (match "^(#.*|$)" i) != null;
72 # ignore -> [ignore bool]
76 split = match "^(!?)(.*)" l;
84 handleHashesBangs = replaceStrings [ "\\#" "\\!" ] [ "#" "!" ];
95 [ (substring 0 1 str) ] ++ (optionals (str != "") (recurse (substring 1 (stringLength (str)) str)));
98 chars = s: filter (c: c != "" && !isList c) (splitString s);
99 escape = s: map (c: "\\" + c) (chars s);
123 # (regex -> regex) -> regex -> regex
125 f: r: # rl = regex or list
127 slightFix = replaceStrings [ "\\]" ] [ "]" ];
129 concatStringsSep "" (
130 map (rl: if isList rl then slightFix (elemAt rl 0) else f rl) (split "(\\[([^\\\\]|\\\\.)+])" r)
137 split = (match "^(/?)(.*)" l);
138 findSlash = l: optionalString ((match ".+/.+" l) == null) l;
139 hasSlash = mapAroundCharclass findSlash l != l;
141 (if (elemAt split 0) == "/" || hasSlash then "^" else "(^|.*/)") + (elemAt split 1);
147 split = (match "^(.*)/$" l);
149 if split != null then (elemAt split 0) + "($|/.*)" else l;
151 # (regex -> regex) -> [regex, bool] -> [regex, bool]
160 l: handleSlashSuffix (handleSlashPrefix (handleHashesBangs (mapAroundCharclass substWildcards l)))
161 ) (computeNegation l)
162 ) (filter (l: !isList l && !isComment l) (split "\n" gitignore));
164 gitignoreFilter = ign: root: filterPattern (gitignoreToPatterns ign) root;
166 # string|[string|file] (→ [string|file] → [string]) -> string
167 gitignoreCompileIgnore =
168 file_str_patterns: root:
170 onPath = f: a: if typeOf a == "path" then f a else a;
171 str_patterns = map (onPath readFile) (toList file_str_patterns);
173 concatStringsSep "\n" str_patterns;
175 gitignoreFilterPure =
176 predicate: patterns: root: name: type:
177 gitignoreFilter (gitignoreCompileIgnore patterns root) root name type && predicate name type;
179 # This is a very hacky way of programming this!
180 # A better way would be to reuse existing filtering by making multiple gitignore functions per each root.
181 # Then for each file find the set of roots with gitignores (and functions).
182 # This would make gitignoreFilterSource very different from gitignoreFilterPure.
183 # rootPath → gitignoresConcatenated
184 compileRecursiveGitignore =
187 dirOrIgnore = file: type: baseNameOf file == ".gitignore" || type == "directory";
188 ignores = builtins.filterSource dirOrIgnore root;
191 runCommand "${baseNameOf root}-recursive-gitignore" { } ''
194 find -type f -exec sh -c '
195 rel="$(realpath --relative-to=. "$(dirname "$1")")/"
196 if [ "$rel" = "./" ]; then rel=""; fi
198 awk -v prefix="$rel" -v root="$1" -v top="$(test -z "$rel" && echo 1)" "
199 BEGIN { print \"# \"root }
202 match(\$0, /^!?/, negation)
205 if (top) { middle = \"\" } else { middle = \"**/\" }
207 print negation[0] prefix middle \$0
210 /^!?(\\/|.*\\/.+$)/ {
211 match(\$0, /^!?/, negation)
214 if (!top) sub(/^\//, \"\")
216 print negation[0] prefix \$0
225 withGitignoreFile = patterns: root: toList patterns ++ [ ".git" ] ++ [ (root + "/.gitignore") ];
227 withRecursiveGitignoreFile =
228 patterns: root: toList patterns ++ [ ".git" ] ++ [ (compileRecursiveGitignore root) ];
230 # filterSource derivatives
232 gitignoreFilterSourcePure =
233 predicate: patterns: root:
234 filterSource (gitignoreFilterPure predicate patterns root) root;
236 gitignoreFilterSource =
237 predicate: patterns: root:
238 gitignoreFilterSourcePure predicate (withGitignoreFile patterns root) root;
240 gitignoreFilterRecursiveSource =
241 predicate: patterns: root:
242 gitignoreFilterSourcePure predicate (withRecursiveGitignoreFile patterns root) root;
244 # "Predicate"-less alternatives
246 gitignoreSourcePure = gitignoreFilterSourcePure (_: _: true);
250 type = typeOf patterns;
252 if (type == "string" && pathExists patterns) || type == "path" then
253 throw "type error in gitignoreSource(patterns -> source -> path), " "use [] or \"\" if there are no additional patterns"
255 gitignoreFilterSource (_: _: true) patterns;
257 gitignoreRecursiveSource = gitignoreFilterSourcePure (_: _: true);