Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Incorporate review feedback

  • Loading branch information...
commit 224e672efdde5964a0fef1cdca092869c72e11f5 1 parent 5ca5af7
@ben ben authored
View
20 Classes/GTRepository.h
@@ -84,16 +84,16 @@ typedef void (^GTRepositoryStatusBlock)(NSURL *fileURL, GTRepositoryFileStatus s
// Clone a repository
//
-// originURL - URL to clone from
-// workdirURL - URL to (local) working directory
-// barely - If true, create a bare clone
-// withCheckout - if false, don't checkout the remote HEAD
-// transferProgressBlock - this block is called with network transfer updates
-// checkoutProgressBlock - this block is called with checkout updates (if withCheckout:true)
-//
-// returns a GTRepository object if successful.
-// returns nil if an error occurred, and fills the error parameter.
-+ (id)cloneFromURL:(NSURL *)originURL toWorkdingDirectory:(NSURL *)workdirURL barely:(BOOL)barely withCheckout:(BOOL)withCheckout transferProgressBlock:(void (^)(const git_transfer_progress *))transferProgressBlock checkoutProgressBlock:(void (^)(NSString *path, int completedSteps, int totalSteps))checkoutProgressBlock error:(NSError **)error;
+// originURL - The URL to clone from.
+// workdirURL - A URL to the desired working directory on the local machine.
+// barely - If YES, create a bare clone
+// withCheckout - if NO, don't checkout the remote HEAD
+// error - A pointer to fill in case of trouble.
+// transferProgressBlock - This block is called with network transfer updates.
+// checkoutProgressBlock - This block is called with checkout updates (if withCheckout is YES).
+//
+// returns nil (and fills the error parameter) if an error occurred, or a GTRepository object if successful.
++ (id)cloneFromURL:(NSURL *)originURL toWorkingDirectory:(NSURL *)workdirURL barely:(BOOL)barely withCheckout:(BOOL)withCheckout error:(NSError **)error transferProgressBlock:(void (^)(const git_transfer_progress *))transferProgressBlock checkoutProgressBlock:(void (^)(NSString *path, NSUInteger completedSteps, NSUInteger totalSteps))checkoutProgressBlock;
// Helper for getting the sha1 has of a raw object
//
View
49 Classes/GTRepository.m
@@ -119,6 +119,12 @@ + (id)repositoryWithURL:(NSURL *)localFileURL error:(NSError **)error {
return [[self alloc] initWithURL:localFileURL error:error];
}
+- (id)initWithNativeRepository:(git_repository *)repository {
+ self = [super init];
+ self.git_repository = repository;
+ return self;
+}
+
- (id)initWithURL:(NSURL *)localFileURL error:(NSError **)error {
NSURL *gitDirForLocalURL = [self.class _gitURLForURL:localFileURL error:error];
if (gitDirForLocalURL == nil) return nil;
@@ -139,37 +145,34 @@ - (id)initWithURL:(NSURL *)localFileURL error:(NSError **)error {
return self;
}
-static void c_checkout_progress(const char *path, size_t completedSteps, size_t totalSteps, void *payload) {
- void (^block)(NSString*, int, int) = (__bridge id)payload;
- NSString *nsPath = path ? [NSString stringWithUTF8String:path] : nil;
- if (block) block(nsPath, (int)completedSteps, (int)totalSteps);
+static void checkoutProgressCallback(const char *path, size_t completedSteps, size_t totalSteps, void *payload) {
+ if (payload == NULL) return;
+ void (^block)(NSString *, NSUInteger, NSUInteger) = (__bridge id)payload;
+ NSString *nsPath = (path != NULL ? [NSString stringWithUTF8String:path] : nil);
+ block(nsPath, completedSteps, totalSteps);
}
-static void c_transfer_progress(const git_transfer_progress *prog, void *payload) {
- void (^block)(const git_transfer_progress*) = (__bridge id) payload;
- if (block) block(prog);
+static void transferProgressCallback(const git_transfer_progress *progress, void *payload) {
+ if (payload == NULL) return;
+ void (^block)(const git_transfer_progress *) = (__bridge id)payload;
+ block(progress);
}
-+ (id)cloneFromURL:(NSURL *)originURL toWorkdingDirectory:(NSURL *)workdirURL barely:(BOOL)barely withCheckout:(BOOL)withCheckout transferProgressBlock:(void (^)(const git_transfer_progress *))transferProgressBlock checkoutProgressBlock:(void (^)(NSString *path, int completedSteps, int totalSteps))checkoutProgressBlock error:(NSError **)error {
-
- git_checkout_opts *opts = nil;
- if (withCheckout) {
- opts = calloc(1, sizeof(git_checkout_opts));
- opts->version = GIT_CHECKOUT_OPTS_VERSION;
- opts->checkout_strategy = GIT_CHECKOUT_SAFE;
- opts->progress_cb = c_checkout_progress;
- opts->progress_payload = (__bridge void *)(checkoutProgressBlock);
- }
++ (id)cloneFromURL:(NSURL *)originURL toWorkingDirectory:(NSURL *)workdirURL barely:(BOOL)barely withCheckout:(BOOL)withCheckout error:(NSError **)error transferProgressBlock:(void (^)(const git_transfer_progress *))transferProgressBlock checkoutProgressBlock:(void (^)(NSString *path, NSUInteger completedSteps, NSUInteger totalSteps))checkoutProgressBlock {
- const char *cOriginURL = originURL ? originURL.absoluteString.UTF8String : nil;
- const char *cWorkdirURL = workdirURL ? workdirURL.path.UTF8String : nil;
+ const char *cOriginURL = originURL.absoluteString.UTF8String;
+ const char *cWorkdirURL = workdirURL.path.UTF8String;
git_repository *r;
int gitError;
if (barely) {
- gitError = git_clone_bare(&r, cOriginURL, cWorkdirURL, c_transfer_progress, (__bridge void*)transferProgressBlock);
+ gitError = git_clone_bare(&r, cOriginURL, cWorkdirURL, transferProgressCallback, (__bridge void *)transferProgressBlock);
} else {
- gitError = git_clone(&r, cOriginURL, cWorkdirURL, opts, c_transfer_progress, (__bridge void*)transferProgressBlock);
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+ opts.checkout_strategy = GIT_CHECKOUT_SAFE;
+ opts.progress_cb = checkoutProgressCallback;
+ opts.progress_payload = (__bridge void *)checkoutProgressBlock;
+ gitError = git_clone(&r, cOriginURL, cWorkdirURL, (withCheckout ? &opts : NULL), transferProgressCallback, (__bridge void *)transferProgressBlock);
}
if (gitError < GIT_OK) {
@@ -177,9 +180,7 @@ + (id)cloneFromURL:(NSURL *)originURL toWorkdingDirectory:(NSURL *)workdirURL ba
return nil;
}
- GTRepository *repo = [self alloc];
- repo.git_repository = r;
- return repo;
+ return [[self alloc] initWithNativeRepository:r];
}
View
61 ObjectiveGitTests/GTRepositoryTest.m
@@ -49,13 +49,12 @@ - (void)setUp {
testContentType = GTObjectTypeBlob;
}
-- (void)removeDirectoryAtUrl:(NSURL *)url {
+- (void)removeDirectoryAtURL:(NSURL *)url {
NSFileManager *fm = [[NSFileManager alloc] init];
NSError *error = nil;
if([fm fileExistsAtPath:url.path]) {
- [fm removeItemAtPath:url.path error:&error];
- STAssertNil(error, [error localizedDescription]);
+ STAssertTrue([fm removeItemAtPath:url.path error:&error], [error localizedDescription]);
}
}
@@ -64,7 +63,7 @@ - (void)testCreateRepositoryInDirectory {
NSError *error = nil;
NSURL *newRepoURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"unit_test"]];
- [self removeDirectoryAtUrl:newRepoURL];
+ [self removeDirectoryAtURL:newRepoURL];
STAssertTrue([GTRepository initializeEmptyRepositoryAtURL:newRepoURL error:&error], nil);
GTRepository *newRepo = [GTRepository repositoryWithURL:newRepoURL error:&error];
@@ -276,45 +275,59 @@ - (void)testCanRevparse {
- (void)testCanClone {
- // Standard clone
- __block bool transferProgressCalled = false;
- __block bool checkoutProgressCalled = false;
+ __block BOOL transferProgressCalled = NO;
+ __block BOOL checkoutProgressCalled = NO;
+ void (^transferProgressBlock)(const git_transfer_progress *) = ^(const git_transfer_progress *progress) {
+ transferProgressCalled = YES;
+ };
+ void (^checkoutProgressBlock)(NSString *, NSUInteger, NSUInteger) = ^(NSString *path, NSUInteger completedSteps, NSUInteger totalSteps) {
+ checkoutProgressCalled = YES;
+ };
NSURL *originURL = [NSURL fileURLWithPath:TEST_REPO_PATH(self.class)]; //[NSURL URLWithString: @"https://github.com/libgit2/TestGitRepository"];
NSURL *workdirURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"unit_test"]];
NSError *err;
- [self removeDirectoryAtUrl:workdirURL];
+ [self removeDirectoryAtURL:workdirURL];
- repo = [GTRepository cloneFromURL:originURL toWorkdingDirectory:workdirURL barely:FALSE withCheckout:TRUE transferProgressBlock:^ (const git_transfer_progress *prog) { transferProgressCalled = true; } checkoutProgressBlock:^ (NSString *path, int curObj, int totalObjs) { checkoutProgressCalled = true; } error: &err];
- STAssertNil(err, err.localizedDescription);
+ repo = [GTRepository cloneFromURL:originURL toWorkingDirectory:workdirURL barely:FALSE withCheckout:TRUE error: &err transferProgressBlock:transferProgressBlock checkoutProgressBlock:checkoutProgressBlock];
- STAssertFalse(repo.isBare, @"Standard repo should not be bare");
- STAssertNotNil(repo, @"Failed to clone repository");
+ STAssertNotNil(repo, err.localizedDescription);
+ STAssertFalse([repo isBare], @"Standard repo should not be bare");
STAssertTrue(transferProgressCalled, @"Transfer progress handler never called");
STAssertTrue(checkoutProgressCalled, @"checkout progress handler never called");
GTReference *head = [repo headReferenceWithError:&err];
- STAssertNil(err, err.localizedDescription);
+ STAssertNotNil(head, err.localizedDescription);
STAssertEqualObjects(head.target, @"36060c58702ed4c2a40832c51758d5344201d89a", nil);
STAssertEqualObjects(head.type, @"commit", nil);
+}
+- (void)testCanCloneBarely {
+ __block BOOL transferProgressCalled = NO;
+ __block BOOL checkoutProgressCalled = NO;
+ void (^transferProgressBlock)(const git_transfer_progress *) = ^(const git_transfer_progress *progress) {
+ transferProgressCalled = YES;
+ };
+ void (^checkoutProgressBlock)(NSString *, NSUInteger, NSUInteger) = ^(NSString *path, NSUInteger completedSteps, NSUInteger totalSteps) {
+ checkoutProgressCalled = YES;
+ };
+ NSURL *originURL = [NSURL fileURLWithPath:TEST_REPO_PATH(self.class)]; //[NSURL URLWithString: @"https://github.com/libgit2/TestGitRepository"];
+ NSURL *workdirURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"unit_test"]];
+ NSError *err;
-
// Bare clone
- [self removeDirectoryAtUrl:workdirURL];
- transferProgressCalled = checkoutProgressCalled = false;
+ [self removeDirectoryAtURL:workdirURL];
+ transferProgressCalled = checkoutProgressCalled = NO;
- repo = [GTRepository cloneFromURL:originURL toWorkdingDirectory:workdirURL barely:TRUE withCheckout:TRUE transferProgressBlock:^(const git_transfer_progress *prog) { transferProgressCalled = true; } checkoutProgressBlock:^(NSString *path, int curObj, int totalObjs) { checkoutProgressCalled = true; } error: &err];
- STAssertNil(err, err.localizedDescription);
+ repo = [GTRepository cloneFromURL:originURL toWorkingDirectory:workdirURL barely:TRUE withCheckout:TRUE error: &err transferProgressBlock:transferProgressBlock checkoutProgressBlock:checkoutProgressBlock];
- STAssertNotNil(repo, @"Failed to clone repository");
+ STAssertNotNil(repo, err.localizedDescription);
+ STAssertTrue([repo isBare], @"Bare repo should be bare");
STAssertTrue(transferProgressCalled, @"Transfer progress handler never called");
- STAssertFalse(checkoutProgressCalled, @"checkout progress handler was called for bare repo");
+ STAssertFalse(checkoutProgressCalled, @"Checkout progress handler was called for bare repo");
- STAssertTrue(repo.isBare, @"Bare repo should be bare");
-
- head = [repo headReferenceWithError:&err];
- STAssertNil(err, err.localizedDescription);
+ GTReference *head = [repo headReferenceWithError:&err];
+ STAssertNotNil(head, err.localizedDescription);
STAssertEqualObjects(head.target, @"36060c58702ed4c2a40832c51758d5344201d89a", nil);
STAssertEqualObjects(head.type, @"commit", nil);
}
Please sign in to comment.
Something went wrong with that request. Please try again.