forked from libgit2/objective-git
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathGTObjectDatabase.m
More file actions
142 lines (114 loc) · 4.21 KB
/
GTObjectDatabase.m
File metadata and controls
142 lines (114 loc) · 4.21 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
//
// GTObjectDatabase.m
// ObjectiveGitFramework
//
// Created by Dave DeLong on 5/17/2011.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import "GTObjectDatabase.h"
#import "GTRepository.h"
#import "NSError+Git.h"
#import "GTOdbObject.h"
#import "GTOID.h"
#import "NSString+Git.h"
#import "GTOID.h"
#import "EXTScope.h"
#import "git2/errors.h"
#import "git2/odb.h"
#import "git2/odb_backend.h"
#import "git2/repository.h"
@interface GTObjectDatabase ()
@property (nonatomic, readonly, assign) git_odb *git_odb;
@end
@implementation GTObjectDatabase
- (NSString *)description {
return [NSString stringWithFormat:@"<%@: %p> repository: %@", NSStringFromClass([self class]), self, self.repository];
}
- (void)dealloc {
if (_git_odb != NULL) {
git_odb_free(_git_odb);
_git_odb = NULL;
}
}
#pragma mark API
- (instancetype)init {
NSAssert(NO, @"Call to an unavailable initializer.");
return nil;
}
- (instancetype)initWithRepository:(GTRepository *)repo error:(NSError **)error {
NSParameterAssert(repo != nil);
self = [super init];
if (self == nil) return nil;
_repository = repo;
int gitError = git_repository_odb(&_git_odb, repo.git_repository);
if (gitError != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to get ODB for repo."];
return nil;
}
return self;
}
- (GTOdbObject *)objectWithOID:(GTOID *)oid error:(NSError **)error {
git_odb_object *obj;
int gitError = git_odb_read(&obj, self.git_odb, oid.git_oid);
if (gitError != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to read raw object with OID %@", oid.SHA];
return nil;
}
return [[GTOdbObject alloc] initWithOdbObj:obj repository:self.repository];
}
- (GTOdbObject *)objectWithSHA:(NSString *)sha error:(NSError **)error {
GTOID *oid = [[GTOID alloc] initWithSHA:sha error:error];
if (oid == nil) return nil;
return [self objectWithOID:oid error:error];
}
#pragma mark Writing
- (GTOID *)writeData:(NSData *)data type:(GTObjectType)type error:(NSError **)error {
NSParameterAssert(data != nil);
git_odb_stream *stream;
int gitError = git_odb_open_wstream(&stream, self.git_odb, data.length, (git_object_t)type);
@onExit {
if (stream != NULL) git_odb_stream_free(stream);
};
if (gitError != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to open write stream on odb."];
return nil;
}
gitError = git_odb_stream_write(stream, data.bytes, data.length);
if (gitError != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to write to stream on odb."];
return nil;
}
git_oid oid;
gitError = git_odb_stream_finalize_write(&oid, stream);
if (gitError != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to finalize write on odb."];
return nil;
}
return [GTOID oidWithGitOid:&oid];
}
- (BOOL)containsObjectWithSHA:(NSString *)sha error:(NSError **)error {
GTOID *oid = [[GTOID alloc] initWithSHA:sha error:error];
if (oid == nil) return NO;
return [self containsObjectWithOID:oid];
}
- (BOOL)containsObjectWithOID:(GTOID *)oid {
return git_odb_exists(self.git_odb, oid.git_oid) ? YES : NO;
}
@end