forked from libgit2/objective-git
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathGTOID.m
More file actions
145 lines (108 loc) · 3.28 KB
/
GTOID.m
File metadata and controls
145 lines (108 loc) · 3.28 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
//
// GTOID.m
// ObjectiveGitFramework
//
// Created by Josh Abernathy on 4/9/13.
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
//
#import "GTOID.h"
#import "NSError+Git.h"
#import "git2/errors.h"
#import "git2/odb.h"
@interface GTOID () {
git_oid _git_oid;
}
@end
@implementation GTOID
#pragma mark Properties
- (const git_oid *)git_oid {
return &_git_oid;
}
- (NSString *)SHA {
char *SHA = git_oid_tostr_s(self.git_oid);
NSString *str = [[NSString alloc] initWithBytes:SHA
length:GIT_OID_HEXSZ
encoding:NSUTF8StringEncoding];
NSAssert(str != nil, @"Failed to create SHA string");
return str;
}
#pragma mark Lifecycle
- (instancetype)init {
NSAssert(NO, @"Call to an unavailable initializer.");
return nil;
}
- (instancetype)initWithGitOid:(const git_oid *)oid {
NSParameterAssert(oid != NULL);
self = [super init];
if (self == nil) return nil;
git_oid_cpy(&_git_oid, oid);
return self;
}
- (instancetype)initWithSHA:(NSString *)SHA error:(NSError **)error {
NSParameterAssert(SHA != nil);
const char *SHACString = SHA.UTF8String;
NSAssert(SHACString, @"Unexpected nil SHA");
return [self initWithSHACString:SHACString error:error];
}
- (instancetype)initWithSHA:(NSString *)SHA {
return [self initWithSHA:SHA error:NULL];
}
- (instancetype)initWithSHACString:(const char *)string error:(NSError **)error {
NSParameterAssert(string != NULL);
self = [super init];
if (self == nil) return nil;
int status = git_oid_fromstr(&_git_oid, string);
if (status != GIT_OK) {
if (error != NULL) {
*error = [NSError git_errorFor:status description:@"Failed to convert string '%s' to object id", string];
}
return nil;
}
return self;
}
- (instancetype)initWithSHACString:(const char *)string {
return [self initWithSHACString:string error:NULL];
}
+ (instancetype)oidWithGitOid:(const git_oid *)git_oid {
return [[self alloc] initWithGitOid:git_oid];
}
+ (instancetype)oidWithSHA:(NSString *)SHA {
return [[self alloc] initWithSHA:SHA];
}
+ (instancetype)oidWithSHACString:(const char *)SHA {
return [[self alloc] initWithSHACString:SHA];
}
- (BOOL)isZero {
return git_oid_is_zero(self.git_oid) != 0;
}
#pragma mark NSObject
- (NSString *)description {
return [NSString stringWithFormat:@"<%@: %p>{ SHA: %@ }", self.class, self, self.SHA];
}
- (NSUInteger)hash {
// Hash the raw OID.
NSData *data = [[NSData alloc] initWithBytesNoCopy:_git_oid.id length:GIT_OID_RAWSZ freeWhenDone:NO];
return data.hash;
}
- (BOOL)isEqual:(GTOID *)object {
if (object == self) return YES;
if (![object isKindOfClass:GTOID.class]) return NO;
return (BOOL)git_oid_equal(self.git_oid, object.git_oid);
}
- (id)copyWithZone:(NSZone *)zone {
// Optimization: Since this class is immutable we don't need to create an actual copy.
return self;
}
@end
@implementation GTOID (GTObjectDatabase)
+ (instancetype)OIDByHashingData:(NSData *)data type:(GTObjectType)type error:(NSError **)error {
NSParameterAssert(data != nil);
git_oid oid;
int gitError = git_odb_hash(&oid, data.bytes, data.length, (git_object_t)type);
if (gitError != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to hash"];
return nil;
}
return [[self alloc] initWithGitOid:&oid];
}
@end