Permalink
Browse files

Download branch kickoff

  • Loading branch information...
1 parent b76172d commit eb3dfc31975aef0ae2b5140c477b5e70bac95e61 @seppo0010 committed Apr 1, 2012
Showing with 71 additions and 3 deletions.
  1. +1 −1 nios/ide/Model/NGRepository.h
  2. +56 −2 nios/ide/Model/NGRepository.m
  3. +14 −0 nios/ide/SelectBranchViewController.m
@@ -43,7 +43,7 @@
@property (assign) NGUser* user;
@property (retain) NSString* name;
-- (BOOL) isDownloaded;
- (void) getBranches:(void(^)(NSArray*))successBlock_ failure:(void(^)(NSError *))failureBlock_;
+- (void) downloadBranch:(NSString*)branchName success:(void(^)())successBlock_ failure:(void(^)(NSError *))failureBlock_;
@end
@@ -11,6 +11,7 @@
#import "NSObject+DictionaryInitialization.h"
#import "UAGithubEngine.h"
#import "NSThread+BlockAddition.h"
+#import "NSData+Base64.h"
@implementation NGRepository
@@ -41,9 +42,62 @@ - (BOOL) isDownloaded {
return FALSE;
}
-- (void) download:(void(^)(NSArray *))successBlock_ failure:(void(^)(NSError *))failureBlock_ {
-}
+- (void) downloadBranch:(NSString*)branchName success:(void(^)())successBlock_ failure:(void(^)(NSError *))failureBlock_ {
+// [NSThread performBlockInBackground:^() {
+ NSString* repo = [NSString stringWithFormat:@"%@/%@", user.login, name];
+ NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
+ NSString* documentsPath = [paths objectAtIndex:0];
+ NSString* path = [[documentsPath stringByAppendingPathComponent:repo] stringByAppendingPathComponent:branchName];
+
+ NSError* error;
+ if (![[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error]) {
+ failureBlock_(error);
+ return;
+ }
+ [user.engine reference:[NSString stringWithFormat:@"heads/%@", branchName] inRepository:repo completion:^id(id response) {
+ if ([response isKindOfClass:[NSError class]]) {
+ failureBlock_(error);
+ return nil;
+ } else if (response != nil && [response isKindOfClass:[NSArray class]] == FALSE) {
+ failureBlock_(nil);
+ return nil;
+ }
+ NSString* treeSha = [[[response objectAtIndex:0] valueForKey:@"object"] valueForKey:@"sha"];
+ [user.engine tree:treeSha inRepository:repo recursive:YES completion:^id(id response) {
+ NSArray* tree = [[response objectAtIndex:0] valueForKey:@"tree"];
+ for (NSDictionary* file in tree) {
+ [user.engine blobForSHA:[file valueForKey:@"sha"] inRepository:repo completion:^id(id blobs) {
+ if ([blobs isKindOfClass:[NSError class]]) {
+ failureBlock_(error);
+ return nil;
+ } else if (blobs != nil && [blobs isKindOfClass:[NSArray class]] == FALSE) {
+ failureBlock_(nil);
+ return nil;
+ }
+ NSError* error;
+ NSDictionary* blob = [blobs objectAtIndex:0];
+ NSString* encodedContent = [blob valueForKey:@"content"];
+ NSData* content;
+ if ([[blob valueForKey:@"encoding"] isEqual:@"base64"]) {
+ content = [NSData dataFromBase64String:encodedContent];
+ } else if ([[blob valueForKey:@"encoding"] isEqual:@"utf-8"]) {
+ content = [encodedContent dataUsingEncoding:NSUTF8StringEncoding];
+ }
+ if (![content writeToFile:[path stringByAppendingPathComponent:[file valueForKey:@"path"]] options:NSDataWritingAtomic error:&error]) {
+ failureBlock_(error);
+ return nil;
+ }
+ return nil;
+ }];
+ }
+ return nil;
+ }];
+ return nil;
+ }];
+// }];
+
+}
- (void) setOwner:(id)_owner {
[[_owner retain] autorelease];
[owner release];
@@ -52,6 +52,20 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
return cell;
}
+- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
+ NSString* branch = [branches objectAtIndex:indexPath.row];
+ [repository downloadBranch:branch success:^void(){
+ } failure:^void(NSError* error){
+ UIAlertView* alert = [[UIAlertView alloc] init];
+ [alert setTitle:@"Ooops..."];
+ [alert setMessage:[error localizedDescription]];
+ [alert addButtonWithTitle:@"OK"];
+ [alert show];
+ [alert release];
+ }];
+}
+
+
- (void)viewDidUnload {
[super viewDidUnload];
[table release];

0 comments on commit eb3dfc3

Please sign in to comment.