From 9bfccb5ea5d91411c4244ac75f689532eb936dbc Mon Sep 17 00:00:00 2001 From: Pieter de Bie Date: Thu, 28 Aug 2008 17:29:34 +0200 Subject: [PATCH] Grapher: add first part of displaying refs This adds the "hasRef" boolean member in PBGitCellInfo which is set to true if the specific commit has symbolic refs. This is the first part in supporting labels just like gitk has. For now, commits with refs are just displayed with a red circle. Things that need to be done to support all refs: * Make the NSDictionary in PBGitRepository contain arrays of refs, not a single string * Make PBGitGrapher store all refs of a commit in the PBGitCellInfo * Figure out a nice way to display the labels in PBGitRevisionCell --- PBGitGrapher.h | 2 ++ PBGitGrapher.m | 14 +++++++++++++- PBGitRepository.h | 5 +++-- PBGitRepository.m | 20 +++++++++++--------- PBGitRevList.h | 2 +- PBGitRevList.m | 2 +- PBGitRevisionCell.m | 6 +++++- PBGraphCellInfo.h | 2 ++ PBGraphCellInfo.m | 2 +- 9 files changed, 39 insertions(+), 16 deletions(-) diff --git a/PBGitGrapher.h b/PBGitGrapher.h index 26741a4..2b46430 100644 --- a/PBGitGrapher.h +++ b/PBGitGrapher.h @@ -21,8 +21,10 @@ struct PBGitGraphColumn { @interface PBGitGrapher : NSObject { NSMutableArray* cellsInfo; + PBGitRepository* repository; } +- (id) initWithRepository: (PBGitRepository*) repo; - (void) parseCommits: (NSArray *) array; - (PBGraphCellInfo*) cellInfoForRow: (int) row; @end diff --git a/PBGitGrapher.m b/PBGitGrapher.m index a264f3b..a66369d 100644 --- a/PBGitGrapher.m +++ b/PBGitGrapher.m @@ -12,12 +12,21 @@ @implementation PBGitGrapher +- (id) initWithRepository: (PBGitRepository*) repo +{ + repository = repo; + return self; +} - (void) parseCommits: (NSArray *) commits { cellsInfo = [NSMutableArray arrayWithCapacity: [commits count]]; int row = 0; + NSDictionary* refs = nil; + if (repository) + refs = repository.refs; + PBGraphCellInfo* previous; NSMutableArray* previousLanes = [NSMutableArray array]; @@ -120,7 +129,10 @@ ++row; previous = [[PBGraphCellInfo alloc] initWithPosition:newPos andLines:lines]; - + + if (refs && [refs objectForKey:commit.sha]) + previous.hasRef = TRUE; + // If a parent was added, we have room to not indent. if (addedParent) previous.numColumns = [currentLanes count] - 1; diff --git a/PBGitRepository.h b/PBGitRepository.h index 22af9a5..98fe88e 100644 --- a/PBGitRepository.h +++ b/PBGitRepository.h @@ -15,6 +15,7 @@ extern NSString* PBGitRepositoryErrorDomain; PBGitRevList* revisionList; NSArray* branches; NSString* currentBranch; + NSDictionary* refs; } - (NSFileHandle*) handleForCommand:(NSString*) cmd; @@ -22,7 +23,7 @@ extern NSString* PBGitRepositoryErrorDomain; - (NSString*) outputForCommand:(NSString*) cmd; - (NSString*) outputForArguments:(NSArray*) args; -- (void) readBranches; +- (void) readRefs; - (void) readCurrentBranch; - (NSString*) parseSymbolicReference:(NSString*) ref; @@ -34,5 +35,5 @@ extern NSString* PBGitRepositoryErrorDomain; @property (readonly) PBGitRevList* revisionList; @property (assign) NSArray* branches; @property (assign) NSString* currentBranch; - +@property (assign) NSDictionary* refs; @end diff --git a/PBGitRepository.m b/PBGitRepository.m index fc7b66f..09327b2 100644 --- a/PBGitRepository.m +++ b/PBGitRepository.m @@ -17,7 +17,7 @@ NSString* PBGitRepositoryErrorDomain = @"GitXErrorDomain"; @implementation PBGitRepository -@synthesize revisionList, branches, currentBranch; +@synthesize revisionList, branches, currentBranch, refs; static NSString* gitPath; + (void) initialize @@ -112,7 +112,7 @@ static NSString* gitPath; } if (success) { - [self readBranches]; + [self readRefs]; [self readCurrentBranch]; revisionList = [[PBGitRevList alloc] initWithRepository:self andRevListParameters:[NSArray array]]; } @@ -139,21 +139,23 @@ static NSString* gitPath; [controller release]; } -- (void) readBranches +- (void) readRefs { - NSString* output = [PBEasyPipe outputForCommand:gitPath withArgs:[NSArray arrayWithObjects:@"for-each-ref", @"refs/heads", nil] inDir: self.fileURL.path]; + NSString* output = [PBEasyPipe outputForCommand:gitPath withArgs:[NSArray arrayWithObjects:@"for-each-ref", @"refs", nil] inDir: self.fileURL.path]; NSArray* lines = [output componentsSeparatedByString:@"\n"]; + NSMutableDictionary* newRefs = [NSMutableDictionary dictionary]; NSMutableArray* newBranches = [NSMutableArray array]; for (NSString* line in lines) { NSString* substr = [line substringWithRange: NSMakeRange(40,19)]; - if (![substr isEqualToString:@" commit\trefs/heads/"]) { - NSLog(@"Cannot parse branch %@. (%@)", line, substr); - continue; + if ([substr isEqualToString:@" commit\trefs/heads/"]) { + NSString* branch = [line substringFromIndex:59]; + [newBranches addObject: branch]; } - NSString* branch = [line substringFromIndex:59]; - [newBranches addObject: branch]; + NSArray* components = [line componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" \t"]]; + [newRefs setObject:[components objectAtIndex:2] forKey:[components objectAtIndex:0]]; } self.branches = newBranches; + self.refs = newRefs; } - (void) readCurrentBranch diff --git a/PBGitRevList.h b/PBGitRevList.h index 1417cac..60e96bf 100644 --- a/PBGitRevList.h +++ b/PBGitRevList.h @@ -18,7 +18,7 @@ } - initWithRepository:(id)repo andRevListParameters:(NSArray*) params; -- readCommits; +- (void) readCommits; @property(retain) NSArray* commits; @property(retain) id grapher; diff --git a/PBGitRevList.m b/PBGitRevList.m index a627326..631aa30 100644 --- a/PBGitRevList.m +++ b/PBGitRevList.m @@ -103,7 +103,7 @@ [self performSelectorOnMainThread:@selector(setCommits:) withObject:newArray waitUntilDone:YES]; - PBGitGrapher* g = [[PBGitGrapher alloc] init]; + PBGitGrapher* g = [[PBGitGrapher alloc] initWithRepository: repository]; [g parseCommits: self.commits]; [self performSelectorOnMainThread:@selector(setGrapher:) withObject:g waitUntilDone:YES]; [self performSelectorOnMainThread:@selector(setCommits:) withObject:newArray waitUntilDone:YES]; diff --git a/PBGitRevisionCell.m b/PBGitRevisionCell.m index 109a2ac..e57254e 100644 --- a/PBGitRevisionCell.m +++ b/PBGitRevisionCell.m @@ -63,7 +63,11 @@ - (void) drawCircleForColumn: (int) c inRect: (NSRect) r { - [[NSColor blackColor] set]; + if (!cellInfo.hasRef) + [[NSColor blackColor] set]; + else + [[NSColor redColor] set]; + int columnWidth = 10; NSPoint origin = r.origin; NSPoint columnOrigin = { origin.x + columnWidth * c, origin.y}; diff --git a/PBGraphCellInfo.h b/PBGraphCellInfo.h index 8f4b84b..8e6e464 100644 --- a/PBGraphCellInfo.h +++ b/PBGraphCellInfo.h @@ -14,9 +14,11 @@ int position; NSArray* lines; int numColumns; + BOOL hasRef; } @property(readonly) NSArray* lines; @property(assign) int position, numColumns; +@property(assign) BOOL hasRef; - (id)initWithPosition: (int) p andLines: (NSArray*) l; diff --git a/PBGraphCellInfo.m b/PBGraphCellInfo.m index f5e96b5..890ce7f 100644 --- a/PBGraphCellInfo.m +++ b/PBGraphCellInfo.m @@ -10,7 +10,7 @@ @implementation PBGraphCellInfo -@synthesize lines, position, numColumns; +@synthesize lines, position, numColumns, hasRef; - (id)initWithPosition: (int) p andLines: (NSArray*) l { position = p;