From ff5f3f7979d470acdd55bb9869114802278d2314 Mon Sep 17 00:00:00 2001 From: Nathan Kinsinger Date: Tue, 2 Feb 2010 19:29:38 -0700 Subject: [PATCH] Add methods dealing with menus needed for future features. Switch from storing both the ref and the commit to storing just one or the other as a refish. Change action methods that receive PBRefMenuItem to support the change in functionality. In particular the removeRefSheetDidEnd:returnCode:contextInfo: method in PBRefController now looks up the commit for the ref (this was the only place that needed both the ref and the commit). --- PBGitRevisionCell.m | 2 +- PBRefContextDelegate.h | 3 +- PBRefController.h | 5 ++- PBRefController.m | 27 ++++++++----- PBRefMenuItem.h | 12 +++--- PBRefMenuItem.m | 87 ++++++++++++++++++++++++++++------------ PBWebHistoryController.m | 2 +- 7 files changed, 93 insertions(+), 45 deletions(-) diff --git a/PBGitRevisionCell.m b/PBGitRevisionCell.m index 579168b..a79667a 100644 --- a/PBGitRevisionCell.m +++ b/PBGitRevisionCell.m @@ -291,7 +291,7 @@ if (!ref) return [self menu]; - NSArray *items = [contextMenuDelegate menuItemsForRef:ref commit:[self objectValue]]; + NSArray *items = [contextMenuDelegate menuItemsForRef:ref]; NSMenu *menu = [[NSMenu alloc] init]; for (NSMenuItem *item in items) [menu addItem:item]; diff --git a/PBRefContextDelegate.h b/PBRefContextDelegate.h index 81019ce..1b7a973 100644 --- a/PBRefContextDelegate.h +++ b/PBRefContextDelegate.h @@ -9,5 +9,6 @@ @protocol PBRefContextDelegate -- (NSArray *) menuItemsForRef:(PBGitRef *)ref commit:(PBGitCommit *)commit; +- (NSArray *) menuItemsForRef:(PBGitRef *)ref; +- (NSArray *) menuItemsForCommit:(PBGitCommit *)commit; @end diff --git a/PBRefController.h b/PBRefController.h index 0706fc5..163c996 100644 --- a/PBRefController.h +++ b/PBRefController.h @@ -13,6 +13,8 @@ #import "PBGitCommit.h" #import "PBRefContextDelegate.h" +@class PBRefMenuItem; + @interface PBRefController : NSObject { IBOutlet __weak PBGitHistoryController *historyController; IBOutlet NSArrayController *commitController; @@ -29,7 +31,8 @@ - (IBAction)closeSheet:(id) sender; - (IBAction)saveSheet:(id) sender; -- (NSArray *) menuItemsForRef:(PBGitRef *)ref commit:(PBGitCommit *)commit; +- (NSArray *) menuItemsForRef:(PBGitRef *)ref; +- (NSArray *) menuItemsForCommit:(PBGitCommit *)commit; - (void) changeBranch:(NSMenuItem *)sender; - (void) selectCurrentBranch; diff --git a/PBRefController.m b/PBRefController.m index f89f434..63483be 100644 --- a/PBRefController.m +++ b/PBRefController.m @@ -39,20 +39,21 @@ if (returnCode == NSAlertDefaultReturn) { int ret = 1; PBRefMenuItem *refMenuItem = contextInfo; - [historyController.repository outputForArguments:[NSArray arrayWithObjects:@"update-ref", @"-d", [[refMenuItem ref] ref], nil] retValue: &ret]; + [historyController.repository outputForArguments:[NSArray arrayWithObjects:@"update-ref", @"-d", [[refMenuItem refish] refishName], nil] retValue: &ret]; if (ret) { NSLog(@"Removing ref failed!"); return; } - [historyController.repository removeBranch:[[PBGitRevSpecifier alloc] initWithRef:[refMenuItem ref]]]; - [[refMenuItem commit] removeRef:[refMenuItem ref]]; + [historyController.repository removeBranch:[[PBGitRevSpecifier alloc] initWithRef:[refMenuItem refish]]]; + PBGitCommit *commitForRef = [historyController.repository commitForRef:[refMenuItem refish]]; + [commitForRef removeRef:[refMenuItem refish]]; [commitController rearrangeObjects]; } } - (void) removeRef:(PBRefMenuItem *)sender { - NSString *ref_desc = [NSString stringWithFormat:@"%@ %@", [[sender ref] type], [[sender ref] shortName]]; + NSString *ref_desc = [NSString stringWithFormat:@"%@ %@", [(PBGitRef *)[sender refish] type], [[sender refish] shortName]]; NSString *question = [NSString stringWithFormat:@"Are you sure you want to remove the %@?", ref_desc]; NSBeginAlertSheet([NSString stringWithFormat:@"Delete %@?", ref_desc], @"Delete", @"Cancel", nil, [[historyController view] window], self, @selector(removeRefSheetDidEnd:returnCode:contextInfo:), NULL, sender, question); } @@ -60,7 +61,7 @@ - (void) checkoutRef:(PBRefMenuItem *)sender { int ret = 1; - [historyController.repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"checkout", [[sender ref] shortName], nil] retValue: &ret]; + [historyController.repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"checkout", [[sender refish] shortName], nil] retValue: &ret]; if (ret) { [[historyController.repository windowController] showMessageSheet:@"Checking out branch failed" infoText:@"There was an error checking out the branch. Perhaps your working directory is not clean?"]; return; @@ -71,10 +72,10 @@ - (void) tagInfo:(PBRefMenuItem *)sender { - NSString *message = [NSString stringWithFormat:@"Info for tag: %@", [[sender ref] shortName]]; + NSString *message = [NSString stringWithFormat:@"Info for tag: %@", [[sender refish] shortName]]; int ret = 1; - NSString *info = [historyController.repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"tag", @"-n50", @"-l", [[sender ref] shortName], nil] retValue: &ret]; + NSString *info = [historyController.repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"tag", @"-n50", @"-l", [[sender refish] shortName], nil] retValue: &ret]; if (!ret) { [[historyController.repository windowController] showMessageSheet:message infoText:info]; @@ -82,11 +83,19 @@ return; } -- (NSArray *) menuItemsForRef:(PBGitRef *)ref commit:(PBGitCommit *)commit +#pragma mark Contextual menus + +- (NSArray *) menuItemsForRef:(PBGitRef *)ref { - return [PBRefMenuItem defaultMenuItemsForRef:ref commit:commit target:self]; + return [PBRefMenuItem defaultMenuItemsForRef:ref inRepository:historyController.repository target:self]; } +- (NSArray *) menuItemsForCommit:(PBGitCommit *)commit +{ + return [PBRefMenuItem defaultMenuItemsForCommit:commit target:self]; +} + + # pragma mark Tableview delegate methods - (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard*)pboard diff --git a/PBRefMenuItem.h b/PBRefMenuItem.h index 0e3f455..9ad334d 100644 --- a/PBRefMenuItem.h +++ b/PBRefMenuItem.h @@ -11,13 +11,13 @@ #import "PBGitCommit.h" @interface PBRefMenuItem : NSMenuItem { - PBGitRef *ref; - PBGitCommit *commit; + id refish; } - -@property (retain) PBGitCommit *commit; -@property (retain) PBGitRef *ref; -+ (NSArray *)defaultMenuItemsForRef:(PBGitRef *)ref commit:(PBGitCommit *)commit target:(id)target; +@property (retain) id refish; + ++ (PBRefMenuItem *) separatorItem; ++ (NSArray *) defaultMenuItemsForRef:(PBGitRef *)ref inRepository:(PBGitRepository *)repo target:(id)target; ++ (NSArray *) defaultMenuItemsForCommit:(PBGitCommit *)commit target:(id)target; @end diff --git a/PBRefMenuItem.m b/PBRefMenuItem.m index 22aaae5..6740b52 100644 --- a/PBRefMenuItem.m +++ b/PBRefMenuItem.m @@ -10,37 +10,72 @@ @implementation PBRefMenuItem -@synthesize ref, commit; +@synthesize refish; -+ (NSArray *)defaultMenuItemsForRef:(PBGitRef *)ref commit:(PBGitCommit *)commit target:(id)target ++ (PBRefMenuItem *) itemWithTitle:(NSString *)title action:(SEL)selector enabled:(BOOL)isEnabled { - NSMutableArray *array = [NSMutableArray array]; - NSString *type = [ref type]; - if ([type isEqualToString:@"remote"]) - type = @"remote branch"; - else if ([type isEqualToString:@"head"]) - type = @"branch"; + if (!isEnabled) + selector = nil; - [array addObject:[[PBRefMenuItem alloc] initWithTitle:[@"Delete " stringByAppendingString:type] - action:@selector(removeRef:) - keyEquivalent: @""]]; - if ([type isEqualToString:@"branch"]) - [array addObject:[[PBRefMenuItem alloc] initWithTitle:@"Checkout branch" - action:@selector(checkoutRef:) - keyEquivalent: @""]]; + PBRefMenuItem *item = [[PBRefMenuItem alloc] initWithTitle:title action:selector keyEquivalent:@""]; + [item setEnabled:isEnabled]; + return item; +} - if ([type isEqualToString:@"tag"]) - [array addObject:[[PBRefMenuItem alloc] initWithTitle:@"View tag info" - action:@selector(tagInfo:) - keyEquivalent: @""]]; - - for (PBRefMenuItem *item in array) - { - [item setTarget: target]; - [item setRef: ref]; - [item setCommit:commit]; + ++ (PBRefMenuItem *) separatorItem +{ + PBRefMenuItem *item = (PBRefMenuItem *)[super separatorItem]; + return item; +} + + ++ (NSArray *) defaultMenuItemsForRef:(PBGitRef *)ref inRepository:(PBGitRepository *)repo target:(id)target +{ + if (!ref || !repo || !target) { + return nil; } - return array; + NSMutableArray *items = [NSMutableArray array]; + + NSString *targetRefName = [ref shortName]; + + PBGitRef *headRef = [[repo headRef] ref]; + BOOL isHead = [ref isEqualToRef:headRef]; + + // checkout ref + NSString *checkoutTitle = [@"Checkout " stringByAppendingString:targetRefName]; + [items addObject:[PBRefMenuItem itemWithTitle:checkoutTitle action:@selector(checkoutRef:) enabled:!isHead]]; + + // view tag info + if ([ref isTag]) + [items addObject:[PBRefMenuItem itemWithTitle:@"View tag info" action:@selector(tagInfo:) enabled:YES]]; + + // delete ref + [items addObject:[PBRefMenuItem separatorItem]]; + NSString *deleteTitle = [NSString stringWithFormat:@"Delete %@…", targetRefName]; + [items addObject:[PBRefMenuItem itemWithTitle:deleteTitle action:@selector(removeRef:) enabled:YES]]; + + for (PBRefMenuItem *item in items) { + [item setTarget:target]; + [item setRefish:ref]; + } + + return items; } + + ++ (NSArray *) defaultMenuItemsForCommit:(PBGitCommit *)commit target:(id)target +{ + NSMutableArray *items = [NSMutableArray array]; + + for (PBRefMenuItem *item in items) { + [item setTarget:target]; + [item setRefish:commit]; + } + + return items; +} + + @end diff --git a/PBWebHistoryController.m b/PBWebHistoryController.m index 1934b73..13d1cfe 100644 --- a/PBWebHistoryController.m +++ b/PBWebHistoryController.m @@ -115,7 +115,7 @@ contextMenuItemsForElement:(NSDictionary *)element for (PBGitRef *ref in historyController.webCommit.refs) { if ([[ref shortName] isEqualToString:selectedRefString]) - return [contextMenuDelegate menuItemsForRef:ref commit:historyController.webCommit]; + return [contextMenuDelegate menuItemsForRef:ref]; } NSLog(@"Could not find selected ref!"); return defaultMenuItems;