~jelmer/loggerhead/breezy-compat

« back to all changes in this revision

Viewing changes to loggerhead/controllers/filediff_ui.py

  • Committer: Colin Watson
  • Date: 2019-09-19 08:10:36 UTC
  • mfrom: (491.2.62 breezy)
  • Revision ID: cjwatson@canonical.com-20190919081036-q1symc2h2iedtlh3
[r=cjwatson] Switch loggerhead over to using the Breezy rather than Bazaar APIs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from StringIO import StringIO
2
 
import urllib
3
 
 
4
 
from bzrlib import diff
5
 
from bzrlib import errors
6
 
from bzrlib import osutils
7
 
 
8
 
from loggerhead import util
9
 
from loggerhead.controllers import TemplatedBranchView
 
1
from io import BytesIO
 
2
 
 
3
from breezy import (
 
4
    diff,
 
5
    errors,
 
6
    osutils,
 
7
    urlutils,
 
8
    )
 
9
 
 
10
from .. import util
 
11
from ..controllers import TemplatedBranchView
 
12
 
10
13
 
11
14
def _process_diff(difftext):
12
15
    chunks = []
13
16
    chunk = None
 
17
    def decode_line(line):
 
18
        return line.decode('utf-8', 'replace')
14
19
    for line in difftext.splitlines():
15
20
        if len(line) == 0:
16
21
            continue
17
 
        if line.startswith('+++ ') or line.startswith('--- '):
 
22
        if line.startswith(b'+++ ') or line.startswith(b'--- '):
18
23
            continue
19
 
        if line.startswith('@@ '):
 
24
        if line.startswith(b'@@ '):
20
25
            # new chunk
21
26
            if chunk is not None:
22
27
                chunks.append(chunk)
23
28
            chunk = util.Container()
24
29
            chunk.diff = []
25
 
            split_lines = line.split(' ')[1:3]
26
 
            lines = [int(x.split(',')[0][1:]) for x in split_lines]
 
30
            split_lines = line.split(b' ')[1:3]
 
31
            lines = [int(x.split(b',')[0][1:]) for x in split_lines]
27
32
            old_lineno = lines[0]
28
33
            new_lineno = lines[1]
29
 
        elif line.startswith(' '):
 
34
        elif line.startswith(b' '):
30
35
            chunk.diff.append(util.Container(old_lineno=old_lineno,
31
36
                                             new_lineno=new_lineno,
32
37
                                             type='context',
33
 
                                             line=line[1:]))
 
38
                                             line=decode_line(line[1:])))
34
39
            old_lineno += 1
35
40
            new_lineno += 1
36
 
        elif line.startswith('+'):
 
41
        elif line.startswith(b'+'):
37
42
            chunk.diff.append(util.Container(old_lineno=None,
38
43
                                             new_lineno=new_lineno,
39
 
                                             type='insert', line=line[1:]))
 
44
                                             type='insert', line=decode_line(line[1:])))
40
45
            new_lineno += 1
41
 
        elif line.startswith('-'):
 
46
        elif line.startswith(b'-'):
42
47
            chunk.diff.append(util.Container(old_lineno=old_lineno,
43
48
                                             new_lineno=None,
44
 
                                             type='delete', line=line[1:]))
 
49
                                             type='delete', line=decode_line(line[1:])))
45
50
            old_lineno += 1
46
51
        else:
47
52
            chunk.diff.append(util.Container(old_lineno=None,
60
65
    lines = {}
61
66
    args = []
62
67
    for r in (compare_revid, revid):
63
 
        if r == 'null:':
 
68
        if r == b'null:':
64
69
            lines[r] = []
65
70
        else:
66
71
            args.append((file_id, r, r))
67
72
    for r, bytes_iter in repository.iter_files_bytes(args):
68
 
        lines[r] = osutils.split_lines(''.join(bytes_iter))
69
 
    buffer = StringIO()
 
73
        lines[r] = osutils.split_lines(b''.join(bytes_iter))
 
74
    buffer = BytesIO()
70
75
    try:
71
76
        diff.internal_diff('', lines[compare_revid], '', lines[revid], buffer, context_lines=context_lines)
72
77
    except errors.BinaryFile:
73
 
        difftext = ''
 
78
        difftext = b''
74
79
    else:
75
80
        difftext = buffer.getvalue()
76
81
 
79
84
 
80
85
class FileDiffUI(TemplatedBranchView):
81
86
 
82
 
    template_path = 'loggerhead.templates.filediff'
 
87
    template_name = 'filediff'
83
88
    supports_json = True
84
89
 
85
90
    def get_values(self, path, kwargs, headers):
86
 
        revid = urllib.unquote(self.args[0])
87
 
        compare_revid = urllib.unquote(self.args[1])
88
 
        file_id = urllib.unquote(self.args[2])
 
91
        revid = urlutils.unquote_to_bytes(self.args[0])
 
92
        compare_revid = urlutils.unquote_to_bytes(self.args[1])
 
93
        file_id = urlutils.unquote_to_bytes(self.args[2])
89
94
 
90
95
        try:
91
96
            context_lines = int(kwargs['context'])