Skip to content

Commit 771daa7

Browse files
committed
PBGitRef: add support methods that will be needed in future commits.
These add functionality and will improve readability.
1 parent 03dc781 commit 771daa7

File tree

2 files changed

+95
-3
lines changed

2 files changed

+95
-3
lines changed

PBGitRef.h

+19
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,31 @@
99
#import <Cocoa/Cocoa.h>
1010

1111

12+
extern NSString * const kGitXTagRefPrefix;
13+
extern NSString * const kGitXBranchRefPrefix;
14+
extern NSString * const kGitXRemoteRefPrefix;
15+
16+
1217
@interface PBGitRef : NSObject {
1318
NSString* ref;
1419
}
1520

1621
- (NSString*) shortName;
22+
- (NSString *) tagName;
23+
- (NSString *) branchName;
24+
- (NSString *) remoteName;
25+
- (NSString *) remoteBranchName;
26+
1727
- (NSString*) type;
28+
- (BOOL) isBranch;
29+
- (BOOL) isTag;
30+
- (BOOL) isRemote;
31+
- (BOOL) isRemoteBranch;
32+
33+
- (PBGitRef *) remoteRef;
34+
35+
- (BOOL) isEqualToRef:(PBGitRef *)otherRef;
36+
1837
+ (PBGitRef*) refFromString: (NSString*) s;
1938
- (PBGitRef*) initWithString: (NSString*) s;
2039
@property(readonly) NSString* ref;

PBGitRef.m

+76-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
#import "PBGitRef.h"
1010

1111

12+
NSString * const kGitXTagRefPrefix = @"refs/tags/";
13+
NSString * const kGitXBranchRefPrefix = @"refs/heads/";
14+
NSString * const kGitXRemoteRefPrefix = @"refs/remotes/";
15+
16+
1217
@implementation PBGitRef
1318

1419
@synthesize ref;
@@ -19,17 +24,85 @@ - (NSString*) shortName
1924
return ref;
2025
}
2126

27+
- (NSString *) tagName
28+
{
29+
if (![self isTag])
30+
return nil;
31+
32+
return [self shortName];
33+
}
34+
35+
- (NSString *) branchName
36+
{
37+
if (![self isBranch])
38+
return nil;
39+
40+
return [self shortName];
41+
}
42+
43+
- (NSString *) remoteName
44+
{
45+
if (![self isRemote])
46+
return nil;
47+
48+
return (NSString *)[[ref componentsSeparatedByString:@"/"] objectAtIndex:2];
49+
}
50+
51+
- (NSString *) remoteBranchName
52+
{
53+
if (![self isRemoteBranch])
54+
return nil;
55+
56+
return [[self shortName] substringFromIndex:[[self remoteName] length] + 1];;
57+
}
58+
2259
- (NSString*) type
2360
{
24-
if ([ref hasPrefix:@"refs/heads"])
61+
if ([self isBranch])
2562
return @"head";
26-
if ([ref hasPrefix:@"refs/tags"])
63+
if ([self isTag])
2764
return @"tag";
28-
if ([ref hasPrefix:@"refs/remotes"])
65+
if ([self isRemote])
2966
return @"remote";
3067
return nil;
3168
}
3269

70+
- (BOOL) isBranch
71+
{
72+
return [ref hasPrefix:kGitXBranchRefPrefix];
73+
}
74+
75+
- (BOOL) isTag
76+
{
77+
return [ref hasPrefix:kGitXTagRefPrefix];
78+
}
79+
80+
- (BOOL) isRemote
81+
{
82+
return [ref hasPrefix:kGitXRemoteRefPrefix];
83+
}
84+
85+
- (BOOL) isRemoteBranch
86+
{
87+
if (![self isRemote])
88+
return NO;
89+
90+
return ([[ref componentsSeparatedByString:@"/"] count] > 3);
91+
}
92+
93+
- (BOOL) isEqualToRef:(PBGitRef *)otherRef
94+
{
95+
return [ref isEqualToString:[otherRef ref]];
96+
}
97+
98+
- (PBGitRef *) remoteRef
99+
{
100+
if (![self isRemote])
101+
return nil;
102+
103+
return [PBGitRef refFromString:[@"refs/remotes/" stringByAppendingString:[self remoteName]]];
104+
}
105+
33106
+ (PBGitRef*) refFromString: (NSString*) s
34107
{
35108
return [[PBGitRef alloc] initWithString:s];

0 commit comments

Comments
 (0)