Skip to content

Commit 2a11eaf

Browse files
authored
Merge pull request libgit2#4521 from pks-t/pks/config-crlf-lines
config: handle CRLF-only lines and BOM
2 parents f722594 + ba4faf6 commit 2a11eaf

File tree

4 files changed

+60
-11
lines changed

4 files changed

+60
-11
lines changed

src/buf_text.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ bool git_buf_text_is_binary(const git_buf *buf)
188188
git_bom_t bom;
189189
int printable = 0, nonprintable = 0;
190190

191-
scan += git_buf_text_detect_bom(&bom, buf, 0);
191+
scan += git_buf_text_detect_bom(&bom, buf);
192192

193193
if (bom > GIT_BOM_UTF8)
194194
return 1;
@@ -215,18 +215,18 @@ bool git_buf_text_contains_nul(const git_buf *buf)
215215
return (memchr(buf->ptr, '\0', buf->size) != NULL);
216216
}
217217

218-
int git_buf_text_detect_bom(git_bom_t *bom, const git_buf *buf, size_t offset)
218+
int git_buf_text_detect_bom(git_bom_t *bom, const git_buf *buf)
219219
{
220220
const char *ptr;
221221
size_t len;
222222

223223
*bom = GIT_BOM_NONE;
224-
/* need at least 2 bytes after offset to look for any BOM */
225-
if (buf->size < offset + 2)
224+
/* need at least 2 bytes to look for any BOM */
225+
if (buf->size < 2)
226226
return 0;
227227

228-
ptr = buf->ptr + offset;
229-
len = buf->size - offset;
228+
ptr = buf->ptr;
229+
len = buf->size;
230230

231231
switch (*ptr++) {
232232
case 0:
@@ -274,7 +274,7 @@ bool git_buf_text_gather_stats(
274274
memset(stats, 0, sizeof(*stats));
275275

276276
/* BOM detection */
277-
skip = git_buf_text_detect_bom(&stats->bom, buf, 0);
277+
skip = git_buf_text_detect_bom(&stats->bom, buf);
278278
if (skip_bom)
279279
scan += skip;
280280

src/buf_text.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,9 @@ extern bool git_buf_text_contains_nul(const git_buf *buf);
9999
*
100100
* @param bom Set to the type of BOM detected or GIT_BOM_NONE
101101
* @param buf Buffer in which to check the first bytes for a BOM
102-
* @param offset Offset into buffer to look for BOM
103102
* @return Number of bytes of BOM data (or 0 if no BOM found)
104103
*/
105-
extern int git_buf_text_detect_bom(
106-
git_bom_t *bom, const git_buf *buf, size_t offset);
104+
extern int git_buf_text_detect_bom(git_bom_t *bom, const git_buf *buf);
107105

108106
/**
109107
* Gather stats for a piece of text

src/config_parse.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ static int skip_bom(git_parse_ctx *parser)
217217
{
218218
git_buf buf = GIT_BUF_INIT_CONST(parser->content, parser->content_len);
219219
git_bom_t bom;
220-
int bom_offset = git_buf_text_detect_bom(&bom, &buf, parser->content_len);
220+
int bom_offset = git_buf_text_detect_bom(&bom, &buf);
221221

222222
if (bom == GIT_BOM_UTF8)
223223
git_parse_advance_chars(parser, bom_offset);
@@ -475,6 +475,11 @@ int git_config_parse(
475475
size_t line_len = parser->ctx.line_len;
476476
char c;
477477

478+
/*
479+
* Get either first non-whitespace character or, if that does
480+
* not exist, the first whitespace character. This is required
481+
* to preserve whitespaces when writing back the file.
482+
*/
478483
if (git_parse_peek(&c, ctx, GIT_PARSE_PEEK_SKIP_WHITESPACE) < 0 &&
479484
git_parse_peek(&c, ctx, 0) < 0)
480485
continue;
@@ -490,6 +495,7 @@ int git_config_parse(
490495
break;
491496

492497
case '\n': /* comment or whitespace-only */
498+
case '\r':
493499
case ' ':
494500
case '\t':
495501
case ';':

tests/config/read.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,3 +703,48 @@ void test_config_read__path(void)
703703
git_buf_free(&expected_path);
704704
git_config_free(cfg);
705705
}
706+
707+
void test_config_read__crlf_style_line_endings(void)
708+
{
709+
git_buf buf = GIT_BUF_INIT;
710+
git_config *cfg;
711+
712+
cl_set_cleanup(&clean_test_config, NULL);
713+
cl_git_mkfile("./testconfig", "[some]\r\n var = value\r\n");
714+
cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));
715+
cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.var"));
716+
cl_assert_equal_s(buf.ptr, "value");
717+
718+
git_config_free(cfg);
719+
git_buf_free(&buf);
720+
}
721+
722+
void test_config_read__trailing_crlf(void)
723+
{
724+
git_buf buf = GIT_BUF_INIT;
725+
git_config *cfg;
726+
727+
cl_set_cleanup(&clean_test_config, NULL);
728+
cl_git_mkfile("./testconfig", "[some]\r\n var = value\r\n\r\n");
729+
cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));
730+
cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.var"));
731+
cl_assert_equal_s(buf.ptr, "value");
732+
733+
git_config_free(cfg);
734+
git_buf_free(&buf);
735+
}
736+
737+
void test_config_read__bom(void)
738+
{
739+
git_buf buf = GIT_BUF_INIT;
740+
git_config *cfg;
741+
742+
cl_set_cleanup(&clean_test_config, NULL);
743+
cl_git_mkfile("./testconfig", "\xEF\xBB\xBF[some]\n var = value\n");
744+
cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));
745+
cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.var"));
746+
cl_assert_equal_s(buf.ptr, "value");
747+
748+
git_config_free(cfg);
749+
git_buf_free(&buf);
750+
}

0 commit comments

Comments
 (0)