2024-05-02 00:46:19 +00:00
|
|
|
# https://github.com/siers/nix-gitignore/
|
|
|
|
|
|
|
|
{ lib, runCommand }:
|
|
|
|
|
|
|
|
# An interesting bit from the gitignore(5):
|
|
|
|
# - A slash followed by two consecutive asterisks then a slash matches
|
|
|
|
# - zero or more directories. For example, "a/**/b" matches "a/b",
|
|
|
|
# - "a/x/b", "a/x/y/b" and so on.
|
|
|
|
|
|
|
|
let
|
|
|
|
inherit (builtins) filterSource;
|
|
|
|
|
|
|
|
inherit (lib)
|
|
|
|
concatStringsSep
|
|
|
|
elemAt
|
|
|
|
filter
|
|
|
|
head
|
|
|
|
isList
|
|
|
|
length
|
|
|
|
optionals
|
|
|
|
optionalString
|
|
|
|
pathExists
|
|
|
|
readFile
|
|
|
|
removePrefix
|
|
|
|
replaceStrings
|
|
|
|
stringLength
|
|
|
|
sub
|
|
|
|
substring
|
|
|
|
toList
|
|
|
|
trace
|
|
|
|
;
|
|
|
|
|
|
|
|
inherit (lib.strings) match split typeOf;
|
|
|
|
|
|
|
|
debug = a: trace a a;
|
|
|
|
last = l: elemAt l ((length l) - 1);
|
2024-06-30 08:16:52 +00:00
|
|
|
in
|
|
|
|
rec {
|
2024-05-02 00:46:19 +00:00
|
|
|
# [["good/relative/source/file" true] ["bad.tmpfile" false]] -> root -> path
|
2024-06-30 08:16:52 +00:00
|
|
|
filterPattern =
|
|
|
|
patterns: root:
|
|
|
|
(
|
|
|
|
name: _type:
|
2024-05-02 00:46:19 +00:00
|
|
|
let
|
|
|
|
relPath = removePrefix ((toString root) + "/") name;
|
|
|
|
matches = pair: (match (head pair) relPath) != null;
|
2024-06-30 08:16:52 +00:00
|
|
|
matched = map (pair: [
|
|
|
|
(matches pair)
|
|
|
|
(last pair)
|
|
|
|
]) patterns;
|
2024-05-02 00:46:19 +00:00
|
|
|
in
|
2024-06-30 08:16:52 +00:00
|
|
|
last (
|
|
|
|
last (
|
|
|
|
[
|
|
|
|
[
|
|
|
|
true
|
|
|
|
true
|
|
|
|
]
|
|
|
|
]
|
|
|
|
++ (filter head matched)
|
|
|
|
)
|
|
|
|
)
|
2024-05-02 00:46:19 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
# string -> [[regex bool]]
|
2024-06-30 08:16:52 +00:00
|
|
|
gitignoreToPatterns =
|
|
|
|
gitignore:
|
2024-05-02 00:46:19 +00:00
|
|
|
let
|
|
|
|
# ignore -> bool
|
|
|
|
isComment = i: (match "^(#.*|$)" i) != null;
|
|
|
|
|
|
|
|
# ignore -> [ignore bool]
|
2024-06-30 08:16:52 +00:00
|
|
|
computeNegation =
|
|
|
|
l:
|
|
|
|
let
|
|
|
|
split = match "^(!?)(.*)" l;
|
|
|
|
in
|
|
|
|
[
|
|
|
|
(elemAt split 1)
|
|
|
|
(head split == "!")
|
|
|
|
];
|
2024-05-02 00:46:19 +00:00
|
|
|
|
|
|
|
# regex -> regex
|
2024-06-30 08:16:52 +00:00
|
|
|
handleHashesBangs =
|
|
|
|
replaceStrings
|
|
|
|
[
|
|
|
|
"\\#"
|
|
|
|
"\\!"
|
|
|
|
]
|
|
|
|
[
|
|
|
|
"#"
|
|
|
|
"!"
|
|
|
|
];
|
2024-05-02 00:46:19 +00:00
|
|
|
|
|
|
|
# ignore -> regex
|
|
|
|
substWildcards =
|
|
|
|
let
|
|
|
|
special = "^$.+{}()";
|
|
|
|
escs = "\\*?";
|
|
|
|
splitString =
|
2024-06-30 08:16:52 +00:00
|
|
|
let
|
|
|
|
recurse =
|
|
|
|
str:
|
|
|
|
[ (substring 0 1 str) ] ++ (optionals (str != "") (recurse (substring 1 (stringLength (str)) str)));
|
|
|
|
in
|
|
|
|
str: recurse str;
|
2024-05-02 00:46:19 +00:00
|
|
|
chars = s: filter (c: c != "" && !isList c) (splitString s);
|
|
|
|
escape = s: map (c: "\\" + c) (chars s);
|
|
|
|
in
|
2024-06-30 08:16:52 +00:00
|
|
|
replaceStrings
|
|
|
|
(
|
|
|
|
(chars special)
|
|
|
|
++ (escape escs)
|
|
|
|
++ [
|
|
|
|
"**/"
|
|
|
|
"**"
|
|
|
|
"*"
|
|
|
|
"?"
|
|
|
|
]
|
|
|
|
)
|
|
|
|
(
|
|
|
|
(escape special)
|
|
|
|
++ (escape escs)
|
|
|
|
++ [
|
|
|
|
"(.*/)?"
|
|
|
|
".*"
|
|
|
|
"[^/]*"
|
|
|
|
"[^/]"
|
|
|
|
]
|
|
|
|
);
|
2024-05-02 00:46:19 +00:00
|
|
|
|
|
|
|
# (regex -> regex) -> regex -> regex
|
2024-06-30 08:16:52 +00:00
|
|
|
mapAroundCharclass =
|
|
|
|
f: r: # rl = regex or list
|
|
|
|
let
|
|
|
|
slightFix = replaceStrings [ "\\]" ] [ "]" ];
|
2024-05-02 00:46:19 +00:00
|
|
|
in
|
2024-06-30 08:16:52 +00:00
|
|
|
concatStringsSep "" (
|
|
|
|
map (rl: if isList rl then slightFix (elemAt rl 0) else f rl) (split "(\\[([^\\\\]|\\\\.)+])" r)
|
|
|
|
);
|
2024-05-02 00:46:19 +00:00
|
|
|
|
|
|
|
# regex -> regex
|
2024-06-30 08:16:52 +00:00
|
|
|
handleSlashPrefix =
|
|
|
|
l:
|
2024-05-02 00:46:19 +00:00
|
|
|
let
|
|
|
|
split = (match "^(/?)(.*)" l);
|
|
|
|
findSlash = l: optionalString ((match ".+/.+" l) == null) l;
|
|
|
|
hasSlash = mapAroundCharclass findSlash l != l;
|
|
|
|
in
|
2024-06-30 08:16:52 +00:00
|
|
|
(if (elemAt split 0) == "/" || hasSlash then "^" else "(^|.*/)") + (elemAt split 1);
|
2024-05-02 00:46:19 +00:00
|
|
|
|
|
|
|
# regex -> regex
|
2024-06-30 08:16:52 +00:00
|
|
|
handleSlashSuffix =
|
|
|
|
l:
|
|
|
|
let
|
|
|
|
split = (match "^(.*)/$" l);
|
|
|
|
in
|
|
|
|
if split != null then (elemAt split 0) + "($|/.*)" else l;
|
2024-05-02 00:46:19 +00:00
|
|
|
|
|
|
|
# (regex -> regex) -> [regex, bool] -> [regex, bool]
|
2024-06-30 08:16:52 +00:00
|
|
|
mapPat = f: l: [
|
|
|
|
(f (head l))
|
|
|
|
(last l)
|
|
|
|
];
|
2024-05-02 00:46:19 +00:00
|
|
|
in
|
2024-06-30 08:16:52 +00:00
|
|
|
map (
|
|
|
|
l: # `l' for "line"
|
|
|
|
mapPat (
|
|
|
|
l: handleSlashSuffix (handleSlashPrefix (handleHashesBangs (mapAroundCharclass substWildcards l)))
|
|
|
|
) (computeNegation l)
|
|
|
|
) (filter (l: !isList l && !isComment l) (split "\n" gitignore));
|
2024-05-02 00:46:19 +00:00
|
|
|
|
|
|
|
gitignoreFilter = ign: root: filterPattern (gitignoreToPatterns ign) root;
|
|
|
|
|
|
|
|
# string|[string|file] (→ [string|file] → [string]) -> string
|
2024-06-30 08:16:52 +00:00
|
|
|
gitignoreCompileIgnore =
|
|
|
|
file_str_patterns: root:
|
2024-05-02 00:46:19 +00:00
|
|
|
let
|
|
|
|
onPath = f: a: if typeOf a == "path" then f a else a;
|
|
|
|
str_patterns = map (onPath readFile) (toList file_str_patterns);
|
2024-06-30 08:16:52 +00:00
|
|
|
in
|
|
|
|
concatStringsSep "\n" str_patterns;
|
2024-05-02 00:46:19 +00:00
|
|
|
|
2024-06-30 08:16:52 +00:00
|
|
|
gitignoreFilterPure =
|
|
|
|
predicate: patterns: root: name: type:
|
|
|
|
gitignoreFilter (gitignoreCompileIgnore patterns root) root name type && predicate name type;
|
2024-05-02 00:46:19 +00:00
|
|
|
|
|
|
|
# This is a very hacky way of programming this!
|
|
|
|
# A better way would be to reuse existing filtering by making multiple gitignore functions per each root.
|
|
|
|
# Then for each file find the set of roots with gitignores (and functions).
|
|
|
|
# This would make gitignoreFilterSource very different from gitignoreFilterPure.
|
|
|
|
# rootPath → gitignoresConcatenated
|
2024-06-30 08:16:52 +00:00
|
|
|
compileRecursiveGitignore =
|
|
|
|
root:
|
2024-05-02 00:46:19 +00:00
|
|
|
let
|
|
|
|
dirOrIgnore = file: type: baseNameOf file == ".gitignore" || type == "directory";
|
|
|
|
ignores = builtins.filterSource dirOrIgnore root;
|
2024-06-30 08:16:52 +00:00
|
|
|
in
|
|
|
|
readFile (
|
|
|
|
runCommand "${baseNameOf root}-recursive-gitignore" { } ''
|
2024-05-02 00:46:19 +00:00
|
|
|
cd ${ignores}
|
|
|
|
|
|
|
|
find -type f -exec sh -c '
|
|
|
|
rel="$(realpath --relative-to=. "$(dirname "$1")")/"
|
|
|
|
if [ "$rel" = "./" ]; then rel=""; fi
|
|
|
|
|
|
|
|
awk -v prefix="$rel" -v root="$1" -v top="$(test -z "$rel" && echo 1)" "
|
|
|
|
BEGIN { print \"# \"root }
|
|
|
|
|
|
|
|
/^!?[^\\/]+\/?$/ {
|
|
|
|
match(\$0, /^!?/, negation)
|
|
|
|
sub(/^!?/, \"\")
|
|
|
|
|
|
|
|
if (top) { middle = \"\" } else { middle = \"**/\" }
|
|
|
|
|
|
|
|
print negation[0] prefix middle \$0
|
|
|
|
}
|
|
|
|
|
|
|
|
/^!?(\\/|.*\\/.+$)/ {
|
|
|
|
match(\$0, /^!?/, negation)
|
|
|
|
sub(/^!?/, \"\")
|
|
|
|
|
|
|
|
if (!top) sub(/^\//, \"\")
|
|
|
|
|
|
|
|
print negation[0] prefix \$0
|
|
|
|
}
|
|
|
|
|
|
|
|
END { print \"\" }
|
|
|
|
" "$1"
|
|
|
|
' sh {} \; > $out
|
2024-06-30 08:16:52 +00:00
|
|
|
''
|
|
|
|
);
|
2024-05-02 00:46:19 +00:00
|
|
|
|
2024-06-30 08:16:52 +00:00
|
|
|
withGitignoreFile = patterns: root: toList patterns ++ [ ".git" ] ++ [ (root + "/.gitignore") ];
|
2024-05-02 00:46:19 +00:00
|
|
|
|
2024-06-30 08:16:52 +00:00
|
|
|
withRecursiveGitignoreFile =
|
|
|
|
patterns: root: toList patterns ++ [ ".git" ] ++ [ (compileRecursiveGitignore root) ];
|
2024-05-02 00:46:19 +00:00
|
|
|
|
|
|
|
# filterSource derivatives
|
|
|
|
|
2024-06-30 08:16:52 +00:00
|
|
|
gitignoreFilterSourcePure =
|
|
|
|
predicate: patterns: root:
|
2024-05-02 00:46:19 +00:00
|
|
|
filterSource (gitignoreFilterPure predicate patterns root) root;
|
|
|
|
|
2024-06-30 08:16:52 +00:00
|
|
|
gitignoreFilterSource =
|
|
|
|
predicate: patterns: root:
|
2024-05-02 00:46:19 +00:00
|
|
|
gitignoreFilterSourcePure predicate (withGitignoreFile patterns root) root;
|
|
|
|
|
2024-06-30 08:16:52 +00:00
|
|
|
gitignoreFilterRecursiveSource =
|
|
|
|
predicate: patterns: root:
|
2024-05-02 00:46:19 +00:00
|
|
|
gitignoreFilterSourcePure predicate (withRecursiveGitignoreFile patterns root) root;
|
|
|
|
|
|
|
|
# "Predicate"-less alternatives
|
|
|
|
|
|
|
|
gitignoreSourcePure = gitignoreFilterSourcePure (_: _: true);
|
2024-06-30 08:16:52 +00:00
|
|
|
gitignoreSource =
|
|
|
|
patterns:
|
|
|
|
let
|
|
|
|
type = typeOf patterns;
|
|
|
|
in
|
|
|
|
if (type == "string" && pathExists patterns) || type == "path" then
|
|
|
|
throw "type error in gitignoreSource(patterns -> source -> path), " "use [] or \"\" if there are no additional patterns"
|
|
|
|
else
|
|
|
|
gitignoreFilterSource (_: _: true) patterns;
|
2024-05-02 00:46:19 +00:00
|
|
|
|
|
|
|
gitignoreRecursiveSource = gitignoreFilterSourcePure (_: _: true);
|
|
|
|
}
|