Repository navigation
gcfg: Read a leading BOM and continued values the way git does - #16
Conversation
A file starting with a UTF-8 BOM, which Windows editors such as Notepad write, failed with "illegal character U+FEFF". git skips the BOM. A value continued on the next line with a trailing backslash kept the newline in the value. git-config documents that "the backslash and the end-of-line are stripped", and git reports "first \tsecond" where gcfg gave "first \n\tsecond". Inside quotes, a backslash followed by CRLF was rejected as an unknown escape sequence. TestReadWithCallback expected the newline to be kept and now expects the value git reads. Assisted-by: Claude Opus 5.5 Signed-off-by: Rohit Behera <[email protected]>
pjbgf
left a comment
There was a problem hiding this comment.
@r0h1tb thanks for working on this. I checked this against git 2.55 and upstream config.c, and the BOM, LF and CRLF continuation results all match git, including keeping the tab in first \tsecond.
One change before merging it though: in scanValString (scanner/scanner.go:257-261), the new quoted branch consumes \r after a backslash even when no \n follows. With a lone CR, a = "a\<CR>b" now parses successfully as "a\x08": the CR is stripped and \b becomes a backspace.
Could you only skip the \r when the next byte is \n, for example:
if s.ch == '\r' && s.rdOffset < len(s.src) && s.src[s.rdOffset] == '\n' {
On main this was rejected with "unknown escape sequence". git rejects it with "bad config line", because get_next_char only folds CRLF (config.c:806-813) and parse_value rejects a \ followed by \r.
Then add a test that expects an error for that input and another to confirm that a leading BOM and a partial BOM (EF, EF BB) is still rejected.
Inside quotes, a backslash followed by a lone CR consumed the CR, so "a\<CR>b" was read as "a\b" (a backspace) instead of being rejected. git only folds CRLF here and rejects a lone CR. Also cover partial and non-leading BOMs, which are still rejected. Assisted-by: Claude Opus 5.5 Signed-off-by: Rohit Behera <[email protected]>
|
Done in 0db9248: the quoted branch only skips the CR when an LF follows, so The unquoted branch has the same lone-CR behaviour, but it predates this PR: on main |
@r0h1tb that can be a follow-up PR. |
gcfg reads two things differently from git, which go-git inherits through
plumbing/format/config:illegal character U+FEFF, so go-git can't open a repository or load a global config that has one. git skips it. The scanner now ignores a BOM at the very start, asgo/scannerdoes.\, git-config documents that "the backslash and the end-of-line are stripped". gcfg kept the newline, soa = first \+<TAB>secondread as"first \n\tsecond"where git reads"first \tsecond". Inside quotes,\followed by CRLF failed as an unknown escape sequence.TestReadWithCallbackexpected the newline to be kept (from #2); it now expects git's value. The newTestReadWithCallbackMatchesGitcases use whatgit config --list(2.50.1) reports for the same text, and each fails with its part of the change reverted.Checked downstream: go-git's tests with a
replacepointing at this branch give the same results as main (plumbing/format/configandconfigpass; the only failure,TestConformanceSuite/TestIgnoreDoubleStarPrefix, fails on main too). A side-by-side of go-git andgit config --listover a set of tricky configs now differs only where go-git can't represent git's result (valueless keys,[remote ""]), plus the deprecated[section.sub]header syntax and variables after a header on the same line, which I left out.This was written with AI assistance (Claude Code) and reviewed and tested as described above.