Skip to content

Commit 5c9d716

Browse files
committed
badge system recoded
1 parent 0151598 commit 5c9d716

10 files changed

+64
-47
lines changed

PBGitRepository.m

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,6 @@ - (void) reloadRefs
269269
PBGitRef *newRef = [PBGitRef refFromString:[components objectAtIndex:0]];
270270
PBGitRevSpecifier *revSpec = [[PBGitRevSpecifier alloc] initWithRef:newRef];
271271

272-
NSString *bName;
273-
if([PBGitRepository isLocalBranch:[components objectAtIndex:0] branchNameInto:&bName]){
274-
[revSpec setAhead:[self countCommintsOf:[NSString stringWithFormat:@"origin/%@..%@",bName,bName]]];
275-
[revSpec setBehind:[self countCommintsOf:[NSString stringWithFormat:@"%@..origin/%@",bName,bName]]];
276-
}
277272
[self addBranch:revSpec];
278273
[self addRef:newRef fromParameters:components];
279274
[oldBranches removeObject:revSpec];
@@ -299,19 +294,6 @@ +(bool)isLocalBranch:(NSString *)branch branchNameInto:(NSString **)name
299294
return is;
300295
}
301296

302-
-(NSNumber *)countCommintsOf:(NSString *)branchs
303-
{
304-
NSLog(@"branchs:'%@'",branchs);
305-
NSArray *args = [NSArray arrayWithObjects:@"rev-list", branchs, nil];
306-
int ret;
307-
NSString *o = [self outputForArguments:args retValue:&ret];
308-
if ((ret!=0) || ([o length]==0)) {
309-
return NULL;
310-
}
311-
NSArray *commits = [o componentsSeparatedByString:@"\n"];
312-
return [NSNumber numberWithInt:[commits count]];
313-
}
314-
315297
- (void) lazyReload
316298
{
317299
if (!hasChanged)

PBGitSVBranchItem.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,15 @@
1111

1212

1313
@interface PBGitSVBranchItem : PBSourceViewItem {
14-
14+
BOOL isCheckedOut;
15+
NSNumber *behind;
16+
NSNumber *ahead;
1517
}
1618

1719
+ (id)branchItemWithRevSpec:(PBGitRevSpecifier *)revSpecifier;
1820

21+
@property (assign) BOOL isCheckedOut;
22+
@property (assign) NSNumber *behind;
23+
@property (assign) NSNumber *ahead;
24+
1925
@end

PBGitSVBranchItem.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
@implementation PBGitSVBranchItem
1313

14+
@synthesize isCheckedOut;
15+
@synthesize behind,ahead;
1416

1517
+ (id)branchItemWithRevSpec:(PBGitRevSpecifier *)revSpecifier
1618
{
@@ -30,4 +32,17 @@ - (NSImage *) icon
3032
return branchImage;
3133
}
3234

35+
36+
- (NSString *) badge{
37+
NSMutableString *badge=nil;
38+
if(isCheckedOut || ahead || behind){
39+
badge=[NSMutableString string];
40+
if(isCheckedOut) [badge appendString:@""];
41+
if(ahead) [badge appendFormat:@"+%@",ahead];
42+
if(behind) [badge appendFormat:@"-%@",behind];
43+
}
44+
return badge;
45+
}
46+
47+
3348
@end

PBGitSVRemoteItem.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,8 @@ - (PBGitRef *) ref
3939
return [PBGitRef refFromString:[kGitXRemoteRefPrefix stringByAppendingString:self.title]];
4040
}
4141

42+
- (NSString *)badge
43+
{
44+
return (alert ? @"!" : nil);
45+
}
4246
@end

PBGitSidebarController.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
- (void)setHistorySearch:(NSString *)searchString mode:(NSInteger)mode;
4242

43+
-(NSNumber *)countCommintsOf:(NSString *)range;
4344
-(bool)remoteNeedFetch:(NSString *)remote;
4445

4546
@property(readonly) NSMutableArray *items;

PBGitSidebarController.m

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,34 @@ - (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(
101101
NSLog(@"remote.title=%@",[remote title]);
102102
[remote setAlert:[self remoteNeedFetch:[remote title]]];
103103
}
104+
105+
for(PBGitSVBranchItem* branch in [branches children]){
106+
NSString *bName=[branch title];
107+
[branch setAhead:[self countCommintsOf:[NSString stringWithFormat:@"origin/%@..%@",bName,bName]]];
108+
[branch setBehind:[self countCommintsOf:[NSString stringWithFormat:@"%@..origin/%@",bName,bName]]];
109+
[branch setIsCheckedOut:[branch.revSpecifier isEqual:[repository headRef]]];
110+
}
111+
104112
}else{
105113
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
106114
}
107115
}
108116

