Skip to content

Commit d1cc35d

Browse files
committed
Add ahead/behind count and enumeration over OIDs
1 parent 3b5e04d commit d1cc35d

File tree

3 files changed

+46
-29
lines changed

3 files changed

+46
-29
lines changed

ObjectiveGit/GTBranch.m

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -146,22 +146,7 @@ - (GTBranchType)branchType {
146146
}
147147

148148
- (NSArray *)uniqueCommitsRelativeToBranch:(GTBranch *)otherBranch error:(NSError **)error {
149-
NSParameterAssert(otherBranch != nil);
150-
151-
GTCommit *mergeBase = [self.repository mergeBaseBetweenFirstOID:self.reference.OID secondOID:otherBranch.reference.OID error:error];
152-
if (mergeBase == nil) return nil;
153-
154-
GTEnumerator *enumerator = [[GTEnumerator alloc] initWithRepository:self.repository error:error];
155-
if (enumerator == nil) return nil;
156-
157-
[enumerator resetWithOptions:GTEnumeratorOptionsTimeSort];
158-
159-
BOOL success = [enumerator pushSHA:self.OID.SHA error:error];
160-
if (!success) return nil;
161-
162-
success = [enumerator hideSHA:mergeBase.OID.SHA error:error];
163-
if (!success) return nil;
164-
149+
GTEnumerator *enumerator = [self.repository enumerateUniqueCommitsUpToOID:self.OID relativeToOID:otherBranch.OID error:error];
165150
return [enumerator allObjectsWithError:error];
166151
}
167152

@@ -225,19 +210,7 @@ - (GTBranch *)reloadedBranchWithError:(NSError **)error {
225210
}
226211

227212
- (BOOL)calculateAhead:(size_t *)ahead behind:(size_t *)behind relativeTo:(GTBranch *)branch error:(NSError **)error {
228-
if (branch == nil) {
229-
*ahead = 0;
230-
*behind = 0;
231-
return YES;
232-
}
233-
234-
int errorCode = git_graph_ahead_behind(ahead, behind, self.repository.git_repository, self.reference.git_oid, branch.reference.git_oid);
235-
if (errorCode != GIT_OK && error != NULL) {
236-
*error = [NSError git_errorFor:errorCode description:@"Failed to calculate ahead/behind count of %@ relative to %@", self, branch];
237-
return NO;
238-
}
239-
240-
return YES;
213+
return [self.repository calculateAhead:ahead behind:behind ofOID:self.OID relativeToOID:branch.OID error:error];
241214
}
242215

243216
@end

ObjectiveGit/GTRepository.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,4 +498,16 @@ extern NSString * const GTRepositoryInitOptionsOriginURLString;
498498
/// distinguished using the value of `success`.
499499
- (GTFilterList *)filterListWithPath:(NSString *)path blob:(GTBlob *)blob mode:(GTFilterSourceMode)mode options:(GTFilterListOptions)options success:(BOOL *)success error:(NSError **)error;
500500

501+
/// Creates an enumerator for finding all commits in the history of `headOID`
502+
/// that do not exist in the history of `baseOID`.
503+
///
504+
/// Returns the created enumerator upon success, or `nil` if an error occurred.
505+
- (GTEnumerator *)enumerateUniqueCommitsUpToOID:(GTOID *)headOID relativeToOID:(GTOID *)baseOID error:(NSError **)error;
506+
507+
/// Calculates how far ahead/behind the commit represented by `headOID` is,
508+
/// relative to the commit represented by `baseOID`.
509+
///
510+
/// Returns whether `ahead` and `behind` were successfully calculated.
511+
- (BOOL)calculateAhead:(size_t *)ahead behind:(size_t *)behind ofOID:(GTOID *)headOID relativeToOID:(GTOID *)baseOID error:(NSError **)error;
512+
501513
@end

ObjectiveGit/GTRepository.m

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,4 +885,36 @@ - (GTFilterList *)filterListWithPath:(NSString *)path blob:(GTBlob *)blob mode:(
885885
}
886886
}
887887

888+
- (GTEnumerator *)enumerateUniqueCommitsUpToOID:(GTOID *)headOID relativeToOID:(GTOID *)baseOID error:(NSError **)error {
889+
NSParameterAssert(headOID != nil);
890+
NSParameterAssert(baseOID != nil);
891+
892+
GTCommit *mergeBase = [self mergeBaseBetweenFirstOID:headOID secondOID:baseOID error:error];
893+
if (mergeBase == nil) return nil;
894+
895+
GTEnumerator *enumerator = [[GTEnumerator alloc] initWithRepository:self error:error];
896+
if (enumerator == nil) return nil;
897+
898+
[enumerator resetWithOptions:GTEnumeratorOptionsTimeSort];
899+
900+
if (![enumerator pushSHA:headOID.SHA error:error]) return nil;
901+
if (![enumerator hideSHA:mergeBase.OID.SHA error:error]) return nil;
902+
903+
return enumerator;
904+
}
905+
906+
- (BOOL)calculateAhead:(size_t *)ahead behind:(size_t *)behind ofOID:(GTOID *)headOID relativeToOID:(GTOID *)baseOID error:(NSError **)error {
907+
NSParameterAssert(headOID != nil);
908+
NSParameterAssert(baseOID != nil);
909+
910+
int errorCode = git_graph_ahead_behind(ahead, behind, self.git_repository, headOID.git_oid, baseOID.git_oid);
911+
if (errorCode != GIT_OK) {
912+
if (error != NULL) *error = [NSError git_errorFor:errorCode description:@"Failed to calculate ahead/behind count of %@ relative to %@", headOID, baseOID];
913+
914+
return NO;
915+
}
916+
917+
return YES;
918+
}
919+
888920
@end

0 commit comments

Comments
 (0)