@@ -124,19 +124,23 @@ - (NSString *)remoteName {
124124}
125125
126126- (GTCommit *)targetCommitWithError : (NSError **)error {
127- if (self.OID == nil ) {
127+ GTOID *oid = self.OID ;
128+ if (oid == nil ) {
128129 if (error != NULL ) *error = GTReference.invalidReferenceError ;
129130 return nil ;
130131 }
131132
132- return [self .repository lookUpObjectByOID: self .OID objectType: GTObjectTypeCommit error: error];
133+ return [self .repository lookUpObjectByOID: oid objectType: GTObjectTypeCommit error: error];
133134}
134135
135136- (NSUInteger )numberOfCommitsWithError : (NSError **)error {
136137 GTEnumerator *enumerator = [[GTEnumerator alloc ] initWithRepository: self .repository error: error];
137138 if (enumerator == nil ) return NSNotFound ;
138139
139- if (![enumerator pushSHA: self .OID.SHA error: error]) return NSNotFound ;
140+ GTOID *oid = self.OID ;
141+ if (oid == nil ) return NSNotFound ;
142+
143+ if (![enumerator pushSHA: oid.SHA error: error]) return NSNotFound ;
140144 return [enumerator countRemainingObjects: error];
141145}
142146
@@ -157,7 +161,9 @@ - (BOOL)isHEAD {
157161}
158162
159163- (NSArray *)uniqueCommitsRelativeToBranch : (GTBranch *)otherBranch error : (NSError **)error {
160- GTEnumerator *enumerator = [self .repository enumeratorForUniqueCommitsFromOID: self .OID relativeToOID: otherBranch.OID error: error];
164+ GTOID *oid = self.OID ;
165+ GTOID *otherOID = otherBranch.OID ;
166+ GTEnumerator *enumerator = [self .repository enumeratorForUniqueCommitsFromOID: oid relativeToOID: otherOID error: error];
161167 return [enumerator allObjectsWithError: error];
162168}
163169
@@ -179,14 +185,21 @@ - (BOOL)rename:(NSString *)name force:(BOOL)force error:(NSError **)error {
179185 return NO ;
180186 }
181187
182- _reference = [[GTReference alloc ] initWithGitReference: git_ref repository: self .repository];
188+ GTReference *renamedRef = [[GTReference alloc ] initWithGitReference: git_ref repository: self .repository];
189+ NSAssert (renamedRef, @" Unable to allocate renamed ref" );
190+ _reference = renamedRef;
183191
184192 return YES ;
185193}
186194
187195- (GTBranch *)trackingBranchWithError : (NSError **)error success : (BOOL *)success {
196+ BOOL underSuccess = NO ;
197+ if (success == NULL ) {
198+ success = &underSuccess;
199+ }
200+
188201 if (self.branchType == GTBranchTypeRemote) {
189- if (success != NULL ) *success = YES ;
202+ *success = YES ;
190203 return self;
191204 }
192205
@@ -195,25 +208,32 @@ - (GTBranch *)trackingBranchWithError:(NSError **)error success:(BOOL *)success
195208
196209 // GIT_ENOTFOUND means no tracking branch found.
197210 if (gitError == GIT_ENOTFOUND) {
198- if (success != NULL ) *success = YES ;
211+ *success = YES ;
199212 return nil ;
200213 }
201214
202215 if (gitError != GIT_OK) {
203- if (success != NULL ) *success = NO ;
216+ *success = NO ;
204217 if (error != NULL ) *error = [NSError git_errorFor: gitError description: @" Failed to create reference to tracking branch from %@ " , self ];
205218 return nil ;
206219 }
207220
208221 if (trackingRef == NULL ) {
209- if (success != NULL ) *success = NO ;
222+ *success = NO ;
210223 if (error != NULL ) *error = [NSError git_errorFor: gitError description: @" Got a NULL remote ref for %@ " , self ];
211224 return nil ;
212225 }
213226
214- if (success != NULL ) *success = YES ;
227+ GTReference *upsteamRef = [[GTReference alloc ] initWithGitReference: trackingRef repository: self .repository];
228+ if (upsteamRef == nil ) {
229+ *success = NO ;
230+ if (error != NULL ) *error = [NSError git_errorFor: GIT_ERROR description: @" Failed to allocate upstream ref" ];
231+ return nil ;
232+ }
233+
234+ *success = YES ;
215235
216- return [[self class ] branchWithReference: [[GTReference alloc ] initWithGitReference: trackingRef repository: self .repository] ];
236+ return [[self class ] branchWithReference: upsteamRef ];
217237}
218238
219239- (BOOL )updateTrackingBranch : (GTBranch *)trackingBranch error : (NSError **)error {
@@ -239,7 +259,9 @@ - (GTBranch *)reloadedBranchWithError:(NSError **)error {
239259}
240260
241261- (BOOL )calculateAhead : (size_t *)ahead behind : (size_t *)behind relativeTo : (GTBranch *)branch error : (NSError **)error {
242- return [self .repository calculateAhead: ahead behind: behind ofOID: self .OID relativeToOID: branch.OID error: error];
262+ GTOID *oid = self.OID ;
263+ GTOID *branchOID = branch.OID ;
264+ return [self .repository calculateAhead: ahead behind: behind ofOID: oid relativeToOID: branchOID error: error];
243265}
244266
245267@end
0 commit comments