mirror of
https://github.com/kennethreitz-archive/gitx.git
synced 2026-06-05 23:40:18 +00:00
c17215e55e
This is a convenience class to replace using NSStrings to store and compare SHAs. PBGitSHA has a much faster isEqual: function. It is <NSCopying> compliant and implements isEqual: and hash so it can be used as a key in dictionaries.
140 lines
1.8 KiB
Objective-C
140 lines
1.8 KiB
Objective-C
//
|
|
// PBGitSHA.m
|
|
// GitX
|
|
//
|
|
// Created by BrotherBard on 3/28/10.
|
|
// Copyright 2010 BrotherBard. All rights reserved.
|
|
//
|
|
|
|
#import "PBGitSHA.h"
|
|
|
|
|
|
@interface PBGitSHA ()
|
|
|
|
- (id)initWithOID:(git_oid)g_oid;
|
|
|
|
@end
|
|
|
|
|
|
@implementation PBGitSHA
|
|
|
|
|
|
@synthesize oid;
|
|
@synthesize string;
|
|
|
|
|
|
+ (PBGitSHA *)shaWithOID:(git_oid)oid
|
|
{
|
|
return [[PBGitSHA alloc] initWithOID:oid];
|
|
}
|
|
|
|
|
|
+ (PBGitSHA *)shaWithString:(NSString *)shaString
|
|
{
|
|
git_oid oid;
|
|
int err = git_oid_mkstr(&oid, [shaString UTF8String]);
|
|
if (err == GIT_ENOTOID)
|
|
return nil;
|
|
|
|
return [self shaWithOID:oid];
|
|
}
|
|
|
|
|
|
+ (PBGitSHA *)shaWithCString:(const char *)shaCString
|
|
{
|
|
git_oid oid;
|
|
int err = git_oid_mkstr(&oid, shaCString);
|
|
if (err == GIT_ENOTOID)
|
|
return nil;
|
|
|
|
return [self shaWithOID:oid];
|
|
}
|
|
|
|
|
|
+ (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
|
|
{
|
|
return NO;
|
|
}
|
|
|
|
|
|
+ (BOOL)isKeyExcludedFromWebScript:(const char *)name
|
|
{
|
|
return NO;
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
#pragma mark PBGitSHA
|
|
|
|
- (id)initWithOID:(git_oid)g_oid
|
|
{
|
|
self = [super init];
|
|
if (!self)
|
|
return nil;
|
|
|
|
oid = g_oid;
|
|
|
|
return self;
|
|
}
|
|
|
|
|
|
- (NSString *)string
|
|
{
|
|
if (!string) {
|
|
char *hex = git_oid_mkhex(&oid);
|
|
if (hex == NULL)
|
|
return nil;
|
|
string = [NSString stringWithUTF8String:hex];
|
|
free(hex);
|
|
}
|
|
|
|
return string;
|
|
}
|
|
|
|
|
|
- (BOOL)isEqual:(id)otherSHA
|
|
{
|
|
if (self == otherSHA)
|
|
return YES;
|
|
|
|
git_oid other_oid = [(PBGitSHA *)otherSHA oid];
|
|
return git_oid_cmp(&oid, &other_oid) == 0;
|
|
}
|
|
|
|
|
|
- (BOOL)isEqualToOID:(git_oid)other_oid
|
|
{
|
|
return git_oid_cmp(&oid, &other_oid) == 0;
|
|
}
|
|
|
|
|
|
- (NSUInteger)hash
|
|
{
|
|
NSUInteger hash;
|
|
memcpy(&hash, &(oid.id), sizeof(NSUInteger));
|
|
|
|
return hash;
|
|
}
|
|
|
|
|
|
- (NSString *)description
|
|
{
|
|
return [self string];
|
|
}
|
|
|
|
|
|
|
|
#pragma mark <NSCopying>
|
|
|
|
- (id)copyWithZone:(NSZone *)zone
|
|
{
|
|
git_oid oidCopy;
|
|
git_oid_cpy(&oidCopy, &oid);
|
|
PBGitSHA *copy = [[[self class] allocWithZone:zone] initWithOID:oidCopy];
|
|
|
|
return copy;
|
|
}
|
|
|
|
@end
|