Testing: add missing file
[GitX.git] / PBGitBinary.m
blob795891d7a47c495bf9d52ae67c467602b38e8875
1 //
2 //  PBGitBinary.m
3 //  GitX
4 //
5 //  Created by Pieter de Bie on 04-10-08.
6 //  Copyright 2008 __MyCompanyName__. All rights reserved.
7 //
9 #import "PBGitBinary.h"
10 #import "PBEasyPipe.h"
12 @implementation PBGitBinary
14 static NSString* gitPath = nil;
16 + (NSString *)versionForPath:(NSString *)path
18         if (!path)
19                 return nil;
21         if (![[NSFileManager defaultManager] fileExistsAtPath:path])
22                 return nil;
24         NSString *version = [PBEasyPipe outputForCommand:path withArgs:[NSArray arrayWithObject:@"--version"]];
25         if ([version hasPrefix:@"git version "])
26                 return [version substringFromIndex:12];
28         return nil;
31 + (BOOL) acceptBinary:(NSString *)path
33         if (!path)
34                 return NO;
36         NSString *version = [self versionForPath:path];
37         if (!version)
38                 return NO;
40         int c = [version compare:@"" MIN_GIT_VERSION];
41         if (c == NSOrderedSame || c == NSOrderedDescending) {
42                 gitPath = path;
43                 return YES;
44         }
46         NSLog(@"Found a git binary at %@, but is only version %@", path, version);
47         return NO;
50 + (void) initialize
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])
57                         return;
58                 [[NSAlert alertWithMessageText:@"Invalid git path"
59                                                 defaultButton:@"OK"
60                                           alternateButton:nil
61                                                   otherButton:nil
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];
65         }
67         // Try to find the path of the Git binary
68         char* path = getenv("GIT_PATH");
69         if (path && [self acceptBinary:[NSString stringWithUTF8String:path]])
70                 return;
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])
75                 return;
77         // Still no path. Let's try some default locations.
78         for (NSString* location in [PBGitBinary searchLocations]) {
79                 if ([self acceptBinary:location])
80                         return;
81         }
83         NSLog(@"Could not find a git binary higher than version " MIN_GIT_VERSION);
86 + (NSString *) path;
88         return gitPath;
91 static NSMutableArray *locations = nil;
93 + (NSArray *) searchLocations
95         if (locations)
96                 return locations;
98         locations = [NSMutableArray arrayWithObjects:@"/opt/local/bin/git",
99                                                   @"/sw/bin/git",
100                                                   @"/opt/git/bin/git",
101                                                   @"/usr/local/bin/git",
102                                                   @"/usr/local/git/bin/git",
103                                                   nil];
105         [locations addObject:[@"~/bin/git" stringByExpandingTildeInPath]];
106         return locations;
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];
116         }
117         return error;
121 + (NSString *)version
123         return [self versionForPath:gitPath];
127 @end