Skip to content

Commit 3f26309

Browse files
committed
Merge branch 'ms/maint-config-error-at-eol-linecount'
When "git config" diagnoses an error in a configuration file and shows the line number for the offending line, it miscounted if the error was at the end of line. By Martin Stenberg * ms/maint-config-error-at-eol-linecount: config: report errors at the EOL with correct line number Conflicts: t/t1300-repo-config.sh
2 parents 9d9bfea + 4b34059 commit 3f26309

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

config.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,10 @@ static char *parse_value(void)
196196
for (;;) {
197197
int c = get_next_char();
198198
if (c == '\n') {
199-
if (quote)
199+
if (quote) {
200+
cf->linenr--;
200201
return NULL;
202+
}
201203
return cf->value.buf;
202204
}
203205
if (comment)
@@ -287,7 +289,7 @@ static int get_extended_base_var(char *name, int baselen, int c)
287289
{
288290
do {
289291
if (c == '\n')
290-
return -1;
292+
goto error_incomplete_line;
291293
c = get_next_char();
292294
} while (isspace(c));
293295

@@ -299,13 +301,13 @@ static int get_extended_base_var(char *name, int baselen, int c)
299301
for (;;) {
300302
int c = get_next_char();
301303
if (c == '\n')
302-
return -1;
304+
goto error_incomplete_line;
303305
if (c == '"')
304306
break;
305307
if (c == '\\') {
306308
c = get_next_char();
307309
if (c == '\n')
308-
return -1;
310+
goto error_incomplete_line;
309311
}
310312
name[baselen++] = c;
311313
if (baselen > MAXNAME / 2)
@@ -316,6 +318,9 @@ static int get_extended_base_var(char *name, int baselen, int c)
316318
if (get_next_char() != ']')
317319
return -1;
318320
return baselen;
321+
error_incomplete_line:
322+
cf->linenr--;
323+
return -1;
319324
}
320325

321326
static int get_base_var(char *name)

t/t1300-repo-config.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,4 +985,35 @@ test_expect_success 'git config --edit respects core.editor' '
985985
test_cmp expect actual
986986
'
987987

988+
# malformed configuration files
989+
test_expect_success 'barf on syntax error' '
990+
cat >.git/config <<-\EOF &&
991+
# broken section line
992+
[section]
993+
key garbage
994+
EOF
995+
test_must_fail git config --get section.key >actual 2>error &&
996+
grep " line 3 " error
997+
'
998+
999+
test_expect_success 'barf on incomplete section header' '
1000+
cat >.git/config <<-\EOF &&
1001+
# broken section line
1002+
[section
1003+
key = value
1004+
EOF
1005+
test_must_fail git config --get section.key >actual 2>error &&
1006+
grep " line 2 " error
1007+
'
1008+
1009+
test_expect_success 'barf on incomplete string' '
1010+
cat >.git/config <<-\EOF &&
1011+
# broken section line
1012+
[section]
1013+
key = "value string
1014+
EOF
1015+
test_must_fail git config --get section.key >actual 2>error &&
1016+
grep " line 3 " error
1017+
'
1018+
9881019
test_done

0 commit comments

Comments
 (0)