Add some more checking/validation methods to PBGitRepository.

- Add a shaExists method that takes string with a SHA and either returns the full
  SHA if it exists or nil if not.
- Add a completeRefForString: method which completes partial refs like "xyz/master"
  to "refs/remotes/xyz/master" for example.
- Add a checkRefFormatForBranch: method which does the same as checkRefFormat 
  except it also works with partial branch refs like "xyz/master".
- Add headRefForSHA skeleton to encapsulate the shell-script-fu from populateList
  in PBGitSidebarController that deals with getting the head ref for a SHA.
This commit is contained in:
André Berg
2010-04-05 16:45:34 +02:00
parent 5cb73b4d42
commit 4b4912a57a
2 changed files with 46 additions and 0 deletions
+4
View File
@@ -103,7 +103,11 @@ static NSString * PBStringFromBranchFilterType(PBGitXBranchFilterType type) {
- (BOOL) isSHAOnHeadBranch:(NSString *)testSHA;
- (BOOL) isRefOnHeadBranch:(PBGitRef *)testRef;
- (BOOL) checkRefFormat:(NSString *)refName;
- (BOOL) checkRefFormatForBranch:(NSString *)shaOrRefName;
- (PBGitRef *) completeRefForString:(NSString *)partialRefString;
- (BOOL) refExists:(PBGitRef *)ref;
// checks to see if the ref or sha exists - returns nil if not found and the complete sha if found
- (NSString *) shaExists:(NSString *)sha;
- (NSArray *) remotes;
- (BOOL) hasRemotes;
+42
View File
@@ -311,6 +311,13 @@ static NSString * repositoryBasePath = nil;
return [self shaForRef:[[self headRef] ref]];
}
- (NSString *) headRefForSHA:(NSString *)sha
{
// TODO: move code over
// from PBGitSidebarController populateList
// that deals with getting the head refs path
}
- (PBGitCommit *) headCommit
{
return [self commitForSHA:[self headSHA]];
@@ -417,6 +424,32 @@ static NSString * repositoryBasePath = nil;
return YES;
}
- (BOOL) checkRefFormatForBranch:(NSString *)shaOrRefName
{
int retValue = 1;
[self outputInWorkdirForArguments:[NSArray arrayWithObjects:@"check-ref-format", @"--branch", shaOrRefName, nil] retValue:&retValue];
if (retValue)
return NO;
return YES;
}
- (PBGitRef *) completeRefForString:(NSString *)partialRefString {
if (![self checkRefFormat:partialRefString] &&
![self checkRefFormatForBranch:partialRefString]) {
return nil;
}
NSArray * prefixes = [NSArray arrayWithObjects:kGitXBranchRefPrefix, kGitXRemoteRefPrefix, kGitXTagRefPrefix, nil];
PBGitRef * completeRef = nil;
for (NSString * prefix in prefixes) {
PBGitRef * tryref = [PBGitRef refFromString:[NSString stringWithFormat:@"%@%@", prefix, partialRefString]];
if ([self refExists:tryref]) {
completeRef = tryref;
break;
}
}
return completeRef;
}
- (BOOL) refExists:(PBGitRef *)ref
{
int retValue = 1;
@@ -425,6 +458,15 @@ static NSString * repositoryBasePath = nil;
return NO;
return YES;
}
- (NSString *) shaExists:(NSString *)sha
{
int retValue = 1;
NSString *output = [self outputInWorkdirForArguments:[NSArray arrayWithObjects:@"rev-parse", sha, nil] retValue:&retValue];
if (retValue || [output isEqualToString:@""])
return nil;
return output;
}
// Returns either this object, or an existing, equal object
- (PBGitRevSpecifier*) addBranch:(PBGitRevSpecifier*)branch