Skip to content

Commit b923272

Browse files
committed
Add "--git-dir=" to gitx CLI
Allows specifying a repository in a particular location for the gitx operation.
1 parent e60bb32 commit b923272

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

gitx.m

+34-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ void usage(char const *programName)
2828
printf(" -v, --version prints version info for both GitX and git\n");
2929
printf(" --git-path prints the path to the directory containing git\n");
3030
printf("\n");
31+
printf("Repository path\n");
32+
printf(" By default gitx opens the repository in the current directory.\n");
33+
printf(" Use --git-dir= to send commands to a repository somewhere else.\n");
34+
printf(" Note: This must be the first argument.\n");
35+
printf("\n");
36+
printf(" --git-dir=<path> [gitx commands]\n");
37+
printf(" send the gitx commands to the repository located at <path>\n");
38+
printf("\n");
3139
printf("Commit/Stage view\n");
3240
printf(" -c, --commit start GitX in commit/stage mode\n");
3341
printf("\n");
@@ -153,9 +161,32 @@ void handleOpenRepository(NSURL *repositoryURL, NSMutableArray *arguments)
153161
#pragma mark -
154162
#pragma mark main
155163

156-
NSURL *workingDirectoryURL()
164+
165+
#define kGitDirPrefix @"--git-dir="
166+
167+
NSURL *workingDirectoryURL(NSMutableArray *arguments)
157168
{
158-
NSString *path = [[[NSProcessInfo processInfo] environment] objectForKey:@"PWD"];
169+
NSString *path = nil;
170+
171+
if ([arguments count] && [[arguments objectAtIndex:0] hasPrefix:kGitDirPrefix]) {
172+
path = [[[arguments objectAtIndex:0] substringFromIndex:[kGitDirPrefix length]] stringByStandardizingPath];
173+
174+
// the path must exist and point to a directory
175+
BOOL isDirectory = YES;
176+
if (![[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory] || !isDirectory) {
177+
if (!isDirectory)
178+
printf("Fatal: --git-dir path does not point to a directory.\n");
179+
else
180+
printf("Fatal: --git-dir path does not exist.\n");
181+
printf("Cannot open git repository at path: '%s'\n", [path UTF8String]);
182+
exit(2);
183+
}
184+
185+
// remove the git-dir argument
186+
[arguments removeObjectAtIndex:0];
187+
} else {
188+
path = [[[NSProcessInfo processInfo] environment] objectForKey:@"PWD"];
189+
}
159190

160191
NSURL *url = [NSURL fileURLWithPath:path isDirectory:YES];
161192
if (!url) {
@@ -195,7 +226,7 @@ int main(int argc, const char** argv)
195226

196227
// From this point, we require a working directory and the arguments
197228
NSMutableArray *arguments = argumentsArray();
198-
NSURL *wdURL = workingDirectoryURL();
229+
NSURL *wdURL = workingDirectoryURL(arguments);
199230

200231
if ([arguments count] > 0 && ([[arguments objectAtIndex:0] isEqualToString:@"--diff"] ||
201232
[[arguments objectAtIndex:0] isEqualToString:@"-d"])) {

0 commit comments

Comments
 (0)