From 0bd684791d991195a2ee3d9239151de0d5f0b88b Mon Sep 17 00:00:00 2001 From: German Laullon Date: Tue, 4 Jan 2011 23:59:32 -0800 Subject: [PATCH] Branch commit ahead/behind values --- PBGitRepository.h | 3 +++ PBGitRepository.m | 21 +++++++++++++++++++++ PBGitRevSpecifier.h | 4 ++++ PBGitRevSpecifier.m | 1 + PBGitSidebarController.m | 5 +++-- PBSourceViewBadge.h | 1 + PBSourceViewCell.h | 3 +++ PBSourceViewCell.m | 10 +++++++--- 8 files changed, 43 insertions(+), 5 deletions(-) diff --git a/PBGitRepository.h b/PBGitRepository.h index 7599c6c..a16acc0 100644 --- a/PBGitRepository.h +++ b/PBGitRepository.h @@ -128,6 +128,9 @@ static NSString * PBStringFromBranchFilterType(PBGitXBranchFilterType type) { // for the scripting bridge - (void)findInModeScriptCommand:(NSScriptCommand *)command; +-(NSNumber *)countCommintsOf:(NSString *)branchs; ++(bool)isLocalBranch:(NSString *)name; + @property (assign) BOOL hasChanged; @property (readonly) PBGitWindowController *windowController; diff --git a/PBGitRepository.m b/PBGitRepository.m index 3d4489b..edec8ec 100644 --- a/PBGitRepository.m +++ b/PBGitRepository.m @@ -269,6 +269,10 @@ NSString* PBGitRepositoryErrorDomain = @"GitXErrorDomain"; PBGitRef *newRef = [PBGitRef refFromString:[components objectAtIndex:0]]; PBGitRevSpecifier *revSpec = [[PBGitRevSpecifier alloc] initWithRef:newRef]; + if([PBGitRepository isLocalBranch:[components objectAtIndex:0]]){ + [revSpec setAhead:[self countCommintsOf:[NSString stringWithFormat:@"origin..%@",[components objectAtIndex:0]]]]; + [revSpec setBehind:[self countCommintsOf:[NSString stringWithFormat:@"%@..origin",[components objectAtIndex:0]]]]; + } [self addBranch:revSpec]; [self addRef:newRef fromParameters:components]; [oldBranches removeObject:revSpec]; @@ -284,6 +288,23 @@ NSString* PBGitRepositoryErrorDomain = @"GitXErrorDomain"; [[[self windowController] window] setTitle:[self displayName]]; } ++(bool)isLocalBranch:(NSString *)name +{ + NSScanner *scanner=[NSScanner scannerWithString:name]; + return [scanner scanString:@"refs/heads/" intoString:NULL]; +} + +-(NSNumber *)countCommintsOf:(NSString *)branchs +{ + NSArray *args = [NSArray arrayWithObjects:@"rev-list", branchs, nil]; + NSString *o = [self outputForArguments:args]; + if ([o length]==0) { + return NULL; + } + NSArray *commits = [o componentsSeparatedByString:@"\n"]; + return [NSNumber numberWithInt:[commits count]]; +} + - (void) lazyReload { if (!hasChanged) diff --git a/PBGitRevSpecifier.h b/PBGitRevSpecifier.h index a8d97f2..32e96c4 100644 --- a/PBGitRevSpecifier.h +++ b/PBGitRevSpecifier.h @@ -14,6 +14,8 @@ NSArray *parameters; NSURL *workingDirectory; BOOL isSimpleRef; + NSNumber *behind; + NSNumber *ahead; } - (id) initWithParameters:(NSArray *)params description:(NSString *)descrip; @@ -37,5 +39,7 @@ @property(readonly) NSArray *parameters; @property(retain) NSURL *workingDirectory; @property(readonly) BOOL isSimpleRef; +@property(assign) NSNumber *behind; +@property(assign) NSNumber *ahead; @end diff --git a/PBGitRevSpecifier.m b/PBGitRevSpecifier.m index 9e2b785..1adc9ed 100644 --- a/PBGitRevSpecifier.m +++ b/PBGitRevSpecifier.m @@ -13,6 +13,7 @@ @synthesize parameters, description, workingDirectory; @synthesize isSimpleRef; +@synthesize behind,ahead; // internal designated init diff --git a/PBGitSidebarController.m b/PBGitSidebarController.m index 0c01104..301bf16 100644 --- a/PBGitSidebarController.m +++ b/PBGitSidebarController.m @@ -223,12 +223,13 @@ - (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(PBSourceViewCell *)cell forTableColumn:(NSTableColumn *)tableColumn item:(PBSourceViewItem *)item { - NSLog(@"%@ -- %d",item.revSpecifier,(item.revSpecifier!=NULL)); if(item.revSpecifier!=NULL){ cell.isCheckedOut = [item.revSpecifier isEqual:[repository headRef]]; - cell.behind=[NSNumber numberWithInt:10]; + cell.behind=[item.revSpecifier behind]; + cell.ahead=[item.revSpecifier ahead]; }else{ cell.behind=nil; + cell.ahead=nil; } [cell setImage:[item icon]]; } diff --git a/PBSourceViewBadge.h b/PBSourceViewBadge.h index 1754000..3058458 100644 --- a/PBSourceViewBadge.h +++ b/PBSourceViewBadge.h @@ -13,6 +13,7 @@ } ++ (NSImage *) badge:(NSString *)badge forCell:(NSTextFieldCell *)cell; + (NSImage *) checkedOutBadgeForCell:(NSTextFieldCell *)cell; + (NSImage *) numericBadge:(NSInteger)number forCell:(NSTextFieldCell *)cell; diff --git a/PBSourceViewCell.h b/PBSourceViewCell.h index e2e5074..fb80ece 100644 --- a/PBSourceViewCell.h +++ b/PBSourceViewCell.h @@ -13,9 +13,12 @@ @interface PBSourceViewCell : PBIconAndTextCell { BOOL isCheckedOut; NSNumber *behind; + NSNumber *ahead; } @property (assign) BOOL isCheckedOut; @property (assign) NSNumber *behind; +@property (assign) NSNumber *ahead; + @end diff --git a/PBSourceViewCell.m b/PBSourceViewCell.m index 55dbeca..f207e04 100644 --- a/PBSourceViewCell.m +++ b/PBSourceViewCell.m @@ -16,7 +16,7 @@ @implementation PBSourceViewCell @synthesize isCheckedOut; -@synthesize behind; +@synthesize behind,ahead; # pragma mark context menu delegate methods @@ -35,8 +35,12 @@ - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)outlineView { - if(behind){ - NSImage *checkedOutImage = [PBSourceViewBadge badge:[NSString stringWithFormat:@"%@-%@",(isCheckedOut?@"✔ ":@""),behind] forCell:self]; + if(behind || ahead || isCheckedOut){ + NSMutableString *badge=[NSMutableString string]; + if(isCheckedOut) [badge appendString:@"✔ "]; + if(ahead) [badge appendFormat:@"+%@",ahead]; + if(behind) [badge appendFormat:@"-%@",behind]; + NSImage *checkedOutImage = [PBSourceViewBadge badge:badge forCell:self]; NSSize imageSize = [checkedOutImage size]; NSRect imageFrame; NSDivideRect(cellFrame, &imageFrame, &cellFrame, imageSize.width + 3, NSMaxXEdge);