117+
#pragma mark Badges Methods
118+
119+
-(NSNumber *)countCommintsOf:(NSString *)range
120+
{
121+
NSArray *args = [NSArray arrayWithObjects:@"rev-list", range, nil];
122+
int ret;
123+
NSString *o = [repository outputForArguments:args retValue:&ret];
124+
if ((ret!=0) || ([o length]==0)) {
125+
return NULL;
126+
}
127+
NSArray *commits = [o componentsSeparatedByString:@"\n"];
128+
return [NSNumber numberWithInt:[commits count]];
129+
}
130+
131+
109132
-(bool)remoteNeedFetch:(NSString *)remote
110133
{
111134
int ret;
@@ -114,6 +137,8 @@ -(bool)remoteNeedFetch:(NSString *)remote
114137
return ((ret==0) && ([o length]!=0));
115138
}
116139

140+
#pragma mark -----
141+
117142
- (PBSourceViewItem *) selectedItem
118143
{
119144
NSInteger index = [sourceView selectedRow];
@@ -234,19 +259,7 @@ - (BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item
234259

235260
- (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(PBSourceViewCell *)cell forTableColumn:(NSTableColumn *)tableColumn item:(PBSourceViewItem *)item
236261
{
237-
cell.isCheckedOut = [item.revSpecifier isEqual:[repository headRef]];
238-
if(item.revSpecifier!=NULL){
239-
cell.behind=[item.revSpecifier behind];
240-
cell.ahead=[item.revSpecifier ahead];
241-
}else{
242-
cell.behind=nil;
243-
cell.ahead=nil;
244-
}
245-
246-
if([item isKindOfClass:[PBGitSVRemoteItem class]]){
247-
NSLog(@"title: %@",[item title]);
248-
cell.isCheckedOut=[item alert];
249-
}
262+
[cell setBadge:[item badge]];
250263
[cell setImage:[item icon]];
251264
}
252265

PBSourceViewCell.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,10 @@
99
#import <Cocoa/Cocoa.h>
1010
#import "PBIconAndTextCell.h"
1111

12-
1312
@interface PBSourceViewCell : PBIconAndTextCell {
14-
BOOL isCheckedOut;
15-
NSNumber *behind;
16-
NSNumber *ahead;
13+
NSString *badge;
1714
}
1815

19-
@property (assign) BOOL isCheckedOut;
20-
@property (assign) NSNumber *behind;
21-
@property (assign) NSNumber *ahead;
22-
16+
@property (assign) NSString *badge;
2317

2418
@end

PBSourceViewCell.m

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515

1616
@implementation PBSourceViewCell
1717

18-
@synthesize isCheckedOut;
19-
@synthesize behind,ahead;
18+
@synthesize badge;
2019

2120
# pragma mark context menu delegate methods
2221

@@ -35,11 +34,7 @@ - (NSMenu *) menuForEvent:(NSEvent *)event inRect:(NSRect)rect ofView:(NSOutline
3534

3635
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)outlineView
3736
{
38-
if(behind || ahead || isCheckedOut){
39-
NSMutableString *badge=[NSMutableString string];
40-
if(isCheckedOut) [badge appendString:@""];
41-
if(ahead) [badge appendFormat:@"+%@",ahead];
42-
if(behind) [badge appendFormat:@"-%@",behind];
37+
if(badge){
4338
NSImage *checkedOutImage = [PBSourceViewBadge badge:badge forCell:self];
4439
NSSize imageSize = [checkedOutImage size];
4540
NSRect imageFrame;

PBSourceViewItem.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
+ (id)itemWithRevSpec:(PBGitRevSpecifier *)revSpecifier;
2727
+ (id)itemWithTitle:(NSString *)title;
2828

29+
- (NSString *)badge;
30+
2931
- (void)addChild:(PBSourceViewItem *)child;
3032
- (void)removeChild:(PBSourceViewItem *)child;
3133

PBSourceViewItem.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ - (PBSourceViewItem *)findRev:(PBGitRevSpecifier *)rev
109109
return nil;
110110
}
111111

112+
- (NSString *)badge
113+
{
114+
return nil;
115+
}
116+
112117
- (NSImage *) icon
113118
{
114119
return nil;

0 commit comments

Comments
 (0)