git am can't import patches with the UTF8 BOM

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

this happens to me very often when someone sends a patch using windows
(and I use linux):

$ git am ~/Desktop/0001-1664-nonsymbolic-systems-may-need-basis-recalculatio.patch
Patch format detection failed.

and the problem is that the patch contains the byte-order mark (BOM)
at the beginning:

$ hexdump -C ~/Desktop/0001-1664-nonsymbolic-systems-may-need-basis-recalculatio.patch
| less
00000000  ef bb bf 46 72 6f 6d 20  39 31 37 63 30 39 36 32  |...From 917c0962|
00000010  32 38 35 30 37 37 31 66  38 33 33 62 35 66 39 34  |2850771f833b5f94|
00000020  30 36 65 30 64 65 37 33  30 35 61 34 30 38 66 65  |06e0de7305a408fe|
00000030  20 4d 6f 6e 20 53 65 70  20 31 37 20 30 30 3a 30  | Mon Sep 17 00:0|
00000040  30 3a 30 30 20 32 30 30  31 0a 46 72 6f 6d 3a 20  |0:00 2001.From: |

e.g. it's the "ef bb bf" as can be checked on the wikipedia:

http://en.wikipedia.org/wiki/Byte-order_mark#Representations_of_byte_order_marks_by_encoding

for utf-8.

So either the windows version of git should not send the BOM in the
first place, or the linux version of git should be able to handle it.
Which of those should be fixed?

Thanks,
Ondrej

P.S. I currently use this simple python script to strip it:

------------------------
#!/usr/bin/python
"""
Fixes a bogus git patch.

Sometimes a patch that people submit to sympy contains the UTF-8 byte-order
mark (BOM), which are 3 character at the beginning, that cause "git am" to fail
when applying it. The solution is to remove them, which is the purpose of this
script. See this link for more info:

http://en.wikipedia.org/wiki/Byte-order_mark

Usage:

  git-fix-patch some.patch > some_fixed.patch

you can also rewrite the original file with the fix (inplace) by:

  git-fix-patch -s some.patch

"""

from textwrap import fill
import os
import re
from optparse import OptionParser

def main():
    parser = OptionParser(usage="[options] args")
    parser.add_option("-s", "--save", dest="save", action="store_true",
            default=False,
            help="Rewrite the original file with the fixed patch")
    options, args = parser.parse_args()
    if len(args) != 1:
        parser.print_help()
        return

    filename = args[0]
    s = open(filename).read()
    start = s.find("From")
    if start > 10:
        raise Exception("Uknown format of the git patch")

    # strip the bogus characters at the beginning of the file
    s = s[start:]

    # either save to a file or dump to stdout:
    if options.save:
        outfile = filename
        open(outfile, "w").write(s)
    else:
        print s

if __name__ == '__main__':
    main()
-----------------------
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]