Skip to content

Commit c974184

Browse files
committed
Merge branch 'kn/git-cd-to-empty' into maint
"git -C '' subcmd" refused to work in the current directory, unlike "cd ''" which silently behaves as a no-op. * kn/git-cd-to-empty: git: treat "git -C '<path>'" as a no-op when <path> is empty
2 parents 84a37fa + 6a536e2 commit c974184

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)