From 6316b99d0dda686849c53f90566caf22c184c630 Mon Sep 17 00:00:00 2001 From: Pieter de Bie Date: Tue, 14 Oct 2008 15:51:39 +0200 Subject: [PATCH 1/4] gitx-cli: add a --version option This displays GitX version information, handy for debugging. Also shows where GitX will find a git binary. --- gitx.mm | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/gitx.mm b/gitx.mm index 2d1d3fa..956b440 100644 --- a/gitx.mm +++ b/gitx.mm @@ -19,7 +19,7 @@ NSDistantObject* connect() void usage(char const *programName) { - printf("Usage: %s --help\n", programName); + printf("Usage: %s (--help|--version)\n", programName); printf(" or: %s (--commit|-h)\n", programName); printf(" or: %s \n", programName); printf("\n"); @@ -35,10 +35,23 @@ void usage(char const *programName) exit(1); } +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]); + else + printf("GitX cannot find a git binary\n"); + exit(1); +} + int main(int argc, const char** argv) { if (argc >= 2 && (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h"))) usage(argv[0]); + if (argc >= 2 && (!strcmp(argv[1], "--version") || !strcmp(argv[1], "-v"))) + version_info(); if (![PBGitBinary path]) { printf("%s\n", [[PBGitBinary notFoundError] cStringUsingEncoding:NSUTF8StringEncoding]); From 3ba009dcde96a0633faf095d5340a7326ebde161 Mon Sep 17 00:00:00 2001 From: Pieter de Bie Date: Tue, 14 Oct 2008 16:27:50 +0200 Subject: [PATCH 2/4] 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); From 7c28f56f77e6dd3aef7a6c6bd9a65935a0928eed Mon Sep 17 00:00:00 2001 From: Pieter de Bie Date: Tue, 14 Oct 2008 16:28:57 +0200 Subject: [PATCH 3/4] Move CLI files to cli group --- GitX.xcodeproj/project.pbxproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/GitX.xcodeproj/project.pbxproj b/GitX.xcodeproj/project.pbxproj index edaa979..b9c328d 100644 --- a/GitX.xcodeproj/project.pbxproj +++ b/GitX.xcodeproj/project.pbxproj @@ -322,6 +322,8 @@ 913D5E420E5563FD00CECEA2 /* cli */ = { isa = PBXGroup; children = ( + 913D5E5D0E556A9300CECEA2 /* PBCLIProxy.h */, + 913D5E5E0E556A9300CECEA2 /* PBCLIProxy.mm */, 913D5E440E55640C00CECEA2 /* gitx.mm */, ); name = cli; @@ -351,8 +353,6 @@ F57CC4400E05E496000472E2 /* PBGitWindowController.m */, 911111F60E594F3F00BF76B4 /* PBRepositoryDocumentController.h */, 911111F70E594F3F00BF76B4 /* PBRepositoryDocumentController.m */, - 913D5E5D0E556A9300CECEA2 /* PBCLIProxy.h */, - 913D5E5E0E556A9300CECEA2 /* PBCLIProxy.mm */, F5E926040E8827D300056E75 /* PBViewController.h */, F5EF8C8C0E9D4A5D0050906B /* PBWebController.h */, F5E926050E8827D300056E75 /* PBViewController.m */, From 59e3a181ed751483e8c5f7786944701bbfca7595 Mon Sep 17 00:00:00 2001 From: Pieter de Bie Date: Tue, 14 Oct 2008 16:35:31 +0200 Subject: [PATCH 4/4] cli-tool: Hint at another error if there is a git binary This will show something like Vienna:~ pieter$ gitx Error opening repository at /Users/pieter: Could not create document. Perhaps this isn't a git repository? if you try to open GitX in a directory that isn't supported. That should be more helpful than the suggestion that GitX can't find a binary. --- PBCLIProxy.mm | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/PBCLIProxy.mm b/PBCLIProxy.mm index 99d3a42..e72ad5b 100644 --- a/PBCLIProxy.mm +++ b/PBCLIProxy.mm @@ -11,6 +11,7 @@ #import "PBGitRevSpecifier.h" #import "PBGitRepository.h" #import "PBGitWindowController.h" +#import "PBGitBinary.h" @implementation PBCLIProxy @synthesize connection; @@ -39,7 +40,11 @@ PBGitRepository *document = [[PBRepositoryDocumentController sharedDocumentController] documentForLocation:url]; if (!document) { if (error) { - NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"Could not create document. Perhaps GitX can't find you git binary?" forKey:NSLocalizedFailureReasonErrorKey]; + NSString *suggestion = [PBGitBinary path] ? @"this isn't a git repository" : @"GitX can't find your git binary"; + + NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Could not create document. Perhaps %@", suggestion] + forKey:NSLocalizedFailureReasonErrorKey]; + *error = [NSError errorWithDomain:PBGitRepositoryErrorDomain code:2 userInfo:userInfo]; } return NO;