【问题标题】:How to run a git command from macOS app in Swift?如何在 Swift 中从 macOS 应用程序运行 git 命令?
【发布时间】:2020-06-13 01:09:21
【问题描述】:

我想运行一个 git 命令,然后从 macOS 应用程序启动 Xcode。

但我目前在第一步被阻止:运行 git 命令

我使用ProcessNSWorkspace.shared.openApplication 尝试了很多代码,但仍然被阻止。你可以在这里看到我的最后一次尝试。

@discardableResult
func shell(_ app: String, _ args: String...) -> Int32 {
    let task = Process()
    task.launchPath = "/usr/bin/\(app)"
    task.arguments = args
    task.launch()
    task.waitUntilExit()
    return task.terminationStatus
}

shell("git", "clone https://github.com/user/repo.git")

但是抛出了一个错误:

git: 'clone https://github.com/user/repo.git' 不是 git 命令。请参阅“git --help”。

我错了吗?这是正确的做法吗? 我需要授予权限或类似的东西吗?

【问题讨论】:

  • 单独的参数:shell("git", "clone", "https://github.com/user/repo.git")

标签: swift git macos shell


【解决方案1】:

您将单个参数传递给git 命令:"clone https://github.com/user/repo.git"。相反,您需要将“克隆”和 URL 作为单独的参数传递:

shell("git", "clone", "https://github.com/user/repo.git")

【讨论】:

    猜你喜欢
    • 2018-06-24
    • 2014-08-06
    • 1970-01-01
    • 2018-10-28
    • 2020-09-19
    • 2015-11-23
    • 2018-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多