threads / discuss / 47263

core.safecrlf warning is confusing[improvement suggestion?]

Subject: core.safecrlf warning is confusing[improvement suggestion?]

## tl;dr

6 messages between Nov 21, 2017 and Nov 22, 2017.

replies: 5people: 3as markdown or json

Vladimir Nikishkin· Nov 21, 2017, 05:18 UTC · lore
Hello, everyone.
I have the following question.
So I have a fresh git repository after git init, on Windows.
core.autocrlf is true explicitly, and core.safecrlf is true implicitly.

I want to have LF line endings in the repository and CRLF endings in the working copy. (Because I use windows-exclusive tools to develop.)

But for start I have my code with LF endings, because I got it from a fellow developer, who develops on UNIX, with LF line endings.

What I expect git to do: Commit files as is (LF), keep my files in the working directory with LF, but after issuing 'git checkout master' have them converted to CRLF (since this is a checkout procedure).

So I put the source in the working directory and tell git to make
git diff --stat
and I see the (ambiguous) warnings:

'warning: LF will be replaced by CRLF in filename.m. The file will have its original line endings in you working directory.'

How I read them: "even though you have core.autocrlf=true, LF will be replaced by CRLF and the repository will store CRLF files. However, after you checkout them again, the CRLFs will be converted back to LF(because the files will have original line endings in the working directory.)"

 I feel like it's the opposite of what is actually happening.
So, would it make sense to change the warning message to? :

'warning: When you next checkout this commit, your code will have CRLF line endings. However, right now your files will not be altered.'

-- 
Yours sincerely, Vladimir Nikishkin
Junio C Hamano· Nov 21, 2017, 05:44 UTC · re: Vladimir Nikishkin · lore

Re: core.safecrlf warning is confusing[improvement suggestion?]

Vladimir Nikishkin <lockywolf@gmail.com> writes:
Show 21 quoted lines
> So I put the source in the working directory and tell git to make
>
> git diff --stat
>
> and I see the (ambiguous) warnings:
>
> 'warning: LF will be replaced by CRLF in filename.m.
> The file will have its original line endings in you working directory.'
>
> How I read them: "even though you have core.autocrlf=true, LF will be
> replaced by CRLF and the repository will store CRLF files. However,
> after you checkout them again, the CRLFs will be converted back to
> LF(because the files will have original line endings in the working
> directory.)"
>
>  I feel like it's the opposite of what is actually happening.
>
> So, would it make sense to change the warning message to? :
>
> 'warning: When you next checkout this commit, your code will have CRLF
> line endings. However, right now your files will not be altered.'

I actually think "git diff" should not be triggering the warning message. The message may be appropriate when you are doing "git add", at which point the "will be replaced and that may not be what you want to see" warning might apply, but "diff" is doing a read-only thing and will not be replacing anything with any other thing.

Junio C Hamano· Nov 21, 2017, 05:49 UTC · re: Vladimir Nikishkin · lore

Re: core.safecrlf warning is confusing[improvement suggestion?]

Vladimir Nikishkin <lockywolf@gmail.com> writes:
Show 5 quoted lines
> I want to have LF line endings in the repository and CRLF endings in
> the working copy. (Because I use windows-exclusive tools to develop.)
>
> But for start I have my code with LF endings, because I got it from a
> fellow developer, who develops on UNIX, with LF line endings.

I somehow suspect that perhaps the way you "got it" from the other developer is wrong in the first place. Did you make a zip archive or something and extracted it? That's a wrong way to do a hand-off.

If you "git clone" to have your own copy from the other developer, then your working tree files will follow what your local convention is. Also you do not have to worry about malicious commands left inside .git/config when you made a bit-for-bit copy of the repository and the working tree.

Torsten Bögershausen· Nov 21, 2017, 16:18 UTC · re: Vladimir Nikishkin · lore

Re: core.safecrlf warning is confusing[improvement suggestion?]

On Tue, Nov 21, 2017 at 01:18:30PM +0800, Vladimir Nikishkin wrote:
Show 10 quoted lines
> Hello, everyone.
> 
> I have the following question.
> 
> So I have a fresh git repository after git init, on Windows.
> 
> core.autocrlf is true explicitly, and core.safecrlf is true implicitly.
> 
> I want to have LF line endings in the repository and CRLF endings in
> the working copy. (Because I use windows-exclusive tools to develop.)

Side note: If you ever want to push your repository somewhere, it would be good practice to have a .gitattributes file:

echo "* text=auto" >>.gitattributes git add .gitattributes git commit -m "add .gitattributes"

This can be useful if the repo is pulled to a person who has core.autocrlf=false, if case [s]he is creating files with CRLF in the worktree.

