Skip to content

Commit

Permalink
Add throw semantics to getRemoteURL()
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwhitaker committed Jun 6, 2024
1 parent 680cdb0 commit 287e077
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CodeEdit/Features/Git/Client/GitClient+CommitHistory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extension GitClient {
"log -z --pretty=%h¦%H¦%s¦%aN¦%ae¦%cn¦%ce¦%aD¦%b¦%D¦ \(maxCountString) \(branchNameString) \(fileLocalPath)"
.trimmingCharacters(in: .whitespacesAndNewlines)
)
let remoteURL = await getRemoteURL()
let remoteURL = try await getRemoteURL()

return output
.split(separator: "\0")
Expand Down
7 changes: 5 additions & 2 deletions CodeEdit/Features/Git/Client/GitClient+Remote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ extension GitClient {
/// > will be used, unless there’s an upstream branch configured for the current branch.
/// > (Reference: https://git-scm.com/docs/git-ls-remote, https://git-scm.com/docs/git-fetch)
/// - Returns: A URL if a remote is configured, nil otherwise
func getRemoteURL() async -> URL? {
/// - Throws:`GitClientError.outputError` if the underlying git command fails unexpectedly
func getRemoteURL() async throws -> URL? {
do {
let remote = try await run("ls-remote --get-url")
return URL(string: remote.trimmingCharacters(in: .whitespacesAndNewlines))
} catch {
} catch GitClientError.noRemoteConfigured {
return nil
} catch {
throw error
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions CodeEdit/Features/Git/Client/GitClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ class GitClient {
case outputError(String)
case notGitRepository
case failedToDecodeURL
case noRemoteConfigured

var description: String {
switch self {
case .outputError(let string): string
case .notGitRepository: "Not a git repository"
case .failedToDecodeURL: "Failed to decode URL"
case .noRemoteConfigured: "No remote configured"
}
}
}
Expand Down Expand Up @@ -63,6 +65,10 @@ class GitClient {
throw GitClientError.notGitRepository
}

if output.contains("fatal: No remote configured") {
throw GitClientError.noRemoteConfigured
}

if output.hasPrefix("fatal:") {
throw GitClientError.outputError(output)
}
Expand Down

0 comments on commit 287e077

Please sign in to comment.