Skip to content

Show Git history in the History Inspector, even when a remote is not configured #1744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions CodeEdit/Features/Git/Client/GitClient+CommitHistory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ extension GitClient {
"log -z --pretty=%h¦%H¦%s¦%aN¦%ae¦%cn¦%ce¦%aD¦%b¦%D¦ \(maxCountString) \(branchNameString) \(fileLocalPath)"
.trimmingCharacters(in: .whitespacesAndNewlines)
)
let remote = try await run("ls-remote --get-url")
let remoteURL = URL(string: remote.trimmingCharacters(in: .whitespacesAndNewlines))
let remoteURL = try await getRemoteURL()

return output
.split(separator: "\0")
Expand Down
17 changes: 17 additions & 0 deletions CodeEdit/Features/Git/Client/GitClient+Remote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ extension GitClient {
func removeRemote(name: String) async throws {
_ = try await run("remote rm \(name)")
}

/// Get the URL of the remote
/// > Note: If a git repository has multiple remotes, by default the `origin` remote
/// > 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
/// - 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 GitClientError.noRemoteConfigured {
return nil
} catch {
throw error
}
}
}

func parseGitRemotes(from output: String) -> [GitRemote] {
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