Skip to content

GTTreeBuilder: don't add blob data lazily #566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 4 additions & 42 deletions ObjectiveGit/GTTreeBuilder.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ @interface GTTreeBuilder ()
@property (nonatomic, assign, readonly) git_treebuilder *git_treebuilder;
@property (nonatomic, strong, readonly) GTRepository *repository;

// Data to be written with the tree, keyed by the file name. This should only be
// accessed while synchronized on self.
//
// This is needed because we don't want to add the entries to the object
// database until the tree's been written.
@property (nonatomic, strong, readonly) NSMutableDictionary *fileNameToPendingData;

@end

@implementation GTTreeBuilder
Expand Down Expand Up @@ -80,8 +73,6 @@ - (instancetype)initWithTree:(GTTree *)treeOrNil repository:(GTRepository *)repo
}

_repository = repository;
_fileNameToPendingData = [NSMutableDictionary dictionary];

return self;
}

Expand Down Expand Up @@ -122,12 +113,11 @@ - (GTTreeEntry *)addEntryWithData:(NSData *)data fileName:(NSString *)fileName f
NSParameterAssert(data != nil);
NSParameterAssert(fileName != nil);

GTOID *OID = [GTOID OIDByHashingData:data type:GTObjectTypeBlob error:error];
if (OID == nil) return nil;
GTObjectDatabase *odb = [self.repository objectDatabaseWithError:error];
if (odb == nil) return nil;

@synchronized (self) {
self.fileNameToPendingData[fileName] = data;
}
GTOID *OID = [odb writeData:data type:GTObjectTypeBlob error:error];
if (OID == nil) return nil;

return [self addEntryWithOID:OID fileName:fileName fileMode:fileMode error:error];
}
Expand All @@ -153,38 +143,10 @@ - (BOOL)removeEntryWithFileName:(NSString *)fileName error:(NSError **)error {
if (error != NULL) *error = [NSError git_errorFor:status description:@"Failed to remove entry with name %@ from tree builder.", fileName];
}

@synchronized (self) {
[self.fileNameToPendingData removeObjectForKey:fileName];
}

return status == GIT_OK;
}

- (BOOL)writePendingData:(NSError **)error {
NSDictionary *copied;
@synchronized (self) {
copied = [self.fileNameToPendingData copy];
[self.fileNameToPendingData removeAllObjects];
}

if (copied.count != 0) {
GTObjectDatabase *odb = [self.repository objectDatabaseWithError:error];
if (odb == nil) return NO;

for (NSString *fileName in copied) {
NSData *data = copied[fileName];
GTOID *dataOID = [odb writeData:data type:GTObjectTypeBlob error:error];
if (dataOID == nil) return NO;
}
}

return YES;
}

- (GTTree *)writeTree:(NSError **)error {
BOOL success = [self writePendingData:error];
if (!success) return nil;

git_oid treeOid;
int status = git_treebuilder_write(&treeOid, self.git_treebuilder);
if (status != GIT_OK) {
Expand Down
6 changes: 2 additions & 4 deletions ObjectiveGitTests/GTTreeBuilderSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,17 @@
expect(foundEntry.SHA).to(equal(entry.SHA));
});

it(@"should write new blobs when the tree is written", ^{
it(@"should be possible to write a blob with data", ^{
GTTreeEntry *entry = [builder addEntryWithData:[@"Hello, World!" dataUsingEncoding:NSUTF8StringEncoding] fileName:@"test.txt" fileMode:GTFileModeBlob error:NULL];
expect(entry).notTo(beNil());

GTObjectDatabase *database = [repo objectDatabaseWithError:NULL];
expect(database).notTo(beNil());

expect(@([database containsObjectWithOID:entry.OID])).to(beFalsy());
expect(@([database containsObjectWithOID:entry.OID])).to(beTruthy());

GTTree *tree = [builder writeTree:NULL];
expect(tree).notTo(beNil());

expect(@([database containsObjectWithOID:entry.OID])).to(beTruthy());
});

it(@"should be possible to write a builder to a repository", ^{
Expand Down