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 <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Feb 24, 2016 at 13:48 UTC
638fa623d5b5fb392a062edf61ccf086d35ab26b
2 files changed
+14
builtin/config.c
+3
@@ -352,6 +352,9 @@ static int get_colorbool(const char *var, int print)
352
353
static void check_write(void)
354
{
355
+ if (!given_config_source.file && !startup_info->have_repository)
356
+ die("not in a git directory");
357
+
358
if (given_config_source.use_stdin)
359
die("writing to stdin is not supported");
360
t/t1308-config-set.sh
+11
@@ -218,4 +218,15 @@ test_expect_success 'check line errors for malformed values' '
218
test_i18ngrep "fatal: .*alias\.br.*\.git/config.*line 2" result
219
'
220
221
+test_expect_success 'error on modifying repo config without repo' '
222
+ mkdir no-repo &&
223
+ (
224
+ GIT_CEILING_DIRECTORIES=$(pwd) &&
225
+ export GIT_CEILING_DIRECTORIES &&
226
+ cd no-repo &&
227
+ test_must_fail git config a.b c 2>err &&
228
+ grep "not in a git directory" err
229
+ )
230
+'
231
+
232
test_done