Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 126b394

Browse files
committed
Reorganize GTBranch.
1 parent c6720b3 commit 126b394

File tree

2 files changed

+86
-72
lines changed

2 files changed

+86
-72
lines changed

Classes/GTBranch.h

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@ typedef enum {
3636

3737
@interface GTBranch : NSObject
3838

39+
@property (nonatomic, readonly, strong) GTRepository *repository;
40+
@property (nonatomic, readonly, strong) GTReference *reference;
3941
@property (nonatomic, readonly) NSString *name;
4042
@property (nonatomic, readonly) NSString *shortName;
41-
@property (nonatomic, readonly) NSString *SHA;
4243
@property (nonatomic, readonly) NSString *remoteName;
44+
@property (nonatomic, readonly) NSString *SHA;
4345
@property (nonatomic, readonly) GTBranchType branchType;
44-
@property (nonatomic, readonly, strong) GTRepository *repository;
45-
@property (nonatomic, readonly, strong) GTReference *reference;
46+
@property (nonatomic) GTBranch *upstreamBranch;
4647

4748
+ (NSString *)localNamePrefix;
4849
+ (NSString *)remoteNamePrefix;
@@ -61,22 +62,9 @@ typedef enum {
6162
// returns a GTCommit object or nil if an error occurred
6263
- (GTCommit *)targetCommitAndReturnError:(NSError **)error;
6364

64-
// Count all commits in this branch
65-
//
66-
// error(out) - will be filled if an error occurs
67-
//
68-
// returns number of commits in the branch or NSNotFound if an error occurred
69-
- (NSUInteger)numberOfCommitsWithError:(NSError **)error;
70-
71-
- (NSArray *)uniqueCommitsRelativeToBranch:(GTBranch *)otherBranch error:(NSError **)error;
72-
7365
// Deletes the local branch and nils out the reference.
7466
- (BOOL)deleteWithError:(NSError **)error;
7567

76-
// If the receiver is a local branch, looks up and returns its tracking branch.
77-
// If the receiver is a remote branch, returns self. If no tracking branch was
78-
// found, returns nil and sets `success` to YES.
79-
- (GTBranch *)trackingBranchWithError:(NSError **)error success:(BOOL *)success;
8068

8169
// Reloads the branch's reference and creates a new branch based off that newly
8270
// loaded reference.
@@ -88,6 +76,20 @@ typedef enum {
8876
// Returns the reloaded branch, or nil if an error occurred.
8977
- (GTBranch *)reloadedBranchWithError:(NSError **)error;
9078

79+
// If the receiver is a local branch, looks up and returns its tracking branch.
80+
// If the receiver is a remote branch, returns self. If no tracking branch was
81+
// found, returns nil and sets `success` to YES.
82+
- (GTBranch *)trackingBranchWithError:(NSError **)error success:(BOOL *)success;
83+
84+
// Count all commits in this branch
85+
//
86+
// error(out) - will be filled if an error occurs
87+
//
88+
// returns number of commits in the branch or NSNotFound if an error occurred
89+
- (NSUInteger)numberOfCommitsWithError:(NSError **)error;
90+
91+
- (NSArray *)uniqueCommitsRelativeToBranch:(GTBranch *)otherBranch error:(NSError **)error;
92+
9193
// Calculate the ahead/behind count from this branch to the given branch.
9294
//
9395
// ahead - The number of commits which are unique to the receiver. Cannot be

Classes/GTBranch.m

Lines changed: 68 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,8 @@
3232

3333
@implementation GTBranch
3434

35-
- (NSString *)description {
36-
return [NSString stringWithFormat:@"<%@: %p> name: %@, shortName: %@, sha: %@, remoteName: %@, repository: %@", NSStringFromClass([self class]), self, self.name, self.shortName, self.SHA, self.remoteName, self.repository];
37-
}
38-
39-
- (BOOL)isEqual:(GTBranch *)otherBranch {
40-
if (otherBranch == self) return YES;
41-
if (![otherBranch isKindOfClass:self.class]) return NO;
42-
43-
return [self.name isEqual:otherBranch.name] && [self.SHA isEqual:otherBranch.SHA];
44-
}
45-
46-
- (NSUInteger)hash {
47-
return self.name.hash ^ self.SHA.hash;
48-
}
49-
50-
51-
#pragma mark API
35+
#pragma mark -
36+
#pragma mark Class methods
5237

5338
+ (NSString *)localNamePrefix {
5439
return @"refs/heads/";
@@ -58,6 +43,9 @@ + (NSString *)remoteNamePrefix {
5843
return @"refs/remotes/";
5944
}
6045

46+
#pragma mark -
47+
#pragma mark Lifecycle
48+
6149
+ (id)branchWithName:(NSString *)branchName repository:(GTRepository *)repo error:(NSError **)error {
6250
return [[self alloc] initWithName:branchName repository:repo error:error];
6351
}
@@ -89,6 +77,27 @@ - (id)initWithReference:(GTReference *)ref repository:(GTRepository *)repo {
8977
return self;
9078
}
9179

80+
#pragma mark -
81+
#pragma mark NSObject
82+
83+
- (NSString *)description {
84+
return [NSString stringWithFormat:@"<%@: %p> name: %@, shortName: %@, sha: %@, remoteName: %@, repository: %@", NSStringFromClass([self class]), self, self.name, self.shortName, self.SHA, self.remoteName, self.repository];
85+
}
86+
87+
- (BOOL)isEqual:(GTBranch *)otherBranch {
88+
if (otherBranch == self) return YES;
89+
if (![otherBranch isKindOfClass:self.class]) return NO;
90+
91+
return [self.name isEqual:otherBranch.name] && [self.SHA isEqual:otherBranch.SHA];
92+
}
93+
94+
- (NSUInteger)hash {
95+
return self.name.hash ^ self.SHA.hash;
96+
}
97+
98+
#pragma mark -
99+
#pragma mark Properties
100+
92101
- (NSString *)name {
93102
return self.reference.name;
94103
}
@@ -109,10 +118,6 @@ - (NSString *)shortName {
109118
return @(name);
110119
}
111120

112-
- (NSString *)SHA {
113-
return self.reference.targetSHA;
114-
}
115-
116121
- (NSString *)remoteName {
117122
if (self.branchType == GTBranchTypeLocal) return nil;
118123

@@ -127,21 +132,8 @@ - (NSString *)remoteName {
127132
return [[NSString alloc] initWithBytes:name length:end - name encoding:NSUTF8StringEncoding];
128133
}
129134

130-
- (GTCommit *)targetCommitAndReturnError:(NSError **)error {
131-
if (self.SHA == nil) {
132-
if (error != NULL) *error = GTReference.invalidReferenceError;
133-
return nil;
134-
}
135-
136-
return [self.repository lookupObjectBySHA:self.SHA objectType:GTObjectTypeCommit error:error];
137-
}
138-
139-
- (NSUInteger)numberOfCommitsWithError:(NSError **)error {
140-
GTEnumerator *enumerator = [[GTEnumerator alloc] initWithRepository:self.repository error:error];
141-
if (enumerator == nil) return NSNotFound;
142-
143-
if (![enumerator pushSHA:self.SHA error:error]) return NSNotFound;
144-
return [enumerator countRemainingObjects:error];
135+
- (NSString *)SHA {
136+
return self.reference.targetSHA;
145137
}
146138

147139
- (GTBranchType)branchType {
@@ -152,24 +144,16 @@ - (GTBranchType)branchType {
152144
}
153145
}
154146

155-
- (NSArray *)uniqueCommitsRelativeToBranch:(GTBranch *)otherBranch error:(NSError **)error {
156-
NSParameterAssert(otherBranch != nil);
157-
158-
GTCommit *mergeBase = [self.repository mergeBaseBetweenFirstOID:self.reference.OID secondOID:otherBranch.reference.OID error:error];
159-
if (mergeBase == nil) return nil;
160-
161-
GTEnumerator *enumerator = [[GTEnumerator alloc] initWithRepository:self.repository error:error];
162-
if (enumerator == nil) return nil;
163-
164-
[enumerator resetWithOptions:GTEnumeratorOptionsTimeSort];
165-
166-
BOOL success = [enumerator pushSHA:self.SHA error:error];
167-
if (!success) return nil;
147+
#pragma mark -
148+
#pragma mark API
168149

169-
success = [enumerator hideSHA:mergeBase.SHA error:error];
170-
if (!success) return nil;
150+
- (GTCommit *)targetCommitAndReturnError:(NSError **)error {
151+
if (self.SHA == nil) {
152+
if (error != NULL) *error = GTReference.invalidReferenceError;
153+
return nil;
154+
}
171155

172-
return [enumerator allObjectsWithError:error];
156+
return [self.repository lookupObjectBySHA:self.SHA objectType:GTObjectTypeCommit error:error];
173157
}
174158

175159
- (BOOL)deleteWithError:(NSError **)error {
@@ -182,6 +166,13 @@ - (BOOL)deleteWithError:(NSError **)error {
182166
return YES;
183167
}
184168

169+
- (GTBranch *)reloadedBranchWithError:(NSError **)error {
170+
GTReference *reloadedRef = [self.reference reloadedReferenceWithError:error];
171+
if (reloadedRef == nil) return nil;
172+
173+
return [[self.class alloc] initWithReference:reloadedRef repository:self.repository];
174+
}
175+
185176
- (GTBranch *)trackingBranchWithError:(NSError **)error success:(BOOL *)success {
186177
if (self.branchType == GTBranchTypeRemote) {
187178
if (success != NULL) *success = YES;
@@ -214,11 +205,32 @@ - (GTBranch *)trackingBranchWithError:(NSError **)error success:(BOOL *)success
214205
return [[self class] branchWithReference:[[GTReference alloc] initWithGitReference:trackingRef repository:self.repository] repository:self.repository];
215206
}
216207

217-
- (GTBranch *)reloadedBranchWithError:(NSError **)error {
218-
GTReference *reloadedRef = [self.reference reloadedReferenceWithError:error];
219-
if (reloadedRef == nil) return nil;
208+
- (NSUInteger)numberOfCommitsWithError:(NSError **)error {
209+
GTEnumerator *enumerator = [[GTEnumerator alloc] initWithRepository:self.repository error:error];
210+
if (enumerator == nil) return NSNotFound;
220211

221-
return [[self.class alloc] initWithReference:reloadedRef repository:self.repository];
212+
if (![enumerator pushSHA:self.SHA error:error]) return NSNotFound;
213+
return [enumerator countRemainingObjects:error];
214+
}
215+
216+
- (NSArray *)uniqueCommitsRelativeToBranch:(GTBranch *)otherBranch error:(NSError **)error {
217+
NSParameterAssert(otherBranch != nil);
218+
219+
GTCommit *mergeBase = [self.repository mergeBaseBetweenFirstOID:self.reference.OID secondOID:otherBranch.reference.OID error:error];
220+
if (mergeBase == nil) return nil;
221+
222+
GTEnumerator *enumerator = [[GTEnumerator alloc] initWithRepository:self.repository error:error];
223+
if (enumerator == nil) return nil;
224+
225+
[enumerator resetWithOptions:GTEnumeratorOptionsTimeSort];
226+
227+
BOOL success = [enumerator pushSHA:self.SHA error:error];
228+
if (!success) return nil;
229+
230+
success = [enumerator hideSHA:mergeBase.SHA error:error];
231+
if (!success) return nil;
232+
233+
return [enumerator allObjectsWithError:error];
222234
}
223235

224236
- (BOOL)calculateAhead:(size_t *)ahead behind:(size_t *)behind relativeTo:(GTBranch *)branch error:(NSError **)error {

0 commit comments

Comments
 (0)