"Launch path not accessible"使用 NSTask 创建 Git 提交



我正在尝试使用 NSTask创建git commit并在该提交中添加消息。

这是我尝试过的代码。

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收到projectPath(标准OS X打开对话框(。

在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];

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

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

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

带有乔什·卡斯韦尔(Josh Casswell(答案的指针,我设法弄清楚了。我发现我必须从项目路径中删除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过程可能会有 在此存储库中坠毁:手动将文件删除到 继续。

最新更新