Skip to content

Commit 6a536e2

Browse files
KarthikNayakgitster
authored andcommitted
git: treat "git -C '<path>'" as a no-op when <path> is empty
'git -C ""' unhelpfully dies with error "Cannot change to ''", whereas the shell treats `cd ""' as a no-op. Taking the shell's behavior as a precedent, teach git to treat `-C ""' as a no-op, as well. Helped-by: Junio C Hamano <[email protected]> Helped-by: Eric Sunshine <[email protected]> Signed-off-by: Karthik Nayak <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9874fca commit 6a536e2

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

git.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,12 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
204204
fprintf(stderr, "No directory given for -C.\n" );
205205
usage(git_usage_string);
206206
}
207-
if (chdir((*argv)[1]))
208-
die_errno("Cannot change to '%s'", (*argv)[1]);
209-
if (envchanged)
210-
*envchanged = 1;
207+
if ((*argv)[1][0]) {
208+
if (chdir((*argv)[1]))
209+
die_errno("Cannot change to '%s'", (*argv)[1]);
210+
if (envchanged)
211+
*envchanged = 1;
212+
}
211213
(*argv)++;
212214
(*argc)--;
213215
} else {

t/t0056-git-C.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ test_expect_success '"git -C <path>" runs git from the directory <path>' '
1414
test_cmp expected actual
1515
'
1616

17+
test_expect_success '"git -C <path>" with an empty <path> is a no-op' '
18+
(
19+
mkdir -p dir1/subdir &&
20+
cd dir1/subdir &&
21+
git -C "" rev-parse --show-prefix >actual &&
22+
echo subdir/ >expect &&
23+
test_cmp expect actual
24+
)
25+
'
26+
1727
test_expect_success 'Multiple -C options: "-C dir1 -C dir2" is equivalent to "-C dir1/dir2"' '
1828
test_create_repo dir1/dir2 &&
1929
echo 1 >dir1/dir2/b.txt &&

0 commit comments

Comments
 (0)