Permalink
Browse files

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).
  • Loading branch information...
1 parent b61028a commit ff5f3f7979d470acdd55bb9869114802278d2314 @brotherbard brotherbard committed Feb 3, 2010
Showing with 95 additions and 47 deletions.
  1. +1 −1 PBGitRevisionCell.m
  2. +2 −1 PBRefContextDelegate.h
  3. +4 −1 PBRefController.h
  4. +18 −9 PBRefController.m
  5. +6 −6 PBRefMenuItem.h
  6. +63 −28 PBRefMenuItem.m
  7. +1 −1 PBWebHistoryController.m
View
@@ -291,7 +291,7 @@ - (NSMenu *) menuForEvent:(NSEvent *)event inRect:(NSRect)rect ofView:(NSView *)
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];
View
@@ -9,5 +9,6 @@
@protocol PBRefContextDelegate
-- (NSArray *) menuItemsForRef:(PBGitRef *)ref commit:(PBGitCommit *)commit;
+- (NSArray *) menuItemsForRef:(PBGitRef *)ref;
+- (NSArray *) menuItemsForCommit:(PBGitCommit *)commit;
@end
View
@@ -13,6 +13,8 @@
#import "PBGitCommit.h"
#import "PBRefContextDelegate.h"
+@class PBRefMenuItem;
+
@interface PBRefController : NSObject <PBRefContextDelegate> {
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;
View
@@ -39,28 +39,29 @@ - (void) removeRefSheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode conte
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);
}
- (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,22 +72,30 @@ - (void) checkoutRef:(PBRefMenuItem *)sender
- (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];
}
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
View
@@ -11,13 +11,13 @@
#import "PBGitCommit.h"
@interface PBRefMenuItem : NSMenuItem {
- PBGitRef *ref;
- PBGitCommit *commit;
+ id <PBGitRefish> refish;
}
-
-@property (retain) PBGitCommit *commit;
-@property (retain) PBGitRef *ref;
-+ (NSArray *)defaultMenuItemsForRef:(PBGitRef *)ref commit:(PBGitCommit *)commit target:(id)target;
+@property (retain) id <PBGitRefish> refish;
+
++ (PBRefMenuItem *) separatorItem;
++ (NSArray *) defaultMenuItemsForRef:(PBGitRef *)ref inRepository:(PBGitRepository *)repo target:(id)target;
++ (NSArray *) defaultMenuItemsForCommit:(PBGitCommit *)commit target:(id)target;
@end
View
@@ -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";
-
- [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: @""]];
-
- 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];
+ if (!isEnabled)
+ selector = nil;
+
+ PBRefMenuItem *item = [[PBRefMenuItem alloc] initWithTitle:title action:selector keyEquivalent:@""];
+ [item setEnabled:isEnabled];
+ return item;
+}
+
+
++ (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;
+ }
+
+ 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 array;
+ 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
View
@@ -115,7 +115,7 @@ - (NSArray *) webView:(WebView *)sender
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;

0 comments on commit ff5f3f7

Please sign in to comment.