1# Functions for copying sources to the Nix store.
2
3let lib = import ./default.nix; in
4
5rec {
6
7 # Returns the type of a path: regular (for file), symlink, or directory
8 pathType = p: with builtins; getAttr (baseNameOf p) (readDir (dirOf p));
9
10 # Returns true if the path exists and is a directory, false otherwise
11 pathIsDirectory = p: if builtins.pathExists p then (pathType p) == "directory" else false;
12
13 # Bring in a path as a source, filtering out all Subversion and CVS
14 # directories, as well as backup files (*~).
15 cleanSourceFilter = name: type: let baseName = baseNameOf (toString name); in ! (
16 # Filter out Subversion and CVS directories.
17 (type == "directory" && (baseName == ".git" || baseName == ".svn" || baseName == "CVS" || baseName == ".hg")) ||
18 # Filter out backup files.
19 lib.hasSuffix "~" baseName ||
20 builtins.match "^.*\.sw[a-z]$" baseName != null ||
21
22 # Filter out generates files.
23 lib.hasSuffix ".o" baseName ||
24 lib.hasSuffix ".so" baseName ||
25 # Filter out nix-build result symlinks
26 (type == "symlink" && lib.hasPrefix "result" baseName)
27 );
28
29 cleanSource = builtins.filterSource cleanSourceFilter;
30
31 # Filter sources by a list of regular expressions.
32 #
33 # E.g. `src = sourceByRegex ./my-subproject [".*\.py$" "^database.sql$"]`
34 sourceByRegex = src: regexes: builtins.filterSource (path: type:
35 let relPath = lib.removePrefix (toString src + "/") (toString path);
36 in lib.any (re: builtins.match re relPath != null) regexes) src;
37
38 # Get all files ending with the specified suffices from the given
39 # directory or its descendants. E.g. `sourceFilesBySuffices ./dir
40 # [".xml" ".c"]'.
41 sourceFilesBySuffices = path: exts:
42 let filter = name: type:
43 let base = baseNameOf (toString name);
44 in type == "directory" || lib.any (ext: lib.hasSuffix ext base) exts;
45 in builtins.filterSource filter path;
46
47
48 # Get the commit id of a git repo
49 # Example: commitIdFromGitRepo <nixpkgs/.git>
50 commitIdFromGitRepo =
51 let readCommitFromFile = path: file:
52 with builtins;
53 let fileName = toString path + "/" + file;
54 packedRefsName = toString path + "/packed-refs";
55 in if lib.pathExists fileName
56 then
57 let fileContent = lib.fileContents fileName;
58 # Sometimes git stores the commitId directly in the file but
59 # sometimes it stores something like: «ref: refs/heads/branch-name»
60 matchRef = match "^ref: (.*)$" fileContent;
61 in if isNull matchRef
62 then fileContent
63 else readCommitFromFile path (lib.head matchRef)
64 # Sometimes, the file isn't there at all and has been packed away in the
65 # packed-refs file, so we have to grep through it:
66 else if lib.pathExists packedRefsName
67 then
68 let fileContent = readFile packedRefsName;
69 matchRef = match (".*\n([^\n ]*) " + file + "\n.*") fileContent;
70 in if isNull matchRef
71 then throw ("Could not find " + file + " in " + packedRefsName)
72 else lib.head matchRef
73 else throw ("Not a .git directory: " + path);
74 in lib.flip readCommitFromFile "HEAD";
75}