This fixes erratic behaviour when running 'gitx' from a plain directory.
When 'git rev-parse --git-dir' is called outside a git-repository, it
still returns text but indicates failure using the return code (128).
This way, the error can be caught by us and indicated by returning nil.

Furthermore we ensure the smooth operation of 'gitx' by catching
possible exceptions and printing them.

Signed-off-by: Johannes Gilger <[email protected]>
---
Hi list,

this patch fixes the error described by Andrew vonderLuft on 19.7.2010 and also
noted in Nathan's GitHub issue-tracker at
http://github.com/brotherbard/gitx/issues#issue/21

Greetings,
Jojo

 PBCLIProxy.h                     |    2 +-
 PBCLIProxy.m                     |    2 +-
 PBGitRepository.m                |    7 +++++--
 PBRepositoryDocumentController.m |    9 +++++++--
 gitx.m                           |   19 +++++++++++++------
 5 files changed, 27 insertions(+), 12 deletions(-)

diff --git a/PBCLIProxy.h b/PBCLIProxy.h
index eb4d735..20a00f3 100644
--- a/PBCLIProxy.h
+++ b/PBCLIProxy.h
@@ -20,6 +20,6 @@
 #define PBCLIProxyErrorDomain @"PBCLIProxyErrorDomain"
 
 @protocol GitXCliToolProtocol
-- (BOOL)openRepository:(NSURL*)repositoryPath arguments: (NSArray*) args 
error:(NSError**)error;
+- (BOOL)openRepository:(NSURL*)repositoryPath arguments:(NSArray*)args 
error:(NSError**)error;
 - (void)openDiffWindowWithDiff:(NSString *)diff;
 @end
\ No newline at end of file
diff --git a/PBCLIProxy.m b/PBCLIProxy.m
index 416c917..d9d9d11 100644
--- a/PBCLIProxy.m
+++ b/PBCLIProxy.m
@@ -30,7 +30,7 @@ - (id)init
        return self;
 }
 
-- (BOOL)openRepository:(NSURL*)repositoryPath arguments: (NSArray*) args 
error:(NSError**)error;
+- (BOOL)openRepository:(NSURL*)repositoryPath arguments:(NSArray*)args 
error:(NSError**)error
 {
        // FIXME I found that creating this redundant NSURL reference was 
necessary to
        // work around an apparent bug with GC and Distributed Objects
diff --git a/PBGitRepository.m b/PBGitRepository.m
index 5b77b23..0ca1e2b 100644
--- a/PBGitRepository.m
+++ b/PBGitRepository.m
@@ -51,9 +51,12 @@ + (NSURL*)gitDirForURL:(NSURL*)repositoryURL;
        if ([self isBareRepository:repositoryPath])
                return repositoryURL;
 
-
        // Use rev-parse to find the .git dir for the repository being opened
-       NSString* newPath = [PBEasyPipe outputForCommand:[PBGitBinary path] 
withArgs:[NSArray arrayWithObjects:@"rev-parse", @"--git-dir", nil] 
inDir:repositoryPath];
+       int ret = 0;
+       NSString* newPath = [PBEasyPipe outputForCommand:[PBGitBinary path] 
withArgs:[NSArray arrayWithObjects:@"rev-parse", @"--git-dir", nil] 
inDir:repositoryPath retValue:&ret];
+       if(ret)
+               return nil;
+
        if ([newPath isEqualToString:@".git"])
                return [NSURL fileURLWithPath:[repositoryPath 
stringByAppendingPathComponent:@".git"]];
        if ([newPath length] > 0)
diff --git a/PBRepositoryDocumentController.m b/PBRepositoryDocumentController.m
index 83bd887..ba85693 100644
--- a/PBRepositoryDocumentController.m
+++ b/PBRepositoryDocumentController.m
@@ -23,7 +23,11 @@ - (NSInteger)runModalOpenPanel:(NSOpenPanel *)openPanel 
forTypes:(NSArray *)exte
 // Convert paths to the .git dir before searching for an already open document
 - (id)documentForURL:(NSURL *)URL
 {
-       return [super documentForURL:[PBGitRepository gitDirForURL:URL]];
+       NSURL* gitDir = [PBGitRepository gitDirForURL:URL];
+       if(gitDir)
+               return [super documentForURL:gitDir];
+
+       return nil;
 }
 
 - (void)noteNewRecentDocumentURL:(NSURL*)url
@@ -36,7 +40,8 @@ - (id) documentForLocation:(NSURL*) url
        id document = [self documentForURL:url];
        if (!document) {
                
-               if (!(document = [[PBGitRepository alloc] initWithURL:url]))
+               document = [[PBGitRepository alloc] initWithURL:url];
+               if(!document)
                        return nil;
 
                [self addDocument:document];
diff --git a/gitx.m b/gitx.m
index 6449556..1b1c779 100644
--- a/gitx.m
+++ b/gitx.m
@@ -148,13 +148,20 @@ int main(int argc, const char** argv)
                handleDiffWithArguments([arguments 
subarrayWithRange:NSMakeRange(1, [arguments count] - 1)], pwd, proxy);
 
        // No diff, just open the current dir
-       NSURL* url = [NSURL fileURLWithPath:pwd];
-       NSError* error = nil;
+       NSURL *url = [NSURL fileURLWithPath:pwd];
+       NSError *error = nil;
+
+       @try {
+               if(![proxy openRepository:url arguments:arguments 
error:&error]) {
+                       fprintf(stderr, "Error opening repository at %s\n", 
[[url path] UTF8String]);
+                       if (error)
+                               fprintf(stderr, "%s\n", [[error 
localizedFailureReason] UTF8String]);
+                       return 1;
+               }
+       }
 
-       if (![proxy openRepository:url arguments: arguments error:&error]) {
-               fprintf(stderr, "Error opening repository at %s\n", [[url path] 
UTF8String]);
-               if (error)
-                       fprintf(stderr, "\t%s\n", [[error 
localizedFailureReason] UTF8String]);
+       @catch(NSException *exception) {
+               NSLog(@"main: Caught: %@: %@", [exception name], [exception 
reason]);
        }
 
        return 0;
-- 
1.7.2.2.178.gd8a94

Reply via email to