git/list[1] front-page[2] threads[3] people[4] search[5] about
 

git am can't import patches with the UTF8 BOM

From
Ondrej Certik <ondrej@certik.cz>
Date
Oct 15, 2009, 17:51 UTC
Message-ID
<85b5c3130910151051h799770b9yd0e56472125426cf@mail.gmail.com>
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()
-----------------------
Message 1 of 1 in “git am can't import patches with the UTF8 BOM”
  1. Ondrej CertikOct 15, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.