[PATCH] Teach git to change to a given directory using -C option

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This is similar in spirit to to "make -C dir ..." and "tar -C dir ...".

Signed-off-by: Nazri Ramliy <ayiehere@xxxxxxxxx>
---
Often I find myself needing to find out quickly the status of a repository that
is not in my currenct working directory, like this:

         $ (cd ~/foo; git log -1)

With this patch now i can simply do:

         $ git -C ~/.zsh log -1 

That's just one example. I think those who are familiar with the -C arguments
to "make" and "tar" commands would get the "handiness" of having this option in
git.

 Documentation/git.txt |  3 +++
 git.c                 | 11 ++++++++++-
 t/t0050-filesystem.sh |  9 +++++++++
 3 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/Documentation/git.txt b/Documentation/git.txt
index 6a875f2..20bba86 100644
--- a/Documentation/git.txt
+++ b/Documentation/git.txt
@@ -379,6 +379,9 @@ displayed. See linkgit:git-help[1] for more information,
 because `git --help ...` is converted internally into `git
 help ...`.
 
+-C <directory>::
+	Change to given directory before doing anything else.
+
 -c <name>=<value>::
 	Pass a configuration parameter to the command. The value
 	given will override values from configuration files.
diff --git a/git.c b/git.c
index 1ada169..6426a2e 100644
--- a/git.c
+++ b/git.c
@@ -53,7 +53,16 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
 		/*
 		 * Check remaining flags.
 		 */
-		if (!prefixcmp(cmd, "--exec-path")) {
+		if (!prefixcmp(cmd, "-C")) {
+			if (*argc < 2) {
+				fprintf(stderr, "No directory given for -C.\n" );
+				usage(git_usage_string);
+			}
+			if (chdir((*argv)[1]))
+				die_errno("Cannot change to '%s'", (*argv)[1]);
+			(*argv)++;
+			(*argc)--;
+		} else if (!prefixcmp(cmd, "--exec-path")) {
 			cmd += 11;
 			if (*cmd == '=')
 				git_set_argv_exec_path(cmd + 1);
diff --git a/t/t0050-filesystem.sh b/t/t0050-filesystem.sh
index 05d78d2..ef1cb75 100755
--- a/t/t0050-filesystem.sh
+++ b/t/t0050-filesystem.sh
@@ -88,6 +88,15 @@ test_expect_failure CASE_INSENSITIVE_FS 'add (with different case)' '
 	test "z$(git cat-file blob :$camel)" = z1
 '
 
+test_expect_success 'git -C <dir> changes directory to <dir>' '
+	test_create_repo dir1 &&
+	echo 1 >dir1/a.txt &&
+	git -C dir1 add a.txt &&
+	git -C dir1 commit -m "initial in dir1" &&
+	t1=$(git -C dir1 log --format=%s) &&
+	test "$t1" = "initial in dir1"
+'
+
 test_expect_success "setup unicode normalization tests" '
 	test_create_repo unicode &&
 	cd unicode &&
-- 
1.8.2.1.339.g52a3e01

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]