forked from pieter/gitx
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PBGitBinary.m
127 lines (101 loc) · 3.12 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// 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:@"" MIN_GIT_VERSION];
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
{
// Check what we might have in user defaults
// NOTE: Currently this should NOT have a registered default, or the searching bits below won't work
gitPath = [[NSUserDefaults standardUserDefaults] stringForKey:@"gitExecutable"];
if (gitPath.length > 0) {
if ([self acceptBinary:gitPath])
return;
[[NSAlert alertWithMessageText:@"Invalid git path"
defaultButton:@"OK"
alternateButton:nil
otherButton:nil
informativeTextWithFormat:@"You entered a custom git path in the Preferences pane, "
"but this path is not a valid git v" MIN_GIT_VERSION " or higher binary. We're going to use the default "
"search paths instead"] runModal];
}
// Try to find the path of the Git binary
char* path = getenv("GIT_PATH");
if (path && [self acceptBinary:[NSString stringWithUTF8String: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 " MIN_GIT_VERSION);
}
+ (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 " MIN_GIT_VERSION " 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