GitX v0.4.1
[GitX.git] / PBGitBinary.m
blobaeb6d79d447269a92c9fa6ee0d95e3edcb43415e
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;
16 + (void) initialize
18         gitPath = nil;
20         // Try to find the path of the Git binary
21         char* path = getenv("GIT_PATH");
22         if (path != nil) {
23                 gitPath = [NSString stringWithCString:path];
24                 return;
25         }
27         // No explicit path. Try it with "which"
28         gitPath = [PBEasyPipe outputForCommand:@"/usr/bin/which" withArgs:[NSArray arrayWithObject:@"git"]];
29         if (gitPath.length > 0)
30                 return;
32         // Still no path. Let's try some default locations.
33         for (NSString* location in [PBGitBinary searchLocations]) {
34                 if ([[NSFileManager defaultManager] fileExistsAtPath:location]) {
35                         gitPath = location;
36                         return;
37                 }
38         }
40         NSLog(@"Could not find a git binary!");
43 + (NSString *) path;
45         return gitPath;
48 static NSMutableArray *locations = nil;
50 + (NSArray *) searchLocations
52         if (locations)
53                 return locations;
55         locations = [NSMutableArray arrayWithObjects:@"/opt/local/bin/git",
56                                                   @"/sw/bin/git",
57                                                   @"/opt/git/bin/git",
58                                                   @"/usr/local/bin/git",
59                                                   @"/usr/local/git/bin/git",
60                                                   nil];
62         [locations addObject:[@"~/bin/git" stringByExpandingTildeInPath]];
63         return locations;
66 + (NSString *) notFoundError
68         NSMutableString *error = [NSMutableString stringWithString:
69                                                           @"Could not find a git binary\n"
70                                                           "Please make sure there is a git binary in one of the following locations:\n\n"];
71         for (NSString *location in [PBGitBinary searchLocations]) {
72                 [error appendFormat:@"\t%@\n", location];
73         }
74         return error;
77 @end