From 39def32a1b1c3b09bd5334d7e9cd8a10b7428680 Mon Sep 17 00:00:00 2001 From: Nathan Kinsinger Date: Mon, 1 Feb 2010 19:34:28 -0700 Subject: [PATCH] Add 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. --- GitX.xcodeproj/project.pbxproj | 2 ++ PBGitCommit.h | 12 +++++++++- PBGitCommit.m | 23 ++++++++++++++++++ PBGitRef.h | 16 ++++++++++--- PBGitRef.m | 43 +++++++++++++++++++++++++++------- PBGitRefish.h | 27 +++++++++++++++++++++ 6 files changed, 111 insertions(+), 12 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 = ""; }; 93FCCBA80EA8AF450061B02B /* PBGitConfig.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGitConfig.m; sourceTree = ""; }; D26DC6440E782C9000C777B2 /* gitx.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; path = gitx.icns; sourceTree = ""; }; + D85B94B710E576B4007F3C28 /* PBGitRefish.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGitRefish.h; sourceTree = ""; }; EB2A73480FEE3F09006601CF /* PBCollapsibleSplitView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBCollapsibleSplitView.h; sourceTree = ""; }; EB2A73490FEE3F09006601CF /* PBCollapsibleSplitView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBCollapsibleSplitView.m; sourceTree = ""; }; F50A411D0EBB874C00208746 /* mainSplitterBar.tiff */ = {isa = PBXFileReference; lastKnownFileType = image.tiff; name = mainSplitterBar.tiff; path = Images/mainSplitterBar.tiff; sourceTree = ""; }; @@ -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 9a7832e..6f55b77 100644 --- a/PBGitCommit.h +++ b/PBGitCommit.h @@ -9,9 +9,14 @@ #import #import "PBGitRepository.h" #import "PBGitTree.h" +#import "PBGitRefish.h" #include "git/oid.h" -@interface PBGitCommit : NSObject { + +extern NSString * const kGitXCommitType; + + +@interface PBGitCommit : NSObject { git_oid sha; git_oid *parentShas; int nParents; @@ -35,6 +40,11 @@ - (NSString *)realSha; +// +- (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 9ebe3b5..a8c56ad 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)isKeyExcludedFromWebScript:(const char *)name { return NO; } + + +#pragma mark + +- (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 fbd01e0..80bae6e 100644 --- a/PBGitRef.h +++ b/PBGitRef.h @@ -7,24 +7,34 @@ // #import +#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 { NSString* ref; } -- (NSString*) shortName; +// +- (NSString *) refishName; +- (NSString *) shortName; +- (NSString *) refishType; + - (NSString *) tagName; - (NSString *) branchName; - (NSString *) remoteName; - (NSString *) remoteBranchName; -- (NSString*) type; +- (NSString *) type; - (BOOL) isBranch; - (BOOL) isTag; - (BOOL) isRemote; diff --git a/PBGitRef.m b/PBGitRef.m index 4bf03b8..c9ab938 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 @@ NSString * const kGitXRemoteRefPrefix = @"refs/remotes/"; @implementation PBGitRef @synthesize ref; -- (NSString*) shortName -{ - if ([self type]) - return [ref substringFromIndex:[[self type] length] + 7]; - return ref; -} - (NSString *) tagName { @@ -56,7 +55,7 @@ NSString * const kGitXRemoteRefPrefix = @"refs/remotes/"; return [[self shortName] substringFromIndex:[[self remoteName] length] + 1];; } -- (NSString*) type +- (NSString *) type { if ([self isBranch]) return @"head"; @@ -100,7 +99,7 @@ NSString * const kGitXRemoteRefPrefix = @"refs/remotes/"; if (![self isRemote]) return nil; - return [PBGitRef refFromString:[@"refs/remotes/" stringByAppendingString:[self remoteName]]]; + return [PBGitRef refFromString:[kGitXRemoteRefPrefix stringByAppendingString:[self remoteName]]]; } + (PBGitRef*) refFromString: (NSString*) s @@ -123,4 +122,32 @@ NSString * const kGitXRemoteRefPrefix = @"refs/remotes/"; return NO; } + +#pragma mark + +- (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..ac0f9d8 --- /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 + + +// Several git commands can take a ref "refs/heads/master" or an SHA. +// Use 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 + +- (NSString *) refishName; +- (NSString *) shortName; +- (NSString *) refishType; + +@end