Skip to content

Commit 39def32

Browse files
committed
Add <PBGitRefish> protocol.
This will simplify methods that execute git commands that can take a ref or an SHA. Add some string constants so there is only one place for the type strings.
1 parent 771daa7 commit 39def32

File tree

6 files changed

+111
-12
lines changed

6 files changed

+111
-12
lines changed

GitX.xcodeproj/project.pbxproj

+2
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@
208208
93F7857E0EA3ABF100C1F443 /* PBCommitMessageView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBCommitMessageView.m; sourceTree = "<group>"; };
209209
93FCCBA80EA8AF450061B02B /* PBGitConfig.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGitConfig.m; sourceTree = "<group>"; };
210210
D26DC6440E782C9000C777B2 /* gitx.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; path = gitx.icns; sourceTree = "<group>"; };
211+
D85B94B710E576B4007F3C28 /* PBGitRefish.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGitRefish.h; sourceTree = "<group>"; };
211212
EB2A73480FEE3F09006601CF /* PBCollapsibleSplitView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBCollapsibleSplitView.h; sourceTree = "<group>"; };
212213
EB2A73490FEE3F09006601CF /* PBCollapsibleSplitView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBCollapsibleSplitView.m; sourceTree = "<group>"; };
213214
F50A411D0EBB874C00208746 /* mainSplitterBar.tiff */ = {isa = PBXFileReference; lastKnownFileType = image.tiff; name = mainSplitterBar.tiff; path = Images/mainSplitterBar.tiff; sourceTree = "<group>"; };
@@ -625,6 +626,7 @@
625626
F5AD56770E79B78100EDAAFE /* PBCommitList.h */,
626627
F5AD56780E79B78100EDAAFE /* PBCommitList.m */,
627628
F5C6F6750E65FE2B00478D97 /* Graphing */,
629+
D85B94B710E576B4007F3C28 /* PBGitRefish.h */,
628630
F56524EE0E02D45200F03B52 /* PBGitCommit.h */,
629631
F56524EF0E02D45200F03B52 /* PBGitCommit.m */,
630632
F5C007730E731B48007B84B2 /* PBGitRef.h */,

PBGitCommit.h

+11-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@
99
#import <Cocoa/Cocoa.h>
1010
#import "PBGitRepository.h"
1111
#import "PBGitTree.h"
12+
#import "PBGitRefish.h"
1213
#include "git/oid.h"
1314