Show 12 quoted lines
> 
> But for start I have my code with LF endings, because I got it from a
> fellow developer, who develops on UNIX, with LF line endings.
> 
> What I expect git to do:
> Commit files as is (LF), keep my files in the working directory with
> LF, but after issuing 'git checkout master' have them converted to
> CRLF (since this is a checkout procedure).
> 
> So I put the source in the working directory and tell git to make
> 
> git diff --stat
As Junio pointed out, it is probably not git diff saying this ?
Typically "git add" gives the warning.
Show 18 quoted lines
>
> and I see the (ambiguous) warnings:
> 
> 'warning: LF will be replaced by CRLF in filename.m.
> The file will have its original line endings in you working directory.'
> 
> How I read them: "even though you have core.autocrlf=true, LF will be
> replaced by CRLF and the repository will store CRLF files. However,
> after you checkout them again, the CRLFs will be converted back to
> LF(because the files will have original line endings in the working
> directory.)"
> 
>  I feel like it's the opposite of what is actually happening.
> 
> So, would it make sense to change the warning message to? :
> 
> 'warning: When you next checkout this commit, your code will have CRLF
> line endings. However, right now your files will not be altered.'

I could agree. Do you want to send a patch for this ? (The code to be changd is in convert.c, plus some test cases)

> 
> -- 
> Yours sincerely, Vladimir Nikishkin
Junio C Hamano· Nov 22, 2017, 02:01 UTC · re: Torsten Bögershausen · lore

Re: core.safecrlf warning is confusing[improvement suggestion?]

Torsten Bögershausen <tboegi@web.de> writes:
Show 6 quoted lines
>> I want to have LF line endings in the repository and CRLF endings in
>> the working copy. (Because I use windows-exclusive tools to develop.)
>
> Side note: If you ever want to push your repository somewhere,
> it would be good practice to have a .gitattributes file:
> ...
Now we got your attention ;-)

What would be the BCP we would give if somebody has just a tarball without .git that has LF endings?

    $ git init a-project
    $ cd a-project
    $ tar xf ../a-project.tar
    $ git add .
    $ git commit -m 'Initial import'

would achieve one half of the original wish (i.e. "I want to end up with repository data in LF eol"); disabling the "safe crlf" before running that "git add ." step may also not be a bad idea, because it reduces the number of things that can get in the way by one.

But the above also leaves the "working tree" files in LF eol (i.e. it goes against "I want to work with CRLF in my working tree"). What would be our recommendation?

One big-hammer way I can think of is
    $ git rm -f .
    $ git reset --hard

and that actually may be a good enough solution, given that you'd be doing this just once at the beginning of "your" project that starts from an inherited code drop.

Torsten Bögershausen· Nov 22, 2017, 14:56 UTC · re: Junio C Hamano · lore

Re: core.safecrlf warning is confusing[improvement suggestion?]

On Wed, Nov 22, 2017 at 11:01:22AM +0900, Junio C Hamano wrote:
Show 19 quoted lines
> Torsten Bögershausen <tboegi@web.de> writes:
> 
> >> I want to have LF line endings in the repository and CRLF endings in
> >> the working copy. (Because I use windows-exclusive tools to develop.)
> >
> > Side note: If you ever want to push your repository somewhere,
> > it would be good practice to have a .gitattributes file:
> > ...
> 
> Now we got your attention ;-)
> 
> What would be the BCP we would give if somebody has just a tarball
> without .git that has LF endings?
> 
>     $ git init a-project
>     $ cd a-project
>     $ tar xf ../a-project.tar
>     $ git add .
>     $ git commit -m 'Initial import'
There is room for small improvements:
     $ cd /tmp
     $ git init a-project
     $ cd a-project
     $ tar xf ../a-project.tar
     $ git -c core.autocrlf=false add .
     $ git commit -m 'Initial import'
     # Make up your mind: is it truly cross-platform ?
       $ echo "* text=auto" >.gitattributes
       # E.g. if you have shell scripts:
       $ echo "*.sh text eol=lf" >>.gitattributes
       # E.g. if you are a git developer:
       $ echo "/GIT-VERSION-GEN eol=lf" >>.gitattributes   
      # Or, is it e.g. a project where a tool needs some line endings
      # visual studio is one example, there are many others:
       $ echo "* -text" >.gitattributes
      # in any case, we need to commit: 
      $ git add .gitattributes && git commit -m "Add .gitattributes"
# Now we have the repo. I we don't want the hammer, simply clone it:
     $ cd $HOME
     $ git clone /tmp/a-project

That should work for project small enough not to fill the disk. And other adjustments may be needed to the .gitattributes file. A final check with $ git ls-files --eol may give inspiration.

Show 18 quoted lines
> 
> would achieve one half of the original wish (i.e. "I want to end up
> with repository data in LF eol"); disabling the "safe crlf" before
> running that "git add ." step may also not be a bad idea, because it
> reduces the number of things that can get in the way by one.
> 
> But the above also leaves the "working tree" files in LF eol
> (i.e. it goes against "I want to work with CRLF in my working
> tree").  What would be our recommendation?
> 
> One big-hammer way I can think of is
> 
>     $ git rm -f .
>     $ git reset --hard
> 
> and that actually may be a good enough solution, given that you'd be
> doing this just once at the beginning of "your" project that starts
> from an inherited code drop.

← back to recent threads