hgext/win32text.py
author Lee Cantey <lcantey@gmail.com>
Mon, 09 Jul 2007 10:46:41 -0700
changeset 4858 30762680fcd2
parent 1300 e58b1c9a0dec
child 4859 8c5aca855b5d
permissions -rw-r--r--
Fix for win32text corrupting files that have CRLF line endings. (issue302) Also generate a warning when encountering this condition. From Shun-ichi Goto in attachment win32text-patch3.txt for issue 302.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4858
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
     1
from mercurial import util, ui
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
     2
from mercurial.i18n import gettext as _
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
     3
import re
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
     4
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
     5
# regexp for single LF without CR preceding.
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
     6
re_single_lf = re.compile('(^|[^\r])\n', re.MULTILINE)
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
     7
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
     8
def dumbdecode(s, cmd):
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
     9
    # warn if already has CRLF in repository.
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    10
    # it might cause unexpected eol conversion.
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    11
    # see issue 302:
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    12
    #   http://www.selenic.com/mercurial/bts/issue302
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    13
    if '\r\n' in s:
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    14
        u = ui.ui()
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    15
        u.warn(_('WARNING: file in repository already has CRLF line ending \n'
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    16
                 ' which does not need eol conversion by win32text plugin.\n'
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    17
                 ' Please reconsider encode/decode setting in'
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    18
                 ' mercurial.ini or .hg/hgrc\n'
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    19
                 ' before next commit.\n'))
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    20
    # replace single LF to CRLF
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    21
    return re_single_lf.sub('\\1\r\n', s)
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    22
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    23
def dumbencode(s, cmd):
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    24
    return s.replace('\r\n', '\n')
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    25
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    26
def clevertest(s, cmd):
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    27
    if '\0' in s: return False
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    28
    return True
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    29
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    30
def cleverdecode(s, cmd):
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    31
    if clevertest(s, cmd):
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    32
        return dumbdecode(s, cmd)
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    33
    return s
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    34
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    35
def cleverencode(s, cmd):
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    36
    if clevertest(s, cmd):
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    37
        return dumbencode(s, cmd)
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    38
    return s
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    39
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    40
util.filtertable.update({
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    41
    'dumbdecode:': dumbdecode,
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    42
    'dumbencode:': dumbencode,
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    43
    'cleverdecode:': cleverdecode,
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    44
    'cleverencode:': cleverencode,
30762680fcd2 Fix for win32text corrupting files that have CRLF line endings. (issue302)
Lee Cantey <lcantey@gmail.com>
parents: 1300
diff changeset
    45
    })