Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 172 lines (138 sloc) 4.089 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
//
// PBGitRevSpecifier.m
// GitX
//
// Created by Pieter de Bie on 12-09-08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "PBGitRevSpecifier.h"


@implementation PBGitRevSpecifier

@synthesize parameters, description, workingDirectory;
@synthesize isSimpleRef;


// internal designated init
- (id) initWithParameters:(NSArray *)params description:(NSString *)descrip
{
parameters = params;
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) initWithParameters:(NSArray *)params
{
[self initWithParameters:params description:nil];
return self;
}

- (id) initWithRef:(PBGitRef *)ref
{
[self initWithParameters:[NSArray arrayWithObject:ref.ref] description:ref.shortName];
return self;
}

- (id) initWithCoder:(NSCoder *)coder
{
[self initWithParameters:[coder decodeObjectForKey:@"Parameters"] description:[coder decodeObjectForKey:@"Description"]];
return self;
}

+ (PBGitRevSpecifier *)allBranchesRevSpec
{
return [[PBGitRevSpecifier alloc] initWithParameters:[NSArray arrayWithObject:@"--all"] description:@"All branches"];
}

+ (PBGitRevSpecifier *)localBranchesRevSpec
{
return [[PBGitRevSpecifier alloc] initWithParameters:[NSArray arrayWithObject:@"--branches"] description:@"Local branches"];
}

- (NSString*) simpleRef
{
if (![self isSimpleRef])
return nil;
return [parameters objectAtIndex:0];
}

- (PBGitRef *) ref
{
if (![self isSimpleRef])
return nil;

return [PBGitRef refFromString:[self simpleRef]];
}

- (NSString *) description
{
if (!description)
description = [parameters componentsJoinedByString:@" "];

return description;
}

- (NSString *) title
{
NSString *title = nil;

if ([self.description isEqualToString:@"HEAD"])
title = @"detached HEAD";
else if ([self isSimpleRef])
title = [[self ref] shortName];
else if ([self.description hasPrefix:@"-S"])
title = [self.description substringFromIndex:[@"-S" length]];
else if ([self.description hasPrefix:@"HEAD -- "])
title = [self.description substringFromIndex:[@"HEAD -- " length]];
else if ([self.description hasPrefix:@"-- "])
title = [self.description substringFromIndex:[@"-- " length]];
else if ([self.description hasPrefix:@"--left-right "])
title = [self.description substringFromIndex:[@"--left-right " length]];
else
title = self.description;

return [NSString stringWithFormat:@"\"%@\"", title];
}

- (BOOL) hasPathLimiter;
{
for (NSString* param in parameters)
if ([param isEqualToString:@"--"])
return YES;
return NO;
}

- (BOOL) hasLeftRight
{
for (NSString* param in parameters)
if ([param isEqualToString:@"--left-right"])
return YES;
return NO;
}

- (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.description isEqualToString:other.description];
}

- (NSUInteger) hash
{
if ([self isSimpleRef])
return [[[self parameters] objectAtIndex:0] hash];

return [self.description hash];
}

- (BOOL) isAllBranchesRev
{
return [self isEqual:[PBGitRevSpecifier allBranchesRevSpec]];
}

- (BOOL) isLocalBranchesRev
{
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
Something went wrong with that request. Please try again.