Skip to content

bug(grep): incorrect line-ending handling #6437

Description

@Kristinita

1. Summary

grep from Git for Windows incorrectly handles line endings for me.

2. The possible reasons for the bug

  1. Git for Windows 2.55.0 contains grep 3.0. Cygwin has grep 3.12.

  2. Line-ending handling was discussed in the issue grep crash with -i #5046. The comment by the “benevolent dictator” of Git for Windows:

    The CR/LF handling was removed. Now grep always opens files in binary mode, which means that if you grep through a file that contains CR/LF line endings, regular expressions like x$ will never match, as the \r at the end of the line is interpreted as a character (one that differs from x in x$).

3. MCVE

3.1. Reproducibility

  1. Reproduced:

    1. The grep binary from Git for Windows launched from Git Bash and cmd.exe.
  2. Not reproduced:

    1. grep on Ubuntu. The live demo in the online REPL OneCompiler.
    2. The grep binary from Cygwin launched from Cygwin and cmd.exe.

3.2. Command

  1. Cygwin, Git Bash and Ubuntu shells:

    grep --file=<(printf 'Kira Goddess!\r\r\n') <(printf 'Kira Goddess!\r\r\nKira Amazing!\r\r\n')
  2. cmd.exe — the same command wrapped in bash -c "":

    bash -c "grep --file=<(printf 'Kira Goddess!\r\r\n') <(printf 'Kira Goddess!\r\r\nKira Amazing!\r\r\n')"

3.3. Behavior

3.3.1. Expected
Kira Goddess!
3.3.2. Actual

No output.

4. The answers to possible replies

4.1. “Just use grep from Cygwin”

In all remote CIs that I used, Git for Windows is pre-installed, and a path to its binaries is already added to PATH. I don’t want to take additional steps for remote CIs.

4.2. “Just use diff, comm, or other alternatives”

grep is a better solution for my purpose. Read the section “Advantages” of my answer on Stack Overflow.

4.3. “Just trim line endings”

Yes, I can add something like tr --delete '\r'. This command returns the expected result:

grep --file=<(printf 'Kira Goddess!\r\r\n' | tr --delete '\r') <(printf 'Kira Goddess!\r\r\nKira Amazing!\r\r\n')

Kira Goddess!

But I’m sure that additional commands for bypassing bugs aren’t the best idea.

5. Environment

  1. Windows 11 64-bit [Version 10.0.26200.7840]
  2. Git for Windows 2.55.0.windows.5

Thanks.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions