gitbay/Repos/LogViewModel.swift
36 lines · 1102 bytes
1import Foundation
2import Observation
3
4/// `repo log`: the default branch's history, signature verdict per commit.
5@Observable
6@MainActor
7final class LogViewModel {
8
9 private(set) var state: LoadState<[Commit]> = .loading
10
11 private let client: GitbayClient
12 let repoPath: String
13 /// nil reads the default branch, as the command does.
14 let ref: String?
15 /// Set to follow one file's history, as the blob page does.
16 let path: String?
17
18 init(client: GitbayClient, repoPath: String, ref: String? = nil, path: String? = nil) {
19 self.client = client
20 self.repoPath = repoPath
21 self.ref = ref
22 self.path = path
23 }
24
25 func load() async {
26 var argv = ["repo", "log", repoPath]
27 if let ref { argv.append(contentsOf: ["--ref", ref]) }
28 if let path { argv.append(contentsOf: ["--path", path]) }
29 do {
30 let commits = try await client.readList(argv, of: Commit.self)
31 state = commits.isEmpty ? .empty("No commits yet.") : .loaded(commits)
32 } catch {
33 state = .from(error)
34 }
35 }
36}