Skip to content

Commit

Permalink
git: properly parse urls with scp-like syntax #1094
Browse files Browse the repository at this point in the history
  • Loading branch information
bummoblizard committed Jun 17, 2024
1 parent 46d888e commit 1cc491e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
26 changes: 22 additions & 4 deletions CodeApp/Managers/FileSystem/Local/LocalGitCredentialsHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,33 @@ final class LocalGitCredentialsHelper {
return try credentialsForRemoteURL(url: url)
}

private func parseRemoteURL(url: URL) -> URL? {
if url.absoluteString.starts(with: "git@") {
return URL(string: "ssh://\(url.absoluteString)")
static func parseRemoteURL(url: URL) -> URL? {
if url.scheme == nil {
// Handle scp-like syntax urls.
// Reference: https://gitirc.eu/git-clone.html#URLS

// From: [<user>@]<host>:/<path-to-git-repo> [<user>@]<host>:~<user>/<path-to-git-repo>
// To: git://<host>[:<port>]/~<user>/<path-to-git-repo>
guard
let userAtHost = url.absoluteString.split(separator: ":").first,
let pathToGitRepo = url.absoluteString.split(separator: "/", maxSplits: 1).last
else {
return nil
}
let user = url.absoluteString.dropFirst(userAtHost.count + 1).dropLast(
pathToGitRepo.count + 1)

if user.count > 0 {
return URL(string: "ssh://\(userAtHost)/\(user)/\(pathToGitRepo)")
} else {
return URL(string: "ssh://\(userAtHost)/\(pathToGitRepo)")
}
}
return url
}

func credentialsForRemoteURL(url: URL) throws -> Credentials {
guard let url = parseRemoteURL(url: url),
guard let url = LocalGitCredentialsHelper.parseRemoteURL(url: url),
let hostname = url.host,
let _scheme = url.scheme,
let scheme = SchemeType(rawValue: _scheme)
Expand Down
20 changes: 20 additions & 0 deletions CodeUITests/CodeUITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,24 @@ final class CodeUITests: XCTestCase {

UserDefaults.standard.removeObject(forKey: "alwaysOpenInNewTab")
}

func testParseRemoteURL_userExtension() throws {
let url = URL(string: "[email protected]:thebaselab/codeapp.git")!
let parsed = LocalGitCredentialsHelper.parseRemoteURL(url: url)

XCTAssertNotNil(parsed)
XCTAssertEqual(parsed!.host, "github.com")
XCTAssertEqual(parsed!.scheme, "ssh")
XCTAssertEqual(parsed!.path, "/thebaselab/codeapp.git")
}

func testParseRemoteURL_noUserExtension() throws {
let url = URL(string: "[email protected]:/codeapp.git")!
let parsed = LocalGitCredentialsHelper.parseRemoteURL(url: url)

XCTAssertNotNil(parsed)
XCTAssertEqual(parsed!.host, "github.com")
XCTAssertEqual(parsed!.scheme, "ssh")
XCTAssertEqual(parsed!.path, "/codeapp.git")
}
}

0 comments on commit 1cc491e

Please sign in to comment.