import Foundation public enum GitDirectory { /// The real git directory: `/.git`, or the `gitdir:` target when `.git` is a file (worktrees, submodules). public static func resolve(root: URL) -> URL { let dot = root.appendingPathComponent(".git") var isDir: ObjCBool = false guard FileManager.default.fileExists(atPath: dot.path, isDirectory: &isDir), !isDir.boolValue, let text = try? String(contentsOf: dot, encoding: .utf8) else { return dot } let line = text.trimmingCharacters(in: .whitespacesAndNewlines) guard line.hasPrefix("gitdir:") else { return dot } let target = line.dropFirst("gitdir:".count).trimmingCharacters(in: .whitespaces) return URL(fileURLWithPath: target, relativeTo: root).standardizedFileURL } /// The entries whose change means "git state moved": branch switch, staging, commits, pushes, merges. public static func watchedEntries(gitDirectory dir: URL) -> [URL] { ["HEAD", "index", "refs/heads", "refs/remotes", "logs/HEAD", "MERGE_HEAD"].map { dir.appendingPathComponent($0) } } }