Skip to content

Commit 17f50e3

Browse files
committed
Add --init and --clone to gitx CLI and Applescript
Allows creating repositories from the command line, Applescript, or the scripting bridge. These are basic commands, if you need to use commandline options then use git itself.
1 parent b923272 commit 17f50e3

10 files changed

+135
-15
lines changed

GitX.h

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
- (void) quit; // Quit the application.
2929
- (BOOL) exists:(id)x; // Verify that an object exists.
3030
- (void) showDiff:(NSString *)x; // Show the supplied diff output in a GitX window.
31+
- (void) initRepository:(NSURL *)x; // Create a git repository at the given filesystem URL.
32+
- (void) cloneRepository:(NSString *)x to:(NSURL *)to isBare:(BOOL)isBare; // Clone a repository.
3133

3234
@end
3335

GitX.sdef

+12
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,18 @@
160160
<command name="show diff" code="GitXShDf" description="Show the supplied diff output in a GitX window.">
161161
<direct-parameter type="text" description="The textual output from a diff tool."/>
162162
</command>
163+
<command name="init repository" code="GitXInit" description="Create a git repository at the given filesystem URL.">
164+
<direct-parameter type="file" description="The URL of the repository to clone."/>
165+
</command>
166+
<command name="clone repository" code="GitXClon" description="Clone a repository.">
167+
<direct-parameter type="text" description="The URL of the repository to clone."/>
168+
<parameter name="to" code="URL " type="file" description="The location for the new repository.">
169+
<cocoa key="destinationURL"/>
170+
</parameter>
171+
<parameter name="is bare" code="Bare" type="boolean" optional="yes" description="Indicates whether the created repository should be a bare repository.">
172+
<cocoa key="isBare"/>
173+
</parameter>
174+
</command>
163175

164176
</suite>
165177

GitXScriptingConstants.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@
99
#define kGitXBundleIdentifier @"nl.frim.GitX"
1010

1111

12-
#define kGitXAEKeyArgumentsList 'ARGS'
12+
#define kGitXAEKeyArgumentsList 'ARGS'
13+
14+
#define kGitXCloneDestinationURLKey @"destinationURL"
15+
#define kGitXCloneIsBareKey @"isBare"

NSApplication+GitXScripting.h

+2
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@
1212
@interface NSApplication (GitXScripting)
1313

1414
- (void)showDiffScriptCommand:(NSScriptCommand *)command;
15+
- (void)initRepositoryScriptCommand:(NSScriptCommand *)command;
16+
- (void)cloneRepositoryScriptCommand:(NSScriptCommand *)command;
1517

1618
@end

NSApplication+GitXScripting.m

+24
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
//
88

99
#import "NSApplication+GitXScripting.h"
10+
#import "GitXScriptingConstants.h"
1011
#import "PBDiffWindowController.h"
12+
#import "PBRepositoryDocumentController.h"
13+
#import "PBCloneRepositoryPanel.h"
1114

1215

1316
@implementation NSApplication (GitXScripting)
@@ -22,4 +25,25 @@ - (void)showDiffScriptCommand:(NSScriptCommand *)command
2225
}
2326
}
2427

28+
- (void)initRepositoryScriptCommand:(NSScriptCommand *)command
29+
{
30+
NSURL *repositoryURL = [command directParameter];
31+
if (repositoryURL)
32+
[[PBRepositoryDocumentController sharedDocumentController] initNewRepositoryAtURL:repositoryURL];
33+
}
34+
35+
- (void)cloneRepositoryScriptCommand:(NSScriptCommand *)command
36+
{
37+
NSString *repository = [command directParameter];
38+
if (repository) {
39+
NSDictionary *arguments = [command arguments];
40+
NSURL *destinationURL = [arguments objectForKey:kGitXCloneDestinationURLKey];
41+
if (destinationURL) {
42+
BOOL isBare = [[arguments objectForKey:kGitXCloneIsBareKey] boolValue];
43+
44+
[PBCloneRepositoryPanel beginCloneRepository:repository toURL:destinationURL isBare:isBare];
45+
}
46+
}
47+
}
48+
2549
@end

