Skip to content

Commit aefd6f0

Browse files
committed
GitDiff: unified diff viewer + PR diffs
1 parent 91babaa commit aefd6f0

16 files changed

Lines changed: 2675 additions & 727 deletions
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import Foundation
2+
3+
enum DiffDocumentSource: Hashable {
4+
case workingTree(scope: GitDiffScope)
5+
case pullRequest(number: Int)
6+
}
7+
8+
struct DiffDocument: Hashable {
9+
var source: DiffDocumentSource
10+
var files: [DiffFile]
11+
}
12+
13+
enum DiffFileStatus: String, Hashable {
14+
case modified
15+
case added
16+
case deleted
17+
case renamed
18+
case copied
19+
case binary
20+
case combinedUnsupported
21+
case unknown
22+
}
23+
24+
struct DiffFile: Identifiable, Hashable {
25+
let pathOld: String?
26+
let pathNew: String?
27+
let status: DiffFileStatus
28+
let additions: Int
29+
let deletions: Int
30+
let hunks: [DiffHunk]
31+
32+
// Used for binary diffs and unsupported combined diffs.
33+
let fallbackText: String?
34+
let isTooLargeToRender: Bool
35+
36+
var id: String { primaryPath }
37+
38+
var primaryPath: String {
39+
(pathNew?.isEmpty == false ? pathNew : nil)
40+
?? (pathOld?.isEmpty == false ? pathOld : nil)
41+
?? "Diff"
42+
}
43+
44+
var displayTitle: String {
45+
guard let pathOld, let pathNew, !pathOld.isEmpty, !pathNew.isEmpty, pathOld != pathNew else {
46+
return primaryPath
47+
}
48+
return "\(pathNew) \u{2190} \(pathOld)"
49+
}
50+
51+
var isBinary: Bool { status == .binary }
52+
var isCombinedUnsupported: Bool { status == .combinedUnsupported }
53+
}
54+
55+
struct DiffHunk: Identifiable, Hashable {
56+
let id: String
57+
let header: String
58+
let oldStart: Int
59+
let oldCount: Int
60+
let newStart: Int
61+
let newCount: Int
62+
let lines: [DiffLine]
63+
}
64+
65+
enum DiffLineKind: Hashable {
66+
case context
67+
case add
68+
case del
69+
case meta
70+
}
71+
72+
struct DiffLine: Identifiable, Hashable {
73+
let id: String
74+
let kind: DiffLineKind
75+
let oldLine: Int?
76+
let newLine: Int?
77+
let text: String
78+
}

0 commit comments

Comments
 (0)