Files
gitx/PBGitRevSpecifier.m
T
André Berg 10daa222b5 Major overhaul of the handling/effect of CLI args (gitx/PBCLIProxy).
Since we now have that gorgeous sidebar and commit list with branch filters, 
command line arguments to gitx should utilize these to greater effect.

For example, if I pass:

"--local" >> read current branch and set the branch filter to "Local"
"--all" >> same as above but set "All" branch filter
- partial or full SHA-1 >> should get the branch most likely associated with 
that SHA, select it in the sidebar and scroll the commit list to the commit 
with the passed SHA.
- partial ref like "xyz/master" >> select the master branch
of the "xyz" folder in the sidebar and scroll the commit list to its most
recent commit.
"--subject=Test" >> filter commit list using a predicate on the commit list's 
array controller (basically populating the search field programmatically).
For other "--..." CLI switches it's the same, just the filter predicate is a 
bit different each time. You get the idea.

To accomplish this we make the following changes:

- gitx sets two environment variables via setenv(), one for a concatenated 
  version of the CLI args, and one for the indicator that we launched from gitx.
  Using setenv() is unfortunate but I couldn't find a way to do it through DO
  and PBCLIProxy since that proxy will do it's processing far too late into 
  the app's event cycle. 

- the now shared application controller stores the two env vars in a BOOL
  'launchedFromGitx' and an NSString 'cliArgs'.

- Because GitX makes heavy use of KVO and context switching during the app
  launch we introduce the notion of a deferred selection so that we can worry
  about selecting the branch and commit the user has passed to gitx in some 
  form or another, when the app has finished launching completely and all 
  KVO notifications up to this point have been handled.
  
- ApplicationController does the bulk work. It stores most state
  changes, handles most command line switches (except for a few still in gitx.m),
  deals with the deferredSelectObject.

- PBGitSidebarController populateList includes logic for fixing up a ref or 
  a partial SHA when appController.launchedFromGitx is true. If it can be
  resolved we set this as the deferred select object so it can be selected
  later on.

I'll leave the excess NSLogs in there for this commit so another can see
what went into this before.
2010-04-05 23:50:03 +02:00

173 lines
4.0 KiB
Objective-C

//
// PBGitRevSpecifier.m
// GitX
//
// Created by Pieter de Bie on 12-09-08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "PBGitRevSpecifier.h"
@implementation PBGitRevSpecifier
@synthesize parameters, description, workingDirectory;
@synthesize isSimpleRef;
// internal designated init
- (id) initWithParameters:(NSArray *)params description:(NSString *)descrip
{
parameters = params;
description = descrip;
if (([parameters count] > 1) || ([parameters count] == 0))
isSimpleRef = NO;
else {
NSString *param = [parameters objectAtIndex:0];
if ([param hasPrefix:@"-"] ||
[param rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"^@{}~:"]].location != NSNotFound ||
[param rangeOfString:@".."].location != NSNotFound)
isSimpleRef = NO;
else
isSimpleRef = YES;
}
return self;
}
- (id) initWithParameters:(NSArray *)params
{
[self initWithParameters:params description:nil];
return self;
}
- (id) initWithRef:(PBGitRef *)ref
{
[self initWithParameters:[NSArray arrayWithObject:ref.ref] description:ref.shortName];
return self;
}
- (id) initWithCoder:(NSCoder *)coder
{
[self initWithParameters:[coder decodeObjectForKey:@"Parameters"] description:[coder decodeObjectForKey:@"Description"]];
return self;
}
+ (PBGitRevSpecifier *)allBranchesRevSpec
{
return [[PBGitRevSpecifier alloc] initWithParameters:[NSArray arrayWithObject:@"--all"] description:@"All branches"];
}
+ (PBGitRevSpecifier *)localBranchesRevSpec
{
return [[PBGitRevSpecifier alloc] initWithParameters:[NSArray arrayWithObject:@"--branches"] description:@"Local branches"];
}
- (NSString*) simpleRef
{
if (![self isSimpleRef])
return nil;
return [parameters objectAtIndex:0];
}
- (PBGitRef *) ref
{
if (![self isSimpleRef])
return nil;
return [PBGitRef refFromString:[self simpleRef]];
}
- (NSString *) description
{
if (!description)
description = [parameters componentsJoinedByString:@" "];
return description;
}
- (NSString *) title
{
NSString *title = nil;
if ([self.description isEqualToString:@"HEAD"])
title = @"detached HEAD";
else if ([self isSimpleRef])
title = [[self ref] shortName];
else if ([self.description hasPrefix:@"-S"])
title = [self.description substringFromIndex:[@"-S" length]];
else if ([self.description hasPrefix:@"HEAD -- "])
title = [self.description substringFromIndex:[@"HEAD -- " length]];
else if ([self.description hasPrefix:@"-- "])
title = [self.description substringFromIndex:[@"-- " length]];
else if ([self.description hasPrefix:@"--left-right "])
title = [self.description substringFromIndex:[@"--left-right " length]];
else
title = self.description;
return [NSString stringWithFormat:@"\"%@\"", title];
}
- (BOOL) hasPathLimiter;
{
for (NSString* param in parameters)
if ([param isEqualToString:@"--"])
return YES;
return NO;
}
- (BOOL) hasLeftRight
{
for (NSString* param in parameters)
if ([param isEqualToString:@"--left-right"])
return YES;
return NO;
}
- (BOOL) isEqual:(PBGitRevSpecifier *)other
{
if ([self isSimpleRef] ^ [other isSimpleRef])
return NO;
if ([self isSimpleRef])
return [[[self parameters] objectAtIndex:0] isEqualToString:[other.parameters objectAtIndex:0]];
return [self.description isEqualToString:other.description];
}
- (NSUInteger) hash
{
if ([self isSimpleRef])
return [[[self parameters] objectAtIndex:0] hash];
return [self.description hash];
}
- (BOOL) isAllBranchesRev
{
return [self isEqual:[PBGitRevSpecifier allBranchesRevSpec]];
}
- (BOOL) isLocalBranchesRev
{
return [self isEqual:[PBGitRevSpecifier localBranchesRevSpec]];
}
- (void) encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:description forKey:@"Description"];
[coder encodeObject:parameters forKey:@"Parameters"];
}
- (id)copyWithZone:(NSZone *)zone
{
PBGitRevSpecifier *copy = [[[self class] allocWithZone:zone] initWithParameters:[self.parameters copy]];
copy.description = [self.description copy];
copy.workingDirectory = [self.workingDirectory copy];
return copy;
}
@end