14-
@interface PBGitCommit : NSObject {
15+
16+
extern NSString * const kGitXCommitType;
17+
18+
19+
@interface PBGitCommit : NSObject <PBGitRefish> {
1520
git_oid sha;
1621
git_oid *parentShas;
1722
int nParents;
@@ -35,6 +40,11 @@
3540

3641
- (NSString *)realSha;
3742

43+
// <PBGitRefish>
44+
- (NSString *) refishName;
45+
- (NSString *) shortName;
46+
- (NSString *) refishType;
47+
3848
@property (readonly) git_oid *sha;
3949
@property (copy) NSString* subject;
4050
@property (copy) NSString* author;

PBGitCommit.m

+23
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
#import "PBGitCommit.h"
1010
#import "PBGitDefaults.h"
1111

12+
13+
NSString * const kGitXCommitType = @"commit";
14+
15+
1216
@implementation PBGitCommit
1317

1418
@synthesize repository, subject, timestamp, author, parentShas, nParents, sign, lineInfo;
@@ -128,4 +132,23 @@ + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
128132
+ (BOOL)isKeyExcludedFromWebScript:(const char *)name {
129133
return NO;
130134
}
135+
136+
137+
#pragma mark <PBGitRefish>
138+
139+
- (NSString *) refishName
140+
{
141+
return [self realSha];
142+
}
143+
144+
- (NSString *) shortName
145+
{
146+
return [[self realSha] substringToIndex:10];
147+
}
148+
149+
- (NSString *) refishType
150+
{
151+
return kGitXCommitType;
152+
}
153+
131154
@end

PBGitRef.h

+13-3
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,34 @@
77
//
88

99
#import <Cocoa/Cocoa.h>
10+
#import "PBGitRefish.h"
1011

1112

13+
extern NSString * const kGitXTagType;
14+
extern NSString * const kGitXBranchType;
15+
extern NSString * const kGitXRemoteType;
16+
extern NSString * const kGitXRemoteBranchType;
17+
1218
extern NSString * const kGitXTagRefPrefix;
1319
extern NSString * const kGitXBranchRefPrefix;
1420
extern NSString * const kGitXRemoteRefPrefix;
1521

1622

17-
@interface PBGitRef : NSObject {
23+
@interface PBGitRef : NSObject <PBGitRefish> {
1824
NSString* ref;
1925
}
2026

21-
- (NSString*) shortName;
27+
// <PBGitRefish>
28+
- (NSString *) refishName;
29+
- (NSString *) shortName;
30+
- (NSString *) refishType;
31+
2232
- (NSString *) tagName;
2333
- (NSString *) branchName;
2434
- (NSString *) remoteName;
2535
- (NSString *) remoteBranchName;
2636

27-
- (NSString*) type;
37+
- (NSString *) type;
2838
- (BOOL) isBranch;
2939
- (BOOL) isTag;
3040
- (BOOL) isRemote;

PBGitRef.m

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

1111

12+
NSString * const kGitXTagType = @"tag";
13+
NSString * const kGitXBranchType = @"branch";
14+
NSString * const kGitXRemoteType = @"remote";
15+
NSString * const kGitXRemoteBranchType = @"remote branch";
16+
1217
NSString * const kGitXTagRefPrefix = @"refs/tags/";
1318
NSString * const kGitXBranchRefPrefix = @"refs/heads/";
1419
NSString * const kGitXRemoteRefPrefix = @"refs/remotes/";
@@ -17,12 +22,6 @@
1722
@implementation PBGitRef
1823

1924
@synthesize ref;
20-
- (NSString*) shortName
21-
{
22-
if ([self type])
23-
return [ref substringFromIndex:[[self type] length] + 7];
24-
return ref;
25-
}
2625

2726
- (NSString *) tagName
2827
{
@@ -56,7 +55,7 @@ - (NSString *) remoteBranchName
5655
return [[self shortName] substringFromIndex:[[self remoteName] length] + 1];;
5756
}
5857

59-
- (NSString*) type
58+
- (NSString *) type
6059
{
6160
if ([self isBranch])
6261
return @"head";
@@ -100,7 +99,7 @@ - (PBGitRef *) remoteRef
10099
if (![self isRemote])
101100
return nil;
102101

103-
return [PBGitRef refFromString:[@"refs/remotes/" stringByAppendingString:[self remoteName]]];
102+
return [PBGitRef refFromString:[kGitXRemoteRefPrefix stringByAppendingString:[self remoteName]]];
104103
}
105104

106105
+ (PBGitRef*) refFromString: (NSString*) s
@@ -123,4 +122,32 @@ + (BOOL)isKeyExcludedFromWebScript:(const char *)name {
123122
return NO;
124123
}
125124

125+
126+
#pragma mark <PBGitRefish>
127+
128+
- (NSString *) refishName
129+
{
130+
return ref;
131+
}
132+
133+
- (NSString *) shortName
134+
{
135+
if ([self type])
136+
return [ref substringFromIndex:[[self type] length] + 7];
137+
return ref;
138+
}
139+
140+
- (NSString *) refishType
141+
{
142+
if ([self isBranch])
143+
return kGitXBranchType;
144+
if ([self isTag])
145+
return kGitXTagType;
146+
if ([self isRemoteBranch])
147+
return kGitXRemoteBranchType;
148+
if ([self isRemote])
149+
return kGitXRemoteType;
150+
return nil;
151+
}
152+
126153
@end

PBGitRefish.h

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// PBGitRefish.h
3+
// GitX
4+
//
5+
// Created by Nathan Kinsinger on 12/25/09.
6+
// Copyright 2009 Nathan Kinsinger. All rights reserved.
7+
//
8+
9+
#import <Cocoa/Cocoa.h>
10+
11+
12+
// Several git commands can take a ref "refs/heads/master" or an SHA.
13+
// Use <PBGitRefish> to accept a PBGitRef or a PBGitCommit without having to write
14+
// two separate methods.
15+
//
16+
// refishName the full name of the ref "refs/heads/master" or the full SHA
17+
// used in git commands
18+
// shortName a more user friendly version of the refName, "master" or a short SHA
19+
// refishType a short name for the type
20+
21+
@protocol PBGitRefish <NSObject>
22+
23+
- (NSString *) refishName;
24+
- (NSString *) shortName;
25+
- (NSString *) refishType;
26+
27+
@end

0 commit comments

Comments
 (0)