Guess user's git location

This commit is contained in:
Pieter de Bie
2008-06-14 20:06:07 +02:00
parent 4ba028dcde
commit 3dd9e2a41c
2 changed files with 30 additions and 1 deletions
+1
View File
@@ -14,6 +14,7 @@
}
+ (PBGitRepository*) repositoryWithPath:(NSString*) path;
+ (void) setGitPath;
- (NSFileHandle*) handleForCommand:(NSString*) cmd;
- (void) addCommit: (id)obj;
+29 -1
View File
@@ -13,15 +13,43 @@
@implementation PBGitRepository
static NSString* gitPath = @"/opt/pieter/bin/git";
static NSString* gitPath = @"/usr/bin/env";
+ (PBGitRepository*) repositoryWithPath:(NSString*) path
{
[self setGitPath];
PBGitRepository* repo = [[PBGitRepository alloc] init];
repo.path = path;
return repo;
}
+ (void) setGitPath
{
char* path = getenv("GIT_PATH");
if (path != nil) {
gitPath = [NSString stringWithCString:path];
NSLog(@"Git path is now '%@'", gitPath);
return;
}
// No explicit path. Try it with "which"
NSTask* task = [[NSTask alloc] init];
task.launchPath = @"/usr/bin/which";
task.arguments = [NSArray arrayWithObject:@"git"];
NSPipe* pipe = [NSPipe pipe];
NSFileHandle* handle = [pipe fileHandleForReading];
task.standardOutput = pipe;
[task launch];
NSString* a = [handle readLine];
gitPath = a;
NSLog(@"Git path is now '%@'", gitPath);
if (a.length == 0) {
NSLog(@"Git path not found. Defaulting to /opt/pieter/bin/git");
gitPath = @"/opt/pieter/bin/git";
}
}
- (void) addCommit: (id) obj
{
self.commits = [self.commits arrayByAddingObject:obj];