From: Nathan Kinsinger <[email protected]>

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.
---
 GitX.xcodeproj/project.pbxproj |    2 ++
 PBGitCommit.h                  |   12 +++++++++++-
 PBGitCommit.m                  |   23 +++++++++++++++++++++++
 PBGitRef.h                     |   14 ++++++++++++--
 PBGitRef.m                     |   39 +++++++++++++++++++++++++++++++++------
 PBGitRefish.h                  |   27 +++++++++++++++++++++++++++
 6 files changed, 108 insertions(+), 9 deletions(-)
 create mode 100644 PBGitRefish.h

diff --git a/GitX.xcodeproj/project.pbxproj b/GitX.xcodeproj/project.pbxproj
index 8f8f62c..d8bba5a 100644
--- a/GitX.xcodeproj/project.pbxproj
+++ b/GitX.xcodeproj/project.pbxproj
@@ -208,6 +208,7 @@
                93F7857E0EA3ABF100C1F443 /* PBCommitMessageView.m */ = {isa = 
PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path 
= PBCommitMessageView.m; sourceTree = "<group>"; };
                93FCCBA80EA8AF450061B02B /* PBGitConfig.m */ = {isa = 
PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path 
= PBGitConfig.m; sourceTree = "<group>"; };
                D26DC6440E782C9000C777B2 /* gitx.icns */ = {isa = 
PBXFileReference; lastKnownFileType = image.icns; path = gitx.icns; sourceTree 
= "<group>"; };
+               D85B94B710E576B4007F3C28 /* PBGitRefish.h */ = {isa = 
PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = 
PBGitRefish.h; sourceTree = "<group>"; };
                EB2A73480FEE3F09006601CF /* PBCollapsibleSplitView.h */ = {isa 
= PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path 
= PBCollapsibleSplitView.h; sourceTree = "<group>"; };
                EB2A73490FEE3F09006601CF /* PBCollapsibleSplitView.m */ = {isa 
= PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; 
path = PBCollapsibleSplitView.m; sourceTree = "<group>"; };
                F50A411D0EBB874C00208746 /* mainSplitterBar.tiff */ = {isa = 
PBXFileReference; lastKnownFileType = image.tiff; name = mainSplitterBar.tiff; 
path = Images/mainSplitterBar.tiff; sourceTree = "<group>"; };
@@ -625,6 +626,7 @@
                                F5AD56770E79B78100EDAAFE /* PBCommitList.h */,
                                F5AD56780E79B78100EDAAFE /* PBCommitList.m */,
                                F5C6F6750E65FE2B00478D97 /* Graphing */,
+                               D85B94B710E576B4007F3C28 /* PBGitRefish.h */,
                                F56524EE0E02D45200F03B52 /* PBGitCommit.h */,
                                F56524EF0E02D45200F03B52 /* PBGitCommit.m */,
                                F5C007730E731B48007B84B2 /* PBGitRef.h */,
diff --git a/PBGitCommit.h b/PBGitCommit.h
index 9f0adea..5849cc1 100644
--- a/PBGitCommit.h
+++ b/PBGitCommit.h
@@ -9,9 +9,14 @@
 #import <Cocoa/Cocoa.h>
 #import "PBGitRepository.h"
 #import "PBGitTree.h"
+#import "PBGitRefish.h"
 #include "git/oid.h"
 
