···2929 in type == "directory" || lib.any (ext: lib.hasSuffix ext base) exts;
3030 in builtins.filterSource filter path;
31313232+ # Get the commit id of a git repo
3333+ # Example: commitIdFromGitRepo <nixpkgs/.git>
3434+ commitIdFromGitRepo =
3535+ let readCommitFromFile = path: file:
3636+ with builtins;
3737+ let fileName = toString path + "/" + file;
3838+ packedRefsName = toString path + "/packed-refs";
3939+ in if lib.pathExists fileName
4040+ then
4141+ let fileContent = readFile fileName;
4242+ # Sometimes git stores the commitId directly in the file but
4343+ # sometimes it stores something like: «ref: refs/heads/branch-name»
4444+ matchRef = match "^ref: (.*)\n$" fileContent;
4545+ in if isNull matchRef
4646+ then lib.removeSuffix "\n" fileContent
4747+ else readCommitFromFile path (lib.head matchRef)
4848+ # Sometimes, the file isn't there at all and has been packed away in the
4949+ # packed-refs file, so we have to grep through it:
5050+ else if lib.pathExists packedRefsName
5151+ then
5252+ let packedRefs = lib.splitString "\n" (readFile packedRefsName);
5353+ matchRule = match ("^(.*) " + file + "$");
5454+ matchedRefs = lib.flatten (lib.filter (m: ! (isNull m)) (map matchRule packedRefs));
5555+ in lib.head matchedRefs
5656+ else throw ("Not a .git directory: " + path);
5757+ in lib.flip readCommitFromFile "HEAD";
3258}