Skip to content

Commit 1e7b9ce

Browse files
Show Git history in the History Inspector, even when a remote is not configured (#1744)
* Add a getRemoteURL function in GitClient * Make fetching the remote URL non-fatal when the remote doesn't exist * Add throw semantics to getRemoteURL()
1 parent 376a3ee commit 1e7b9ce

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

CodeEdit/Features/Git/Client/GitClient+CommitHistory.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ extension GitClient {
3434
"log -z --pretty=%h¦%H¦%s¦%aN¦%ae¦%cn¦%ce¦%aD¦%b¦%D¦ \(maxCountString) \(branchNameString) \(fileLocalPath)"
3535
.trimmingCharacters(in: .whitespacesAndNewlines)
3636
)
37-
let remote = try await run("ls-remote --get-url")
38-
let remoteURL = URL(string: remote.trimmingCharacters(in: .whitespacesAndNewlines))
37+
let remoteURL = try await getRemoteURL()
3938

4039
return output
4140
.split(separator: "\0")

CodeEdit/Features/Git/Client/GitClient+Remote.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ extension GitClient {
3131
func removeRemote(name: String) async throws {
3232
_ = try await run("remote rm \(name)")
3333
}
34+
35+
/// Get the URL of the remote
36+
/// > Note: If a git repository has multiple remotes, by default the `origin` remote
37+
/// > will be used, unless there’s an upstream branch configured for the current branch.
38+
/// > (Reference: https://git-scm.com/docs/git-ls-remote, https://git-scm.com/docs/git-fetch)
39+
/// - Returns: A URL if a remote is configured, nil otherwise
40+
/// - Throws: `GitClientError.outputError` if the underlying git command fails unexpectedly
41+
func getRemoteURL() async throws -> URL? {
42+
do {
43+
let remote = try await run("ls-remote --get-url")
44+
return URL(string: remote.trimmingCharacters(in: .whitespacesAndNewlines))
45+
} catch GitClientError.noRemoteConfigured {
46+
return nil
47+
} catch {
48+
throw error
49+
}
50+
}
3451
}
3552

3653
func parseGitRemotes(from output: String) -> [GitRemote] {

CodeEdit/Features/Git/Client/GitClient.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ class GitClient {
1313
case outputError(String)
1414
case notGitRepository
1515
case failedToDecodeURL
16+
case noRemoteConfigured
1617

1718
var description: String {
1819
switch self {
1920
case .outputError(let string): string
2021
case .notGitRepository: "Not a git repository"
2122
case .failedToDecodeURL: "Failed to decode URL"
23+
case .noRemoteConfigured: "No remote configured"
2224
}
2325
}
2426
}
@@ -63,6 +65,10 @@ class GitClient {
6365
throw GitClientError.notGitRepository
6466
}
6567

68+
if output.contains("fatal: No remote configured") {
69+
throw GitClientError.noRemoteConfigured
70+
}
71+
6672
if output.hasPrefix("fatal:") {
6773
throw GitClientError.outputError(output)
6874
}

0 commit comments

Comments
 (0)