Skip to content

Commit d2c426c

Browse files
committed
Always use Scripting Bridge to invoke GUI from CLI
When the CLI command is invoked without any arguments or with some refs, the CLI command was using a low-level NSAppleEventDescriptor to communicate with the main GUI process. That behavior was inconsistent with the behavior for other command-line options, where communication is done with high-level Scripting Bridge. Moreover, the GUI side was failing to handle the NSAppleEventDescriptor and thus ignoring command line arguments passed from CLI (cf. #196). This commit modifies the code so that CLI and GUI always use Scripting Bridge to send and receive command-line arguments correctly.
1 parent 85f99c5 commit d2c426c

File tree

6 files changed

+35
-72
lines changed

6 files changed

+35
-72
lines changed

Classes/GitXScriptingConstants.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@
1414
#define kGitXCloneDestinationURLKey @"destinationURL"
1515
#define kGitXCloneIsBareKey @"isBare"
1616

17+
#define kGitXOpenArgumentsStringKey @"openArguments"
18+
1719
#define kGitXFindSearchStringKey @"searchString"
1820
#define kGitXFindInModeKey @"inMode"

Classes/git/PBGitRepository.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ static NSString * PBStringFromBranchFilterType(PBGitXBranchFilterType type) {
140140
- (NSURL*) getIndexURL;
141141

142142
// for the scripting bridge
143+
- (void)openWithScriptCommand:(NSScriptCommand *)command;
143144
- (void)findInModeScriptCommand:(NSScriptCommand *)command;
144145

145146
@end

Classes/git/PBGitRepository.m

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -943,31 +943,22 @@ - (void)handleRevListArguments:(NSArray *)arguments
943943
[self.windowController showHistoryView:self];
944944
}
945945

946-
- (void)handleBranchFilterEventForFilter:(PBGitXBranchFilterType)filter additionalArguments:(NSMutableArray *)arguments
946+
- (void)handleBranchFilterEventForFilter:(PBGitXBranchFilterType)filter additionalArguments:(NSArray *)arguments
947947
{
948948
self.currentBranchFilter = filter;
949949
[PBGitDefaults setShowStageView:NO];
950950
[self.windowController showHistoryView:self];
951951

952952
// treat any additional arguments as a rev-list specifier
953953
if ([arguments count] > 1) {
954-
[arguments removeObjectAtIndex:0];
955-
[self handleRevListArguments:arguments];
954+
NSMutableArray *argumentsButFirst = [arguments mutableCopy];
955+
[argumentsButFirst removeObjectAtIndex:0];
956+
[self handleRevListArguments:argumentsButFirst];
956957
}
957958
}
958959

959-
- (void)handleGitXScriptingArguments:(NSAppleEventDescriptor *)argumentsList
960+
- (void)handleGitXScriptingArguments:(NSArray *)arguments
960961
{
961-
NSMutableArray *arguments = [NSMutableArray array];
962-
uint argumentsIndex = 1; // AppleEvent list descriptor's are one based
963-
while(1) {
964-
NSAppleEventDescriptor *arg = [argumentsList descriptorAtIndex:argumentsIndex++];
965-
if (arg)
966-
[arguments addObject:[arg stringValue]];
967-
else
968-
break;
969-
}
970-
971962
if (![arguments count])
972963
return;
973964

@@ -998,32 +989,12 @@ - (void)handleGitXScriptingArguments:(NSAppleEventDescriptor *)argumentsList
998989
[self handleRevListArguments:arguments];
999990
}
1000991

1001-
// see if the current appleEvent has the command line arguments from the gitx cli
1002-
// this could be from an openApplication or an openDocument apple event
1003-
// when opening a repository this is called before the sidebar controller gets it's awakeFromNib: message
1004-
// if the repository is already open then this is also a good place to catch the event as the window is about to be brought forward
1005-
- (void)showWindows
992+
// for the scripting bridge
993+
- (void)openWithScriptCommand:(NSScriptCommand *)command
1006994
{
1007-
NSAppleEventDescriptor *currentAppleEvent = [[NSAppleEventManager sharedAppleEventManager] currentAppleEvent];
1008-
1009-
if (currentAppleEvent) {
1010-
NSAppleEventDescriptor *eventRecord = [currentAppleEvent paramDescriptorForKeyword:keyAEPropData];
1011-
1012-
// on app launch there may be many repositories opening, so double check that this is the right repo
1013-
NSString *path = [[eventRecord paramDescriptorForKeyword:typeFileURL] stringValue];
1014-
if (path) {
1015-
NSURL *workingDirectory = [NSURL URLWithString:path];
1016-
if ([[GitRepoFinder gitDirForURL:workingDirectory] isEqual:[self fileURL]]) {
1017-
NSAppleEventDescriptor *argumentsList = [eventRecord paramDescriptorForKeyword:kGitXAEKeyArgumentsList];
1018-
[self handleGitXScriptingArguments:argumentsList];
1019-
1020-
// showWindows may be called more than once during app launch so remove the CLI data after we handle the event
1021-
[currentAppleEvent removeDescriptorWithKeyword:keyAEPropData];
1022-
}
1023-
}
1024-
}
1025-
1026-
[super showWindows];
995+
NSDictionary *commandArguments = [command arguments];
996+
NSArray *arguments = [commandArguments objectForKey:kGitXOpenArgumentsStringKey];
997+
[self handleGitXScriptingArguments:arguments];
1027998
}
1028999

10291000
// for the scripting bridge

Classes/gitx.m

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -145,39 +145,6 @@ void handleDiffWithArguments(NSURL *repositoryURL, NSArray *arguments)
145145
exit(0);
146146
}
147147

