gitbay/MRs/RangeDiffViewModel.swift

v1.1.1
gitbay-ios/gitbay/MRs/RangeDiffViewModel.swift history · blame · raw

36 lines · 1030 bytes

 1import Foundation
 2import Observation
 3
 4/// `mr range-diff <owner/name> <n> --from <sha> --to <sha>` — git's
 5/// range-diff between two heads the merge request has had, as text.
 6@Observable
 7@MainActor
 8final class RangeDiffViewModel {
 9
10    private(set) var state: LoadState<String> = .loading
11
12    private let client: GitbayClient
13    let repoPath: String
14    let number: Int64
15    let from: String
16    let to: String
17
18    init(client: GitbayClient, repoPath: String, number: Int64, from: String, to: String) {
19        self.client = client
20        self.repoPath = repoPath
21        self.number = number
22        self.from = from
23        self.to = to
24    }
25
26    func load() async {
27        state = .loading
28        do {
29            let text = try await client.readText(
30                ["mr", "range-diff", repoPath, String(number), "--from", from, "--to", to])
31            state = text.isEmpty ? .empty("The two revisions carry the same commits.") : .loaded(text)
32        } catch {
33            state = .from(error)
34        }
35    }
36}