-...@interface PBGitCommit : NSObject {
+
+extern NSString * const kGitXCommitType;
+
+
+...@interface PBGitCommit : NSObject <PBGitRefish> {
        git_oid sha;
        git_oid *parentShas;
        int nParents;
@@ -35,6 +40,11 @@
 
 - (NSString *)realSha;
 
+// <PBGitRefish>
+- (NSString *)refishName;
+- (NSString *)shortName;
+- (NSString *)refishType;
+
 @property (readonly) git_oid *sha;
 @property (copy) NSString* subject;
 @property (copy) NSString* author;
diff --git a/PBGitCommit.m b/PBGitCommit.m
index 0d785e4..547cfe1 100644
--- a/PBGitCommit.m
+++ b/PBGitCommit.m
@@ -9,6 +9,10 @@
 #import "PBGitCommit.h"
 #import "PBGitDefaults.h"
 
+
+NSString * const kGitXCommitType = @"commit";
+
+
 @implementation PBGitCommit
 
 @synthesize repository, subject, timestamp, author, parentShas, nParents, 
sign, lineInfo;
@@ -128,4 +132,23 @@ + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
 + (BOOL)isKeyExcludedFromWebScript:(const char *)name {
        return NO;
 }
+
+
+#pragma mark <PBGitRefish>
+
+- (NSString *)refishName
+{
+       return [self realSha];
+}
+
+- (NSString *)shortName
+{
+       return [[self realSha] substringToIndex:10];
+}
+
+- (NSString *)refishType
+{
+       return kGitXCommitType;
+}
+
 @end
diff --git a/PBGitRef.h b/PBGitRef.h
index 5a8662a..f066cac 100644
--- a/PBGitRef.h
+++ b/PBGitRef.h
@@ -7,18 +7,28 @@
 //
 
 #import <Cocoa/Cocoa.h>
+#import "PBGitRefish.h"
 
 
+extern NSString * const kGitXTagType;
+extern NSString * const kGitXBranchType;
+extern NSString * const kGitXRemoteType;
+extern NSString * const kGitXRemoteBranchType;
+
 extern NSString * const kGitXTagRefPrefix;
 extern NSString * const kGitXBranchRefPrefix;
 extern NSString * const kGitXRemoteRefPrefix;
 
 
-...@interface PBGitRef : NSObject {
+...@interface PBGitRef : NSObject <PBGitRefish> {
        NSString* ref;
 }
 
-- (NSString*) shortName;
+// <PBGitRefish>
+- (NSString *)refishName;
+- (NSString *)shortName;
+- (NSString *)refishType;
+
 - (NSString *)tagName;
 - (NSString *)branchName;
 - (NSString *)remoteName;
diff --git a/PBGitRef.m b/PBGitRef.m
index 2291c02..54233f5 100644
--- a/PBGitRef.m
+++ b/PBGitRef.m
@@ -9,6 +9,11 @@
 #import "PBGitRef.h"
 
 
+NSString * const kGitXTagType    = @"tag";
+NSString * const kGitXBranchType = @"branch";
+NSString * const kGitXRemoteType = @"remote";
+NSString * const kGitXRemoteBranchType = @"remote branch";
+
 NSString * const kGitXTagRefPrefix    = @"refs/tags/";
 NSString * const kGitXBranchRefPrefix = @"refs/heads/";
 NSString * const kGitXRemoteRefPrefix = @"refs/remotes/";
@@ -17,12 +22,6 @@
 @implementation PBGitRef
 
 @synthesize ref;
-- (NSString*) shortName
-{
-       if ([self type])
-               return [ref substringFromIndex:[[self type] length] + 7];
-       return ref;
-}
 
 - (NSString *)tagName
 {
@@ -131,4 +130,32 @@ + (BOOL)isKeyExcludedFromWebScript:(const char *)name {
        return NO;
 }
 
+
+#pragma mark <PBGitRefish>
+
+- (NSString *)refishName
+{
+       return ref;
+}
+
+- (NSString *)shortName
+{
+       if ([self type])
+               return [ref substringFromIndex:[[self type] length] + 7];
+       return ref;
+}
+
+- (NSString *)refishType
+{
+       if ([self isBranch])
+               return kGitXBranchType;
+       if ([self isTag])
+               return kGitXTagType;
+       if ([self isRemoteBranch])
+               return kGitXRemoteBranchType;
+       if ([self isRemote])
+               return kGitXRemoteType;
+       return nil;
+}
+
 @end
diff --git a/PBGitRefish.h b/PBGitRefish.h
new file mode 100644
index 0000000..0ac3b97
--- /dev/null
+++ b/PBGitRefish.h
@@ -0,0 +1,27 @@
+//
+//  PBGitRefish.h
+//  GitX
+//
+//  Created by Nathan Kinsinger on 12/25/09.
+//  Copyright 2009 Nathan Kinsinger. All rights reserved.
+//
+
+#import <Cocoa/Cocoa.h>
+
+
+//  Several git commands can take a ref "refs/heads/master" or an SHA.
+//  Use <PBGitRefish> to accept a PBGitRef or a PBGitCommit without having to 
write
+//  two separate methods.
+//
+//  refishName  the full name of the ref "refs/heads/master" or the full SHA
+//              used in git commands
+//  shortName   a more user friendly version of the refName, "master" or a 
short SHA
+//  refishType  a short name for the type
+
+...@protocol PBGitRefish <NSObject>
+
+- (NSString *)refishName;
+- (NSString *)shortName;
+- (NSString *)refishType;
+
+...@end
-- 
1.7.0.3

To unsubscribe from this group, send email to gitx+unsubscribegooglegroups.com 
or reply to this email with the words "REMOVE ME" as the subject.

Reply via email to