Skip to content

Commit ff5f3f7

Browse files
committed
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).
1 parent b61028a commit ff5f3f7

7 files changed

+95
-47
lines changed

PBGitRevisionCell.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ - (NSMenu *) menuForEvent:(NSEvent *)event inRect:(NSRect)rect ofView:(NSView *)
291291
if (!ref)
292292
return [self menu];
293293

294-
NSArray *items = [contextMenuDelegate menuItemsForRef:ref commit:[self objectValue]];
294+
NSArray *items = [contextMenuDelegate menuItemsForRef:ref];
295295
NSMenu *menu = [[NSMenu alloc] init];
296296
for (NSMenuItem *item in items)
297297
[menu addItem:item];

PBRefContextDelegate.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99

1010

1111
@protocol PBRefContextDelegate
12-
- (NSArray *) menuItemsForRef:(PBGitRef *)ref commit:(PBGitCommit *)commit;
12+
- (NSArray *) menuItemsForRef:(PBGitRef *)ref;
13+
- (NSArray *) menuItemsForCommit:(PBGitCommit *)commit;
1314
@end

PBRefController.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#import "PBGitCommit.h"
1414
#import "PBRefContextDelegate.h"
1515

16+
@class PBRefMenuItem;
17+
1618
@interface PBRefController : NSObject <PBRefContextDelegate> {
1719
IBOutlet __weak PBGitHistoryController *historyController;
1820
IBOutlet NSArrayController *commitController;
@@ -29,7 +31,8 @@
2931
- (IBAction)closeSheet:(id) sender;
3032
- (IBAction)saveSheet:(id) sender;
3133

32-
- (NSArray *) menuItemsForRef:(PBGitRef *)ref commit:(PBGitCommit *)commit;
34+
- (NSArray *) menuItemsForRef:(PBGitRef *)ref;
35+
- (NSArray *) menuItemsForCommit:(PBGitCommit *)commit;
3336

3437
- (void) changeBranch:(NSMenuItem *)sender;
3538
- (void) selectCurrentBranch;

PBRefController.m

+18-9
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,29 @@ - (void) removeRefSheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode conte
3939
if (returnCode == NSAlertDefaultReturn) {
4040
int ret = 1;
4141
PBRefMenuItem *refMenuItem = contextInfo;
42-
[historyController.repository outputForArguments:[NSArray arrayWithObjects:@"update-ref", @"-d", [[refMenuItem ref] ref], nil] retValue: &ret];
42+
[historyController.repository outputForArguments:[NSArray arrayWithObjects:@"update-ref", @"-d", [[refMenuItem refish] refishName], nil] retValue: &ret];
4343
if (ret) {
4444
NSLog(@"Removing ref failed!");
4545
return;
4646
}
47-
[historyController.repository removeBranch:[[PBGitRevSpecifier alloc] initWithRef:[refMenuItem ref]]];
48-
[[refMenuItem commit] removeRef:[refMenuItem ref]];
47+
[historyController.repository removeBranch:[[PBGitRevSpecifier alloc] initWithRef:[refMenuItem refish]]];
48+
PBGitCommit *commitForRef = [historyController.repository commitForRef:[refMenuItem refish]];
49+
[commitForRef removeRef:[refMenuItem refish]];
4950
[commitController rearrangeObjects];
5051
}
5152
}
5253

5354
- (void) removeRef:(PBRefMenuItem *)sender
5455
{
55-
NSString *ref_desc = [NSString stringWithFormat:@"%@ %@", [[sender ref] type], [[sender ref] shortName]];
56+
NSString *ref_desc = [NSString stringWithFormat:@"%@ %@", [(PBGitRef *)[sender refish] type], [[sender refish] shortName]];
5657
NSString *question = [NSString stringWithFormat:@"Are you sure you want to remove the %@?", ref_desc];
5758
NSBeginAlertSheet([NSString stringWithFormat:@"Delete %@?", ref_desc], @"Delete", @"Cancel", nil, [[historyController view] window], self, @selector(removeRefSheetDidEnd:returnCode:contextInfo:), NULL, sender, question);
5859
}
5960

