{"thread":{"id":"21252","subject":"git am can't import patches with the UTF8 BOM","startedAt":"2009-10-15T17:51:48Z","lastAt":"2009-10-15T17:51:48Z","messageCount":1,"participants":["Ondrej Certik"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"125091","messageId":"85b5c3130910151051h799770b9yd0e56472125426cf@mail.gmail.com","threadId":"21252","inReplyTo":null,"subject":"git am can't import patches with the UTF8 BOM","fromName":"Ondrej Certik","fromEmail":"ondrej@certik.cz","sentAt":"2009-10-15T17:51:48Z","receivedAt":"2009-10-15T17:51:48Z","isPatch":false,"sender":{"key":"ondrej@certik.cz","avatar":"https://gravatar.com/avatar/106d05822138a98292e09d1580dbe8f27e05faecd87f13d5d58d889d0687771c?d=mp&s=160"},"body":"Hi,\n\nthis happens to me very often when someone sends a patch using windows\n(and I use linux):\n\n$ git am ~/Desktop/0001-1664-nonsymbolic-systems-may-need-basis-recalculatio.patch\nPatch format detection failed.\n\nand the problem is that the patch contains the byte-order mark (BOM)\nat the beginning:\n\n$ hexdump -C ~/Desktop/0001-1664-nonsymbolic-systems-may-need-basis-recalculatio.patch\n| less\n00000000  ef bb bf 46 72 6f 6d 20  39 31 37 63 30 39 36 32  |...From 917c0962|\n00000010  32 38 35 30 37 37 31 66  38 33 33 62 35 66 39 34  |2850771f833b5f94|\n00000020  30 36 65 30 64 65 37 33  30 35 61 34 30 38 66 65  |06e0de7305a408fe|\n00000030  20 4d 6f 6e 20 53 65 70  20 31 37 20 30 30 3a 30  | Mon Sep 17 00:0|\n00000040  30 3a 30 30 20 32 30 30  31 0a 46 72 6f 6d 3a 20  |0:00 2001.From: |\n\ne.g. it's the \"ef bb bf\" as can be checked on the wikipedia:\n\nhttp://en.wikipedia.org/wiki/Byte-order_mark#Representations_of_byte_order_marks_by_encoding\n\nfor utf-8.\n\nSo either the windows version of git should not send the BOM in the\nfirst place, or the linux version of git should be able to handle it.\nWhich of those should be fixed?\n\nThanks,\nOndrej\n\nP.S. I currently use this simple python script to strip it:\n\n------------------------\n#!/usr/bin/python\n\"\"\"\nFixes a bogus git patch.\n\nSometimes a patch that people submit to sympy contains the UTF-8 byte-order\nmark (BOM), which are 3 character at the beginning, that cause \"git am\" to fail\nwhen applying it. The solution is to remove them, which is the purpose of this\nscript. See this link for more info:\n\nhttp://en.wikipedia.org/wiki/Byte-order_mark\n\nUsage:\n\n  git-fix-patch some.patch > some_fixed.patch\n\nyou can also rewrite the original file with the fix (inplace) by:\n\n  git-fix-patch -s some.patch\n\n\"\"\"\n\nfrom textwrap import fill\nimport os\nimport re\nfrom optparse import OptionParser\n\ndef main():\n    parser = OptionParser(usage=\"[options] args\")\n    parser.add_option(\"-s\", \"--save\", dest=\"save\", action=\"store_true\",\n            default=False,\n            help=\"Rewrite the original file with the fixed patch\")\n    options, args = parser.parse_args()\n    if len(args) != 1:\n        parser.print_help()\n        return\n\n    filename = args[0]\n    s = open(filename).read()\n    start = s.find(\"From\")\n    if start > 10:\n        raise Exception(\"Uknown format of the git patch\")\n\n    # strip the bogus characters at the beginning of the file\n    s = s[start:]\n\n    # either save to a file or dump to stdout:\n    if options.save:\n        outfile = filename\n        open(outfile, \"w\").write(s)\n    else:\n        print s\n\nif __name__ == '__main__':\n    main()\n-----------------------\n"}]}