patch/git apply – Hunk #hunkNumber FAILED at lineNumber (different line endings).

This error commonly issues with .patch files when the patch and target file use different line ending types (i.e. Windows/DOS/CLRF vs. Unix/LF):

touch .build/x64/status/clone
cd .build/x64/src/sshfs && for f in /cygdrive/c/github.com/sshfs-win/patches/*.patch; do patch --binary -p1 <$f; done
patching file sshfs.c
Hunk #1 FAILED at 365 (different line endings).

The unix2dos / dos2unix and commands can be used to perform conversion. One way to determine the line endings type is to open the files in a text editor like Notepad++ which can indicate the line endings type. But this can also be achieved using the unix2dos command’s -i / --info option:

dos2unix -i 00-passwd.patch
      36       0       0  no_bom    text    00-passwd.patch

The 36 indicates the number of lines that use DOS-style line endings. Here is a wrapper python script for this which prints out the line endings type:

import os
import sys
import re

if __name__ == '__main__':
    filename = sys.argv[1]
    infooutput = os.popen(f'dos2unix -i {filename}').read().strip()
    doslines = int(re.split(r'\s+', infooutput)[0])
    line_ending_type = 'Windows/DOS (CR LF)' if doslines > 0 else 'Unix (LF)'
    print(line_ending_type)Code language: JavaScript (javascript)

Leave a Reply