/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5273.1.5 by Vincent Ladeuil
Merge bzr.dev into cleanup
1
# Copyright (C) 2009, 2010 Canonical Ltd
4208.4.1 by Ian Clatworthy
eol conversion support
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17
"""End of Line Conversion filters.
18
19
See bzr help eol for details.
20
"""
21
6379.6.1 by Jelmer Vernooij
Import absolute_import in a few places.
22
from __future__ import absolute_import
23
4208.4.1 by Ian Clatworthy
eol conversion support
24
25
import re, sys
26
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
27
from ..errors import BzrError
28
from ..filters import ContentFilter
4334.1.1 by Ian Clatworthy
(igc) Raise an error for unknown eol values. (Brian de Alwis)
29
4208.4.1 by Ian Clatworthy
eol conversion support
30
5278.1.2 by Martin Pool
Don't say 'Linux' except when specifically talking about the kernel
31
# Real Unix newline - \n without \r before it
5278.1.4 by Martin Pool
Change _LINUX_NL to _UNIX_NL
32
_UNIX_NL_RE = re.compile(r'(?<!\r)\n')
4208.4.1 by Ian Clatworthy
eol conversion support
33
34
35
def _to_lf_converter(chunks, context=None):
36
    """A content file that converts crlf to lf."""
37
    content = ''.join(chunks)
38
    if '\x00' in content:
39
        return [content]
40
    else:
41
        return [content.replace('\r\n', '\n')]
42
43
44
def _to_crlf_converter(chunks, context=None):
45
    """A content file that converts lf to crlf."""
46
    content = ''.join(chunks)
47
    if '\x00' in content:
48
        return [content]
49
    else:
5278.1.4 by Martin Pool
Change _LINUX_NL to _UNIX_NL
50
        return [_UNIX_NL_RE.sub('\r\n', content)]
4208.4.1 by Ian Clatworthy
eol conversion support
51
52
6379.2.1 by Jelmer Vernooij
Lazy load bzrlib.filters.eol.
53
if sys.platform == 'win32':
54
    _native_output = _to_crlf_converter
55
else:
56
    _native_output = _to_lf_converter
57
_eol_filter_stack_map = {
58
    'exact': [],
59
    'native': [ContentFilter(_to_lf_converter, _native_output)],
60
    'lf':     [ContentFilter(_to_lf_converter, _to_lf_converter)],
61
    'crlf':   [ContentFilter(_to_lf_converter, _to_crlf_converter)],
62
    'native-with-crlf-in-repo':
63
        [ContentFilter(_to_crlf_converter, _native_output)],
64
    'lf-with-crlf-in-repo':
65
        [ContentFilter(_to_crlf_converter, _to_lf_converter)],
66
    'crlf-with-crlf-in-repo':
67
        [ContentFilter(_to_crlf_converter, _to_crlf_converter)],
68
    }
69
def eol_lookup(key):
70
    filter = _eol_filter_stack_map.get(key)
71
    if filter is None:
72
        raise BzrError("Unknown eol value '%s'" % key)
73
    return filter