summaryrefslogtreecommitdiff
path: root/Hutch/Views/Repositories/CommitDetailView.swift
blob: 1dcbf689fdf3d3c31c754f5489d891a24a0a8cf1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import SwiftUI

struct CommitDetailView: View {
    let commitSummary: CommitSummary
    let repository: RepositorySummary

    @Environment(AppState.self) private var appState
    @Environment(\.openURL) private var openURL
    @State private var viewModel: CommitDetailViewModel?

    var body: some View {
        Group {
            if let viewModel {
                commitContent(viewModel)
            } else {
                ProgressView()
            }
        }
        .navigationTitle(commitSummary.shortId)
        .navigationBarTitleDisplayMode(.inline)
        .toolbar {
            ToolbarItemGroup(placement: .topBarTrailing) {
                Menu {
                    if let commitURL = SRHTWebURL.commit(repository: repository, commitId: commitSummary.id) {
                        Button {
                            openURL(commitURL)
                        } label: {
                            Label("Open in Browser", systemImage: "safari")
                        }

                        Button {
                            appState.copyToPasteboard(commitURL.absoluteString, label: "commit URL")
                        } label: {
                            Label("Copy URL", systemImage: "doc.on.doc")
                        }
                    }

                    Button {
                        appState.copyToPasteboard(commitSummary.id, label: "commit SHA")
                    } label: {
                        Label("Copy Full SHA", systemImage: "doc.on.doc")
                    }

                    Button {
                        appState.copyToPasteboard(commitSummary.shortId, label: "short commit SHA")
                    } label: {
                        Label("Copy Short SHA", systemImage: "number")
                    }
                } label: {
                    Image(systemName: "ellipsis.circle")
                }
                .accessibilityLabel("Commit actions")

                SRHTShareButton(url: SRHTWebURL.commit(repository: repository, commitId: commitSummary.id), target: .commit) {
                    Image(systemName: "square.and.arrow.up")
                }
            }
        }
        .task {
            if viewModel == nil {
                let vm = CommitDetailViewModel(
                    repositoryRid: repository.rid,
                    service: repository.service,
                    commitId: commitSummary.id,
                    client: appState.client
                )
                viewModel = vm
                await vm.loadCommit()
            }
        }
    }

    @ViewBuilder
    private func commitContent(_ viewModel: CommitDetailViewModel) -> some View {
        if viewModel.isLoading {
            ProgressView()
        } else if let error = viewModel.error {
            ContentUnavailableView {
                Label("Error", systemImage: "exclamationmark.triangle")
            } description: {
                Text(error)
            } actions: {
                Button("Retry") {
                    Task { await viewModel.loadCommit() }
                }
            }
        } else if let commit = viewModel.commit {
            ScrollView {
                LazyVStack(alignment: .leading, spacing: 0) {
                    // Header
                    commitHeader(commit)

                    sectionDivider

                    // Message
                    commitMessage(commit)

                    // Trailers
                    if !commit.trailers.isEmpty {
                        sectionDivider
                        trailersSection(commit.trailers)
                    }

                    // Parents
                    if !commit.parents.isEmpty {
                        sectionDivider
                        parentsSection(commit.parents)
                    }

                    // Diff
                    if let diff = commit.diff, !diff.isEmpty {
                        sectionDivider
                        diffSection(diff)
                    }

                    // Tree
                    if let tree = commit.tree, !tree.entries.results.isEmpty {
                        sectionDivider
                        treeSection(tree.entries.results)
                    }
                }
            }
            .navigationDestination(for: ParentCommit.self) { parent in
                CommitDetailView(
                    commitSummary: CommitSummary(
                        id: parent.id,
                        shortId: parent.shortId,
                        author: CommitAuthor(name: parent.author.name, email: nil, time: .now),
                        message: ""
                    ),
                    repository: repository
                )
            }
        }
    }

    // MARK: - Header

    @ViewBuilder
    private func commitHeader(_ commit: CommitDetail) -> some View {
        VStack(alignment: .leading, spacing: 8) {
            // Full hash — tappable to copy
            Button {
                appState.copyToPasteboard(commit.id, label: "commit SHA")
            } label: {
                HStack(spacing: 4) {
                    Text(commit.id)
                        .font(.caption.monospaced())
                        .lineLimit(1)
                        .truncationMode(.middle)
                    Image(systemName: "doc.on.doc")
                        .font(.caption2)
                }
                .foregroundStyle(.secondary)
            }

            // Author
            HStack {
                Label(commit.author.name, systemImage: "person")
                Spacer()
                Text(commit.author.time.relativeDescription)
                    .foregroundStyle(.secondary)
            }
            .font(.subheadline)

            // Committer (if different from author)
            if commit.committer.name != commit.author.name
                || commit.committer.email != commit.author.email {
                HStack {
                    Label(commit.committer.name, systemImage: "person.badge.shield.checkmark")
                    Spacer()
                    Text(commit.committer.time.relativeDescription)
                        .foregroundStyle(.secondary)
                }
                .font(.subheadline)
                .foregroundStyle(.secondary)
            }
        }
        .padding()
    }

    // MARK: - Message

