From 3dd9e2a41c828cdaea26601311f4f03834ac65b9 Mon Sep 17 00:00:00 2001 From: Pieter de Bie Date: Sat, 14 Jun 2008 20:06:07 +0200 Subject: [PATCH] Guess user's git location --- PBGitRepository.h | 1 + PBGitRepository.m | 30 +++++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/PBGitRepository.h b/PBGitRepository.h index 7d4513c..d2a7532 100644 --- a/PBGitRepository.h +++ b/PBGitRepository.h @@ -14,6 +14,7 @@ } + (PBGitRepository*) repositoryWithPath:(NSString*) path; ++ (void) setGitPath; - (NSFileHandle*) handleForCommand:(NSString*) cmd; - (void) addCommit: (id)obj; diff --git a/PBGitRepository.m b/PBGitRepository.m index bd923e4..8f639de 100644 --- a/PBGitRepository.m +++ b/PBGitRepository.m @@ -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];