From 17f50e3f7e62bb2f241fcc1a4b746349f5c885ce Mon Sep 17 00:00:00 2001 From: Nathan Kinsinger Date: Sat, 21 Aug 2010 18:50:12 -0600 Subject: [PATCH] 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. --- GitX.h | 2 + GitX.sdef | 12 ++++++ GitXScriptingConstants.h | 5 ++- NSApplication+GitXScripting.h | 2 + NSApplication+GitXScripting.m | 24 ++++++++++++ PBCloneRepositoryPanel.h | 3 ++ PBCloneRepositoryPanel.m | 17 +++++++++ PBRepositoryDocumentController.h | 1 + PBRepositoryDocumentController.m | 21 ++++++----- gitx.m | 63 ++++++++++++++++++++++++++++++-- 10 files changed, 135 insertions(+), 15 deletions(-) diff --git a/GitX.h b/GitX.h index edae903..7825b7f 100644 --- a/GitX.h +++ b/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 diff --git a/GitX.sdef b/GitX.sdef index a0a0642..db0348d 100644 --- a/GitX.sdef +++ b/GitX.sdef @@ -160,6 +160,18 @@ + + + + + + + + + + + + diff --git a/GitXScriptingConstants.h b/GitXScriptingConstants.h index cba0ce8..7a786a4 100644 --- a/GitXScriptingConstants.h +++ b/GitXScriptingConstants.h @@ -9,4 +9,7 @@ #define kGitXBundleIdentifier @"nl.frim.GitX" -#define kGitXAEKeyArgumentsList 'ARGS' \ No newline at end of file +#define kGitXAEKeyArgumentsList 'ARGS' + +#define kGitXCloneDestinationURLKey @"destinationURL" +#define kGitXCloneIsBareKey @"isBare" \ No newline at end of file diff --git a/NSApplication+GitXScripting.h b/NSApplication+GitXScripting.h index d5cab8c..fafab7e 100644 --- a/NSApplication+GitXScripting.h +++ b/NSApplication+GitXScripting.h @@ -12,5 +12,7 @@ @interface NSApplication (GitXScripting) - (void)showDiffScriptCommand:(NSScriptCommand *)command; +- (void)initRepositoryScriptCommand:(NSScriptCommand *)command; +- (void)cloneRepositoryScriptCommand:(NSScriptCommand *)command; @end diff --git a/NSApplication+GitXScripting.m b/NSApplication+GitXScripting.m index 7c8489c..f3bef65 100644 --- a/NSApplication+GitXScripting.m +++ b/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)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 diff --git a/PBCloneRepositoryPanel.h b/PBCloneRepositoryPanel.h index 7c7d102..8b29551 100644 --- a/PBCloneRepositoryPanel.h +++ b/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 diff --git a/PBCloneRepositoryPanel.m b/PBCloneRepositoryPanel.m index 20baaa2..a2aaa51 100644 --- a/PBCloneRepositoryPanel.m +++ b/PBCloneRepositoryPanel.m @@ -21,6 +21,8 @@ @synthesize errorMessage; @synthesize repositoryAccessoryView; +@synthesize isBare; + #pragma mark - @@ -31,6 +33,21 @@ 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 { diff --git a/PBRepositoryDocumentController.h b/PBRepositoryDocumentController.h index d0660e2..1bbaab3 100644 --- a/PBRepositoryDocumentController.h +++ b/PBRepositoryDocumentController.h @@ -16,4 +16,5 @@ } - (id) documentForLocation:(NSURL*) url; +- (void)initNewRepositoryAtURL:(NSURL *)url; @end diff --git a/PBRepositoryDocumentController.m b/PBRepositoryDocumentController.m index 83bd887..cfaeac5 100644 --- a/PBRepositoryDocumentController.m +++ b/PBRepositoryDocumentController.m @@ -47,6 +47,16 @@ 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 @@ [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]]; } diff --git a/gitx.m b/gitx.m index 5f17138..ecb9a6f 100644 --- a/gitx.m +++ b/gitx.m @@ -23,6 +23,8 @@ void usage(char const *programName) printf(" or: %s (--all|--local|--branch) [branch/tag]\n", programName); printf(" or: %s \n", programName); printf(" or: %s (--diff)\n", programName); + printf(" or: %s (--init)\n", programName); + printf(" or: %s (--clone [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 [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