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