| 1 | import Foundation |
| 2 | |
| 3 | public struct ThinChangedFile: Codable, Sendable, Equatable, Identifiable { |
| 4 | public var path: String |
| 5 | public var oldPath: String? = nil |
| 6 | public var status: String |
| 7 | public var additions: Int |
| 8 | public var deletions: Int |
| 9 | public var binary: Bool |
| 10 | public var id: String { path } |
| 11 | } |
| 12 | public struct ThinChangeFiles: Codable, Sendable, Equatable { |
| 13 | public var base: String |
| 14 | public var sha: String |
| 15 | public var files: Int |
| 16 | public var additions: Int |
| 17 | public var deletions: Int |
| 18 | public var binary: Int |
| 19 | public var entries: [ThinChangedFile] |
| 20 | public var truncated: Bool |
| 21 | |
| 22 | var isValid: Bool { |
| 23 | guard (base.isEmpty || Self.commit(base)), Self.commit(sha), files >= 0, additions >= 0, deletions >= 0, |
| 24 | binary >= 0, binary <= files, entries.count <= 200, entries.count <= files, |
| 25 | truncated == (entries.count < files), Set(entries.map(\.path)).count == entries.count else { return false } |
| 26 | var added = 0, removed = 0, binaries = 0 |
| 27 | for file in entries { |
| 28 | guard Self.path(file.path), ["added", "modified", "deleted", "renamed"].contains(file.status), |
| 29 | file.additions >= 0, file.deletions >= 0, |
| 30 | file.additions <= additions - added, file.deletions <= deletions - removed else { return false } |
| 31 | if file.status == "renamed" { |
| 32 | guard let old = file.oldPath, Self.path(old), old != file.path else { return false } |
| 33 | } else if file.oldPath != nil { return false } |
| 34 | added += file.additions; removed += file.deletions |
| 35 | if file.binary { |
| 36 | guard file.additions == 0, file.deletions == 0 else { return false } |
| 37 | binaries += 1 |
| 38 | } |
| 39 | } |
| 40 | guard binaries <= binary, truncated || (added == additions && removed == deletions && binaries == binary), |
| 41 | let size = wireSize, size <= 96 * 1024 else { return false } |
| 42 | return true |
| 43 | } |
| 44 | private var wireSize: Int? { |
| 45 | let encoder = JSONEncoder() |
| 46 | encoder.keyEncodingStrategy = .convertToSnakeCase |
| 47 | encoder.outputFormatting = [.withoutEscapingSlashes] |
| 48 | guard let data = try? encoder.encode(self), var json = String(data: data, encoding: .utf8) else { return nil } |
| 49 | // Match encoding/json's HTML and separator escaping, including old_path. |
| 50 | for (character, escape) in [("<", #"\u003c"#), (">", #"\u003e"#), ("&", #"\u0026"#), ("\u{2028}", #"\u2028"#), ("\u{2029}", #"\u2029"#)] { |
| 51 | json = json.replacingOccurrences(of: character, with: escape) |
| 52 | } |
| 53 | return json.utf8.count |
| 54 | } |
| 55 | static func commit(_ value: String) -> Bool { |
| 56 | value.utf8.count == 40 && value.utf8.allSatisfy { (48...57).contains($0) || (97...102).contains($0) } |
| 57 | } |
| 58 | static func path(_ value: String) -> Bool { |
| 59 | !value.isEmpty && value.utf8.count <= 4096 && !value.contains("\\") |
| 60 | && !value.unicodeScalars.contains { |
| 61 | $0.properties.generalCategory == .control || $0.value == 0x061c || $0.value == 0x200e || $0.value == 0x200f |
| 62 | || (0x202a...0x202e).contains($0.value) || (0x2066...0x2069).contains($0.value) |
| 63 | } |
| 64 | && value.split(separator: "/", omittingEmptySubsequences: false).allSatisfy { !$0.isEmpty && $0 != "." && $0 != ".." } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // Validate the optional presentation field on entry, including decoded payloads |
| 69 | // and programmatic assignment. A malformed optional field cannot hide the task. |
| 70 | @propertyWrapper public struct ValidatedChangeFiles: Codable, Sendable, Equatable { |
| 71 | private var value: ThinChangeFiles? |
| 72 | public var wrappedValue: ThinChangeFiles? { |
| 73 | get { value } |
| 74 | set { value = newValue.flatMap { $0.isValid ? $0 : nil } } |
| 75 | } |
| 76 | public init(wrappedValue: ThinChangeFiles?) { |
| 77 | value = wrappedValue.flatMap { $0.isValid ? $0 : nil } |
| 78 | } |
| 79 | public init(from decoder: Decoder) throws { |
| 80 | self.init(wrappedValue: try? ThinChangeFiles(from: decoder)) |
| 81 | } |
| 82 | public func encode(to encoder: Encoder) throws { |
| 83 | try value.encode(to: encoder) |
| 84 | } |
| 85 | } |
| 86 | extension KeyedDecodingContainer { |
| 87 | func decode(_ type: ValidatedChangeFiles.Type, forKey key: Key) throws -> ValidatedChangeFiles { |
| 88 | try decodeIfPresent(type, forKey: key) ?? ValidatedChangeFiles(wrappedValue: nil) |
| 89 | } |
| 90 | } |
| 91 | extension KeyedEncodingContainer { |
| 92 | mutating func encode(_ value: ValidatedChangeFiles, forKey key: Key) throws { |
| 93 | if let facts = value.wrappedValue { try encode(facts, forKey: key) } |
| 94 | } |
| 95 | } |
| 96 | |