gitline-endingsgitattributesgit-filter

.gitattributes using '* text=auto' with filters


In a cross-platform project, what is the right way to configure CRLF normalization for all text files '* text=auto' together with 'smudge' and 'clean' filters for certain files '*.h filter=myfilter'?

Example:

# my .gitattributes
* text=auto           # in case people don't have core.autocrlf set
*.h filter=myfilter   # filter for header files

After adding *.h filter=myfilter, I am getting warnings like:

warning: LF will be replaced by CRLF in foo.h.
The file will have its original line endings in your working directory.

If I remove *.h filter=myfilter, the warning disappears. Why? Is this line disabling the CRLF normalization for *.h files?

Update

As to the warning, the filters use sed to replace tags in file header comments. When run on windows sed is converting CRLF to LF. This is where the warning is coming from. The solution is to run sed in binary mode (--binary).


Solution

  • Some experimentation produces rather confusing results.

    The gitattributes documentation says this up front:

    When more than one pattern matches the path, a later line overrides an earlier line. This overriding is done per attribute.

    (emphasis mine) This clearly implies that, while *.h filter=myfilter overrides any earlier match for the filter setting, it should not affect any earlier * text=... setting.

    The actual check-in and check-out process seem to be well behaved: the earlier * text=<whatever> is obeyed.

    Other commands, however, seem to treat the filter as overriding the text. (This seems to be where the warning comes from.)

    You can cure the problem in this case by writing:

    *.h text=auto filter=myfilter
    

    but in general this seems to violate the documented behavior.