我正在嘗試使用NSTask
創建Git提交併向該提交添加消息。「啓動路徑不可訪問」使用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的終端,我得到的消息 「啓動路徑無法訪問」
那我做錯了嗎?
更新 喬什 - 卡斯威爾評論之後,這是我的代碼
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];
[任務啓動之後;我在終端「工作目錄不存在」中收到錯誤消息。
感謝您的澄清。你說的是有道理的。不幸的是,我仍然沒有這樣的運氣。當我在終端中輸入:「which git」時,我得到「/ usr/bin/git」,所以我想我應該用launchIn而不是「/ usr/local/bin/git」。但我仍然得到「發射路徑不可訪問」的錯誤。此外,當我添加「currentDirectoryPath」我得到消息「啓動路徑不可訪問」。有什麼方法可以查看正在寫入提示和哪個文件夾?爲了調試這個? – SimpleApp
我不認爲有任何輸出,因爲'NSTask'本身並沒有開始。這聽起來像是存在權限或$ PATH問題,但我無法想象/ usr/bin不在$ PATH中。 –
在Termainal中,我可以從任何文件夾調用git。當我輸入「echo $ PATH」時,我得到「/ usr/local/bin:/ usr/bin:/ bin:/ usr/sbin:/ sbin」 – SimpleApp