PBCloneRepositoryPanel.h

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
}
2424

2525
+ (id) panel;
26+
+ (void)beginCloneRepository:(NSString *)repository toURL:(NSURL *)targetURL isBare:(BOOL)bare;
2627

2728
- (void)showMessageSheet:(NSString *)messageText infoText:(NSString *)infoText;
2829
- (void)showErrorSheet:(NSError *)error;
@@ -38,4 +39,6 @@
3839
@property (assign) IBOutlet NSTextField *errorMessage;
3940
@property (assign) IBOutlet NSView *repositoryAccessoryView;
4041

42+
@property (assign) BOOL isBare;
43+
4144
@end

PBCloneRepositoryPanel.m

+17
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ @implementation PBCloneRepositoryPanel
2121
@synthesize errorMessage;
2222
@synthesize repositoryAccessoryView;
2323

24+
@synthesize isBare;
25+
2426

2527

2628
#pragma mark -
@@ -31,6 +33,21 @@ + (id) panel
3133
return [[self alloc] initWithWindowNibName:@"PBCloneRepositoryPanel"];
3234
}
3335

36+
+ (void)beginCloneRepository:(NSString *)repository toURL:(NSURL *)targetURL isBare:(BOOL)bare
37+
{
38+
if (!repository || [repository isEqualToString:@""] || !targetURL || [[targetURL path] isEqualToString:@""])
39+
return;
40+
41+
PBCloneRepositoryPanel *clonePanel = [PBCloneRepositoryPanel panel];
42+
[clonePanel showWindow:self];
43+
44+
[clonePanel.repositoryURL setStringValue:repository];
45+
[clonePanel.destinationPath setStringValue:[targetURL path]];
46+
clonePanel.isBare = bare;
47+
48+
[clonePanel clone:self];
49+
}
50+
3451

3552
- (void) awakeFromNib
3653
{

PBRepositoryDocumentController.h

+1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@
1616
}
1717

1818
- (id) documentForLocation:(NSURL*) url;
19+
- (void)initNewRepositoryAtURL:(NSURL *)url;
1920
@end

PBRepositoryDocumentController.m

+11-10
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ - (id) documentForLocation:(NSURL*) url
4747
return document;
4848
}
4949

50+
- (void)initNewRepositoryAtURL:(NSURL *)url
51+
{
52+
int terminationStatus;
53+
NSString *result = [PBEasyPipe outputForCommand:[PBGitBinary path] withArgs:[NSArray arrayWithObjects:@"init", @"-q", nil] inDir:[url path] retValue:&terminationStatus];
54+
55+
if (terminationStatus == 0)
56+
[self openDocumentWithContentsOfURL:url display:YES error:NULL];
57+
else
58+
NSRunAlertPanel(@"Failed to create new Git repository", @"Git returned the following error when trying to create the repository: %@", nil, nil, nil, result);
59+
}
5060

5161
- (IBAction)newDocument:(id)sender
5262
{
@@ -58,16 +68,7 @@ - (IBAction)newDocument:(id)sender
5868
[op setMessage:@"Initialize a repository here:"];
5969
[op setTitle:@"New Repository"];
6070
if ([op runModal] == NSFileHandlingPanelOKButton)
61-
{
62-
NSString *path = [op filename];
63-
int terminationStatus;
64-
NSString *result = [PBEasyPipe outputForCommand:[PBGitBinary path] withArgs:[NSArray arrayWithObjects:@"init", @"-q", nil] inDir:path inputString:nil retValue:&terminationStatus];
65-
66-
if (terminationStatus == 0)
67-
[self openDocumentWithContentsOfURL:[op URL] display:YES error:NULL];
68-
else
69-
NSRunAlertPanel(@"Failed to create new Git repository", @"Git returned the following error when trying to create the repository: %@", nil, nil, nil, result);
70-
}
71+
[self initNewRepositoryAtURL:[op URL]];
7172
}
7273

