Search code examples
swiftgitmacosheadless

Attempting to clone a git repository via Swift encounters a Protocol Violation


Darwin-via-Swift Neophyte here.

This is the continuation of: Intro to Process().

I'm trying to get my hand around the use of Swift as a means to automate Darwin-Level processes like cloning a git repository, tarring a folder, etc.

I was given a template to attempt to do a clone (via above link).
I've created a test link; and tried it out at the Darwin prompt.

So I know it works.
But I'm getting the protocol error via my Swift script.

What's the remedy here?
Is there a particular protocol that I must use?

import Foundation

extension Process {

    private static let gitExecURL = URL(fileURLWithPath: "/usr/bin/git")

    public func clone(repo: String, path: String) throws {
        executableURL = Process.gitExecURL
        arguments = ["clone", repo, path]
        try run()
    }

}


let source = "git clone https://AmourineTech@bitbucket.org/AmourineTech/testbit.git"
let target = "/Users/Ric/workarea"

print("Hello, World!")

try! Process().clone(repo: source, path: target)

print("Done")

Console Output:

Hello, World!
Done
Cloning into '/Users/Ric/workarea'...
fatal: protocol 'git clone https' is not supported
Program ended with exit code: 0

Solution

  • It seems source must be an URL, not a git clone command:

    let source = "https://AmourineTech@bitbucket.org/AmourineTech/testbit.git"