Skip to content

Commit 16a57f0

Browse files
committed
Wrap git_index_add_frombuffer
1 parent 6c52f86 commit 16a57f0

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

ObjectiveGit/GTIndex.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@
114114
/// Returns a new GTIndexEntry, or nil if an error occurred.
115115
- (GTIndexEntry *)entryWithName:(NSString *)name error:(NSError **)error;
116116

117+
///TODO: Document
118+
- (NSData *)dataWithName:(NSString *)name error:(NSError **)error;
119+
117120
/// Add an entry to the index.
118121
///
119122
/// Note that this *cannot* add submodules. See -[GTSubmodule addToIndex:].
@@ -143,6 +146,9 @@
143146
/// Returns whether reading the tree was successful.
144147
- (BOOL)addContentsOfTree:(GTTree *)tree error:(NSError **)error;
145148

149+
///TODO: Document
150+
- (BOOL)addData:(NSData *)data withName:(NSString *)name error:(NSError **)error;
151+
146152
/// Remove an entry (by relative path) from the index.
147153
/// Will fail if the receiver's repository is nil.
148154
///

ObjectiveGit/GTIndex.m

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#import "GTRepository+Private.h"
3737
#import "GTRepository.h"
3838
#import "GTTree.h"
39+
#import "GTBlob.h"
3940
#import "NSArray+StringArray.h"
4041
#import "NSError+Git.h"
4142

@@ -158,6 +159,18 @@ - (GTIndexEntry *)entryWithName:(NSString *)name error:(NSError **)error {
158159
return [self entryAtIndex:pos];
159160
}
160161

162+
- (NSData *)dataWithName:(NSString *)name error:(NSError **)error {
163+
GTIndexEntry *entry = [self entryWithName:name error:error];
164+
if (*error) return nil;
165+
166+
const git_oid *oid = &entry.git_index_entry->id;
167+
GTBlob *blob = [self.repository lookUpObjectByGitOid:oid
168+
objectType:GTObjectTypeBlob
169+
error:error];
170+
171+
return [blob data];
172+
}
173+
161174
- (BOOL)addEntry:(GTIndexEntry *)entry error:(NSError **)error {
162175
int status = git_index_add(self.git_index, entry.git_index_entry);
163176
if (status != GIT_OK) {
@@ -191,6 +204,24 @@ - (BOOL)addContentsOfTree:(GTTree *)tree error:(NSError **)error {
191204

192205
return YES;
193206
}
207+
- (BOOL)addData:(NSData *)data withName:(NSString *)name error:(NSError **)error {
208+
NSParameterAssert(data != nil);
209+
NSParameterAssert(name != nil);
210+
211+
git_index_entry entry;
212+
memset(&entry, 0x0, sizeof(git_index_entry));
213+
entry.path = [name cStringUsingEncoding:NSUTF8StringEncoding];
214+
entry.mode = GIT_FILEMODE_BLOB;
215+
216+
int status = git_index_add_frombuffer(self.git_index, &entry, [data bytes], [data length]);
217+
218+
if (status != GIT_OK) {
219+
if (error != NULL) *error = [NSError git_errorFor:status description:@"Failed to add data with name %@ into index.", name];
220+
return NO;
221+
}
222+
223+
return YES;
224+
}
194225

195226
- (BOOL)removeFile:(NSString *)file error:(NSError **)error {
196227
NSString *unicodeString = [self composedUnicodeStringWithString:file];

ObjectiveGitTests/GTIndexSpec.m

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,32 @@
276276
});
277277
});
278278

279+
describe(@"adding data", ^{
280+
__block GTRepository *repo;
281+
__block GTIndex *index;
282+
__block NSError *error;
283+
284+
beforeEach(^{
285+
error = nil;
286+
repo = self.testUnicodeFixtureRepository;
287+
// Not sure why but it doesn't work with an in memory index
288+
// index = [GTIndex inMemoryIndexWithRepository:repo error:&error];
289+
index = [repo indexWithError:&error];
290+
expect(error).to(beNil());
291+
});
292+
293+
it(@"should store data at given path", ^{
294+
NSData *data = [NSData dataWithBytes:"foo" length:4];
295+
[index addData:data withName:@"bar/foo" error:&error];
296+
expect(error).to(beNil());
297+
298+
NSData *returnData = [index dataWithName:@"bar/foo" error:&error];
299+
expect(error).to(beNil());
300+
// expect([[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]).to(equal(@"foo"));
301+
expect(returnData).to(equal(data));
302+
});
303+
});
304+
279305
afterEach(^{
280306
[self tearDown];
281307
});

0 commit comments

Comments
 (0)