7374

gitx.m

+59-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ void usage(char const *programName)
2323
printf(" or: %s (--all|--local|--branch) [branch/tag]\n", programName);
2424
printf(" or: %s <revlist options>\n", programName);
2525
printf(" or: %s (--diff)\n", programName);
26+
printf(" or: %s (--init)\n", programName);
27+
printf(" or: %s (--clone <repository> [destination])\n", programName);
2628
printf("\n");
2729
printf(" -h, --help print this help\n");
2830
printf(" -v, --version prints version info for both GitX and git\n");
@@ -60,6 +62,15 @@ void usage(char const *programName)
6062
printf(" shows the diff in a window in GitX\n");
6163
printf(" git diff [options] | gitx\n");
6264
printf(" use gitx to pipe diff output to a GitX window\n");
65+
printf("\n");
66+
printf("Creating repositories\n");
67+
printf(" These commands will create a git repository and then open it up in GitX\n");
68+
printf("\n");
69+
printf(" --init creates (or reinitializes) a git repository\n");
70+
printf(" --clone <repository URL> [destination path]\n");
71+
printf(" clones the repository (at the specified URL) into the current\n");
72+
printf(" directory or into the specified path\n");
73+
printf("\n");
6374
exit(1);
6475
}
6576

@@ -157,6 +168,37 @@ void handleOpenRepository(NSURL *repositoryURL, NSMutableArray *arguments)
157168
}
158169
}
159170

171+
void handleInit(NSURL *repositoryURL)
172+
{
173+
GitXApplication *gitXApp = [SBApplication applicationWithBundleIdentifier:kGitXBundleIdentifier];
174+
[gitXApp initRepository:repositoryURL];
175+
176+
exit(0);
177+
}
178+
179+
void handleClone(NSURL *repositoryURL, NSMutableArray *arguments)
180+
{
181+
if ([arguments count]) {
182+
NSString *repository = [arguments objectAtIndex:0];
183+
184+
if ([arguments count] > 1) {
185+
NSURL *url = [NSURL fileURLWithPath:[arguments objectAtIndex:1]];
186+
if (url)
187+
repositoryURL = url;
188+
}
189+
190+
GitXApplication *gitXApp = [SBApplication applicationWithBundleIdentifier:kGitXBundleIdentifier];
191+
[gitXApp cloneRepository:repository to:repositoryURL isBare:NO];
192+
}
193+
else {
194+
printf("Error: --clone needs the URL of the repository to clone.\n");
195+
exit(2);
196+
}
197+
198+
199+
exit(0);
200+
}
201+
160202

161203
#pragma mark -
162204
#pragma mark main
@@ -228,10 +270,23 @@ int main(int argc, const char** argv)
228270
NSMutableArray *arguments = argumentsArray();
229271
NSURL *wdURL = workingDirectoryURL(arguments);
230272

231-
if ([arguments count] > 0 && ([[arguments objectAtIndex:0] isEqualToString:@"--diff"] ||
232-
[[arguments objectAtIndex:0] isEqualToString:@"-d"])) {
233-
[arguments removeObjectAtIndex:0];
234-
handleDiffWithArguments(wdURL, arguments);
273+
if ([arguments count]) {
274+
NSString *firstArgument = [arguments objectAtIndex:0];
275+
276+
if ([firstArgument isEqualToString:@"--diff"] || [firstArgument isEqualToString:@"-d"]) {
277+
[arguments removeObjectAtIndex:0];
278+
handleDiffWithArguments(wdURL, arguments);
279+
}
280+
281+
if ([firstArgument isEqualToString:@"--init"]) {
282+
[arguments removeObjectAtIndex:0];
283+
handleInit(wdURL);
284+
}
285+
286+
if ([firstArgument isEqualToString:@"--clone"]) {
287+
[arguments removeObjectAtIndex:0];
288+
handleClone(wdURL, arguments);
289+
}
235290
}
236291

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

0 commit comments

Comments
 (0)