Skip to content

gcfg: Read a leading BOM and continued values the way git does - #16

Merged
pjbgf merged 2 commits into
go-git:mainfrom
r0h1tb:fix/read-like-git
Nov 18, 2026
Merged

pjbgf merged 2 commits into
go-git:mainfrom
r0h1tb:fix/read-like-git

Conversation

@r0h1tb

@r0h1tb r0h1tb commented Oct 19, 2026

Copy link
Copy Markdown

gcfg reads two things differently from git, which go-git inherits through plumbing/format/config:

  • UTF-8 BOM. A config file starting with a BOM (Notepad writes one) fails with 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, as go/scanner does.
  • Continued values. For a value ending in \, git-config documents that "the backslash and the end-of-line are stripped". gcfg kept the newline, so a = first \ + <TAB>second read as "first \n\tsecond" where git reads "first \tsecond". Inside quotes, \ followed by CRLF failed as an unknown escape sequence.

TestReadWithCallback expected the newline to be kept (from #2); it now expects git's value. The new TestReadWithCallbackMatchesGit cases use what git 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 replace pointing at this branch give the same results as main (plumbing/format/config and config pass; the only failure, TestConformanceSuite/TestIgnoreDoubleStarPrefix, fails on main too). A side-by-side of go-git and git config --list over 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.

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 pjbgf left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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]>
@r0h1tb

r0h1tb commented Oct 25, 2026

Copy link
Copy Markdown
Author

Done in 0db9248: the quoted branch only skips the CR when an LF follows, so a = "a\<CR>b" is rejected again with "unknown escape sequence". Added that case to TestScanErrors and the read tests, plus read tests showing a partial BOM (EF, EF BB), a doubled BOM and a BOM after the first line are still rejected.

The unquoted branch has the same lone-CR behaviour, but it predates this PR: on main name = a\<CR>b already reads as "a\b". Guarding it the same way changes the offset in the existing "=\\\r" scanner case, so I left it out here. I can add it if you want it in this PR.

@pjbgf pjbgf left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@r0h1tb thanks for working on this. 🙇

@pjbgf

pjbgf commented Nov 18, 2026

Copy link
Copy Markdown
Member

on main name = a<CR>b already reads as "a\b". Guarding it the same way changes the offset in the existing "=\\r" scanner case, so I left it out here

@r0h1tb that can be a follow-up PR.

@pjbgf
pjbgf merged commit 0e8c4f4 into go-git:main Nov 18, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants