-
Notifications
You must be signed in to change notification settings - Fork 601
/
PBGitBinary.m
112 lines (87 loc) · 2.42 KB
/
PBGitBinary.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
// PBGitBinary.m
// GitX
//
// Created by Pieter de Bie on 04-10-08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "PBGitBinary.h"
#import "PBEasyPipe.h"
@implementation PBGitBinary
static NSString* gitPath = nil;
+ (NSString *)versionForPath:(NSString *)path
{
if (!path)
return nil;
if (![[NSFileManager defaultManager] fileExistsAtPath:path])
return nil;
NSString *version = [PBEasyPipe outputForCommand:path withArgs:[NSArray arrayWithObject:@"--version"]];
if ([version hasPrefix:@"git version "])
return [version substringFromIndex:12];
return nil;
}
+ (BOOL) acceptBinary:(NSString *)path
{
if (!path)
return NO;
NSString *version = [self versionForPath:path];
if (!version)
return NO;
int c = [version compare:@"1.5.4"];
if (c == NSOrderedSame || c == NSOrderedDescending) {
gitPath = path;
return YES;
}
NSLog(@"Found a git binary at %@, but is only version %@", path, version);
return NO;
}
+ (void) initialize
{
// Try to find the path of the Git binary
char* path = getenv("GIT_PATH");
if (path && [self acceptBinary:[NSString stringWithCString:path]])
return;
// No explicit path. Try it with "which"
NSString *whichPath = [PBEasyPipe outputForCommand:@"/usr/bin/which" withArgs:[NSArray arrayWithObject:@"git"]];
if ([self acceptBinary:whichPath])
return;
// Still no path. Let's try some default locations.
for (NSString* location in [PBGitBinary searchLocations]) {
if ([self acceptBinary:location])
return;
}
NSLog(@"Could not find a git binary higher than version 1.5.4.");
}
+ (NSString *) path;
{
return gitPath;
}
static NSMutableArray *locations = nil;
+ (NSArray *) searchLocations
{
if (locations)
return locations;
locations = [NSMutableArray arrayWithObjects:@"/opt/local/bin/git",
@"/sw/bin/git",
@"/opt/git/bin/git",
@"/usr/local/bin/git",
@"/usr/local/git/bin/git",
nil];
[locations addObject:[@"~/bin/git" stringByExpandingTildeInPath]];
return locations;
}
+ (NSString *) notFoundError
{
NSMutableString *error = [NSMutableString stringWithString:
@"Could not find a git binary version 1.5.4 or higher.\n"
"Please make sure there is a git binary in one of the following locations:\n\n"];
for (NSString *location in [PBGitBinary searchLocations]) {
[error appendFormat:@"\t%@\n", location];
}
return error;
}
+ (NSString *)version
{
return [self versionForPath:gitPath];
}
@end