Skip to content

Commit 1f2baa7

Browse files
peffgitster
authored andcommitted
config: treat non-existent config files as empty
The git_config() function signals error by returning -1 in two instances: 1. An actual error occurs in opening a config file (parse errors cause an immediate die). 2. Of the three possible config files, none was found. However, this second case is often not an error at all; it simply means that the user has no configuration (they are outside a repo, and they have no ~/.gitconfig file). This can lead to confusing errors, such as when the bash completion calls "git config --list" outside of a repo. If the user has a ~/.gitconfig, the command completes succesfully; if they do not, it complains to stderr. This patch allows callers of git_config to distinguish between the two cases. Error is signaled by -1, and otherwise the return value is the number of files parsed. This means that the traditional "git_config(...) < 0" check for error should work, but callers who want to know whether we parsed any files or not can still do so. [jc: with tests from Jonathan] Signed-off-by: Jeff King <[email protected]> Signed-off-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1bb28d8 commit 1f2baa7

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

config.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -871,9 +871,7 @@ int git_config(config_fn_t fn, void *data)
871871
if (config_parameters)
872872
found += 1;
873873

874-
if (found == 0)
875-
return -1;
876-
return ret;
874+
return ret == 0 ? found : ret;
877875
}
878876

879877
/*

t/t1300-repo-config.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,14 @@ EOF
288288
test_expect_success 'working --list' \
289289
'git config --list > output && cmp output expect'
290290

291+
cat > expect << EOF
292+
EOF
293+
294+
test_expect_success '--list without repo produces empty output' '
295+
git --git-dir=nonexistent config --list >output &&
296+
test_cmp expect output
297+
'
298+
291299
cat > expect << EOF
292300
beta.noindent sillyValue
293301
nextsection.nonewline wow2 for me
@@ -836,6 +844,27 @@ test_expect_success SYMLINKS 'symlinked configuration' '
836844
837845
'
838846

847+
test_expect_success 'nonexistent configuration' '
848+
(
849+
GIT_CONFIG=doesnotexist &&
850+
export GIT_CONFIG &&
851+
test_must_fail git config --list &&
852+
test_must_fail git config test.xyzzy
853+
)
854+
'
855+
856+
test_expect_success SYMLINKS 'symlink to nonexistent configuration' '
857+
ln -s doesnotexist linktonada &&
858+
ln -s linktonada linktolinktonada &&
859+
(
860+
GIT_CONFIG=linktonada &&
861+
export GIT_CONFIG &&
862+
test_must_fail git config --list &&
863+
GIT_CONFIG=linktolinktonada &&
864+
test_must_fail git config --list
865+
)
866+
'
867+
839868
test_expect_success 'check split_cmdline return' "
840869
git config alias.split-cmdline-fix 'echo \"' &&
841870
test_must_fail git split-cmdline-fix &&

0 commit comments

Comments
 (0)