Skip to content

Commit 041cc8c

Browse files
committed
git config: report when trying to modify a non-existing repo config
It is a pilot error to call `git config section.key value` outside of any Git worktree. The message error: could not lock config file .git/config: No such file or directory is not very helpful in that situation, though. Let's print a helpful message instead. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 2db7919 commit 041cc8c

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

builtin/config.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,9 @@ static int get_colorbool(const char *var, int print)
352352

353353
static void check_write(void)
354354
{
355+
if (!given_config_source.file && !startup_info->have_repository)
356+
die("not in a git directory");
357+
355358
if (given_config_source.use_stdin)
356359
die("writing to stdin is not supported");
357360

t/t1308-config-set.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,13 @@ test_expect_success 'check line errors for malformed values' '
218218
test_i18ngrep "fatal: .*alias\.br.*\.git/config.*line 2" result
219219
'
220220

221+
test_expect_success 'error on modifying repo config without repo' '
222+
mkdir no-repo &&
223+
GIT_CEILING_DIRECTORIES=$(pwd) &&
224+
export GIT_CEILING_DIRECTORIES &&
225+
cd no-repo &&
226+
test_must_fail git config a.b c 2>err &&
227+
grep "not in a git directory" err
228+
'
229+
221230
test_done

0 commit comments

Comments
 (0)