Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163import Darwinimport Foundation
struct NameIndexEntry { let path: String let mode: String let oid: String}
enum NameGit { static func run(_ arguments: [String], root: URL, input: Data? = nil) throws -> Data { try DevProcess.checked("git", arguments, input: input, directory: root) } static func entries(root: URL) throws -> [NameIndexEntry] { try run(["ls-files", "--stage", "-z"], root: root).split(separator: 0).map { record in guard let tab = record.firstIndex(of: 9) else { throw DevError("Invalid Git index record") } let metadata = String(decoding: record[..<tab], as: UTF8.self).split(separator: " ") guard metadata.count == 3 else { throw DevError("Invalid Git index metadata") } let path = String(decoding: record[record.index(after: tab)...], as: UTF8.self) guard metadata[2] == "0" else { throw DevError("Resolve the index conflict in \(path) before checking") } return NameIndexEntry(path: path, mode: String(metadata[0]), oid: String(metadata[1])) } } static func blobs(_ oids: [String], root: URL) throws -> [String: Data] { guard !oids.isEmpty else { return [:] } let input = Data((oids.joined(separator: "\n") + "\n").utf8) let data = try run(["cat-file", "--batch"], root: root, input: input) var position = data.startIndex var result: [String: Data] = [:] for oid in oids { guard let end = data[position...].firstIndex(of: 10) else { throw DevError("Could not read indexed blob \(oid): invalid batch header") } let header = String(decoding: data[position..<end], as: UTF8.self).split(separator: " ") guard header.count == 3, header[0] == oid, header[1] == "blob", let size = Int(header[2]), size >= 0 else { throw DevError("Could not read indexed blob \(oid): invalid batch header") } let start = end + 1 guard size < data.endIndex - start, data[start + size] == 10 else { throw DevError("Could not read indexed blob \(oid): truncated batch data") } result[oid] = data[start..<start + size] position = start + size + 1 } guard position == data.endIndex else { throw DevError("Unexpected trailing data from git cat-file --batch") } return result }}
struct NameIndex { let root: URL let files: [String: Data]
init(root: URL, policies: [URL]) throws { self.root = root let required = try Set(policies.compactMap { try Self.relative($0, root: root) }) let changed = try NameGit.run( ["diff", "--cached", "--name-only", "--no-renames", "--diff-filter=ACMRTUXB", "-z"], root: root) let selected = required.union(changed.split(separator: 0).map { String(decoding: $0, as: UTF8.self) }) var entries: [String: String] = [:] var oids: [String] = [] var known: Set<String> = [] for entry in try NameGit.entries(root: root) where selected.contains(entry.path) { guard entry.mode != "160000" else { throw DevError("Submodule contents cannot be checked: \(entry.path)") } entries[entry.path] = entry.oid if known.insert(entry.oid).inserted { oids.append(entry.oid) } } if let missing = selected.subtracting(entries.keys).sorted().first { throw DevError("Required input is missing from the index: \(missing)") } let blobs = try NameGit.blobs(oids, root: root) var files: [String: Data] = [:] for (path, oid) in entries { guard let blob = blobs[oid] else { throw DevError("Could not read indexed blob \(oid)") } files[path] = blob } self.files = files }
static func relative(_ path: URL, root: URL) throws -> String? { let path = NamePaths.lexical(path) let prefix = root.path + "/" if path.path.hasPrefix(prefix) { return String(path.path.dropFirst(prefix.count)) } var parent = path.deletingLastPathComponent() var suffix = path.lastPathComponent while parent.path != "/" { if let resolved = try NamePaths.existingParent(parent) { if resolved == root { return suffix } if resolved.path.hasPrefix(prefix) { return String(resolved.path.dropFirst(prefix.count)) + "/" + suffix } } suffix = parent.lastPathComponent + "/" + suffix parent.deleteLastPathComponent() } return nil } func read(_ path: URL) throws -> Data { if let relative = try Self.relative(path, root: root) { guard let data = files[relative] else { throw DevError("Required policy input is missing from the index: \(relative)") } return data } return try Data(contentsOf: path) }}
enum NameArtifacts { static let magic: Set<Data> = Set( [ [0xfe, 0xed, 0xfa, 0xce], [0xce, 0xfa, 0xed, 0xfe], [0xfe, 0xed, 0xfa, 0xcf], [0xcf, 0xfa, 0xed, 0xfe], [0xca, 0xfe, 0xba, 0xbe], [0xbe, 0xba, 0xfe, 0xca], [0xca, 0xfe, 0xba, 0xbf], [0xbf, 0xba, 0xfe, 0xca], ].map { Data($0) }) static func files(root: URL) throws -> Set<URL> { var result: Set<URL> = [] let background = root.appendingPathComponent("Packaging/background.png") if FileManager.default.fileExists(atPath: background.path) { result.insert(background) } let excluded: Set<String> = [ "checkouts", "repositories", "artifacts", "ModuleCache", "index", "plugins", "Intermediates.noindex", ] func visit(_ directory: URL) throws { for path in try FileManager.default.contentsOfDirectory(at: directory, includingPropertiesForKeys: nil) .sorted(by: { $0.path < $1.path }) { var info = stat() guard lstat(path.path, &info) == 0 else { throw DevError("Could not inspect artifact \(path.path): \(String(cString: strerror(errno)))") } let kind = info.st_mode & S_IFMT let name = path.lastPathComponent if kind == S_IFDIR { if !excluded.contains(name) && !name.hasSuffix(".build") && !name.hasSuffix(".dSYM") { try visit(path) } } else if kind == S_IFREG { if name == "background.png" { result.insert(path); continue } guard info.st_mode & 0o111 != 0 || ["dylib", "so"].contains(path.pathExtension) else { continue } let handle = try FileHandle(forReadingFrom: path) let header = try handle.read(upToCount: 4) ?? Data() try handle.close() if magic.contains(header) { result.insert(path) } } } } for name in ["build", ".build"] { let directory = root.appendingPathComponent(name) if FileManager.default.fileExists(atPath: directory.path) { try visit(directory) } } return result } static func source(_ source: String) -> String { var parts = source.split(separator: "/").map(String.init) if parts.count > 3 && parts[0] == ".build" && parts[1].hasSuffix("-apple-macosx") && ["debug", "release"].contains(parts[2]) { parts.remove(at: 1) return parts.joined(separator: "/") } return source }}