148-
void handleOpenRepository(NSURL *repositoryURL, NSMutableArray *arguments)
149-
{
150-
// if there are command line arguments send them to GitX through an Apple Event
151-
// the recordDescriptor will be stored in keyAEPropData inside the openDocument or openApplication event
152-
NSAppleEventDescriptor *recordDescriptor = nil;
153-
if ([arguments count]) {
154-
recordDescriptor = [NSAppleEventDescriptor recordDescriptor];
155-
156-
NSAppleEventDescriptor *listDescriptor = [NSAppleEventDescriptor listDescriptor];
157-
uint listIndex = 1; // AppleEvent list descriptor's are one based
158-
for (NSString *argument in arguments)
159-
[listDescriptor insertDescriptor:[NSAppleEventDescriptor descriptorWithString:argument] atIndex:listIndex++];
160-
161-
[recordDescriptor setParamDescriptor:listDescriptor forKeyword:kGitXAEKeyArgumentsList];
162-
163-
// this is used as a double check in GitX
164-
NSAppleEventDescriptor *url = [NSAppleEventDescriptor descriptorWithString:[repositoryURL absoluteString]];
165-
[recordDescriptor setParamDescriptor:url forKeyword:typeFileURL];
166-
}
167-
168-
// use NSWorkspace to open GitX and send the arguments
169-
// this allows the repository document to modify itself before it shows it's GUI
170-
BOOL didOpenURLs = [[NSWorkspace sharedWorkspace] openURLs:[NSArray arrayWithObject:repositoryURL]
171-
withAppBundleIdentifier:kGitXBundleIdentifier
172-
options:0
173-
additionalEventParamDescriptor:recordDescriptor
174-
launchIdentifiers:NULL];
175-
if (!didOpenURLs) {
176-
printf("Unable to open GitX.app\n");
177-
exit(2);
178-
}
179-
}
180-
181148
void handleInit(NSURL *repositoryURL)
182149
{
183150
GitXApplication *gitXApp = [SBApplication applicationWithBundleIdentifier:kGitXBundleIdentifier];
@@ -253,6 +220,15 @@ PBHistorySearchMode searchModeForCommandLineArgument(NSString *argument)
253220
return nil;
254221
}
255222

223+
void handleOpenRepository(NSURL *repositoryURL, NSMutableArray *arguments)
224+
{
225+
GitXApplication *gitXApp = [SBApplication applicationWithBundleIdentifier:kGitXBundleIdentifier];
226+
[gitXApp open:[NSArray arrayWithObject:repositoryURL]];
227+
228+
GitXDocument *repositoryDocument = documentForURL([gitXApp documents], repositoryURL);
229+
[repositoryDocument openArguments:arguments];
230+
}
231+
256232
void handleGitXSearch(NSURL *repositoryURL, NSMutableArray *arguments)
257233
{
258234
NSString *searchString = [arguments componentsJoinedByString:@" "];

GitX.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
- (void) delete; // Delete an object.
4444
- (void) duplicateTo:(SBObject *)to withProperties:(NSDictionary *)withProperties; // Copy an object.
4545
- (void) moveTo:(SBObject *)to; // Move an object to a new location.
46+
- (void) openArguments:(NSArray *)arguments; // Open a repository and optionally select given refs.
4647
- (void) searchString:(NSString *)string inMode:(NSInteger)inMode; // Highlight commits that match the given search string.
4748

4849
@end
@@ -67,6 +68,7 @@
6768
- (void) delete; // Delete an object.
6869
- (void) duplicateTo:(SBObject *)to withProperties:(NSDictionary *)withProperties; // Copy an object.
6970
- (void) moveTo:(SBObject *)to; // Move an object to a new location.
71+
- (void) openArguments:(NSArray *)arguments; // Open a repository and optionally select given refs.
7072
- (void) searchString:(NSString *)string inMode:(NSInteger)inMode; // Highlight commits that match the given search string.
7173

7274
@end

GitX.sdef

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,14 @@
173173
</parameter>
174174
</command>
175175

176+
<command name="open" code="GitXOpen" description="Open a repository and optionally select given refs.">
177+
<direct-parameter type="specifier" description="The repository document to open."/>
178+
<parameter name="arguments" code="ARGS" description="The arguments">
179+
<type type="text" list="yes"/>
180+
<cocoa key="openArguments"/>
181+
</parameter>
182+
</command>
183+
176184
<command name="search" code="GitXSrch" description="Highlight commits that match the given search string.">
177185
<direct-parameter type="specifier" description="The repository document to search."/>
178186
<parameter name="string" code="SRCH" type="text" optional="yes" description="The string to search for.">
@@ -197,6 +205,9 @@
197205

198206
<class-extension extends="document" code="docu" description="A document.">
199207
<cocoa class="PBGitRepository"/>
208+
<responds-to name="open">
209+
<cocoa method="openWithScriptCommand:"/>
210+
</responds-to>
200211
<responds-to name="search">
201212
<cocoa method="findInModeScriptCommand:"/>
202213
</responds-to>

0 commit comments

Comments
 (0)