how to avoid git-apply changing line endings
Asked Answered
U

3

12

I have a git repo set with core.eol=crlf, core.autocrlf=true and core.safecrlf=true.

When I apply a patch from another crlf repo and to my repo all the line endings for the effected file are changed to lf. Currently I'm applying the patch as so:

git apply --ignore-whitespace mychanges.patch

(It seems I have to use --ignore-whitespace to get the patch to successfully apply.)

My current work around is to run unix2dos on the file. Is there a better way of getting apply to conform to my eol settings?

Urbani answered 10/6, 2011 at 15:32 Comment(0)
S
4

Check if the issue persists with Git 2.14.x/2.15 (Q3 2015)

See commit c24f3ab (19 Aug 2017), and commit 2fea9de (13 Aug 2017) by Torsten Bögershausen (tboegi).
(Merged by Junio C Hamano -- gitster -- in commit a17483f, 27 Aug 2017)

apply: file committed with CRLF should roundtrip diff and apply

When a file had been committed with CRLF but now .gitattributes says "* text=auto" (or core.autocrlf is true), the following does not roundtrip, git apply fails:

printf "Added line\r\n" >>file &&
git diff >patch &&
git checkout -- . &&
git apply patch

Before applying the patch, the file from working tree is converted into the index format (clean filter, CRLF conversion, ...).
Here, when commited with CRLF, the line endings should not be converted.

Spain answered 4/9, 2017 at 21:12 Comment(0)
R
3

I would not allow my source control system to control my line endings. Auto crlf is false and showing diffs without the annoying ^M is done by setting core.whitespace to cr-at-eol. Now diff output will be nicer to read.

Reinareinald answered 10/6, 2011 at 15:53 Comment(2)
If I could have my own way I'd also work with lf, but I'm using my git repo to work with my organisation's svn in which the line endings are required to by crlf. When I git svn dcommit, I don't want to push lf eols to the database. I'm not finding any problems anywhere else working with crlf (I'm on Windows), git diff and vim won't show ^M unless there's an inconsistency.Urbani
But this is at a cost of having the line endings stored differently in git vs svn. If you do that, you'll have friction. This is why I recommend autocrlf set to false and then just deal with the way diffs are displayed via core.whitespace.Reinareinald
H
0

Try on a clean working directory:

git apply mychanges.patch
git diff -w > mychangesnows.patch
git reset --hard
git apply mychangesnows.patch
Holoenzyme answered 25/6, 2012 at 12:21 Comment(2)
I expected this to work for me, but for some reason it didn't. Instead, I was successful using : git apply --whitespace=fix mychanges.patch. Bonus: fewer commands!Unseasoned
@blong, you should submit an answer.Filing

© 2022 - 2024 — McMap. All rights reserved.