From 3ba009dcde96a0633faf095d5340a7326ebde161 Mon Sep 17 00:00:00 2001 From: Pieter de Bie Date: Tue, 14 Oct 2008 16:27:50 +0200 Subject: [PATCH] GitBinary: Add git version information This makes sure that any git binary found will actually be version 1.5.4 or higher. --- PBGitBinary.h | 1 + PBGitBinary.m | 61 ++++++++++++++++++++++++++++++++++++++++----------- gitx.mm | 2 +- 3 files changed, 50 insertions(+), 14 deletions(-) diff --git a/PBGitBinary.h b/PBGitBinary.h index 83c31e8..804cbe9 100644 --- a/PBGitBinary.h +++ b/PBGitBinary.h @@ -14,6 +14,7 @@ } + (NSString *) path; ++ (NSString *) version; + (NSArray *) searchLocations; + (NSString *) notFoundError; @end diff --git a/PBGitBinary.m b/PBGitBinary.m index aeb6d79..1c83683 100644 --- a/PBGitBinary.m +++ b/PBGitBinary.m @@ -11,33 +11,61 @@ @implementation PBGitBinary -static NSString* gitPath; +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 { - gitPath = nil; - // Try to find the path of the Git binary char* path = getenv("GIT_PATH"); - if (path != nil) { - gitPath = [NSString stringWithCString:path]; + if (path && [self acceptBinary:[NSString stringWithCString:path]]) return; - } // No explicit path. Try it with "which" - gitPath = [PBEasyPipe outputForCommand:@"/usr/bin/which" withArgs:[NSArray arrayWithObject:@"git"]]; - if (gitPath.length > 0) + 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 ([[NSFileManager defaultManager] fileExistsAtPath:location]) { - gitPath = location; + if ([self acceptBinary:location]) return; - } } - NSLog(@"Could not find a git binary!"); + NSLog(@"Could not find a git binary higher than version 1.5.4."); } + (NSString *) path; @@ -66,7 +94,7 @@ static NSMutableArray *locations = nil; + (NSString *) notFoundError { NSMutableString *error = [NSMutableString stringWithString: - @"Could not find a git binary\n" + @"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]; @@ -74,4 +102,11 @@ static NSMutableArray *locations = nil; return error; } + ++ (NSString *)version +{ + return [self versionForPath:gitPath]; +} + + @end diff --git a/gitx.mm b/gitx.mm index 956b440..fb4cb9c 100644 --- a/gitx.mm +++ b/gitx.mm @@ -40,7 +40,7 @@ void version_info() NSString *version = [[[NSBundle bundleForClass:[PBGitBinary class]] infoDictionary] valueForKey:@"CFBundleVersion"]; printf("This is GitX version %s\n", [version UTF8String]); if ([PBGitBinary path]) - printf("Using git found at %s\n", [[PBGitBinary path] UTF8String]); + printf("Using git found at %s, version %s\n", [[PBGitBinary path] UTF8String], [[PBGitBinary version] UTF8String]); else printf("GitX cannot find a git binary\n"); exit(1);