【问题标题】:"Launch path not accessible" using NSTask to create Git commit“启动路径不可访问”使用 NSTask 创建 Git 提交
【发布时间】:2017-09-20 11:52:02
【问题描述】:

我正在尝试使用 NSTask 创建一个 Git 提交并向该提交添加一条消息。

这是我尝试过的代码。

NSString *projectPath = @"file:///Users/MYNAME/Desktop/MYPROJECT/";
//stage files
NSPipe *pipe = [NSPipe pipe];
NSTask *task = [[NSTask alloc] init];
task.launchPath = projectPath;
task.arguments = @[@"git", @"add", @"."];
task.standardOutput = pipe;
[task launch];

//commit
NSPipe *pipe2 = [NSPipe pipe];
NSTask *task2 = [[NSTask alloc] init];
task2.launchPath = projectPath;
task2.arguments = @[@"git", @"commit", @"-m",@"\"Some Message\""];
task2.standardOutput = pipe2;
[task2 launch];

我通过使用NSOpenPanel(标准 OS X 打开对话框)收到了projectPath

在 Xcode 终端中,我收到消息 “启动路径不可访问”

那我做错了什么?

更新 在 Josh Caswell 发表评论后,这是我的代码

NSString *projectPath = @"file:///Users/MYNAME/Desktop/MYPROJECT/";
NSString *gitPath = @"/usr/local/bin/git"; //location of the GIT on my mac

//stage
NSPipe *pipe = [NSPipe pipe];
NSTask *task = [[NSTask alloc] init];
task.launchPath = gitPath;
task.currentDirectoryPath = projectPath;
task.arguments = @[@"add", @"."];
task.standardOutput = pipe;
[task launch];

[任务启动]之后;我在终端中收到错误消息“工作目录不存在。”

【问题讨论】:

标签: objective-c cocoa nstask


【解决方案1】:

任务的launchPath 是您要运行的程序 的路径:这里是Git,因此该路径可能需要为/usr/local/bin/git。并从参数中删除@"git";这不是参数,而是可执行文件。

项目的路径应该用于任务的currentDirectoryPath,以便它具有正确的工作目录。

【讨论】:

  • 感谢您的澄清。你说的有道理。不幸的是,我仍然没有运气。当我输入终端时:“which git”我得到“/usr/bin/git”所以我想我应该使用它而不是“/usr/local/bin/git”作为启动路径。但我仍然收到“无法访问启动路径”错误。此外,当我添加“currentDirectoryPath”时,我收到消息“启动路径不可访问”。有什么方法可以让我看到正在写入提示的内容以及在哪个文件夹中?为了调试这个?
  • 我认为没有任何输出,因为NSTask 本身还没有开始。听起来好像有权限或$PATH 问题,但我无法想象/usr/bin 不在$PATH 中。
  • 在终端中,我可以从任何文件夹调用 git。此外,当我输入“echo $PATH”时,我得到“/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin”
【解决方案2】:

借助 Josh Casswell 的回答,我设法弄明白了。我发现我必须从 projectPath 中删除 file:// 部分。所以项目路径应该是@"/Users/MYNAME/Desktop/MYPROJECT/"

它也不应该包含空格,因为它不适用于%20 转义字符。这有点奇怪,因为当您使用 NSOpenPanel 时,您会得到 NSURL,而当您在其上调用绝对路径时,您会在开头得到 "file://",在路径内得到 "%20" 而不是空格。

TLDR; 此代码适用于 Xcode 8:

NSString *projectPath = @"/Users/MYNAME/Desktop/MYPROJECT/"; //be careful that it does not contain %20
NSString *gitPath = @"/usr/local/bin/git";
NSString *message = @"this is commit message";

//stage
NSPipe *pipe = [NSPipe pipe];
NSTask *task = [[NSTask alloc] init];
task.launchPath = gitPath;
task.currentDirectoryPath = projectPath;
task.arguments = @[@"add", @"."];
task.standardOutput = pipe;
[task launch];
[task waitUntilExit];

//commit
NSPipe *pipe2 = [NSPipe pipe];
NSTask *task2 = [[NSTask alloc] init];
task2.launchPath = gitPath;
task2.currentDirectoryPath = projectPath;
task2.arguments = @[@"commit", @"-m", message];
task2.standardOutput = pipe2;
[task2 launch];
[task2 waitUntilExit];

更新 添加[任务waitUntilExit]; 如果你不断收到消息

致命:无法创建 '/Users/MYNAME/Desktop/MYPROJECT/.git/index.lock': 文件存在。

另一个 git 进程似乎正在此存储库中运行,例如一个 由“git commit”打开的编辑器。请确保所有进程 终止然后再试一次。如果仍然失败,则可能是 git 进程 之前在此存储库中崩溃:手动删除文件以 继续。

【讨论】:

  • 很高兴你知道了;随意将复选标记授予您自己的答案,因为它似乎比我的更完整。
  • 至于路径,您可能希望使用-[NSURL path] 而不是absolutePath。您会注意到空格未转义,并且不包括方案 (file://)。
猜你喜欢
  • 2011-03-14
  • 1970-01-01
  • 2011-08-11
  • 2015-08-03
  • 2013-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多