Skip to content

Commit

Permalink
Add a --commit option to the CLI client
Browse files Browse the repository at this point in the history
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
  • Loading branch information
pieter committed Sep 26, 2008
1 parent de9f4dd commit 86606ef
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 29 deletions.
22 changes: 16 additions & 6 deletions PBCLIProxy.mm
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#import "PBCLIProxy.h"
#import "PBRepositoryDocumentController.h"
#import "PBGitRevSpecifier.h"
#import "PBGitRepository.h"
#import "PBGitWindowController.h"

@implementation PBCLIProxy
@synthesize connection;
Expand All @@ -31,14 +33,22 @@ - (BOOL)openRepository:(NSURL*)repositoryPath arguments: (NSArray*) args error:(
// 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
5 changes: 4 additions & 1 deletion PBGitRepository.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ extern NSString* PBGitRepositoryErrorDomain;

@interface PBGitRepository : NSDocument {
PBGitRevList* revisionList;
NSWindowController *windowController;

NSMutableArray* branches;
NSIndexSet* currentBranch;
NSMutableDictionary* refs;
Expand Down Expand Up @@ -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;
Expand Down
31 changes: 23 additions & 8 deletions PBGitRepository.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

@implementation PBGitRepository

@synthesize revisionList, branches, currentBranch, refs;
@synthesize revisionList, branches, currentBranch, refs, windowController;
static NSString* gitPath;

+ (void) initialize
Expand Down Expand Up @@ -131,12 +131,11 @@ - (BOOL)readFromFileWrapper:(NSFileWrapper *)fileWrapper ofType:(NSString *)type

- (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)
Expand All @@ -146,10 +145,16 @@ - (id) initWithURL: (NSURL*) path andRevSpecifier:(PBGitRevSpecifier*) rev
[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
Expand All @@ -163,11 +168,11 @@ - (NSString*)displayName
// 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];
Expand All @@ -192,6 +197,7 @@ - (void) addRef: (PBGitRef *) ref fromParameters: (NSArray *) components
- (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];
Expand Down Expand Up @@ -240,18 +246,27 @@ - (PBGitRevSpecifier*) addBranch: (PBGitRevSpecifier*) rev
return rev;
}

- (void) showHistoryView
{
if (!windowController)
return;

((PBGitWindowController *)windowController).selectedViewIndex = 0;
}

- (void) selectBranch: (PBGitRevSpecifier*) rev
{
int i;
for (i = 0; i < [branches count]; i++) {
PBGitRevSpecifier* aRev = [branches objectAtIndex:i];
if (rev == aRev) {
self.currentBranch = [NSIndexSet indexSetWithIndex:i];
[self showHistoryView];
return;
}
}
}

- (void) readCurrentBranch
{
[self selectBranch: [self addBranch: [self headRef]]];
Expand Down
6 changes: 3 additions & 3 deletions PBGitWindowController.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 8 additions & 2 deletions PBGitWindowController.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@ @implementation PBGitWindowController

@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;
}

Expand Down Expand Up @@ -76,7 +83,6 @@ - (void)awakeFromNib
// 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];

Expand Down
2 changes: 1 addition & 1 deletion PBRepositoryDocumentController.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@

}

- (id) openRepositoryAtLocation:(NSURL*) url RevSpecifier:(PBGitRevSpecifier*) rev;
- (id) documentForLocation:(NSURL*) url;
@end
13 changes: 5 additions & 8 deletions PBRepositoryDocumentController.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,17 @@ - (void)noteNewRecentDocumentURL:(NSURL*)url
[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

0 comments on commit 86606ef

Please sign in to comment.