6061
- (void) checkoutRef:(PBRefMenuItem *)sender
6162
{
6263
int ret = 1;
63-
[historyController.repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"checkout", [[sender ref] shortName], nil] retValue: &ret];
64+
[historyController.repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"checkout", [[sender refish] shortName], nil] retValue: &ret];
6465
if (ret) {
6566
[[historyController.repository windowController] showMessageSheet:@"Checking out branch failed" infoText:@"There was an error checking out the branch. Perhaps your working directory is not clean?"];
6667
return;
@@ -71,22 +72,30 @@ - (void) checkoutRef:(PBRefMenuItem *)sender
7172

7273
- (void) tagInfo:(PBRefMenuItem *)sender
7374
{
74-
NSString *message = [NSString stringWithFormat:@"Info for tag: %@", [[sender ref] shortName]];
75+
NSString *message = [NSString stringWithFormat:@"Info for tag: %@", [[sender refish] shortName]];
7576

7677
int ret = 1;
77-
NSString *info = [historyController.repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"tag", @"-n50", @"-l", [[sender ref] shortName], nil] retValue: &ret];
78+
NSString *info = [historyController.repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"tag", @"-n50", @"-l", [[sender refish] shortName], nil] retValue: &ret];
7879

7980
if (!ret) {
8081
[[historyController.repository windowController] showMessageSheet:message infoText:info];
8182
}
8283
return;
8384
}
8485

85-
- (NSArray *) menuItemsForRef:(PBGitRef *)ref commit:(PBGitCommit *)commit
86+
#pragma mark Contextual menus
87+
88+
- (NSArray *) menuItemsForRef:(PBGitRef *)ref
8689
{
87-
return [PBRefMenuItem defaultMenuItemsForRef:ref commit:commit target:self];
90+
return [PBRefMenuItem defaultMenuItemsForRef:ref inRepository:historyController.repository target:self];
8891
}
8992

93+
- (NSArray *) menuItemsForCommit:(PBGitCommit *)commit
94+
{
95+
return [PBRefMenuItem defaultMenuItemsForCommit:commit target:self];
96+
}
97+
98+
9099
# pragma mark Tableview delegate methods
91100

92101
- (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard*)pboard

PBRefMenuItem.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
#import "PBGitCommit.h"
1212

1313
@interface PBRefMenuItem : NSMenuItem {
14-
PBGitRef *ref;
15-
PBGitCommit *commit;
14+
id <PBGitRefish> refish;
1615
}
17-
18-
@property (retain) PBGitCommit *commit;
19-
@property (retain) PBGitRef *ref;
2016

21-
+ (NSArray *)defaultMenuItemsForRef:(PBGitRef *)ref commit:(PBGitCommit *)commit target:(id)target;
17+
@property (retain) id <PBGitRefish> refish;
18+
19+
+ (PBRefMenuItem *) separatorItem;
20+
+ (NSArray *) defaultMenuItemsForRef:(PBGitRef *)ref inRepository:(PBGitRepository *)repo target:(id)target;
21+
+ (NSArray *) defaultMenuItemsForCommit:(PBGitCommit *)commit target:(id)target;
2222

2323
@end

PBRefMenuItem.m

+63-28
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,72 @@
1010

1111

1212
@implementation PBRefMenuItem
13-
@synthesize ref, commit;
13+
@synthesize refish;
1414

