Skip to content

Commit 44e1e4d

Browse files
nazrigitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e230c56 commit 44e1e4d

File tree

3 files changed

+109
-2
lines changed

3 files changed

+109
-2
lines changed

Documentation/git.txt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ git - the stupid content tracker
99
SYNOPSIS
1010
--------
1111
[verse]
12-
'git' [--version] [--help] [-c <name>=<value>]
12+
'git' [--version] [--help] [-C <path>] [-c <name>=<value>]
1313
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
1414
[-p|--paginate|--no-pager] [--no-replace-objects] [--bare]
1515
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
@@ -395,6 +395,20 @@ displayed. See linkgit:git-help[1] for more information,
395395
because `git --help ...` is converted internally into `git
396396
help ...`.
397397

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+
398412
-c <name>=<value>::
399413
Pass a configuration parameter to the command. The value
400414
given will override values from configuration files.

git.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "commit.h"
88

99
const char git_usage_string[] =
10-
"git [--version] [--help] [-c name=value]\n"
10+
"git [--version] [--help] [-C <path>] [-c name=value]\n"
1111
" [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]\n"
1212
" [-p|--paginate|--no-pager] [--no-replace-objects] [--bare]\n"
1313
" [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]\n"
@@ -153,6 +153,17 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
153153
set_alternate_shallow_file((*argv)[0]);
154154
if (envchanged)
155155
*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)--;
156167
} else {
157168
fprintf(stderr, "Unknown option: %s\n", cmd);
158169
usage(git_usage_string);

t/t0056-git-C.sh

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)