Skip to content

Commit 92f4b28

Browse files
committed
Refactor the streaming write call to be more "general".
If someone gets an epiphany on how to do that without passing the git_odb_stream object to the block or copying the data…
1 parent dc4cd83 commit 92f4b28

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

Classes/GTObjectDatabase.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
- (GTOdbObject *)objectWithSha:(NSString *)sha error:(NSError **)error;
4747

4848
- (NSString *)shaByInsertingString:(NSString *)data objectType:(GTObjectType)type error:(NSError **)error;
49+
- (GTOdbObject *)objectByInsertingData:(NSData *)data objectType:(GTObjectType)type error:(NSError **)error;
4950

5051
- (BOOL)containsObjectWithSha:(NSString *)sha error:(NSError **)error;
5152

Classes/GTObjectDatabase.m

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#import "GTRepository.h"
2828
#import "NSError+Git.h"
2929
#import "GTOdbObject.h"
30+
#import "GTOID.h"
3031
#import "NSString+Git.h"
3132

3233
#import "git2/odb_backend.h"
@@ -89,24 +90,30 @@ - (GTOdbObject *)objectWithSha:(NSString *)sha error:(NSError **)error {
8990
return [self objectWithOid:&oid error:error];
9091
}
9192

92-
- (NSString *)shaByInsertingString:(NSString *)data objectType:(GTObjectType)type error:(NSError **)error {
93+
- (GTOID *)oidByWritingDataOfLength:(size_t)length type:(GTObjectType)type error:(NSError **)error block:(int (^)(git_odb_stream *stream))block {
94+
NSParameterAssert(length != 0);
95+
NSParameterAssert(block != nil);
9396
git_odb_stream *stream;
9497
git_oid oid;
98+
size_t writtenBytes = 0;
9599

96-
int gitError = git_odb_open_wstream(&stream, self.git_odb, data.length, (git_otype) type);
100+
int gitError = git_odb_open_wstream(&stream, self.git_odb, length, (git_otype) type);
97101
if(gitError < GIT_OK) {
98102
if(error != NULL)
99103
*error = [NSError git_errorFor:gitError withAdditionalDescription:@"Failed to open write stream on odb."];
100104
return nil;
101105
}
102-
103-
gitError = stream->write(stream, [data UTF8String], data.length);
104-
if(gitError < GIT_OK) {
105-
if(error != NULL)
106-
*error = [NSError git_errorFor:gitError withAdditionalDescription:@"Failed to write to stream on odb."];
107-
return nil;
106+
107+
while (length < writtenBytes) {
108+
gitError = block(stream);
109+
if(gitError < GIT_OK) {
110+
if(error != NULL)
111+
*error = [NSError git_errorFor:gitError withAdditionalDescription:@"Failed to write to stream on odb."];
112+
return nil;
113+
}
114+
writtenBytes += gitError;
108115
}
109-
116+
110117
gitError = stream->finalize_write(&oid, stream);
111118
if(gitError < GIT_OK) {
112119
if(error != NULL)
@@ -116,7 +123,21 @@ - (NSString *)shaByInsertingString:(NSString *)data objectType:(GTObjectType)typ
116123

117124
stream->free(stream);
118125

119-
return [NSString git_stringWithOid:&oid];
126+
return [GTOID oidWithGitOid:&oid];
127+
}
128+
129+
- (NSString *)shaByInsertingString:(NSString *)string objectType:(GTObjectType)type error:(NSError **)error {
130+
GTOID *oid = [self oidByWritingDataOfLength:string.length type:type error:error block:^int(git_odb_stream *stream) {
131+
return stream->write(stream, string.UTF8String, string.length);
132+
}];
133+
return [oid SHA];
134+
}
135+
136+
- (GTOdbObject *)objectByInsertingData:(NSData *)data objectType:(GTObjectType)type error:(NSError **)error {
137+
GTOID *oid = [self oidByWritingDataOfLength:data.length type:type error:error block:^int(git_odb_stream *stream) {
138+
return stream->write(stream, data.bytes, data.length);
139+
}];
140+
return [self objectWithOid:oid.git_oid error:error];
120141
}
121142

122143
- (BOOL)containsObjectWithSha:(NSString *)sha error:(NSError **)error {

0 commit comments

Comments
 (0)