Skip to content

Make strict dropCR behavior optional via ParseOptions#78

Open
EmmanuelAC1 wants to merge 4 commits intosourcegraph:masterfrom
EmmanuelAC1:feature/optional-crlf
Open

Make strict dropCR behavior optional via ParseOptions#78
EmmanuelAC1 wants to merge 4 commits intosourcegraph:masterfrom
EmmanuelAC1:feature/optional-crlf

Conversation

@EmmanuelAC1
Copy link

Currently, the library strictly drops all trailing carriage returns (\r) from lines. This change introduces a mechanism to optionally preserve them while maintaining backward compatibility.

Detail:

  • Introduce ParseOptions struct with a KeepCR field.
  • Add *Options variants for [ParseMultiFileDiff], ParseHunks, and their corresponding New*Reader constructors.
  • Update lineReader and readLine to respect the KeepCR option.
  • Maintain existing function signatures as wrappers that default to KeepCR: false, ensuring no breaking changes for current users.
  • Add unit tests to verify KeepCR behavior.

@EmmanuelAC1 EmmanuelAC1 marked this pull request as ready for review February 12, 2026 14:08
@EmmanuelAC1
Copy link
Author

Hi @keegancsmith! I created this PR to make the dropCR behavior optional but keeping current behavior by default for backward compatibility. I'm not able to assign reviewers myself. Could you help guide me on how to get this reviewed?

@keegancsmith keegancsmith self-requested a review February 12, 2026 17:56
Copy link
Member

@keegancsmith keegancsmith left a comment

Choose a reason for hiding this comment

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

Sorry for the slow feedback, finally took a look. I think the issue here is we have a bunch of code that will fail since it won't expect a trailing \r in it. This is mostly around the bits of code which are not the patch file itself.

I think this PR is missing some decent end to end parsing tests.

I do think this is a worthwhile addition. Almost universally tools strip / ignore \r for diffs (mostly due to the unix heritage of this stuff). But we should provide a way to preserve it.

&r.hunk.OrigStartLine, &r.hunk.OrigLines,
&r.hunk.NewStartLine, &r.hunk.NewLines,
}
header, section, err := normalizeHeader(string(line))
Copy link
Member

Choose a reason for hiding this comment

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

with dropCR off this will fail since normalizeHeader's scanf won't expect to see \r at the end.

// handle that case.
return r.hunk, &ParseError{r.line, r.offset, &ErrBadHunkLine{Line: line}}
}
if bytes.Equal(line, []byte(noNewlineMessage)) {
Copy link
Member

Choose a reason for hiding this comment

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

this would also fail with noCR false?

}

var success bool
fd.OrigName, fd.NewName, success = parseDiffGitArgs(fd.Extended[0][len("diff --git "):])
Copy link
Member

Choose a reason for hiding this comment

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

NewName will have a trailing CR now?

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