27
27
#import " GTRepository.h"
28
28
#import " NSError+Git.h"
29
29
#import " GTOdbObject.h"
30
+ #import " GTOID.h"
30
31
#import " NSString+Git.h"
31
32
32
33
#import " git2/odb_backend.h"
@@ -89,24 +90,30 @@ - (GTOdbObject *)objectWithSha:(NSString *)sha error:(NSError **)error {
89
90
return [self objectWithOid: &oid error: error];
90
91
}
91
92
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 );
93
96
git_odb_stream *stream;
94
97
git_oid oid;
98
+ size_t writtenBytes = 0 ;
95
99
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);
97
101
if (gitError < GIT_OK) {
98
102
if (error != NULL )
99
103
*error = [NSError git_errorFor: gitError withAdditionalDescription: @" Failed to open write stream on odb." ];
100
104
return nil ;
101
105
}
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;
108
115
}
109
-
116
+
110
117
gitError = stream->finalize_write (&oid, stream);
111
118
if (gitError < GIT_OK) {
112
119
if (error != NULL )
@@ -116,7 +123,21 @@ - (NSString *)shaByInsertingString:(NSString *)data objectType:(GTObjectType)typ
116
123
117
124
stream->free (stream);
118
125
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];
120
141
}
121
142
122
143
- (BOOL )containsObjectWithSha : (NSString *)sha error : (NSError **)error {
0 commit comments