|
12 | 12 |
|
13 | 13 | #import "QuickSpec+GTFixtures.h" |
14 | 14 |
|
| 15 | +// Helper to quickly create commits |
| 16 | +GTCommit *(^createCommitInRepository)(NSString *, NSData *, NSString *, GTRepository *) = ^(NSString *message, NSData *fileData, NSString *fileName, GTRepository *repo) { |
| 17 | + GTTreeBuilder *treeBuilder = [[GTTreeBuilder alloc] initWithTree:nil error:nil]; |
| 18 | + [treeBuilder addEntryWithData:fileData fileName:fileName fileMode:GTFileModeBlob error:nil]; |
| 19 | + |
| 20 | + GTTree *testTree = [treeBuilder writeTreeToRepository:repo error: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 | +#pragma mark - GTRemotePushSpec |
| 36 | + |
15 | 37 | QuickSpecBegin(GTRemotePushSpec) |
16 | 38 |
|
17 | | -describe(@"push to remote", ^{ |
| 39 | +describe(@"pushing", ^{ |
18 | 40 | __block GTRepository *localRepo; |
19 | 41 | __block GTRepository *remoteRepo; |
| 42 | + __block GTRepository *notBareRepo; |
20 | 43 | __block GTRemote *remote; |
21 | | - __block NSURL *notBareRepoURL; |
22 | | - __block NSURL *remoteRepoFileURL; |
| 44 | + __block NSURL *remoteRepoURL; |
23 | 45 | __block NSURL *localRepoURL; |
24 | 46 | __block GTBranch *masterBranch; |
25 | 47 | __block GTBranch *remoteMasterBranch; |
| 48 | + __block NSError *error = nil; |
26 | 49 |
|
27 | 50 | beforeEach(^{ |
28 | | - NSError *error = nil; |
29 | | - |
30 | 51 | // This repo is not really "bare" |
31 | | - GTRepository *notBareRepo = self.bareFixtureRepository; |
| 52 | + notBareRepo = self.bareFixtureRepository; |
32 | 53 | expect(notBareRepo).notTo(beNil()); |
33 | 54 | expect(@(notBareRepo.isBare)).to(beFalse()); |
34 | | - |
35 | | - // Make a bare clone to serve as the remote |
36 | | - notBareRepoURL = [notBareRepo.gitDirectoryURL.URLByDeletingLastPathComponent URLByAppendingPathComponent:@"barerepo.git"]; |
37 | | - NSDictionary *options = @{ GTRepositoryCloneOptionsBare: @(1) }; |
38 | | - remoteRepo = [GTRepository cloneFromURL:notBareRepo.gitDirectoryURL toWorkingDirectory:notBareRepoURL options:options error:&error transferProgressBlock:NULL checkoutProgressBlock:NULL]; |
39 | | - expect(error).to(beNil()); |
40 | | - expect(remoteRepo).notTo(beNil()); |
41 | | - expect(@(remoteRepo.isBare)).to(beTruthy()); // that's better |
42 | | - |
43 | | - NSArray *remoteBranches = [remoteRepo localBranchesWithError:&error]; |
44 | | - expect(error).to(beNil()); |
45 | | - expect(remoteBranches).notTo(beNil()); |
46 | | - expect(@(remoteBranches.count)).to(beGreaterThanOrEqualTo(@1)); |
47 | | - |
48 | | - remoteMasterBranch = remoteBranches[0]; |
49 | | - expect(@([remoteMasterBranch numberOfCommitsWithError:NULL])).to(equal(@3)); |
50 | | - |
51 | | - NSURL *remoteRepoFileURL = remoteRepo.gitDirectoryURL; |
52 | | - expect(remoteRepoFileURL).notTo(beNil()); |
53 | | - NSURL *localRepoURL = [remoteRepoFileURL.URLByDeletingLastPathComponent URLByAppendingPathComponent:@"localpushrepo"]; |
54 | | - expect(localRepoURL).notTo(beNil()); |
55 | | - |
56 | | - // Ensure repo destination is clear before clone |
57 | | - [NSFileManager.defaultManager removeItemAtURL:localRepoURL error:NULL]; |
58 | | - |
59 | | - // Local clone for testing pushes |
60 | | - localRepo = [GTRepository cloneFromURL:remoteRepoFileURL toWorkingDirectory:localRepoURL options:nil error:&error transferProgressBlock:NULL checkoutProgressBlock:NULL]; |
61 | | - |
62 | | - expect(error).to(beNil()); |
63 | | - expect(localRepo).notTo(beNil()); |
64 | | - |
65 | | - GTConfiguration *configuration = [localRepo configurationWithError:&error]; |
66 | | - expect(error).to(beNil()); |
67 | | - expect(configuration).notTo(beNil()); |
68 | | - |
69 | | - expect(@(configuration.remotes.count)).to(equal(@1)); |
70 | | - |
71 | | - remote = configuration.remotes[0]; |
72 | | - expect(remote.name).to(equal(@"origin")); |
73 | | - |
74 | | - NSArray *branches = [localRepo localBranchesWithError:&error]; |
75 | | - expect(error).to(beNil()); |
76 | | - expect(branches).notTo(beNil()); |
77 | | - expect(@(branches.count)).to(beGreaterThanOrEqualTo(@1)); |
78 | | - |
79 | | - masterBranch = branches[0]; |
80 | | - expect(masterBranch.shortName).to(equal(@"master")); |
81 | | - expect(@([masterBranch numberOfCommitsWithError:NULL])).to(equal(@3)); |
82 | 55 | }); |
83 | 56 |
|
84 | | - afterEach(^{ |
85 | | - [NSFileManager.defaultManager removeItemAtURL:notBareRepoURL error:NULL]; |
86 | | - [NSFileManager.defaultManager removeItemAtURL:remoteRepoFileURL error:NULL]; |
87 | | - [NSFileManager.defaultManager removeItemAtURL:localRepoURL error:NULL]; |
88 | | - }); |
89 | | - |
90 | | - // Helper to quickly create commits |
91 | | - GTCommit *(^createCommitInRepository)(NSString *, NSData *, NSString *, GTRepository *) = ^(NSString *message, NSData *fileData, NSString *fileName, GTRepository *repo) { |
92 | | - GTTreeBuilder *treeBuilder = [[GTTreeBuilder alloc] initWithTree:nil error:nil]; |
93 | | - [treeBuilder addEntryWithData:fileData fileName:fileName fileMode:GTFileModeBlob error:nil]; |
94 | | - |
95 | | - GTTree *testTree = [treeBuilder writeTreeToRepository:repo error:nil]; |
96 | | - |
97 | | - // We need the parent commit to make the new one |
98 | | - GTReference *headReference = [repo headReferenceWithError:nil]; |
99 | | - |
100 | | - GTEnumerator *commitEnum = [[GTEnumerator alloc] initWithRepository:repo error:nil]; |
101 | | - [commitEnum pushSHA:[headReference targetSHA] error:nil]; |
102 | | - GTCommit *parent = [commitEnum nextObject]; |
103 | | - |
104 | | - GTCommit *testCommit = [repo createCommitWithTree:testTree message:message parents:@[parent] updatingReferenceNamed:headReference.name error:nil]; |
105 | | - expect(testCommit).notTo(beNil()); |
106 | | - |
107 | | - return testCommit; |
108 | | - }; |
109 | | - |
110 | | - describe(@"-pushBranch:toRemote:withOptions:error:progress:", ^{ |
111 | | - it(@"pushes nothing when the branch on local and remote are in sync", ^{ |
112 | | - NSError *error = nil; |
113 | | - |
114 | | - expect(@([remoteMasterBranch numberOfCommitsWithError:NULL])).to(equal(@3)); |
115 | | - |
116 | | - __block BOOL transferProgressed = NO; |
117 | | - BOOL result = [localRepo pushBranch:masterBranch toRemote:remote withOptions:nil error:&error progress:^(unsigned int current, unsigned int total, size_t bytes, BOOL *stop) { |
118 | | - transferProgressed = YES; |
119 | | - }]; |
| 57 | + describe(@"to remote", ^{ // via local transport |
| 58 | + beforeEach(^{ |
| 59 | + // Make a bare clone to serve as the remote |
| 60 | + remoteRepoURL = [notBareRepo.gitDirectoryURL.URLByDeletingLastPathComponent URLByAppendingPathComponent:@"bare_remote_repo.git"]; |
| 61 | + NSDictionary *options = @{ GTRepositoryCloneOptionsBare: @1 }; |
| 62 | + remoteRepo = [GTRepository cloneFromURL:notBareRepo.gitDirectoryURL toWorkingDirectory:remoteRepoURL options:options error:&error transferProgressBlock:NULL checkoutProgressBlock:NULL]; |
120 | 63 | expect(error).to(beNil()); |
121 | | - expect(@(result)).to(beTruthy()); |
122 | | - expect(@(transferProgressed)).to(beFalse()); // Local transport doesn't currently call progress callbacks |
| 64 | + expect(remoteRepo).notTo(beNil()); |
| 65 | + expect(@(remoteRepo.isBare)).to(beTruthy()); // that's better |
123 | 66 |
|
124 | | - // Same number of commits after push |
| 67 | + // Get the remote master branch |
| 68 | + NSArray *remoteBranches = [remoteRepo localBranchesWithError:&error]; |
| 69 | + expect(error).to(beNil()); |
| 70 | + expect(remoteBranches).notTo(beNil()); |
| 71 | + expect(@(remoteBranches.count)).to(beGreaterThanOrEqualTo(@1)); |
| 72 | + remoteMasterBranch = remoteBranches[0]; |
125 | 73 | expect(@([remoteMasterBranch numberOfCommitsWithError:NULL])).to(equal(@3)); |
126 | | - }); |
127 | 74 |
|
128 | | - it(@"pushes a new local commit to the remote", ^{ |
129 | | - NSError *error = nil; |
| 75 | + NSURL *localRepoURL = [remoteRepoURL.URLByDeletingLastPathComponent URLByAppendingPathComponent:@"local_push_repo"]; |
| 76 | + expect(localRepoURL).notTo(beNil()); |
130 | 77 |
|
131 | | - // Create a new commit in the master repo |
132 | | - NSString *testData = @"Test"; |
133 | | - NSString *fileName = @"test.txt"; |
134 | | - GTCommit *testCommit = createCommitInRepository(@"Test commit", [testData dataUsingEncoding:NSUTF8StringEncoding], fileName, localRepo); |
135 | | - expect(testCommit).notTo(beNil()); |
| 78 | + // Ensure repo destination is clear before clone |
| 79 | + [NSFileManager.defaultManager removeItemAtURL:localRepoURL error:NULL]; |
136 | 80 |
|
137 | | - // Refetch master branch to ensure the commit count is accurate |
138 | | - masterBranch = [localRepo localBranchesWithError:NULL][0]; |
139 | | - expect(@([masterBranch numberOfCommitsWithError:NULL])).to(equal(@4)); |
| 81 | + // Local clone for testing pushes |
| 82 | + localRepo = [GTRepository cloneFromURL:remoteRepoURL toWorkingDirectory:localRepoURL options:nil error:&error transferProgressBlock:NULL checkoutProgressBlock:NULL]; |
140 | 83 |
|
141 | | - // Number of commits on remote before push |
142 | | - expect(@([remoteMasterBranch numberOfCommitsWithError:NULL])).to(equal(@3)); |
| 84 | + expect(error).to(beNil()); |
| 85 | + expect(localRepo).notTo(beNil()); |
143 | 86 |
|
144 | | - // Push |
145 | | - __block BOOL transferProgressed = NO; |
146 | | - BOOL result = [localRepo pushBranch:masterBranch toRemote:remote withOptions:nil error:&error progress:^(unsigned int current, unsigned int total, size_t bytes, BOOL *stop) { |
147 | | - transferProgressed = YES; |
148 | | - }]; |
| 87 | + GTConfiguration *configuration = [localRepo configurationWithError:&error]; |
149 | 88 | expect(error).to(beNil()); |
150 | | - expect(@(result)).to(beTruthy()); |
151 | | - expect(@(transferProgressed)).to(beFalse()); // Local transport doesn't currently call progress callbacks |
| 89 | + expect(configuration).notTo(beNil()); |
152 | 90 |
|
153 | | - // Refetch master branch to ensure the commit count is accurate |
154 | | - remoteMasterBranch = [remoteRepo localBranchesWithError:NULL][0]; |
| 91 | + expect(@(configuration.remotes.count)).to(equal(@1)); |
155 | 92 |
|
156 | | - // Number of commits on remote after push |
157 | | - expect(@([remoteMasterBranch numberOfCommitsWithError:NULL])).to(equal(@4)); |
| 93 | + remote = configuration.remotes[0]; |
| 94 | + expect(remote.name).to(equal(@"origin")); |
158 | 95 |
|
159 | | - // Verify commit is in remote |
160 | | - GTCommit *pushedCommit = [remoteRepo lookUpObjectByOID:testCommit.OID objectType:GTObjectTypeCommit error:&error]; |
| 96 | + NSArray *branches = [localRepo localBranchesWithError:&error]; |
161 | 97 | expect(error).to(beNil()); |
162 | | - expect(pushedCommit).notTo(beNil()); |
163 | | - expect(pushedCommit.OID).to(equal(testCommit.OID)); |
| 98 | + expect(branches).notTo(beNil()); |
| 99 | + expect(@(branches.count)).to(beGreaterThanOrEqualTo(@1)); |
| 100 | + |
| 101 | + masterBranch = branches[0]; |
| 102 | + expect(masterBranch.shortName).to(equal(@"master")); |
| 103 | + expect(@([masterBranch numberOfCommitsWithError:NULL])).to(equal(@3)); |
| 104 | + }); |
164 | 105 |
|
165 | | - GTTreeEntry *entry = [[pushedCommit tree] entryWithName:fileName]; |
166 | | - expect(entry).notTo(beNil()); |
| 106 | + afterEach(^{ |
| 107 | + [NSFileManager.defaultManager removeItemAtURL:remoteRepoURL error:NULL]; |
| 108 | + [NSFileManager.defaultManager removeItemAtURL:localRepoURL error:NULL]; |
| 109 | + }); |
167 | 110 |
|
168 | | - GTBlob *fileData = (GTBlob *)[entry GTObject:&error]; |
169 | | - expect(error).to(beNil()); |
170 | | - expect(fileData).notTo(beNil()); |
171 | | - expect(fileData.content).to(equal(testData)); |
| 111 | + context(@"when the local and remote branches are in sync", ^{ |
| 112 | + it(@"should push no commits", ^{ |
| 113 | + expect(@([remoteMasterBranch numberOfCommitsWithError:NULL])).to(equal(@3)); |
| 114 | + |
| 115 | + // Push |
| 116 | + __block BOOL transferProgressed = NO; |
| 117 | + BOOL result = [localRepo pushBranch:masterBranch toRemote:remote withOptions:nil error:&error progress:^(unsigned int current, unsigned int total, size_t bytes, BOOL *stop) { |
| 118 | + transferProgressed = YES; |
| 119 | + }]; |
| 120 | + expect(error).to(beNil()); |
| 121 | + expect(@(result)).to(beTruthy()); |
| 122 | + expect(@(transferProgressed)).to(beFalse()); // Local transport doesn't currently call progress callbacks |
| 123 | + |
| 124 | + // Same number of commits after push |
| 125 | + expect(@([remoteMasterBranch numberOfCommitsWithError:NULL])).to(equal(@3)); |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | + context(@"when there is a new local commit", ^{ |
| 130 | + it(@"should push one commit", ^{ |
| 131 | + // Create a new commit in the master repo |
| 132 | + NSString *testData = @"Test"; |
| 133 | + NSString *fileName = @"test.txt"; |
| 134 | + GTCommit *testCommit = createCommitInRepository(@"Test commit", [testData dataUsingEncoding:NSUTF8StringEncoding], fileName, localRepo); |
| 135 | + expect(testCommit).notTo(beNil()); |
| 136 | + |
| 137 | + // Refetch master branch to ensure the commit count is accurate |
| 138 | + masterBranch = [localRepo localBranchesWithError:NULL][0]; |
| 139 | + expect(@([masterBranch numberOfCommitsWithError:NULL])).to(equal(@4)); |
| 140 | + |
| 141 | + // Number of commits on remote before push |
| 142 | + expect(@([remoteMasterBranch numberOfCommitsWithError:NULL])).to(equal(@3)); |
| 143 | + |
| 144 | + // Push |
| 145 | + __block BOOL transferProgressed = NO; |
| 146 | + BOOL result = [localRepo pushBranch:masterBranch toRemote:remote withOptions:nil error:&error progress:^(unsigned int current, unsigned int total, size_t bytes, BOOL *stop) { |
| 147 | + transferProgressed = YES; |
| 148 | + }]; |
| 149 | + expect(error).to(beNil()); |
| 150 | + expect(@(result)).to(beTruthy()); |
| 151 | + expect(@(transferProgressed)).to(beFalse()); // Local transport doesn't currently call progress callbacks |
| 152 | + |
| 153 | + // Refetch master branch to ensure the commit count is accurate |
| 154 | + remoteMasterBranch = [remoteRepo localBranchesWithError:NULL][0]; |
| 155 | + |
| 156 | + // Number of commits on remote after push |
| 157 | + expect(@([remoteMasterBranch numberOfCommitsWithError:NULL])).to(equal(@4)); |
| 158 | + |
| 159 | + // Verify commit is in remote |
| 160 | + GTCommit *pushedCommit = [remoteRepo lookUpObjectByOID:testCommit.OID objectType:GTObjectTypeCommit error:&error]; |
| 161 | + expect(error).to(beNil()); |
| 162 | + expect(pushedCommit).notTo(beNil()); |
| 163 | + expect(pushedCommit.OID).to(equal(testCommit.OID)); |
| 164 | + |
| 165 | + GTTreeEntry *entry = [[pushedCommit tree] entryWithName:fileName]; |
| 166 | + expect(entry).notTo(beNil()); |
| 167 | + |
| 168 | + GTBlob *fileData = (GTBlob *)[entry GTObject:&error]; |
| 169 | + expect(error).to(beNil()); |
| 170 | + expect(fileData).notTo(beNil()); |
| 171 | + expect(fileData.content).to(equal(testData)); |
| 172 | + }); |
172 | 173 | }); |
173 | 174 | }); |
174 | 175 |
|
|
0 commit comments