15-
+ (NSArray *)defaultMenuItemsForRef:(PBGitRef *)ref commit:(PBGitCommit *)commit target:(id)target
15+
+ (PBRefMenuItem *) itemWithTitle:(NSString *)title action:(SEL)selector enabled:(BOOL)isEnabled
1616
{
17-
NSMutableArray *array = [NSMutableArray array];
18-
NSString *type = [ref type];
19-
if ([type isEqualToString:@"remote"])
20-
type = @"remote branch";
21-
else if ([type isEqualToString:@"head"])
22-
type = @"branch";
23-
24-
[array addObject:[[PBRefMenuItem alloc] initWithTitle:[@"Delete " stringByAppendingString:type]
25-
action:@selector(removeRef:)
26-
keyEquivalent: @""]];
27-
if ([type isEqualToString:@"branch"])
28-
[array addObject:[[PBRefMenuItem alloc] initWithTitle:@"Checkout branch"
29-
action:@selector(checkoutRef:)
30-
keyEquivalent: @""]];
31-
32-
if ([type isEqualToString:@"tag"])
33-
[array addObject:[[PBRefMenuItem alloc] initWithTitle:@"View tag info"
34-
action:@selector(tagInfo:)
35-
keyEquivalent: @""]];
36-
37-
for (PBRefMenuItem *item in array)
38-
{
39-
[item setTarget: target];
40-
[item setRef: ref];
41-
[item setCommit:commit];
17+
if (!isEnabled)
18+
selector = nil;
19+
20+
PBRefMenuItem *item = [[PBRefMenuItem alloc] initWithTitle:title action:selector keyEquivalent:@""];
21+
[item setEnabled:isEnabled];
22+
return item;
23+
}
24+
25+
26+
+ (PBRefMenuItem *) separatorItem
27+
{
28+
PBRefMenuItem *item = (PBRefMenuItem *)[super separatorItem];
29+
return item;
30+
}
31+
32+
33+
+ (NSArray *) defaultMenuItemsForRef:(PBGitRef *)ref inRepository:(PBGitRepository *)repo target:(id)target
34+
{
35+
if (!ref || !repo || !target) {
36+
return nil;
37+
}
38+
39+
NSMutableArray *items = [NSMutableArray array];
40+
41+
NSString *targetRefName = [ref shortName];
42+
43+
PBGitRef *headRef = [[repo headRef] ref];
44+
BOOL isHead = [ref isEqualToRef:headRef];
45+
46+
// checkout ref
47+
NSString *checkoutTitle = [@"Checkout " stringByAppendingString:targetRefName];
48+
[items addObject:[PBRefMenuItem itemWithTitle:checkoutTitle action:@selector(checkoutRef:) enabled:!isHead]];
49+
50+
// view tag info
51+
if ([ref isTag])
52+
[items addObject:[PBRefMenuItem itemWithTitle:@"View tag info" action:@selector(tagInfo:) enabled:YES]];
53+
54+
// delete ref
55+
[items addObject:[PBRefMenuItem separatorItem]];
56+
NSString *deleteTitle = [NSString stringWithFormat:@"Delete %@", targetRefName];
57+
[items addObject:[PBRefMenuItem itemWithTitle:deleteTitle action:@selector(removeRef:) enabled:YES]];
58+
59+
for (PBRefMenuItem *item in items) {
60+
[item setTarget:target];
61+
[item setRefish:ref];
4262
}
4363

44-
return array;
64+
return items;
4565
}
66+
67+
68+
+ (NSArray *) defaultMenuItemsForCommit:(PBGitCommit *)commit target:(id)target
69+
{
70+
NSMutableArray *items = [NSMutableArray array];
71+
72+
for (PBRefMenuItem *item in items) {
73+
[item setTarget:target];
74+
[item setRefish:commit];
75+
}
76+
77+
return items;
78+
}
79+
80+
4681
@end

PBWebHistoryController.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ - (NSArray *) webView:(WebView *)sender
115115
for (PBGitRef *ref in historyController.webCommit.refs)
116116
{
117117
if ([[ref shortName] isEqualToString:selectedRefString])
118-
return [contextMenuDelegate menuItemsForRef:ref commit:historyController.webCommit];
118+
return [contextMenuDelegate menuItemsForRef:ref];
119119
}
120120
NSLog(@"Could not find selected ref!");
121121
return defaultMenuItems;

0 commit comments

Comments
 (0)