Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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.
  • Loading branch information...
commit 17f50e3f7e62bb2f241fcc1a4b746349f5c885ce 1 parent b923272
@brotherbard brotherbard authored
View
2  GitX.h
@@ -28,6 +28,8 @@
- (void) quit; // Quit the application.
- (BOOL) exists:(id)x; // Verify that an object exists.
- (void) showDiff:(NSString *)x; // Show the supplied diff output in a GitX window.
+- (void) initRepository:(NSURL *)x; // Create a git repository at the given filesystem URL.
+- (void) cloneRepository:(NSString *)x to:(NSURL *)to isBare:(BOOL)isBare; // Clone a repository.
@end
View
12 GitX.sdef
@@ -160,6 +160,18 @@
<command name="show diff" code="GitXShDf" description="Show the supplied diff output in a GitX window.">
<direct-parameter type="text" description="The textual output from a diff tool."/>
</command>
+ <command name="init repository" code="GitXInit" description="Create a git repository at the given filesystem URL.">
+ <direct-parameter type="file" description="The URL of the repository to clone."/>
+ </command>
+ <command name="clone repository" code="GitXClon" description="Clone a repository.">
+ <direct-parameter type="text" description="The URL of the repository to clone."/>
+ <parameter name="to" code="URL " type="file" description="The location for the new repository.">
+ <cocoa key="destinationURL"/>
+ </parameter>
+ <parameter name="is bare" code="Bare" type="boolean" optional="yes" description="Indicates whether the created repository should be a bare repository.">
+ <cocoa key="isBare"/>
+ </parameter>
+ </command>
</suite>
View
5 GitXScriptingConstants.h
@@ -9,4 +9,7 @@
#define kGitXBundleIdentifier @"nl.frim.GitX"
-#define kGitXAEKeyArgumentsList 'ARGS'
+#define kGitXAEKeyArgumentsList 'ARGS'
+
+#define kGitXCloneDestinationURLKey @"destinationURL"
+#define kGitXCloneIsBareKey @"isBare"
View
2  NSApplication+GitXScripting.h
@@ -12,5 +12,7 @@
@interface NSApplication (GitXScripting)
- (void)showDiffScriptCommand:(NSScriptCommand *)command;
+- (void)initRepositoryScriptCommand:(NSScriptCommand *)command;
+- (void)cloneRepositoryScriptCommand:(NSScriptCommand *)command;
@end
View
24 NSApplication+GitXScripting.m
@@ -7,7 +7,10 @@
//
#import "NSApplication+GitXScripting.h"
+#import "GitXScriptingConstants.h"
#import "PBDiffWindowController.h"
+#import "PBRepositoryDocumentController.h"
+#import "PBCloneRepositoryPanel.h"
@implementation NSApplication (GitXScripting)
@@ -22,4 +25,25 @@ - (void)showDiffScriptCommand:(NSScriptCommand *)command
}
}
+- (void)initRepositoryScriptCommand:(NSScriptCommand *)command
+{
+ NSURL *repositoryURL = [command directParameter];
+ if (repositoryURL)
+ [[PBRepositoryDocumentController sharedDocumentController] initNewRepositoryAtURL:repositoryURL];
+}
+
+- (void)cloneRepositoryScriptCommand:(NSScriptCommand *)command
+{
+ NSString *repository = [command directParameter];
+ if (repository) {
+ NSDictionary *arguments = [command arguments];
+ NSURL *destinationURL = [arguments objectForKey:kGitXCloneDestinationURLKey];
+ if (destinationURL) {
+ BOOL isBare = [[arguments objectForKey:kGitXCloneIsBareKey] boolValue];
+
+ [PBCloneRepositoryPanel beginCloneRepository:repository toURL:destinationURL isBare:isBare];
+ }
+ }
+}
+
@end
View
3  PBCloneRepositoryPanel.h
@@ -23,6 +23,7 @@
}
+ (id) panel;
++ (void)beginCloneRepository:(NSString *)repository toURL:(NSURL *)targetURL isBare:(BOOL)bare;
- (void)showMessageSheet:(NSString *)messageText infoText:(NSString *)infoText;
- (void)showErrorSheet:(NSError *)error;
@@ -38,4 +39,6 @@
@property (assign) IBOutlet NSTextField *errorMessage;
@property (assign) IBOutlet NSView *repositoryAccessoryView;
+@property (assign) BOOL isBare;
+
@end
View
17 PBCloneRepositoryPanel.m
@@ -21,6 +21,8 @@ @implementation PBCloneRepositoryPanel
@synthesize errorMessage;
@synthesize repositoryAccessoryView;
+@synthesize isBare;
+
#pragma mark -
@@ -31,6 +33,21 @@ + (id) panel
return [[self alloc] initWithWindowNibName:@"PBCloneRepositoryPanel"];
}
++ (void)beginCloneRepository:(NSString *)repository toURL:(NSURL *)targetURL isBare:(BOOL)bare
+{
+ if (!repository || [repository isEqualToString:@""] || !targetURL || [[targetURL path] isEqualToString:@""])
+ return;
+
+ PBCloneRepositoryPanel *clonePanel = [PBCloneRepositoryPanel panel];
+ [clonePanel showWindow:self];
+
+ [clonePanel.repositoryURL setStringValue:repository];
+ [clonePanel.destinationPath setStringValue:[targetURL path]];
+ clonePanel.isBare = bare;
+
+ [clonePanel clone:self];
+}
+
- (void) awakeFromNib
{
View
1  PBRepositoryDocumentController.h
@@ -16,4 +16,5 @@
}
- (id) documentForLocation:(NSURL*) url;
+- (void)initNewRepositoryAtURL:(NSURL *)url;
@end
View
21 PBRepositoryDocumentController.m
@@ -47,6 +47,16 @@ - (id) documentForLocation:(NSURL*) url
return document;
}
+- (void)initNewRepositoryAtURL:(NSURL *)url
+{
+ int terminationStatus;
+ NSString *result = [PBEasyPipe outputForCommand:[PBGitBinary path] withArgs:[NSArray arrayWithObjects:@"init", @"-q", nil] inDir:[url path] retValue:&terminationStatus];
+
+ if (terminationStatus == 0)
+ [self openDocumentWithContentsOfURL:url display:YES error:NULL];
+ else
+ NSRunAlertPanel(@"Failed to create new Git repository", @"Git returned the following error when trying to create the repository: %@", nil, nil, nil, result);
+}
- (IBAction)newDocument:(id)sender
{
@@ -58,16 +68,7 @@ - (IBAction)newDocument:(id)sender
[op setMessage:@"Initialize a repository here:"];
[op setTitle:@"New Repository"];
if ([op runModal] == NSFileHandlingPanelOKButton)
- {
- NSString *path = [op filename];
- int terminationStatus;
- NSString *result = [PBEasyPipe outputForCommand:[PBGitBinary path] withArgs:[NSArray arrayWithObjects:@"init", @"-q", nil] inDir:path inputString:nil retValue:&terminationStatus];
-
- if (terminationStatus == 0)
- [self openDocumentWithContentsOfURL:[op URL] display:YES error:NULL];
- else
- NSRunAlertPanel(@"Failed to create new Git repository", @"Git returned the following error when trying to create the repository: %@", nil, nil, nil, result);
- }
+ [self initNewRepositoryAtURL:[op URL]];
}
View
63 gitx.m
@@ -23,6 +23,8 @@ void usage(char const *programName)
printf(" or: %s (--all|--local|--branch) [branch/tag]\n", programName);
printf(" or: %s <revlist options>\n", programName);
printf(" or: %s (--diff)\n", programName);
+ printf(" or: %s (--init)\n", programName);
+ printf(" or: %s (--clone <repository> [destination])\n", programName);
printf("\n");
printf(" -h, --help print this help\n");
printf(" -v, --version prints version info for both GitX and git\n");
@@ -60,6 +62,15 @@ void usage(char const *programName)
printf(" shows the diff in a window in GitX\n");
printf(" git diff [options] | gitx\n");
printf(" use gitx to pipe diff output to a GitX window\n");
+ printf("\n");
+ printf("Creating repositories\n");
+ printf(" These commands will create a git repository and then open it up in GitX\n");
+ printf("\n");
+ printf(" --init creates (or reinitializes) a git repository\n");
+ printf(" --clone <repository URL> [destination path]\n");
+ printf(" clones the repository (at the specified URL) into the current\n");
+ printf(" directory or into the specified path\n");
+ printf("\n");
exit(1);
}
@@ -157,6 +168,37 @@ void handleOpenRepository(NSURL *repositoryURL, NSMutableArray *arguments)
}
}
+void handleInit(NSURL *repositoryURL)
+{
+ GitXApplication *gitXApp = [SBApplication applicationWithBundleIdentifier:kGitXBundleIdentifier];
+ [gitXApp initRepository:repositoryURL];
+
+ exit(0);
+}
+
+void handleClone(NSURL *repositoryURL, NSMutableArray *arguments)
+{
+ if ([arguments count]) {
+ NSString *repository = [arguments objectAtIndex:0];
+
+ if ([arguments count] > 1) {
+ NSURL *url = [NSURL fileURLWithPath:[arguments objectAtIndex:1]];
+ if (url)
+ repositoryURL = url;
+ }
+
+ GitXApplication *gitXApp = [SBApplication applicationWithBundleIdentifier:kGitXBundleIdentifier];
+ [gitXApp cloneRepository:repository to:repositoryURL isBare:NO];
+ }
+ else {
+ printf("Error: --clone needs the URL of the repository to clone.\n");
+ exit(2);
+ }
+
+
+ exit(0);
+}
+
#pragma mark -
#pragma mark main
@@ -228,10 +270,23 @@ int main(int argc, const char** argv)
NSMutableArray *arguments = argumentsArray();
NSURL *wdURL = workingDirectoryURL(arguments);
- if ([arguments count] > 0 && ([[arguments objectAtIndex:0] isEqualToString:@"--diff"] ||
- [[arguments objectAtIndex:0] isEqualToString:@"-d"])) {
- [arguments removeObjectAtIndex:0];
- handleDiffWithArguments(wdURL, arguments);
+ if ([arguments count]) {
+ NSString *firstArgument = [arguments objectAtIndex:0];
+
+ if ([firstArgument isEqualToString:@"--diff"] || [firstArgument isEqualToString:@"-d"]) {
+ [arguments removeObjectAtIndex:0];
+ handleDiffWithArguments(wdURL, arguments);
+ }
+
+ if ([firstArgument isEqualToString:@"--init"]) {
+ [arguments removeObjectAtIndex:0];
+ handleInit(wdURL);
+ }
+
+ if ([firstArgument isEqualToString:@"--clone"]) {
+ [arguments removeObjectAtIndex:0];
+ handleClone(wdURL, arguments);
+ }
}
// No commands handled by gitx, open the current dir in GitX with the arguments
Please sign in to comment.
Something went wrong with that request. Please try again.