From ea29959be7cfe0f9510398d6972851b5cf192a2f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Daniel=20Rodr=C3=ADguez=20Troiti=C3=B1o?= <drodrigueztroitino@yahoo.es>
Date: Sat, 13 Feb 2010 17:45:26 +0100
Subject: [PATCH] Add support for --git-dir in the gitx CLI tool.

--git-dir switch checks that a directory is
specified after the equal, if not, it shows the
usage info.

Also expands the tilde (isn't suppose the shell to
do this?), and assumes absolute path if the path
starts with a slash /.
---
 gitx.m |   26 ++++++++++++++++++++++----
 1 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/gitx.m b/gitx.m
index 6449556..8fcba2c 100644
--- a/gitx.m
+++ b/gitx.m
@@ -49,8 +49,8 @@ void usage(char const *programName)
 {
 
 	printf("Usage: %s (--help|--version)\n", programName);
-	printf("   or: %s (--commit|-h)\n", programName);
-	printf("   or: %s <revlist options>\n", programName);
+	printf("   or: %s [--git-dir=GIT_DIR] (--commit|-h)\n", programName);
+	printf("   or: %s [--git-dir=GIT_DIR] <revlist options>\n", programName);
 	printf("\n");
 	printf("\t-h, --help          print this help\n");
 	printf("\t--commit, -c        start GitX in commit mode\n");
@@ -113,12 +113,19 @@ void handleDiffWithArguments(NSArray *arguments, NSString *directory, id<GitXCli
 
 int main(int argc, const char** argv)
 {
+	NSString *gitDir = nil;
 	if (argc >= 2 && (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")))
 		usage(argv[0]);
 	if (argc >= 2 && (!strcmp(argv[1], "--version") || !strcmp(argv[1], "-v")))
 		version_info();
 	if (argc >= 2 && !strcmp(argv[1], "--git-path"))
 		git_path();
+	if (argc >= 2 && strstr(argv[1], "--git-dir=")) {
+		if (strlen(argv[1]) < 11)
+			usage(argv[0]);
+		gitDir = [[NSString stringWithUTF8String:&argv[1][10]]
+				  stringByExpandingTildeInPath];
+	}
 
 	if (![PBGitBinary path]) {
 		printf("%s\n", [[PBGitBinary notFoundError] cStringUsingEncoding:NSUTF8StringEncoding]);
@@ -130,6 +137,9 @@ int main(int argc, const char** argv)
 
 	// Create arguments
 	argv++; argc--;
+	if (gitDir) {
+		argv++; argc--;
+	}
 	NSMutableArray* arguments = [NSMutableArray arrayWithCapacity:argc];
 	int i = 0;
 	for (i = 0; i < argc; i++)
@@ -143,12 +153,20 @@ int main(int argc, const char** argv)
 	if (!pwd)
 		exit(2);
 
+	// If the gitDir starts with / is suppose to be an absolute path, so we
+	// don't need to interpolate.
+	if (!gitDir) {
+		gitDir = pwd;
+	} else if (![gitDir hasPrefix:@"/"]) {
+		gitDir = [pwd stringByAppendingPathComponent:gitDir];
+	}
+
 	if ([arguments count] > 0 && ([[arguments objectAtIndex:0] isEqualToString:@"--diff"] ||
 		[[arguments objectAtIndex:0] isEqualToString:@"-d"]))
-		handleDiffWithArguments([arguments subarrayWithRange:NSMakeRange(1, [arguments count] - 1)], pwd, proxy);
+		handleDiffWithArguments([arguments subarrayWithRange:NSMakeRange(1, [arguments count] - 1)], gitDir, proxy);
 
 	// No diff, just open the current dir
-	NSURL* url = [NSURL fileURLWithPath:pwd];
+	NSURL* url = [NSURL fileURLWithPath:gitDir];
 	NSError* error = nil;
 
 	if (![proxy openRepository:url arguments: arguments error:&error]) {
-- 
1.6.4.4

