5 // Created by Pieter de Bie on 04-10-08.
6 // Copyright 2008 __MyCompanyName__. All rights reserved.
9 #import "PBGitBinary.h"
10 #import "PBEasyPipe.h"
12 @implementation PBGitBinary
14 static NSString* gitPath = nil;
16 + (NSString *)versionForPath:(NSString *)path
21 if (![[NSFileManager defaultManager] fileExistsAtPath:path])
24 NSString *version = [PBEasyPipe outputForCommand:path withArgs:[NSArray arrayWithObject:@"--version"]];
25 if ([version hasPrefix:@"git version "])
26 return [version substringFromIndex:12];
31 + (BOOL) acceptBinary:(NSString *)path
36 NSString *version = [self versionForPath:path];
40 int c = [version compare:@"" MIN_GIT_VERSION];
41 if (c == NSOrderedSame || c == NSOrderedDescending) {
46 NSLog(@"Found a git binary at %@, but is only version %@", path, version);
52 // Check what we might have in user defaults
53 // NOTE: Currently this should NOT have a registered default, or the searching bits below won't work
54 gitPath = [[NSUserDefaults standardUserDefaults] stringForKey:@"gitExecutable"];
55 if (gitPath.length > 0) {
56 if ([self acceptBinary:gitPath])
58 [[NSAlert alertWithMessageText:@"Invalid git path"
62 informativeTextWithFormat:@"You entered a custom git path in the Preferences pane, "
63 "but this path is not a valid git v" MIN_GIT_VERSION " or higher binary. We're going to use the default "
64 "search paths instead"] runModal];
67 // Try to find the path of the Git binary
68 char* path = getenv("GIT_PATH");
69 if (path && [self acceptBinary:[NSString stringWithUTF8String:path]])
72 // No explicit path. Try it with "which"
73 NSString *whichPath = [PBEasyPipe outputForCommand:@"/usr/bin/which" withArgs:[NSArray arrayWithObject:@"git"]];
74 if ([self acceptBinary:whichPath])
77 // Still no path. Let's try some default locations.
78 for (NSString* location in [PBGitBinary searchLocations]) {
79 if ([self acceptBinary:location])
83 NSLog(@"Could not find a git binary higher than version " MIN_GIT_VERSION);
91 static NSMutableArray *locations = nil;
93 + (NSArray *) searchLocations
98 locations = [NSMutableArray arrayWithObjects:@"/opt/local/bin/git",
101 @"/usr/local/bin/git",
102 @"/usr/local/git/bin/git",
105 [locations addObject:[@"~/bin/git" stringByExpandingTildeInPath]];
109 + (NSString *) notFoundError
111 NSMutableString *error = [NSMutableString stringWithString:
112 @"Could not find a git binary version " MIN_GIT_VERSION " or higher.\n"
113 "Please make sure there is a git binary in one of the following locations:\n\n"];
114 for (NSString *location in [PBGitBinary searchLocations]) {
115 [error appendFormat:@"\t%@\n", location];
121 + (NSString *)version
123 return [self versionForPath:gitPath];