Sources/OrgApp/FileHistory.swift
42 lines · 1554 bytes
1import Foundation
2
3/// The versions of a file the History view lists: those kept in the recovery folder and, in the
4/// download version where the folder is a git repository, the commits that changed it.
5public enum FileHistory {
6 /// A commit that changed the file, with the file's path in it relative to `repository`.
7 public struct Commit: Hashable, Sendable {
8 public let hash: String
9 public let date: Date
10 public let author: String
11 public let subject: String
12 public let path: String
13 public let repository: URL
14
15 public init(hash: String, date: Date, author: String, subject: String, path: String, repository: URL) {
16 self.hash = hash
17 self.date = date
18 self.author = author
19 self.subject = subject
20 self.path = path
21 self.repository = repository
22 }
23 }
24
25 public enum Entry: Hashable, Sendable {
26 case recovery(url: URL, date: Date, label: String)
27 case commit(Commit)
28
29 public var date: Date {
30 switch self {
31 case .recovery(_, let date, _): date
32 case .commit(let commit): commit.date
33 }
34 }
35 }
36
37 /// Recovery versions and commits together, newest first.
38 public static func merged(recovery: [(url: URL, date: Date, label: String)], commits: [Commit]) -> [Entry] {
39 (recovery.map { Entry.recovery(url: $0.url, date: $0.date, label: $0.label) } + commits.map(Entry.commit))
40 .sorted { $0.date > $1.date }
41 }
42}