Skip to content

Instantly share code, notes, and snippets.

@pieter
Created September 26, 2008 21:22
Show Gist options
  • Save pieter/13205 to your computer and use it in GitHub Desktop.
Save pieter/13205 to your computer and use it in GitHub Desktop.
From 86606ef81564f439b3b2539f2af0019cd4ce6744 Mon Sep 17 00:00:00 2001
From: Pieter de Bie <pdebie@ai.rug.nl>
Date: Fri, 26 Sep 2008 23:02:49 +0200
Subject: [PATCH] Add a --commit option to the CLI client
This changes a lot of code, so quick review:
* RepositoryDocumentController now returns the document without selecting a ref
* PBGitWindowController now optionally shows the default view, or selects no view at all
* PBGitRepository keeps a pointer to its WindowController so that it can change views
---
PBCLIProxy.mm | 22 ++++++++++++++++------
PBGitRepository.h | 5 ++++-
PBGitRepository.m | 31 +++++++++++++++++++++++--------
PBGitWindowController.h | 6 +++---
PBGitWindowController.m | 10 ++++++++--
PBRepositoryDocumentController.h | 2 +-
PBRepositoryDocumentController.m | 13 +++++--------
7 files changed, 60 insertions(+), 29 deletions(-)
diff --git a/PBCLIProxy.mm b/PBCLIProxy.mm
index 5652a98..938b4d2 100644
--- a/PBCLIProxy.mm
+++ b/PBCLIProxy.mm
@@ -9,6 +9,8 @@
#import "PBCLIProxy.h"
#import "PBRepositoryDocumentController.h"
#import "PBGitRevSpecifier.h"
+#import "PBGitRepository.h"
+#import "PBGitWindowController.h"
@implementation PBCLIProxy
@synthesize connection;
@@ -31,14 +33,22 @@
// FIXME I found that creating this redundant NSURL reference was necessary to
// work around an apparent bug with GC and Distributed Objects
// I am not familiar with GC though, so perhaps I was doing something wrong.
-
NSURL* url = [NSURL fileURLWithPath:[repositoryPath path]];
NSArray* arguments = [NSArray arrayWithArray:args];
- PBGitRevSpecifier* rev = [[PBGitRevSpecifier alloc] initWithParameters:arguments];
- if ([[PBRepositoryDocumentController sharedDocumentController] openRepositoryAtLocation: url RevSpecifier: rev]) {
- [NSApp activateIgnoringOtherApps:YES];
- return YES;
+
+ PBGitRepository *document = [[PBRepositoryDocumentController sharedDocumentController] documentForLocation:url];
+ if (!document)
+ return NO;
+
+ if ([arguments count] > 0 && ([[arguments objectAtIndex:0] isEqualToString:@"--commit"] ||
+ [[arguments objectAtIndex:0] isEqualToString:@"-c"]))
+ ((PBGitWindowController *)document.windowController).selectedViewIndex = 1;
+ else {
+ PBGitRevSpecifier* rev = [[PBGitRevSpecifier alloc] initWithParameters:arguments];
+ [document selectBranch: [document addBranch: rev]];
}
- return NO;
+ [NSApp activateIgnoringOtherApps:YES];
+
+ return YES;
}
@end
diff --git a/PBGitRepository.h b/PBGitRepository.h
index 273b477..bb49393 100644
--- a/PBGitRepository.h
+++ b/PBGitRepository.h
@@ -14,6 +14,8 @@ extern NSString* PBGitRepositoryErrorDomain;
@interface PBGitRepository : NSDocument {
PBGitRevList* revisionList;
+ NSWindowController *windowController;
+
NSMutableArray* branches;
NSIndexSet* currentBranch;
NSMutableDictionary* refs;
@@ -45,9 +47,10 @@ extern NSString* PBGitRepositoryErrorDomain;
+ (NSURL*)gitDirForURL:(NSURL*)repositoryURL;
+ (NSURL*)baseDirForURL:(NSURL*)repositoryURL;
-- (id) initWithURL: (NSURL*) path andRevSpecifier:(PBGitRevSpecifier*) rev;
+- (id) initWithURL: (NSURL*) path;
- (void) setup;
+@property (readonly) NSWindowController *windowController;
@property (retain) PBGitRevList* revisionList;
@property (assign) NSMutableArray* branches;
@property (assign) NSIndexSet* currentBranch;
diff --git a/PBGitRepository.m b/PBGitRepository.m
index 323c02d..c646521 100644
--- a/PBGitRepository.m
+++ b/PBGitRepository.m
@@ -19,7 +19,7 @@ NSString* PBGitRepositoryErrorDomain = @"GitXErrorDomain";
@implementation PBGitRepository
-@synthesize revisionList, branches, currentBranch, refs;
+@synthesize revisionList, branches, currentBranch, refs, windowController;
static NSString* gitPath;
+ (void) initialize
@@ -131,12 +131,11 @@ static NSString* gitPath;
- (void) setup
{
- self.branches = [NSMutableArray array];
[self reloadRefs];
revisionList = [[PBGitRevList alloc] initWithRepository:self];
}
-- (id) initWithURL: (NSURL*) path andRevSpecifier:(PBGitRevSpecifier*) rev
+- (id) initWithURL: (NSURL*) path
{
NSURL* gitDirURL = [PBGitRepository gitDirForURL:path];
if (!gitDirURL)
@@ -146,10 +145,16 @@ static NSString* gitPath;
[self setFileURL: gitDirURL];
[self setup];
- [self selectBranch: [self addBranch: rev]];
+
+ // We don't want the window controller to display anything yet..
+ // We'll leave that to the caller of this method.
+ windowController = [[PBGitWindowController alloc] initWithRepository:self displayDefault:NO];
+ [self addWindowController:windowController];
+ [self showWindows];
return self;
}
+
// The fileURL the document keeps is to the .git dir, but that’s pretty
// useless for display in the window title bar, so we show the directory above
- (NSString*)displayName
@@ -163,11 +168,11 @@ static NSString* gitPath;
// Overridden to create our custom window controller
- (void)makeWindowControllers
{
- PBGitWindowController* controller = [[PBGitWindowController alloc] initWithRepository:self];
- [self addWindowController:controller];
- [controller release];
+ windowController = [[PBGitWindowController alloc] initWithRepository:self displayDefault:YES];
+ [self addWindowController:windowController];
}
+
- (void) addRef: (PBGitRef *) ref fromParameters: (NSArray *) components
{
NSString* type = [components objectAtIndex:1];
@@ -192,6 +197,7 @@ static NSString* gitPath;
- (BOOL) reloadRefs
{
BOOL ret = NO;
+ self.branches = [NSMutableArray array];
NSString* output = [PBEasyPipe outputForCommand:gitPath withArgs:[NSArray arrayWithObjects:@"for-each-ref", @"--format=%(refname) %(objecttype) %(objectname) %(*objectname)", @"refs", nil] inDir: self.fileURL.path];
NSArray* lines = [output componentsSeparatedByString:@"\n"];
refs = [NSMutableDictionary dictionary];
@@ -240,6 +246,14 @@ static NSString* gitPath;
return rev;
}
+- (void) showHistoryView
+{
+ if (!windowController)
+ return;
+
+ ((PBGitWindowController *)windowController).selectedViewIndex = 0;
+}
+
- (void) selectBranch: (PBGitRevSpecifier*) rev
{
int i;
@@ -247,11 +261,12 @@ static NSString* gitPath;
PBGitRevSpecifier* aRev = [branches objectAtIndex:i];
if (rev == aRev) {
self.currentBranch = [NSIndexSet indexSetWithIndex:i];
+ [self showHistoryView];
return;
}
}
}
-
+
- (void) readCurrentBranch
{
[self selectBranch: [self addBranch: [self headRef]]];
diff --git a/PBGitWindowController.h b/PBGitWindowController.h
index c7f589c..d03e456 100644
--- a/PBGitWindowController.h
+++ b/PBGitWindowController.h
@@ -13,18 +13,18 @@
IBOutlet NSSearchField* searchField;
IBOutlet NSArrayController* searchController;
IBOutlet NSArrayController *branchesController;
- PBGitRepository* repository;
+ __weak PBGitRepository* repository;
int selectedViewIndex;
IBOutlet NSView* contentView;
NSViewController* viewController;
}
-@property (retain) PBGitRepository *repository;
+@property (assign) __weak PBGitRepository *repository;
@property (readonly) NSViewController *viewController;
@property (assign) int selectedViewIndex;
@property (retain) NSArrayController *searchController;
-- (id)initWithRepository:(PBGitRepository*)theRepository;
+- (id)initWithRepository:(PBGitRepository*)theRepository displayDefault:(BOOL)display;
- (void)changeViewController:(NSInteger)whichViewTag;
- (void) focusOnSearchField;
diff --git a/PBGitWindowController.m b/PBGitWindowController.m
index 3fcb0b2..5f92afd 100644
--- a/PBGitWindowController.m
+++ b/PBGitWindowController.m
@@ -16,13 +16,20 @@
@synthesize repository, viewController, searchController, selectedViewIndex;
-- (id)initWithRepository:(PBGitRepository*)theRepository;
+- (id)initWithRepository:(PBGitRepository*)theRepository displayDefault:(BOOL)displayDefault
{
if(self = [self initWithWindowNibName:@"RepositoryWindow"])
{
self.repository = theRepository;
[self showWindow:nil];
}
+
+ if (displayDefault) {
+ self.selectedViewIndex = [[NSUserDefaults standardUserDefaults] integerForKey:@"selectedViewIndex"];
+ } else {
+ self.selectedViewIndex = -1;
+ }
+
return self;
}
@@ -76,7 +83,6 @@
// We bind this ourselves because otherwise we would lose our selection
[branchesController bind:@"selectionIndexes" toObject:repository withKeyPath:@"currentBranch" options:nil];
- self.selectedViewIndex = [[NSUserDefaults standardUserDefaults] integerForKey:@"selectedViewIndex"];
[[self window] setAutorecalculatesContentBorderThickness:NO forEdge:NSMinYEdge];
[[self window] setContentBorderThickness:35.0f forEdge:NSMinYEdge];
diff --git a/PBRepositoryDocumentController.h b/PBRepositoryDocumentController.h
index e23d6b6..d0660e2 100644
--- a/PBRepositoryDocumentController.h
+++ b/PBRepositoryDocumentController.h
@@ -15,5 +15,5 @@
}
-- (id) openRepositoryAtLocation:(NSURL*) url RevSpecifier:(PBGitRevSpecifier*) rev;
+- (id) documentForLocation:(NSURL*) url;
@end
diff --git a/PBRepositoryDocumentController.m b/PBRepositoryDocumentController.m
index 1b1cf90..1ab7cad 100644
--- a/PBRepositoryDocumentController.m
+++ b/PBRepositoryDocumentController.m
@@ -31,20 +31,17 @@
[super noteNewRecentDocumentURL:[PBGitRepository baseDirForURL:url]];
}
-- (id) openRepositoryAtLocation:(NSURL*) url RevSpecifier:(PBGitRevSpecifier*) rev
+- (id) documentForLocation:(NSURL*) url
{
id document = [self documentForURL:url];
if (!document) {
- document = [[PBGitRepository alloc] initWithURL:url andRevSpecifier:rev];
- if (!document)
- return nil;
+
+ if (!(document = [[PBGitRepository alloc] initWithURL:url]))
+ return NO;
[self addDocument:document];
- [document makeWindowControllers];
- } else {
- [document selectBranch: [document addBranch: rev]];
}
- [document showWindows];
+
return document;
}
@end
--
1.6.0.2.415.gf9137
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment