Can't get Git to correctly handle CRLF problems with Eclipse launch configs (XML files)

前端 未结 2 664
清歌不尽
清歌不尽 2021-02-15 23:06

We have a mixed team with some people using Windows and others using Linux. We have configured the IDE (Eclipse) to use LF as line ending for source files which works well.

2条回答
  •  孤城傲影
    2021-02-15 23:55

    As far as I can see there are two problems here:

    1. The file name should be .gitattributes (with a leading s).

    2. The **/ prefix is not understood by git. Simply use *.launch to match all files in all subdirectories and /*.launch to only match files in the top level directory.

    The following shell script demonstrates the correct use of the text attribute:

    #!/bin/bash
    
    set -ex
    
    rm -rf q22629396
    mkdir q22629396
    cd q22629396
    
    git init
    
    echo '*.txt text' > .gitattributes
    git add .gitattributes
    git commit -m 'added .gitattributes'
    
    echo -e 'line with LF' > test.txt
    echo -e 'line with CRLF\r' >> test.txt
    echo -e 'line with LF' >> test.txt
    echo -e 'line with CRLF\r' >> test.txt
    
    mkdir subdir
    echo -e 'line with LF' > subdir/test.txt
    echo -e 'line with CRLF\r' >> subdir/test.txt
    echo -e 'line with LF' >> subdir/test.txt
    echo -e 'line with CRLF\r' >> subdir/test.txt
    
    git add test.txt subdir/test.txt
    git commit -m 'added test.txt and subdir/test.txt'
    
    git show HEAD:test.txt | hexdump -c
    git show HEAD:subdir/test.txt | hexdump -c
    

    (Tested with git version 1.7.9.5 on Ubuntu 12.04.)

    Additional Notes:

    The text attribute only corrects line endings when checking in files, not when checking them out. And eol=lf only prevents automatic conversion to CRLF on checkout, not automatically convert CRLF to LF on checkout. So if the files have CRLF in them in the repository, you have to re-commit with LF.

    If you check out a file with CRLF and have set the text attribute on that file, then I should be marked as "modified" in git status right from the beginning. Now you can run git commit on that file, which will change the line ending in the repository, but not in the working copy.

    Resetting the line ending on the file in the working copy after that is hard. Git now replaces CRLF in the file with LF before comparing it to the index or the repository. Because of this git does not think the file is different from the committed version and thus does not do anything when you use commands such as git checkout -f. The only thing that worked in my tests was removing the file locally and checking it out again: rm test.txt; git checkout -- test.txt

提交回复
热议问题