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.
This commit is contained in:
Nathan Kinsinger
2010-02-01 19:34:28 -07:00
parent 771daa7a2c
commit 39def32a1b
6 changed files with 111 additions and 12 deletions
+2
View File
@@ -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 */,
+11 -1
View File
@@ -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;
+23
View File
@@ -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 <PBGitRefish>
- (NSString *) refishName
{
return [self realSha];
}
- (NSString *) shortName
{
return [[self realSha] substringToIndex:10];
}
- (NSString *) refishType
{
return kGitXCommitType;
}
@end
+13 -3
View File
@@ -7,24 +7,34 @@
//
#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;
- (NSString *) remoteBranchName;
- (NSString*) type;
- (NSString *) type;
- (BOOL) isBranch;
- (BOOL) isTag;
- (BOOL) isRemote;
+35 -8
View File
@@ -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 <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
+27
View File
@@ -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