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;
34 inherit (lib.strings) match split typeOf;
37 last = l: elemAt l ((length l) - 1);
39 # [["good/relative/source/file" true] ["bad.tmpfile" false]] -> root -> path
40 filterPattern = patterns: root:
43 relPath = removePrefix ((toString root) + "/") name;
44 matches = pair: (match (head pair) relPath) != null;
45 matched = map (pair: [(matches pair) (last pair)]) patterns;
47 last (last ([[true true]] ++ (filter head matched)))
50 # string -> [[regex bool]]
51 gitignoreToPatterns = gitignore:
54 isComment = i: (match "^(#.*|$)" i) != null;
56 # ignore -> [ignore bool]
58 let split = match "^(!?)(.*)" l;
59 in [(elemAt split 1) (head split == "!")];
62 handleHashesBangs = replaceStrings ["\\#" "\\!"] ["#" "!"];
70 let recurse = str : [(substring 0 1 str)] ++
71 (optionals (str != "") (recurse (substring 1 (stringLength(str)) str) ));
73 chars = s: filter (c: c != "" && !isList c) (splitString s);
74 escape = s: map (c: "\\" + c) (chars s);
77 ((chars special) ++ (escape escs) ++ ["**/" "**" "*" "?"])
78 ((escape special) ++ (escape escs) ++ ["(.*/)?" ".*" "[^/]*" "[^/]"]);
80 # (regex -> regex) -> regex -> regex
81 mapAroundCharclass = f: r: # rl = regex or list
82 let slightFix = replaceStrings ["\\]"] ["]"];
85 (map (rl: if isList rl then slightFix (elemAt rl 0) else f rl)
86 (split "(\\[([^\\\\]|\\\\.)+])" r));
89 handleSlashPrefix = l:
91 split = (match "^(/?)(.*)" l);
92 findSlash = l: optionalString ((match ".+/.+" l) == null) l;
93 hasSlash = mapAroundCharclass findSlash l != l;
95 (if (elemAt split 0) == "/" || hasSlash
101 handleSlashSuffix = l:
102 let split = (match "^(.*)/$" l);
103 in if split != null then (elemAt split 0) + "($|/.*)" else l;
105 # (regex -> regex) -> [regex, bool] -> [regex, bool]
106 mapPat = f: l: [(f (head l)) (last l)];
108 map (l: # `l' for "line"
109 mapPat (l: handleSlashSuffix (handleSlashPrefix (handleHashesBangs (mapAroundCharclass substWildcards l))))
111 (filter (l: !isList l && !isComment l)
112 (split "\n" gitignore));
114 gitignoreFilter = ign: root: filterPattern (gitignoreToPatterns ign) root;
116 # string|[string|file] (→ [string|file] → [string]) -> string
117 gitignoreCompileIgnore = file_str_patterns: root:
119 onPath = f: a: if typeOf a == "path" then f a else a;
120 str_patterns = map (onPath readFile) (toList file_str_patterns);
121 in concatStringsSep "\n" str_patterns;
123 gitignoreFilterPure = predicate: patterns: root: name: type:
124 gitignoreFilter (gitignoreCompileIgnore patterns root) root name type
125 && predicate name type;
127 # This is a very hacky way of programming this!
128 # A better way would be to reuse existing filtering by making multiple gitignore functions per each root.
129 # Then for each file find the set of roots with gitignores (and functions).
130 # This would make gitignoreFilterSource very different from gitignoreFilterPure.
131 # rootPath → gitignoresConcatenated
132 compileRecursiveGitignore = root:
134 dirOrIgnore = file: type: baseNameOf file == ".gitignore" || type == "directory";
135 ignores = builtins.filterSource dirOrIgnore root;
137 runCommand "${baseNameOf root}-recursive-gitignore" {} ''
140 find -type f -exec sh -c '
141 rel="$(realpath --relative-to=. "$(dirname "$1")")/"
142 if [ "$rel" = "./" ]; then rel=""; fi
144 awk -v prefix="$rel" -v root="$1" -v top="$(test -z "$rel" && echo 1)" "
145 BEGIN { print \"# \"root }
148 match(\$0, /^!?/, negation)
151 if (top) { middle = \"\" } else { middle = \"**/\" }
153 print negation[0] prefix middle \$0
156 /^!?(\\/|.*\\/.+$)/ {
157 match(\$0, /^!?/, negation)
160 if (!top) sub(/^\//, \"\")
162 print negation[0] prefix \$0
170 withGitignoreFile = patterns: root:
171 toList patterns ++ [ ".git" ] ++ [(root + "/.gitignore")];
173 withRecursiveGitignoreFile = patterns: root:
174 toList patterns ++ [ ".git" ] ++ [(compileRecursiveGitignore root)];
176 # filterSource derivatives
178 gitignoreFilterSourcePure = predicate: patterns: root:
179 filterSource (gitignoreFilterPure predicate patterns root) root;
181 gitignoreFilterSource = predicate: patterns: root:
182 gitignoreFilterSourcePure predicate (withGitignoreFile patterns root) root;
184 gitignoreFilterRecursiveSource = predicate: patterns: root:
185 gitignoreFilterSourcePure predicate (withRecursiveGitignoreFile patterns root) root;
187 # "Predicate"-less alternatives
189 gitignoreSourcePure = gitignoreFilterSourcePure (_: _: true);
190 gitignoreSource = patterns: let type = typeOf patterns; in
191 if (type == "string" && pathExists patterns) || type == "path"
193 "type error in gitignoreSource(patterns -> source -> path), "
194 "use [] or \"\" if there are no additional patterns"
195 else gitignoreFilterSource (_: _: true) patterns;
197 gitignoreRecursiveSource = gitignoreFilterSourcePure (_: _: true);