Skip to content

Commit e56ca10

Browse files
committed
Add search commands to the gitx cli
-s or --search= for searching in subject, author or SHA -S or --Search= for git's pickaxe string matching -r or --regex= for git's pickaxe regex matching
1 parent 71cdb0d commit e56ca10

12 files changed

+162
-2
lines changed

Diff for: GitX.h

+13
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) searchString:(NSString *)string inMode:(NSInteger)inMode; // Highlight commits that match the given search string.
4647

4748
@end
4849

@@ -66,6 +67,18 @@
6667
- (void) delete; // Delete an object.
6768
- (void) duplicateTo:(SBObject *)to withProperties:(NSDictionary *)withProperties; // Copy an object.
6869
- (void) moveTo:(SBObject *)to; // Move an object to a new location.
70+
- (void) searchString:(NSString *)string inMode:(NSInteger)inMode; // Highlight commits that match the given search string.
71+
72+
@end
73+
74+
75+
76+
/*
77+
* GitX Suite
78+
*/
79+
80+
// A document.
81+
@interface GitXDocument (GitXSuite)
6982

7083
@end
7184

Diff for: GitX.sdef

+17
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,23 @@
173173
</parameter>
174174
</command>
175175

176+
<command name="search" code="GitXSrch" description="Highlight commits that match the given search string.">
177+
<direct-parameter type="specifier" description="The repository document to search."/>
178+
<parameter name="string" code="SRCH" type="text" optional="yes" description="The string to search for.">
179+
<cocoa key="searchString"/>
180+
</parameter>
181+
<parameter name="in mode" code="Mode" type="integer" optional="yes" description="The type of search (defalts to basic [Subject, Author, SHA]).">
182+
<cocoa key="inMode"/>
183+
</parameter>
184+
</command>
185+
186+
<class-extension extends="document" code="docu" description="A document.">
187+
<cocoa class="PBGitRepository"/>
188+
<responds-to name="search">
189+
<cocoa method="findInModeScriptCommand:"/>
190+
</responds-to>
191+
</class-extension>
192+
176193
</suite>
177194

178195
</dictionary>

Diff for: GitXScriptingConstants.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@
1212
#define kGitXAEKeyArgumentsList 'ARGS'
1313

1414
#define kGitXCloneDestinationURLKey @"destinationURL"
15-
#define kGitXCloneIsBareKey @"isBare"
15+
#define kGitXCloneIsBareKey @"isBare"
16+
17+
#define kGitXFindSearchStringKey @"searchString"
18+
#define kGitXFindInModeKey @"inMode"

Diff for: PBGitRepository.h

+4
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ static NSString * PBStringFromBranchFilterType(PBGitXBranchFilterType type) {
125125
- (void) setup;
126126
- (void) forceUpdateRevisions;
127127

128+
// for the scripting bridge
129+
- (void)findInModeScriptCommand:(NSScriptCommand *)command;
130+
131+
128132
@property (assign) BOOL hasChanged;
129133
@property (readonly) PBGitWindowController *windowController;
130134
@property (readonly) PBGitConfig *config;

Diff for: PBGitRepository.m

+14
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#import "PBGitRevList.h"
2020
#import "PBGitDefaults.h"
2121
#import "GitXScriptingConstants.h"
22+
#import "PBHistorySearchController.h"
2223

2324
NSString* PBGitRepositoryErrorDomain = @"GitXErrorDomain";
2425

@@ -1008,6 +1009,19 @@ - (void)showWindows
10081009
[super showWindows];
10091010
}
10101011

1012+
// for the scripting bridge
1013+
- (void)findInModeScriptCommand:(NSScriptCommand *)command
1014+
{
1015+
NSDictionary *arguments = [command arguments];
1016+
NSString *searchString = [arguments objectForKey:kGitXFindSearchStringKey];
1017+
if (searchString) {
1018+
NSInteger mode = [[arguments objectForKey:kGitXFindInModeKey] integerValue];
1019+
[PBGitDefaults setShowStageView:NO];
1020+
[self.windowController showHistoryView:self];
1021+
[self.windowController setHistorySearch:searchString mode:mode];
1022+
}
1023+
}
1024+
10111025

