Skip to content

Commit 0873503

Browse files
committed
Merge branch 'nr/git-cd-to-a-directory'
Just like "make -C <directory>", make "git -C <directory> ..." to go there before doing anything else. * nr/git-cd-to-a-directory: t0056: "git -C" test updates git: run in a directory given with -C option
2 parents f26f250 + 28a81f8 commit 0873503

File tree

3 files changed

+111
-2
lines changed

3 files changed

+111
-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"
@@ -165,6 +165,17 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
165165
set_alternate_shallow_file((*argv)[0]);
166166
if (envchanged)
167167
*envchanged = 1;
168+
} else if (!strcmp(cmd, "-C")) {
169+
if (*argc < 2) {
170+
fprintf(stderr, "No directory given for -C.\n" );
171+
usage(git_usage_string);
172+
}
173+
if (chdir((*argv)[1]))
174+
die_errno("Cannot change to '%s'", (*argv)[1]);
175+
if (envchanged)
176+
*envchanged = 1;
177+
(*argv)++;
178+
(*argc)--;
168179
} else {
169180
fprintf(stderr, "Unknown option: %s\n", cmd);
170181
usage(git_usage_string);

t/t0056-git-C.sh

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
msg="initial in dir1" &&
11+
(cd dir1 && git add a.txt && git commit -m "$msg") &&
12+
echo "$msg" >expected &&
13+
git -C dir1 log --format=%s >actual &&
14+
test_cmp expected actual
15+
'
16+
17+
test_expect_success 'Multiple -C options: "-C dir1 -C dir2" is equivalent to "-C dir1/dir2"' '
18+
test_create_repo dir1/dir2 &&
19+
echo 1 >dir1/dir2/b.txt &&
20+
git -C dir1/dir2 add b.txt &&
21+
msg="initial in dir1/dir2" &&
22+
echo "$msg" >expected &&
23+
git -C dir1/dir2 commit -m "$msg" &&
24+
git -C dir1 -C dir2 log --format=%s >actual &&
25+
test_cmp expected actual
26+
'
27+
28+
test_expect_success 'Effect on --git-dir option: "-C c --git-dir=a.git" is equivalent to "--git-dir c/a.git"' '
29+
mkdir c &&
30+
mkdir c/a &&
31+
mkdir c/a.git &&
32+
(cd c/a.git && git init --bare) &&
33+
echo 1 >c/a/a.txt &&
34+
git --git-dir c/a.git --work-tree=c/a add a.txt &&
35+
git --git-dir c/a.git --work-tree=c/a commit -m "initial" &&
36+
git --git-dir=c/a.git log -1 --format=%s >expected &&
37+
git -C c --git-dir=a.git log -1 --format=%s >actual &&
38+
test_cmp expected actual
39+
'
40+
41+
test_expect_success 'Order should not matter: "--git-dir=a.git -C c" is equivalent to "-C c --git-dir=a.git"' '
42+
git -C c --git-dir=a.git log -1 --format=%s >expected &&
43+
git --git-dir=a.git -C c log -1 --format=%s >actual &&
44+
test_cmp expected actual
45+
'
46+
47+
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"' '
48+
rm c/a/a.txt &&
49+
git --git-dir=c/a.git --work-tree=c/a status >expected &&
50+
git -C c/a.git --work-tree=../a status >actual &&
51+
test_cmp expected actual
52+
'
53+
54+
test_expect_success 'Order should not matter: "--work-tree=../a -C c/a.git" is equivalent to "-C c/a.git --work-tree=../a"' '
55+
git -C c/a.git --work-tree=../a status >expected &&
56+
git --work-tree=../a -C c/a.git status >actual &&
57+
test_cmp expected actual
58+
'
59+
60+
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"' '
61+
git --git-dir=c/a.git --work-tree=c/a status >expected &&
62+
git -C c --git-dir=a.git --work-tree=a status >actual &&
63+
test_cmp expected actual
64+
'
65+
66+
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"' '
67+
git -C c --git-dir=a.git --work-tree=a status >expected &&
68+
git --git-dir=a.git -C c --work-tree=a status >actual &&
69+
test_cmp expected actual
70+
'
71+
72+
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"' '
73+
git -C c --git-dir=a.git --work-tree=a status >expected &&
74+
git --git-dir=a.git --work-tree=a -C c status >actual &&
75+
test_cmp expected actual
76+
'
77+
78+
test_expect_success 'Relative followed by fullpath: "-C ./here -C /there" is equivalent to "-C /there"' '
79+
echo "initial in dir1/dir2" >expected &&
80+
git -C dir1 -C "$(pwd)/dir1/dir2" log --format=%s >actual &&
81+
test_cmp expected actual
82+
'
83+
84+
test_done

0 commit comments

Comments
 (0)