objective-ccocoanstask

"Launch path not accessible" using NSTask to create Git commit


I am trying to use NSTask to create a Git commit and to add a message to that commit.

This is the code I have tried.

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

I received projectPath by using NSOpenPanel (standard OS X open dialog).

In the Xcode terminal, I get the message "launch path not accessible"

So what am I doing wrong?

Update After comment from Josh Caswell this is my code

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

After [task launch]; I get error message in terminal "working directory doesn't exist."


Solution

  • The task's launchPath is the path to the program you want to run: that's Git here, so that path probably needs to be /usr/local/bin/git. And remove @"git" from the arguments; it's not an argument, it's the executable.

    The path to your project should be used for the task's currentDirectoryPath so that it has the correct working directory.