|
| 1 | +// |
| 2 | +// GTRemotePushSpec.m |
| 3 | +// ObjectiveGitFramework |
| 4 | +// |
| 5 | +// Created by Ben Chatelain on 11/14/2014. |
| 6 | +// Copyright (c) 2014 GitHub, Inc. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import <Nimble/Nimble.h> |
| 10 | +#import <ObjectiveGit/ObjectiveGit.h> |
| 11 | +#import <Quick/Quick.h> |
| 12 | + |
| 13 | +#import "QuickSpec+GTFixtures.h" |
| 14 | + |
| 15 | +// Helper to quickly create commits |
| 16 | +GTCommit *(^createCommitInRepository)(NSString *, NSData *, NSString *, GTRepository *) = ^ GTCommit * (NSString *message, NSData *fileData, NSString *fileName, GTRepository *repo) { |
| 17 | + GTTreeBuilder *treeBuilder = [[GTTreeBuilder alloc] initWithTree:nil repository:repo error:nil]; |
| 18 | + [treeBuilder addEntryWithData:fileData fileName:fileName fileMode:GTFileModeBlob error:nil]; |
| 19 | + |
| 20 | + GTTree *testTree = [treeBuilder writeTree:nil]; |
| 21 | + |
| 22 | + // We need the parent commit to make the new one |
| 23 | + GTReference *headReference = [repo headReferenceWithError:nil]; |
| 24 | + |
| 25 | + GTEnumerator *commitEnum = [[GTEnumerator alloc] initWithRepository:repo error:nil]; |
| 26 | + [commitEnum pushSHA:[headReference targetSHA] error:nil]; |
| 27 | + GTCommit *parent = [commitEnum nextObject]; |
| 28 | + |
| 29 | + GTCommit *testCommit = [repo createCommitWithTree:testTree message:message parents:@[ parent ] updatingReferenceNamed:headReference.name error:nil]; |
| 30 | + expect(testCommit).notTo(beNil()); |
| 31 | + |
| 32 | + return testCommit; |
| 33 | +}; |
| 34 | + |
| 35 | +GTBranch *(^localBranchWithName)(NSString *, GTRepository *) = ^ GTBranch * (NSString *branchName, GTRepository *repo) { |
| 36 | + NSString *reference = [GTBranch.localNamePrefix stringByAppendingString:branchName]; |
| 37 | + NSArray *branches = [repo branchesWithPrefix:reference error:NULL]; |
| 38 | + expect(branches).notTo(beNil()); |
| 39 | + expect(@(branches.count)).to(equal(@1)); |
| 40 | + expect(((GTBranch *)branches[0]).shortName).to(equal(branchName)); |
| 41 | + |
| 42 | + return branches[0]; |
| 43 | +}; |
| 44 | + |
| 45 | +#pragma mark - GTRemotePushSpec |
| 46 | + |
| 47 | +QuickSpecBegin(GTRemotePushSpec) |
| 48 | + |
| 49 | +describe(@"pushing", ^{ |
| 50 | + __block GTRepository *notBareRepo; |
| 51 | + |
| 52 | + beforeEach(^{ |
| 53 | + notBareRepo = self.bareFixtureRepository; |
| 54 | + expect(notBareRepo).notTo(beNil()); |
| 55 | + // This repo is not really "bare" according to libgit2 |
| 56 | + expect(@(notBareRepo.isBare)).to(beFalsy()); |
| 57 | + }); |
| 58 | + |
| 59 | + describe(@"to remote", ^{ // via local transport |
| 60 | + __block NSURL *remoteRepoURL; |
| 61 | + __block NSURL *localRepoURL; |
| 62 | + __block GTRepository *remoteRepo; |
| 63 | + __block GTRepository *localRepo; |
| 64 | + __block GTRemote *remote; |
| 65 | + __block NSError *error; |
| 66 | + |
| 67 | + beforeEach(^{ |
| 68 | + // Make a bare clone to serve as the remote |
| 69 | + remoteRepoURL = [notBareRepo.gitDirectoryURL.URLByDeletingLastPathComponent URLByAppendingPathComponent:@"bare_remote_repo.git"]; |
| 70 | + NSDictionary *options = @{ GTRepositoryCloneOptionsBare: @1 }; |
| 71 | + remoteRepo = [GTRepository cloneFromURL:notBareRepo.gitDirectoryURL toWorkingDirectory:remoteRepoURL options:options error:&error transferProgressBlock:NULL checkoutProgressBlock:NULL]; |
| 72 | + expect(error).to(beNil()); |
| 73 | + expect(remoteRepo).notTo(beNil()); |
| 74 | + expect(@(remoteRepo.isBare)).to(beTruthy()); // that's better |
| 75 | + |
| 76 | + localRepoURL = [remoteRepoURL.URLByDeletingLastPathComponent URLByAppendingPathComponent:@"local_push_repo"]; |
| 77 | + expect(localRepoURL).notTo(beNil()); |
| 78 | + |
| 79 | + // Local clone for testing pushes |
| 80 | + localRepo = [GTRepository cloneFromURL:remoteRepoURL toWorkingDirectory:localRepoURL options:nil error:&error transferProgressBlock:NULL checkoutProgressBlock:NULL]; |
| 81 | + |
| 82 | + expect(error).to(beNil()); |
| 83 | + expect(localRepo).notTo(beNil()); |
| 84 | + |
| 85 | + GTConfiguration *configuration = [localRepo configurationWithError:&error]; |
| 86 | + expect(error).to(beNil()); |
| 87 | + expect(configuration).notTo(beNil()); |
| 88 | + |
| 89 | + expect(@(configuration.remotes.count)).to(equal(@1)); |
| 90 | + |
| 91 | + remote = configuration.remotes[0]; |
| 92 | + expect(remote.name).to(equal(@"origin")); |
| 93 | + }); |
| 94 | + |
| 95 | + afterEach(^{ |
| 96 | + [NSFileManager.defaultManager removeItemAtURL:remoteRepoURL error:&error]; |
| 97 | + expect(error).to(beNil()); |
| 98 | + [NSFileManager.defaultManager removeItemAtURL:localRepoURL error:&error]; |
| 99 | + expect(error).to(beNil()); |
| 100 | + error = NULL; |
| 101 | + }); |
| 102 | + |
| 103 | + context(@"when the local and remote branches are in sync", ^{ |
| 104 | + it(@"should push no commits", ^{ |
| 105 | + GTBranch *masterBranch = localBranchWithName(@"master", localRepo); |
| 106 | + expect(@([masterBranch numberOfCommitsWithError:NULL])).to(equal(@3)); |
| 107 | + |
| 108 | + GTBranch *remoteMasterBranch = localBranchWithName(@"master", remoteRepo); |
| 109 | + expect(@([remoteMasterBranch numberOfCommitsWithError:NULL])).to(equal(@3)); |
| 110 | + |
| 111 | + // Push |
| 112 | + __block BOOL transferProgressed = NO; |
| 113 | + BOOL result = [localRepo pushBranch:masterBranch toRemote:remote withOptions:nil error:&error progress:^(unsigned int current, unsigned int total, size_t bytes, BOOL *stop) { |
| 114 | + transferProgressed = YES; |
| 115 | + }]; |
| 116 | + expect(error).to(beNil()); |
| 117 | + expect(@(result)).to(beTruthy()); |
| 118 | + expect(@(transferProgressed)).to(beFalsy()); // Local transport doesn't currently call progress callbacks |
| 119 | + |
| 120 | + // Same number of commits after push, refresh branch first |
| 121 | + remoteMasterBranch = localBranchWithName(@"master", remoteRepo); |
| 122 | + expect(@([remoteMasterBranch numberOfCommitsWithError:NULL])).to(equal(@3)); |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + it(@"can push one commit", ^{ |
| 127 | + // Create a new commit in the local repo |
| 128 | + NSString *testData = @"Test"; |
| 129 | + NSString *fileName = @"test.txt"; |
| 130 | + GTCommit *testCommit = createCommitInRepository(@"Test commit", [testData dataUsingEncoding:NSUTF8StringEncoding], fileName, localRepo); |
| 131 | + expect(testCommit).notTo(beNil()); |
| 132 | + |
| 133 | + // Refetch master branch to ensure the commit count is accurate |
| 134 | + GTBranch *masterBranch = localBranchWithName(@"master", localRepo); |
| 135 | + expect(@([masterBranch numberOfCommitsWithError:NULL])).to(equal(@4)); |
| 136 | + |
| 137 | + // Number of commits on tracking branch before push |
| 138 | + BOOL success = NO; |
| 139 | + GTBranch *localTrackingBranch = [masterBranch trackingBranchWithError:&error success:&success]; |
| 140 | + expect(error).to(beNil()); |
| 141 | + expect(@(success)).to(beTruthy()); |
| 142 | + expect(@([localTrackingBranch numberOfCommitsWithError:NULL])).to(equal(@3)); |
| 143 | + |
| 144 | + // Number of commits on remote before push |
| 145 | + GTBranch *remoteMasterBranch = localBranchWithName(@"master", remoteRepo); |
| 146 | + expect(@([remoteMasterBranch numberOfCommitsWithError:NULL])).to(equal(@3)); |
| 147 | + |
| 148 | + // Push |
| 149 | + __block BOOL transferProgressed = NO; |
| 150 | + BOOL result = [localRepo pushBranch:masterBranch toRemote:remote withOptions:nil error:&error progress:^(unsigned int current, unsigned int total, size_t bytes, BOOL *stop) { |
| 151 | + transferProgressed = YES; |
| 152 | + }]; |
| 153 | + expect(error).to(beNil()); |
| 154 | + expect(@(result)).to(beTruthy()); |
| 155 | + expect(@(transferProgressed)).to(beFalsy()); // Local transport doesn't currently call progress callbacks |
| 156 | + |
| 157 | + // Number of commits on tracking branch after push |
| 158 | + localTrackingBranch = [masterBranch trackingBranchWithError:&error success:&success]; |
| 159 | + expect(error).to(beNil()); |
| 160 | + expect(@(success)).to(beTruthy()); |
| 161 | + expect(@([localTrackingBranch numberOfCommitsWithError:NULL])).to(equal(@4)); |
| 162 | + |
| 163 | + // Refresh remote master branch to ensure the commit count is accurate |
| 164 | + remoteMasterBranch = localBranchWithName(@"master", remoteRepo); |
| 165 | + |
| 166 | + // Number of commits in remote repo after push |
| 167 | + expect(@([remoteMasterBranch numberOfCommitsWithError:NULL])).to(equal(@4)); |
| 168 | + |
| 169 | + // Verify commit is in remote |
| 170 | + GTCommit *pushedCommit = [remoteRepo lookUpObjectByOID:testCommit.OID objectType:GTObjectTypeCommit error:&error]; |
| 171 | + expect(error).to(beNil()); |
| 172 | + expect(pushedCommit).notTo(beNil()); |
| 173 | + expect(pushedCommit.OID).to(equal(testCommit.OID)); |
| 174 | + |
| 175 | + GTTreeEntry *entry = [[pushedCommit tree] entryWithName:fileName]; |
| 176 | + expect(entry).notTo(beNil()); |
| 177 | + |
| 178 | + GTBlob *fileData = (GTBlob *)[entry GTObject:&error]; |
| 179 | + expect(error).to(beNil()); |
| 180 | + expect(fileData).notTo(beNil()); |
| 181 | + expect(fileData.content).to(equal(testData)); |
| 182 | + }); |
| 183 | + |
| 184 | + it(@"can push two branches", ^{ |
| 185 | + // refs/heads/master on local |
| 186 | + GTBranch *branch1 = localBranchWithName(@"master", localRepo); |
| 187 | + |
| 188 | + // Create refs/heads/new_master on local |
| 189 | + [localRepo createReferenceNamed:@"refs/heads/new_master" fromReference:branch1.reference committer:localRepo.userSignatureForNow message:@"Create new_master branch" error:&error]; |
| 190 | + GTBranch *branch2 = localBranchWithName(@"new_master", localRepo); |
| 191 | + |
| 192 | + BOOL result = [localRepo pushBranches:@[ branch1, branch2 ] toRemote:remote withOptions:nil error:&error progress:NULL]; |
| 193 | + expect(error).to(beNil()); |
| 194 | + expect(@(result)).to(beTruthy()); |
| 195 | + }); |
| 196 | + }); |
| 197 | + |
| 198 | +}); |
| 199 | + |
| 200 | +QuickSpecEnd |
0 commit comments