10121026
#pragma mark low level
10131027

Diff for: PBGitSidebarController.h

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838

3939
- (IBAction) fetchPullPushAction:(id)sender;
4040

41+
- (void)setHistorySearch:(NSString *)searchString mode:(NSInteger)mode;
42+
4143
@property(readonly) NSMutableArray *items;
4244
@property(readonly) NSView *sourceListControlsView;
4345
@end

Diff for: PBGitSidebarController.m

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#import "NSOutlineViewExt.h"
1616
#import "PBAddRemoteSheet.h"
1717
#import "PBGitDefaults.h"
18+
#import "PBHistorySearchController.h"
1819

1920
@interface PBGitSidebarController ()
2021

@@ -184,6 +185,11 @@ - (void) removeRevSpec:(PBGitRevSpecifier *)rev
184185
[sourceView reloadData];
185186
}
186187

188+
- (void)setHistorySearch:(NSString *)searchString mode:(NSInteger)mode
189+
{
190+
[historyViewController.searchController setHistorySearch:searchString mode:mode];
191+
}
192+
187193
#pragma mark NSOutlineView delegate methods
188194

189195
- (void)outlineViewSelectionDidChange:(NSNotification *)notification

Diff for: PBGitWindowController.h

+3
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,7 @@
4747
- (IBAction) openInTerminal:(id)sender;
4848
- (IBAction) cloneTo:(id)sender;
4949
- (IBAction) refresh:(id)sender;
50+
51+
- (void)setHistorySearch:(NSString *)searchString mode:(NSInteger)mode;
52+
5053
@end

Diff for: PBGitWindowController.m

