git: run in a directory given with -C option

This is similar in spirit to "make -C dir ..." and "tar -C dir ...". It takes more keypresses to invoke git command in a different directory without leaving the current directory: 1. (cd ~/foo && git status) git --git-dir=~/foo/.git --work-dir=~/foo status GIT_DIR=~/foo/.git GIT_WORK_TREE=~/foo git status 2. (cd ../..; git grep foo) 3. for d in d1 d2 d3; do (cd $d && git svn rebase); done The methods shown above are acceptable for scripting but are too cumbersome for quick command line invocations. With this new option, the above can be done with fewer keystrokes: 1. git -C ~/foo status 2. git -C ../.. grep foo 3. for d in d1 d2 d3; do git -C $d svn rebase; done A new test script is added to verify the behavior of this option with other path-related options like --git-dir and --work-tree. Signed-off-by: Nazri Ramliy <ayiehere@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nazri Ramliy committed Sep 9, 2013 at 21:47 UTC 44e1e4d67d5148c245db362cc48c3cc6c2ec82ca
3 files changed +109 -2
Documentation/git.txt
+15 -1
@@ -9,7 +9,7 @@ git - the stupid content tracker
9 SYNOPSIS
10 --------
11 [verse]
12 -'git' [--version] [--help] [-c <name>=<value>]
12 +'git' [--version] [--help] [-C <path>] [-c <name>=<value>]
13 [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
14 [-p|--paginate|--no-pager] [--no-replace-objects] [--bare]
15 [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
@@ -395,6 +395,20 @@ displayed. See linkgit:git-help[1] for more information,
395 because `git --help ...` is converted internally into `git
396 help ...`.
397
398 +-C <path>::
399 + Run as if git was started in '<path>' instead of the current working
400 + directory. When multiple `-C` options are given, each subsequent
401 + non-absolute `-C <path>` is interpreted relative to the preceding `-C
402 + <path>`.
403 ++
404 +This option affects options that expect path name like `--git-dir` and
405 +`--work-tree` in that their interpretations of the path names would be
406 +made relative to the working directory caused by the `-C` option. For
407 +example the following invocations are equivalent:
408 +
409 + git --git-dir=a.git --work-tree=b -C c status
410 + git --git-dir=c/a.git --work-tree=c/b status
411 +
412 -c <name>=<value>::
413 Pass a configuration parameter to the command. The value
414 given will override values from configuration files.
git.c
+12 -1
@@ -7,7 +7,7 @@
7 #include "commit.h"
8
9 const char git_usage_string[] =
10 - "git [--version] [--help] [-c name=value]\n"
10 + "git [--version] [--help] [-C <path>] [-c name=value]\n"
11 " [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]\n"
12 " [-p|--paginate|--no-pager] [--no-replace-objects] [--bare]\n"
13 " [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]\n"
@@ -153,6 +153,17 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
153 set_alternate_shallow_file((*argv)[0]);
154 if (envchanged)
155 *envchanged = 1;
156 + } else if (!strcmp(cmd, "-C")) {
157 + if (*argc < 2) {
158 + fprintf(stderr, "No directory given for -C.\n" );
159 + usage(git_usage_string);
160 + }
161 + if (chdir((*argv)[1]))
162 + die_errno("Cannot change to '%s'", (*argv)[1]);
163 + if (envchanged)
164 + *envchanged = 1;
165 + (*argv)++;
166 + (*argc)--;
167 } else {
168 fprintf(stderr, "Unknown option: %s\n", cmd);
169 usage(git_usage_string);
t/t0056-git-C.sh new
+82
@@ -0,0 +1,82 @@
1 +#!/bin/sh
2 +
3 +test_description='"-C <path>" option and its effects on other path-related options'
4 +
5 +. ./test-lib.sh
6 +
7 +test_expect_success '"git -C <path>" runs git from the directory <path>' '
8 + test_create_repo dir1 &&
9 + echo 1 >dir1/a.txt &&
10 + (cd dir1 && git add a.txt && git commit -m "initial in dir1") &&
11 + echo "initial in dir1" >expected &&
12 + git -C dir1 log --format=%s >actual &&
13 + test_cmp expected actual
14 +'
15 +
16 +test_expect_success 'Multiple -C options: "-C dir1 -C dir2" is equivalent to "-C dir1/dir2"' '
17 + test_create_repo dir1/dir2 &&
18 + echo 1 >dir1/dir2/a.txt &&
19 + git -C dir1/dir2 add a.txt &&
20 + echo "initial in dir1/dir2" >expected &&
21 + git -C dir1/dir2 commit -m "initial in dir1/dir2" &&
22 + git -C dir1 -C dir2 log --format=%s >actual &&
23 + test_cmp expected actual
24 +'
25 +
26 +test_expect_success 'Effect on --git-dir option: "-C c --git-dir=a.git" is equivalent to "--git-dir c/a.git"' '
27 + mkdir c &&
28 + mkdir c/a &&
29 + mkdir c/a.git &&
30 + (cd c/a.git && git init --bare) &&
31 + echo 1 >c/a/a.txt &&
32 + git --git-dir c/a.git --work-tree=c/a add a.txt &&
33 + git --git-dir c/a.git --work-tree=c/a commit -m "initial" &&
34 + git --git-dir=c/a.git log -1 --format=%s >expected &&
35 + git -C c --git-dir=a.git log -1 --format=%s >actual &&
36 + test_cmp expected actual
37 +'
38 +
39 +test_expect_success 'Order should not matter: "--git-dir=a.git -C c" is equivalent to "-C c --git-dir=a.git"' '
40 + git -C c --git-dir=a.git log -1 --format=%s >expected &&
41 + git --git-dir=a.git -C c log -1 --format=%s >actual &&
42 + test_cmp expected actual
43 +'
44 +
45 +test_expect_success 'Effect on --work-tree option: "-C c/a.git --work-tree=../a" is equivalent to "--work-tree=c/a --git-dir=c/a.git"' '
46 + rm c/a/a.txt &&
47 + git --git-dir=c/a.git --work-tree=c/a status >expected &&
48 + git -C c/a.git --work-tree=../a status >actual &&
49 + test_cmp expected actual
50 +'
51 +
52 +test_expect_success 'Order should not matter: "--work-tree=../a -C c/a.git" is equivalent to "-C c/a.git --work-tree=../a"' '
53 + git -C c/a.git --work-tree=../a status >expected &&
54 + git --work-tree=../a -C c/a.git status >actual &&
55 + test_cmp expected actual
56 +'
57 +
58 +test_expect_success 'Effect on --git-dir and --work-tree options - "-C c --git-dir=a.git --work-tree=a" is equivalent to "--git-dir=c/a.git --work-tree=c/a"' '
59 + git --git-dir=c/a.git --work-tree=c/a status >expected &&
60 + git -C c --git-dir=a.git --work-tree=a status >actual &&
61 + test_cmp expected actual
62 +'
63 +
64 +test_expect_success 'Order should not matter: "-C c --git-dir=a.git --work-tree=a" is equivalent to "--git-dir=a.git -C c --work-tree=a"' '
65 + git -C c --git-dir=a.git --work-tree=a status >expected &&
66 + git --git-dir=a.git -C c --work-tree=a status >actual &&
67 + test_cmp expected actual
68 +'
69 +
70 +test_expect_success 'Order should not matter: "-C c --git-dir=a.git --work-tree=a" is equivalent to "--git-dir=a.git --work-tree=a -C c"' '
71 + git -C c --git-dir=a.git --work-tree=a status >expected &&
72 + git --git-dir=a.git --work-tree=a -C c status >actual &&
73 + test_cmp expected actual
74 +'
75 +
76 +test_expect_success 'Relative followed by fullpath: "-C ./here -C /there" is equivalent to "-C /there"' '
77 + echo "initial in dir1/dir2" >expected &&
78 + git -C dir1 -C "$(pwd)/dir1/dir2" log --format=%s >actual &&
79 + test_cmp expected actual
80 +'
81 +
82 +test_done