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 <gitster@pobox.com> Helped-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Karthik Nayak committed
Mar 6, 2015 at 16:48 UTC
6a536e2076f02a98e0d6403ff68f3acf717fa1c4
2 files changed
+16
-4
git.c
+6
-4
@@ -204,10 +204,12 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
204
fprintf(stderr, "No directory given for -C.\n" );
205
usage(git_usage_string);
206
}
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
+ }
213
(*argv)++;
214
(*argc)--;
215
} else {
t/t0056-git-C.sh
+10
@@ -14,6 +14,16 @@ test_expect_success '"git -C <path>" runs git from the directory <path>' '
14
test_cmp expected actual
15
'
16
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
+
27
test_expect_success 'Multiple -C options: "-C dir1 -C dir2" is equivalent to "-C dir1/dir2"' '
28
test_create_repo dir1/dir2 &&
29
echo 1 >dir1/dir2/b.txt &&