"Launch path not accessible" 使用 NSTask 创建 Git 提交
"Launch path not accessible" using NSTask to create Git commit
我正在尝试使用 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 终端中,我收到消息
"launch path not accessible"
那我做错了什么?
更新
在 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];
[任务启动]之后;我在终端 "working directory doesn't exist."
中收到错误消息
任务的launchPath
是你想要运行的程序的路径运行:这里是Git,所以那个路径可能需要成为 /usr/local/bin/git
。并从参数中删除 @"git"
;这不是一个参数,它是可执行文件。
项目的路径应该用于任务的 currentDirectoryPath
,以便它具有正确的工作目录。
根据 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];
如果你不断收到消息
fatal: Unable to create
'/Users/MYNAME/Desktop/MYPROJECT/.git/index.lock': File exists.
Another git process seems to be running in this repository, e.g. an
editor opened by 'git commit'. Please make sure all processes are
terminated then try again. If it still fails, a git process may have
crashed in this repository earlier: remove the file manually to
continue.
我正在尝试使用 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 终端中,我收到消息 "launch path not accessible"
那我做错了什么?
更新 在 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];
[任务启动]之后;我在终端 "working directory doesn't exist."
中收到错误消息任务的launchPath
是你想要运行的程序的路径运行:这里是Git,所以那个路径可能需要成为 /usr/local/bin/git
。并从参数中删除 @"git"
;这不是一个参数,它是可执行文件。
项目的路径应该用于任务的 currentDirectoryPath
,以便它具有正确的工作目录。
根据 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]; 如果你不断收到消息
fatal: Unable to create '/Users/MYNAME/Desktop/MYPROJECT/.git/index.lock': File exists.
Another git process seems to be running in this repository, e.g. an editor opened by 'git commit'. Please make sure all processes are terminated then try again. If it still fails, a git process may have crashed in this repository earlier: remove the file manually to continue.