+5
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ - (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(
191191
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
192192
}
193193

194+
- (void)setHistorySearch:(NSString *)searchString mode:(NSInteger)mode
195+
{
196+
[sidebarController setHistorySearch:searchString mode:mode];
197+
}
198+
194199

195200

196201
#pragma mark -

Diff for: PBHistorySearchController.h

+2
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,6 @@ typedef enum historySearchModes {
6060
- (void)clearSearch;
6161
- (IBAction)updateSearch:(id)sender;
6262

63+
- (void)setHistorySearch:(NSString *)searchString mode:(NSInteger)mode;
64+
6365
@end

Diff for: PBHistorySearchController.m

+10
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,16 @@ - (IBAction)updateSearch:(id)sender
110110
[self startBackgroundSearch];
111111
}
112112

113+
- (void)setHistorySearch:(NSString *)searchString mode:(NSInteger)mode
114+
{
115+
if (searchString && ![searchString isEqualToString:@""]) {
116+
self.searchMode = mode;
117+
[searchField setStringValue:searchString];
118+
// use performClick: so that the search field will save it as a recent search
119+
[searchField performClick:self];
120+
}
121+
}
122+
113123
- (void)awakeFromNib
114124
{
115125
[self setupSearchMenuTemplate];

Diff for: gitx.m

+82-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import "PBEasyPipe.h"
1111
#import "GitXScriptingConstants.h"
1212
#import "GitX.h"
13+
#import "PBHistorySearchController.h"
1314

1415

1516

@@ -53,7 +54,6 @@ void usage(char const *programName)
5354
printf("\n");
5455
printf(" <branch> select specific branch or tag\n");
5556
printf(" -- <path(s)> show commits touching paths\n");
56-
printf(" -S<string> show commits that introduce or remove an instance of <string>\n");
5757
printf("\n");
5858
printf("Diff options\n");
5959
printf(" See 'man git-diff' for options you can pass to gitx --diff\n");
@@ -63,6 +63,15 @@ void usage(char const *programName)
6363
printf(" git diff [options] | gitx\n");
6464
printf(" use gitx to pipe diff output to a GitX window\n");
6565
printf("\n");
66+
printf("Search\n");
67+
printf("\n");
68+
printf(" -s<string>, --search=<string>\n");
69+
printf(" search for string in Subject, Author or SHA\n");
70+
printf(" -S<string>, --Search=<string>\n");
71+
printf(" commits that introduce or remove an instance of <string>\n");
72+
printf(" -r<regex>, --regex=<regex>\n");
73+
printf(" commits that introduce or remove strings that match <regex>\n");
74+
printf("\n");
6675
printf("Creating repositories\n");
6776
printf(" These commands will create a git repository and then open it up in GitX\n");
6877
printf("\n");
@@ -199,6 +208,74 @@ void handleClone(NSURL *repositoryURL, NSMutableArray *arguments)
199208
exit(0);
200209
}
201210

211+
#define kShortBasicSearch @"-s"
212+
#define kBasicSearch @"--search="
213+
#define kShortPickaxeSearch @"-S"
214+
#define kPickaxeSearch @"--Search="
215+
#define kShortRegexSearch @"-r"
216+
#define kRegexSearch @"--regex="
217+
218+
NSArray *commandLineSearchPrefixes()
219+
{
220+
return [NSArray arrayWithObjects:kShortBasicSearch, kBasicSearch, kShortPickaxeSearch, kPickaxeSearch, kShortRegexSearch, kRegexSearch, nil];
221+
}
222+
223+
PBHistorySearchMode searchModeForCommandLineArgument(NSString *argument)
224+
{
225+
if ([argument hasPrefix:kShortBasicSearch] || [argument hasPrefix:kBasicSearch])
226+
return kGitXBasicSeachMode;
227+
228+
if ([argument hasPrefix:kShortPickaxeSearch] || [argument hasPrefix:kPickaxeSearch])
229+
return kGitXPickaxeSearchMode;
230+
231+
if ([argument hasPrefix:kShortRegexSearch] || [argument hasPrefix:kRegexSearch])
232+
return kGitXRegexSearchMode;
233+
234+
return 0;
235+
}
236+
237+
GitXDocument *documentForURL(SBElementArray *documents, NSURL *URL)
238+
{
239+
NSString *path = [URL path];
240+
241+
for (GitXDocument *document in documents) {
242+
NSString *documentPath = [[document file] path];
243+
if ([[documentPath lastPathComponent] isEqualToString:@".git"])
244+
documentPath = [documentPath stringByDeletingLastPathComponent];
245+
246+
if ([documentPath isEqualToString:path])
247+
return document;
248+
}
249+
250+
return nil;
251+
}
252+
253+
void handleGitXSearch(NSURL *repositoryURL, NSMutableArray *arguments)
254+
{
255+
NSString *searchString = [arguments componentsJoinedByString:@" "];
256+
NSInteger mode = searchModeForCommandLineArgument(searchString);
257+
258+
// remove the prefix from search string before sending it
259+
NSArray *prefixes = commandLineSearchPrefixes();
260+
for (NSString *prefix in prefixes)
261+
if ([searchString hasPrefix:prefix]) {
262+
searchString = [searchString substringFromIndex:[prefix length]];
263+
break;
264+
}
265+
266+
if ([searchString isEqualToString:@""])
267+
exit(0);
268+
269+
GitXApplication *gitXApp = [SBApplication applicationWithBundleIdentifier:kGitXBundleIdentifier];
270+
[gitXApp open:[NSArray arrayWithObject:repositoryURL]];
271+
272+
// need to find the document after opening it
273+
GitXDocument *repositoryDocument = documentForURL([gitXApp documents], repositoryURL);
274+
[repositoryDocument searchString:searchString inMode:mode];
275+
276+
exit(0);
277+
}
278+
202279

203280
#pragma mark -
204281
#pragma mark main
@@ -287,6 +364,10 @@ int main(int argc, const char** argv)
287364
[arguments removeObjectAtIndex:0];
288365
handleClone(wdURL, arguments);
289366
}
367+
368+
if (searchModeForCommandLineArgument(firstArgument)) {
369+
handleGitXSearch(wdURL, arguments);
370+
}
290371
}
291372

292373
// No commands handled by gitx, open the current dir in GitX with the arguments

0 commit comments

Comments
 (0)