git-apply fails mysteriously, how do I troubleshoot/fix?

后端 未结 3 1761
南笙
南笙 2020-12-17 07:36

I\'m currently trying to to code-style checking on the PRs of a (github) repository, and I want to deliver patches to the submitters with which they can easily fix the codes

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-17 08:14

    In such cases, if you already tried all options listed below:

    1. Investigating potential non-printable/invisible characters in file
    2. Using dos2unix for conversion
    3. changing the git config to adjust the way of handling line endings for eg:
    git config --global core.autocrlf input
    

    Another root of evil in such cases could be your IDE.

    For example, some of the JetBrains IDEs like PHPStorm has following option:

    After spending more than 4 bloody hours today on a weird patching issue ending up with following error:

    git apply < my.git.patch --verbose
    ...
    error: patch failed: plugins/some/file.php:74
    

    I realized that I was victim of my editor (I am not sure if this was the default behavior).

    I was using PHPStorm to check the patch file and it was silently removing a single whitespace from the patch file which literally exist on the target file that needs to be patched.

    This was super sneaky and I ended up with setting Strip trailing spaces on Save for option to None to never hit such issue again.

    Also keep in mind that platform related differences like case-sensitivity of the underlying filesystem (MacOS/Linux for eg) may also be the suspect.

提交回复
热议问题