Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"This diff contains a change in line endings from X to Y" banner is wrong #18691

Open
Niko-O opened this issue May 27, 2024 · 2 comments
Open

Comments

@Niko-O
Copy link
Contributor

Niko-O commented May 27, 2024

The problem

"This diff contains a change in line endings from 'LF' to 'CRLF'." would indicate that the file previously contained LF line endings and either all or a subset of those were changed to be CRLF line endings. This is not the case in my case!
The file has previously contained CRLF line endings and was changed to LF line endings. That's the other way around! The wrong banner is very confusing!

Release version

3.3.14 (x64)

Operating system

Windows 11

Steps to reproduce the behavior

  • Create a repository
  • Create a file OriginalCrLf.txt and fill it with multiline text that uses CRLF line endings.
  • Create a file OriginalLf.txt and fill it with multiline text that uses LF line endings.
  • Commit those new files.
  • Change the line endings of OriginalCrLf.txt to LF and those of OriginalLf.txt to CRLF.
  • Observe the Diffs.

Log files

No response

Screenshots

OriginalCrLf.txt contained CRLF line endings originally and was changed to have LF line endings. The diff shows the entire text being replaced with the same text (i.e. it's not being smart about detecting that only line endings have changed) and the banner says "from 'LF' to 'CRLF'", which is wrong.
grafik

OriginalLf.txt contained LF line endings originally and was changed to have CRLF line endings. The diff shows "No content changes found" (i.e. it's being smart about detecting that only line endings but no other text has changed) and no banner is visible:
grafik

Additional context

The following is the output of git config --list --show-origin executed in the repository that contains the changed file (sensitive information replaced with [...]):

file:C:/Program Files/Git/etc/gitconfig diff.astextplain.textconv=astextplain
file:C:/Program Files/Git/etc/gitconfig filter.lfs.clean=git-lfs clean -- %f
file:C:/Program Files/Git/etc/gitconfig filter.lfs.smudge=git-lfs smudge -- %f
file:C:/Program Files/Git/etc/gitconfig filter.lfs.process=git-lfs filter-process
file:C:/Program Files/Git/etc/gitconfig filter.lfs.required=true
file:C:/Program Files/Git/etc/gitconfig http.sslbackend=openssl
file:C:/Program Files/Git/etc/gitconfig http.sslcainfo=C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt
file:C:/Program Files/Git/etc/gitconfig core.autocrlf=false
file:C:/Program Files/Git/etc/gitconfig core.fscache=true
file:C:/Program Files/Git/etc/gitconfig core.symlinks=false
file:C:/Program Files/Git/etc/gitconfig core.editor=notepad
file:C:/Program Files/Git/etc/gitconfig pull.rebase=false
file:C:/Program Files/Git/etc/gitconfig credential.helper=manager-core
file:C:/Program Files/Git/etc/gitconfig credential.https://dev.azure.com.usehttppath=true
file:C:/Program Files/Git/etc/gitconfig init.defaultbranch=master
file:C:/Users/[...]/.gitconfig    filter.lfs.clean=git-lfs clean -- %f
file:C:/Users/[...]/.gitconfig    filter.lfs.smudge=git-lfs smudge -- %f
file:C:/Users/[...]/.gitconfig    filter.lfs.process=git-lfs filter-process
file:C:/Users/[...]/.gitconfig    filter.lfs.required=true
file:C:/Users/[...]/.gitconfig    user.name=[...]
file:C:/Users/[...]/.gitconfig    user.email=[...]
file:C:/Users/[...]/.gitconfig    init.defaultbranch=master
file:.git/config        core.repositoryformatversion=0
file:.git/config        core.filemode=false
file:.git/config        core.bare=false
file:.git/config        core.logallrefupdates=true
file:.git/config        core.symlinks=false
file:.git/config        core.ignorecase=true
file:.git/config        core.autocrlf=false

Git is intentionally configured to tell me about line ending changes and to not change them automatically.

The following is the output of git diff --word-diff-regex=.:

diff --git a/OriginalCrLf.txt b/OriginalCrLf.txt
index 2b929a9..f9264f7 100644
--- a/OriginalCrLf.txt
+++ b/OriginalCrLf.txt
@@ -1,2 +1,2 @@
Hello[-^M-]
World[-^M-]
diff --git a/OriginalLf.txt b/OriginalLf.txt
index f9264f7..2b929a9 100644
--- a/OriginalLf.txt
+++ b/OriginalLf.txt
@@ -1,2 +1,2 @@
Hello{+^M+}
World{+^M+}
@steveward
Copy link
Member

Thanks for the issue @Niko-O. I tested this out and I believe this banner is correct based on the default of core.autocrlf=true. When testing this out on Windows with the defaults if you run a git diff you'll see the following:

warning: in the working copy of, LF will be replaced by CRLF the next time Git touches it

When you commit line endings are converted to LF, but when checking out files they are converted to CRLF.

If I set autocrlf to false then the warning goes away, so I'm not sure why you're seeing that warning since false will disable any conversion. Do you have anything configured in your .gitattributes file?

Generally I would recommend adding * text=auto in the .gitattributes file to start with since that ensures compatibility.

@Niko-O
Copy link
Contributor Author

Niko-O commented Jun 14, 2024

If I set autocrlf to false then the warning goes away

Please clarify: Did you set autocrlf to false (globally) before creating the repository? Or did you happen to set it after you've already committed the files?
I have it set to false globally before creating the repository.

Do you have anything configured in your .gitattributes file?

I do not have a .gitattributes file in my repository. I created the repository by creating a new folder, opening a terminal in it, executing git init, then creating the two text files and committing them with git add . and git commit -m "a".

Generally I would recommend adding * text=auto in the .gitattributes file to start with since that ensures compatibility.

Do I need to do that before adding any files to the repository?
How does this setting affect how GitHub Desktop handles newline changes, but not Git itself (because the output of git diff --word-diff-regex=. seems correct to me)?

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

No branches or pull requests

2 participants