    @ViewBuilder
    private func commitMessage(_ commit: CommitDetail) -> some View {
        VStack(alignment: .leading, spacing: 8) {
            Text("Message")
                .font(.caption.weight(.semibold))
                .foregroundStyle(.secondary)
                .textCase(.uppercase)

            Text(commit.title)
                .font(.headline)

            if let body = commit.body {
                Text(body)
                    .font(.subheadline.monospaced())
                    .foregroundStyle(.secondary)
            }
        }
        .padding()
        .frame(maxWidth: .infinity, alignment: .leading)
    }

    // MARK: - Trailers

    @ViewBuilder
    private func trailersSection(_ trailers: [CommitTrailer]) -> some View {
        VStack(alignment: .leading, spacing: 8) {
            Text("Trailers")
                .font(.caption.weight(.semibold))
                .foregroundStyle(.secondary)
                .textCase(.uppercase)

            ForEach(trailers) { trailer in
                HStack(alignment: .top, spacing: 4) {
                    Text("\(trailer.name):")
                        .font(.subheadline.monospaced().weight(.medium))
                    Text(trailer.value)
                        .font(.subheadline.monospaced())
                        .foregroundStyle(.secondary)
                }
            }
        }
        .padding()
        .frame(maxWidth: .infinity, alignment: .leading)
    }

    // MARK: - Parents

    @ViewBuilder
    private func parentsSection(_ parents: [ParentCommit]) -> some View {
        VStack(alignment: .leading, spacing: 8) {
            Text("Parents")
                .font(.caption.weight(.semibold))
                .foregroundStyle(.secondary)
                .textCase(.uppercase)

            ForEach(parents) { parent in
                NavigationLink(value: parent) {
                    HStack {
                        Text(parent.shortId)
                            .font(.subheadline.monospaced())
                        Text(parent.author.name)
                            .font(.subheadline)
                            .foregroundStyle(.secondary)
                        Spacer()
                        Image(systemName: "chevron.right")
                            .font(.caption)
                            .foregroundStyle(.tertiary)
                    }
                }
                .buttonStyle(.plain)
            }
        }
        .padding()
        .frame(maxWidth: .infinity, alignment: .leading)
    }

    // MARK: - Diff

    @ViewBuilder
    private func diffSection(_ diff: String) -> some View {
        VStack(alignment: .leading, spacing: 8) {
            Text("Diff")
                .font(.caption.weight(.semibold))
                .foregroundStyle(.secondary)
                .textCase(.uppercase)
                .padding(.horizontal)
                .padding(.top)

            DiffView(diff: invertDiff(diff))
                .padding(.bottom)
        }
    }

    /// The sr.ht API returns diffs comparing current→parent (inverted).
    /// This swaps +/- prefixes so the diff reads as parent→current.
    private func invertDiff(_ diff: String) -> String {
        diff.split(separator: "\n", omittingEmptySubsequences: false)
            .map { line in
                let s = String(line)
                if s.hasPrefix("@@") || s.hasPrefix("diff ") || s.hasPrefix("index ") {
                    return s
                }
                if s.hasPrefix("---") {
                    return "+++" + s.dropFirst(3)
                }
                if s.hasPrefix("+++") {
                    return "---" + s.dropFirst(3)
                }
                if s.hasPrefix("+") {
                    return "-" + s.dropFirst(1)
                }
                if s.hasPrefix("-") {
                    return "+" + s.dropFirst(1)
                }
                return s
            }
            .joined(separator: "\n")
    }

    // MARK: - Tree

    @ViewBuilder
    private func treeSection(_ entries: [CommitTreeEntry]) -> some View {
        VStack(alignment: .leading, spacing: 8) {
            Text("Tree")
                .font(.caption.weight(.semibold))
                .foregroundStyle(.secondary)
                .textCase(.uppercase)

            ForEach(entries) { entry in
                HStack(spacing: 8) {
                    Image(systemName: treeEntryIcon(for: entry))
                        .foregroundStyle(treeEntryColor(for: entry))
                        .frame(width: 20)
                    Text(entry.name)
                        .font(.subheadline.monospaced())
                    Spacer()
                    if let obj = entry.object, let shortId = obj.shortId {
                        Text(shortId)
                            .font(.caption.monospaced())
                            .foregroundStyle(.tertiary)
                    }
                }
            }
        }
        .padding()
        .frame(maxWidth: .infinity, alignment: .leading)
    }

    // MARK: - Helpers

    private var sectionDivider: some View {
        Divider().padding(.horizontal)
    }

    private func treeEntryIcon(for entry: CommitTreeEntry) -> String {
        guard let type = entry.object?.type else {
            return "doc"
        }
        switch type {
        case "tree":   return "folder"
        case "blob":   return "doc.text"
        case "tag":    return "tag"
        case "commit": return "arrow.triangle.branch"
        default:       return "doc"
        }
    }

    private func treeEntryColor(for entry: CommitTreeEntry) -> Color {
        guard let type = entry.object?.type else {
            return .secondary
        }
        switch type {
        case "tree":   return .blue
        case "blob":   return .secondary
        case "tag":    return .orange
        case "commit": return .purple
        default:       return .secondary
        }
    }
}