Permalink
Browse files

Add/modify convenience methods in PBRevSpecifier

    - NSCopying
    - change isEqualTo: to isEqual: so that it works well with NSArray's (containsObject: and removeObject: will use isEqual: instead of pointer equality)
    - add hash to go with isEqual:
    - pre-calculate the isSimpleRef value. The rangeOf... methods are very slow and have a major effect on the tight loop in reloadRefs.
  • Loading branch information...
brotherbard committed Feb 28, 2010
1 parent a0b11cf commit 1323638791261b73db74801eeccdd3d83e5bd62b
Showing with 65 additions and 45 deletions.
  1. +2 −2 PBGitRepository.m
  2. +4 −3 PBGitRevSpecifier.h
  3. +58 −39 PBGitRevSpecifier.m
  4. +1 −1 PBGitSidebarController.m
View
@@ -397,7 +397,7 @@ - (PBGitRevSpecifier*) addBranch:(PBGitRevSpecifier*)branch
// First check if the branch doesn't exist already
for (PBGitRevSpecifier *rev in branches)
- if ([branch isEqualTo: rev])
+ if ([branch isEqual: rev])
return rev;
NSIndexSet *newIndex = [NSIndexSet indexSetWithIndex:[branches count]];
@@ -412,7 +412,7 @@ - (PBGitRevSpecifier*) addBranch:(PBGitRevSpecifier*)branch
- (BOOL) removeBranch:(PBGitRevSpecifier *)branch
{
for (PBGitRevSpecifier *rev in branches) {
- if ([branch isEqualTo:rev]) {
+ if ([branch isEqual:rev]) {
NSIndexSet *oldIndex = [NSIndexSet indexSetWithIndex:[branches indexOfObject:rev]];
[self willChange:NSKeyValueChangeRemoval valuesAtIndexes:oldIndex forKey:@"branches"];
View
@@ -9,23 +9,23 @@
#import <Cocoa/Cocoa.h>
#import <PBGitRef.h>
-@interface PBGitRevSpecifier : NSObject {
+@interface PBGitRevSpecifier : NSObject <NSCopying> {
NSString *description;
NSArray *parameters;
NSURL *workingDirectory;
+ BOOL isSimpleRef;
}
- (id) initWithParameters:(NSArray*) params;
- (id) initWithRef: (PBGitRef*) ref;
-- (BOOL) isSimpleRef;
- (NSString*) simpleRef;
- (PBGitRef *) ref;
- (BOOL) hasPathLimiter;
- (BOOL) hasLeftRight;
- (NSString *) title;
-- (BOOL) isEqualTo: (PBGitRevSpecifier*) other;
+- (BOOL) isEqual: (PBGitRevSpecifier*) other;
- (BOOL) isAllBranchesRev;
- (BOOL) isLocalBranchesRev;
@@ -35,5 +35,6 @@
@property(retain) NSString *description;
@property(readonly) NSArray *parameters;
@property(retain) NSURL *workingDirectory;
+@property(readonly) BOOL isSimpleRef;
@end
View
@@ -12,54 +12,56 @@
@implementation PBGitRevSpecifier
@synthesize parameters, description, workingDirectory;
+@synthesize isSimpleRef;
-- (id) initWithParameters:(NSArray*) params
+
+// internal designated init
+- (id) initWithParameters:(NSArray *)params description:(NSString *)descrip
{
parameters = params;
- description = nil;
+ description = descrip;
+
+ if (([parameters count] > 1) || ([parameters count] == 0))
+ isSimpleRef = NO;
+ else {
+ NSString *param = [parameters objectAtIndex:0];
+ if ([param hasPrefix:@"-"] ||
+ [param rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"^@{}~:"]].location != NSNotFound ||
+ [param rangeOfString:@".."].location != NSNotFound)
+ isSimpleRef = NO;
+ else
+ isSimpleRef = YES;
+ }
+
return self;
}
-- (id) initWithRef: (PBGitRef*) ref
+- (id) initWithParameters:(NSArray *)params
{
- parameters = [NSArray arrayWithObject: ref.ref];
- description = ref.shortName;
+ [self initWithParameters:params description:nil];
return self;
}
-- (id) initWithCoder:(NSCoder *)coder
+- (id) initWithRef:(PBGitRef *)ref
{
- parameters = [coder decodeObjectForKey:@"Parameters"];
- description = [coder decodeObjectForKey:@"Description"];
+ [self initWithParameters:[NSArray arrayWithObject:ref.ref] description:ref.shortName];
return self;
}
-+ (PBGitRevSpecifier *)allBranchesRevSpec
+- (id) initWithCoder:(NSCoder *)coder
{
- id revspec = [[PBGitRevSpecifier alloc] initWithParameters:[NSArray arrayWithObject:@"--all"]];
- [revspec setDescription:@"All branches"];
- return revspec;
+ [self initWithParameters:[coder decodeObjectForKey:@"Parameters"] description:[coder decodeObjectForKey:@"Description"]];
+ return self;
}
-+ (PBGitRevSpecifier *)localBranchesRevSpec
++ (PBGitRevSpecifier *)allBranchesRevSpec
{
- id revspec = [[PBGitRevSpecifier alloc] initWithParameters:[NSArray arrayWithObject:@"--branches"]];
- [revspec setDescription:@"Local branches"];
- return revspec;
+ return [[PBGitRevSpecifier alloc] initWithParameters:[NSArray arrayWithObject:@"--all"] description:@"All branches"];
}
-- (BOOL) isSimpleRef
++ (PBGitRevSpecifier *)localBranchesRevSpec
{
- if ([parameters count] > 1)
- return NO;
-
- NSString *param = [parameters objectAtIndex:0];
- if ([param hasPrefix:@"-"] ||
- [param rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"^@{}~:"]].location != NSNotFound ||
- [param rangeOfString:@".."].location != NSNotFound)
- return NO;
-
- return YES;
+ return [[PBGitRevSpecifier alloc] initWithParameters:[NSArray arrayWithObject:@"--branches"] description:@"Local branches"];
}
- (NSString*) simpleRef
@@ -77,12 +79,12 @@ - (PBGitRef *) ref
return [PBGitRef refFromString:[self simpleRef]];
}
-- (NSString*) description
+- (NSString *) description
{
- if (description)
- return description;
-
- return [parameters componentsJoinedByString:@" "];
+ if (!description)
+ description = [parameters componentsJoinedByString:@" "];
+
+ return description;
}
- (NSString *) title
@@ -122,32 +124,49 @@ - (BOOL) hasLeftRight
return YES;
return NO;
}
-
-- (BOOL) isEqualTo: (PBGitRevSpecifier*) other
+
+- (BOOL) isEqual:(PBGitRevSpecifier *)other
{
if ([self isSimpleRef] ^ [other isSimpleRef])
return NO;
if ([self isSimpleRef])
- return [[[self parameters] objectAtIndex: 0] isEqualToString: [other.parameters objectAtIndex: 0]];
+ return [[[self parameters] objectAtIndex:0] isEqualToString:[other.parameters objectAtIndex:0]];
- return ([[parameters componentsJoinedByString:@" "] isEqualToString: [other.parameters componentsJoinedByString:@" "]] &&
- (!description || [description isEqualToString:other.description]));
+ return [self.description isEqualToString:other.description];
+}
+
+- (NSUInteger) hash
+{
+ if ([self isSimpleRef])
+ return [[[self parameters] objectAtIndex:0] hash];
+
+ return [self.description hash];
}
- (BOOL) isAllBranchesRev
{
- return [self isEqualTo:[PBGitRevSpecifier allBranchesRevSpec]];
+ return [self isEqual:[PBGitRevSpecifier allBranchesRevSpec]];
}
- (BOOL) isLocalBranchesRev
{
- return [self isEqualTo:[PBGitRevSpecifier localBranchesRevSpec]];
+ return [self isEqual:[PBGitRevSpecifier localBranchesRevSpec]];
}
- (void) encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:description forKey:@"Description"];
[coder encodeObject:parameters forKey:@"Parameters"];
}
+
+- (id)copyWithZone:(NSZone *)zone
+{
+ PBGitRevSpecifier *copy = [[[self class] allocWithZone:zone] initWithParameters:[self.parameters copy]];
+ copy.description = [self.description copy];
+ copy.workingDirectory = [self.workingDirectory copy];
+
+ return copy;
+}
+
@end
View
@@ -200,7 +200,7 @@ - (BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item
- (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(PBSourceViewCell *)cell forTableColumn:(NSTableColumn *)tableColumn item:(PBSourceViewItem *)item
{
- cell.isCheckedOut = [item.revSpecifier isEqualTo:[repository headRef]];
+ cell.isCheckedOut = [item.revSpecifier isEqual:[repository headRef]];
[cell setImage:[item icon]];
}

0 comments on commit 1323638

Please sign in to comment.