From 1f783c91c40d7d457926189faa0c52fa4f958cdb Mon Sep 17 00:00:00 2001 From: Pieter de Bie Date: Fri, 19 Sep 2008 19:17:07 +0200 Subject: [PATCH] Reload refs on refresh --- GitX.xcodeproj/project.pbxproj | 4 +- PBDetailController.m | 3 +- PBGitRepository.h | 8 ++-- PBGitRepository.m | 68 ++++++++++++++++++++++------------ PBGitRevList.h | 3 +- PBGitRevList.m | 11 ++++-- PBGitRevSpecifier.m | 2 +- 7 files changed, 64 insertions(+), 35 deletions(-) diff --git a/GitX.xcodeproj/project.pbxproj b/GitX.xcodeproj/project.pbxproj index 59d0476..7b1b441 100644 --- a/GitX.xcodeproj/project.pbxproj +++ b/GitX.xcodeproj/project.pbxproj @@ -273,6 +273,8 @@ F56174540E05887E001DCD79 /* Git */ = { isa = PBXGroup; children = ( + F5C007730E731B48007B84B2 /* PBGitRef.h */, + F5C007740E731B48007B84B2 /* PBGitRef.m */, F5C6F6750E65FE2B00478D97 /* Graphing */, F5FF4E780E082E440006317A /* PBGitGrapher.h */, F5FF4E790E082E440006317A /* PBGitGrapher.m */, @@ -342,8 +344,6 @@ F5C6F6750E65FE2B00478D97 /* Graphing */ = { isa = PBXGroup; children = ( - F5C007730E731B48007B84B2 /* PBGitRef.h */, - F5C007740E731B48007B84B2 /* PBGitRef.m */, F50FE0E10E07BE9600854FCD /* PBGitRevisionCell.h */, F50FE0E20E07BE9600854FCD /* PBGitRevisionCell.m */, F56CC7270E65E0AD004307B4 /* PBGitGraphLine.h */, diff --git a/PBDetailController.m b/PBDetailController.m index 3128105..1d076a0 100644 --- a/PBDetailController.m +++ b/PBDetailController.m @@ -149,7 +149,8 @@ - (IBAction) refresh: sender { - repository.currentBranch = repository.currentBranch; + [repository reloadRefs]; + [repository.revisionList reload]; } - (void) selectCommit: (NSString*) commit diff --git a/PBGitRepository.h b/PBGitRepository.h index 5c284c4..53b2f8c 100644 --- a/PBGitRepository.h +++ b/PBGitRepository.h @@ -16,7 +16,7 @@ extern NSString* PBGitRepositoryErrorDomain; PBGitRevList* revisionList; NSMutableArray* branches; NSIndexSet* currentBranch; - NSDictionary* refs; + NSMutableDictionary* refs; } - (NSFileHandle*) handleForCommand:(NSString*) cmd; @@ -24,7 +24,8 @@ extern NSString* PBGitRepositoryErrorDomain; - (NSString*) outputForCommand:(NSString*) cmd; - (NSString*) outputForArguments:(NSArray*) args; -- (void) readRefs; +- (BOOL) reloadRefs; +- (void) addRef:(PBGitRef *)ref fromParameters:(NSArray *)params; - (void) readCurrentBranch; - (PBGitRevSpecifier*) addBranch: (PBGitRevSpecifier*) rev; - (void) selectBranch: (PBGitRevSpecifier*) rev; @@ -36,9 +37,10 @@ extern NSString* PBGitRepositoryErrorDomain; + (NSURL*)baseDirForURL:(NSURL*)repositoryURL; - (id) initWithURL: (NSURL*) path andRevSpecifier:(PBGitRevSpecifier*) rev; +- (void) setup; @property (retain) PBGitRevList* revisionList; @property (assign) NSMutableArray* branches; @property (assign) NSIndexSet* currentBranch; -@property (assign) NSDictionary* refs; +@property (assign) NSMutableDictionary* refs; @end diff --git a/PBGitRepository.m b/PBGitRepository.m index 8032158..11a935f 100644 --- a/PBGitRepository.m +++ b/PBGitRepository.m @@ -121,8 +121,7 @@ static NSString* gitPath; } if (success) { - [self readRefs]; - revisionList = [[PBGitRevList alloc] initWithRepository:self]; + [self setup]; [self readCurrentBranch]; } } @@ -130,15 +129,20 @@ static NSString* gitPath; return success; } +- (void) setup +{ + self.branches = [NSMutableArray array]; + [self reloadRefs]; + revisionList = [[PBGitRevList alloc] initWithRepository:self]; +} + - (id) initWithURL: (NSURL*) path andRevSpecifier:(PBGitRevSpecifier*) rev { self = [self init]; NSURL* gitDirURL = [PBGitRepository gitDirForURL:path]; [self setFileURL: gitDirURL]; - [self readRefs]; - - revisionList = [[PBGitRevList alloc] initWithRepository:self]; + [self setup]; [self selectBranch: [self addBranch: rev]]; return self; @@ -161,33 +165,49 @@ static NSString* gitPath; [controller release]; } -- (void) readRefs +- (void) addRef: (PBGitRef *) ref fromParameters: (NSArray *) components { + NSString* type = [components objectAtIndex:1]; + + NSString* sha; + if ([type isEqualToString:@"tag"] && [components count] == 4) + sha = [components objectAtIndex:3]; + else + sha = [components objectAtIndex:2]; + + NSMutableArray* curRefs; + if (curRefs = [refs objectForKey:sha]) + [curRefs addObject:ref]; + else + [refs setObject:[NSMutableArray arrayWithObject:ref] forKey:sha]; +} + +// reloadRefs: reload all refs in the repository, like in readRefs +// To stay compatible, this does not remove a ref from the branches list +// even after it has been deleted. +// returns YES when a ref was changed +- (BOOL) reloadRefs +{ + BOOL ret = NO; NSString* output = [PBEasyPipe outputForCommand:gitPath withArgs:[NSArray arrayWithObjects:@"for-each-ref", @"--format=%(refname) %(objecttype) %(objectname) %(*objectname)", @"refs", nil] inDir: self.fileURL.path]; NSArray* lines = [output componentsSeparatedByString:@"\n"]; - NSMutableDictionary* newRefs = [NSMutableDictionary dictionary]; - NSMutableArray* newBranches = [NSMutableArray array]; + refs = [NSMutableDictionary dictionary]; + for (NSString* line in lines) { NSArray* components = [line componentsSeparatedByString:@" "]; - PBGitRef* ref = [PBGitRef refFromString:[components objectAtIndex:0]]; - NSString* type = [components objectAtIndex:1]; - NSString* sha; - if ([type isEqualToString:@"tag"] && [components count] == 4) - sha = [components objectAtIndex:3]; - else - sha = [components objectAtIndex:2]; - if ([[ref type] isEqualToString:@"head"] || [[ref type] isEqualToString:@"remote"]) - [newBranches addObject: [[PBGitRevSpecifier alloc] initWithRef:ref]]; + // First do the ref matching. If this ref is new, add it to our ref list + PBGitRef *newRef = [PBGitRef refFromString:[components objectAtIndex:0]]; + PBGitRevSpecifier* revSpec = [[PBGitRevSpecifier alloc] initWithRef:newRef]; + if ([self addBranch:revSpec] == revSpec) + ret = YES; + + // Also add this ref to the branches list + [self addRef:newRef fromParameters:components]; - NSMutableArray* curRefs; - if (curRefs = [newRefs objectForKey:sha]) - [curRefs addObject:ref]; - else - [newRefs setObject:[NSMutableArray arrayWithObject:ref] forKey:sha]; } - self.refs = newRefs; - self.branches = newBranches; + self.refs = refs; + return ret; } - (PBGitRevSpecifier*) headRef diff --git a/PBGitRevList.h b/PBGitRevList.h index f587556..4b6c839 100644 --- a/PBGitRevList.h +++ b/PBGitRevList.h @@ -17,7 +17,8 @@ } - initWithRepository:(id)repo; -- (void) readCommits; +- (void) readCommitsForce: (BOOL) force; +- (void) reload; @property(retain) NSArray* commits; @property(retain) id grapher; diff --git a/PBGitRevList.m b/PBGitRevList.m index 8bdc268..1ab7eb9 100644 --- a/PBGitRevList.m +++ b/PBGitRevList.m @@ -23,7 +23,12 @@ return self; } -- (void) readCommits +- (void) reload +{ + [self readCommitsForce: YES]; +} + +- (void) readCommitsForce: (BOOL) force { // We use refparse to get the commit sha that we will parse. That way, // we can check if the current branch is the same as the previous one @@ -42,7 +47,7 @@ PBGitRevSpecifier* newRev = [selectedBranches objectAtIndex:0]; NSString* newSha = nil; - if (newRev && [newRev isSimpleRef]) { + if (!force && newRev && [newRev isSimpleRef]) { newSha = [repository parseReference:[newRev simpleRef]]; if ([newSha isEqualToString:lastSha]) return; @@ -57,7 +62,7 @@ change:(NSDictionary *)change context:(void *)context { if (object == repository) - [self readCommits]; + [self readCommitsForce: NO]; } - (void) walkRevisionListWithSpecifier: (PBGitRevSpecifier*) rev diff --git a/PBGitRevSpecifier.m b/PBGitRevSpecifier.m index 55c2dcb..a8d73f7 100644 --- a/PBGitRevSpecifier.m +++ b/PBGitRevSpecifier.m @@ -69,7 +69,7 @@ return NO; if ([self isSimpleRef]) - return [[self description] isEqualToString: [other description]]; + return [[[self parameters] objectAtIndex: 0] isEqualToString: [other.parameters objectAtIndex: 0]]; return ([[parameters componentsJoinedByString:@" "] isEqualToString: [other.parameters componentsJoinedByString:@" "]] && (!description || [description isEqualToString:other.description]));