Skip to content

Commit

Permalink
Add --init and --clone to gitx CLI and Applescript
Browse files Browse the repository at this point in the history
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
brotherbard committed Sep 13, 2010
1 parent b923272 commit 17f50e3
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 15 deletions.
2 changes: 2 additions & 0 deletions GitX.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 12 additions & 0 deletions GitX.sdef
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
5 changes: 4 additions & 1 deletion GitXScriptingConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@
#define kGitXBundleIdentifier @"nl.frim.GitX"


#define kGitXAEKeyArgumentsList 'ARGS'
#define kGitXAEKeyArgumentsList 'ARGS'

#define kGitXCloneDestinationURLKey @"destinationURL"
#define kGitXCloneIsBareKey @"isBare"
2 changes: 2 additions & 0 deletions NSApplication+GitXScripting.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@
@interface NSApplication (GitXScripting)

- (void)showDiffScriptCommand:(NSScriptCommand *)command;
- (void)initRepositoryScriptCommand:(NSScriptCommand *)command;
- (void)cloneRepositoryScriptCommand:(NSScriptCommand *)command;

@end
24 changes: 24 additions & 0 deletions NSApplication+GitXScripting.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
//

#import "NSApplication+GitXScripting.h"
#import "GitXScriptingConstants.h"
#import "PBDiffWindowController.h"
#import "PBRepositoryDocumentController.h"
#import "PBCloneRepositoryPanel.h"


@implementation NSApplication (GitXScripting)
Expand All @@ -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
3 changes: 3 additions & 0 deletions PBCloneRepositoryPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,4 +39,6 @@
@property (assign) IBOutlet NSTextField *errorMessage;
@property (assign) IBOutlet NSView *repositoryAccessoryView;

@property (assign) BOOL isBare;

@end
17 changes: 17 additions & 0 deletions PBCloneRepositoryPanel.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ @implementation PBCloneRepositoryPanel
@synthesize errorMessage;
@synthesize repositoryAccessoryView;

@synthesize isBare;



#pragma mark -
Expand All @@ -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
{
Expand Down
1 change: 1 addition & 0 deletions PBRepositoryDocumentController.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
}

- (id) documentForLocation:(NSURL*) url;
- (void)initNewRepositoryAtURL:(NSURL *)url;
@end
21 changes: 11 additions & 10 deletions PBRepositoryDocumentController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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]];
}


Expand Down
63 changes: 59 additions & 4 deletions gitx.m
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 17f50e3

Please sign in to comment.