~qbzr-dev/qbzr/0.20

« back to all changes in this revision

Viewing changes to diff.py

  • Committer: Lukáš Lalinský
  • Date: 2007-10-13 14:27:30 UTC
  • mfrom: (141.1.4 qbzr.bialix)
  • Revision ID: lalinsky@gmail.com-20071013142730-fbugi5meilyxav61
Merge Alexander's diff/annotate encoding changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
import sys
25
25
import time
26
26
from cStringIO import StringIO
 
27
 
27
28
from PyQt4 import QtCore, QtGui
 
29
 
28
30
from bzrlib.errors import BinaryFile, NoSuchId
29
31
from bzrlib.textfile import check_text_lines
30
 
from bzrlib.commands import Command, register_command
31
32
from bzrlib.config import GlobalConfig
32
33
from bzrlib.diff import show_diff_trees
33
34
from bzrlib.workingtree import WorkingTree
85
86
        self.new_lines = []
86
87
        self.groups = []
87
88
 
88
 
    def make_diff(self, old_lines, new_lines, complete):
 
89
    def make_diff(self, old_lines, new_lines, complete, encoding='utf-8'):
89
90
        try:
90
91
            check_text_lines(old_lines)
91
92
            check_text_lines(new_lines)
104
105
                    self.groups = list([matcher.get_opcodes()])
105
106
                else:
106
107
                    self.groups = list(matcher.get_grouped_opcodes())
 
108
            self.old_lines = [i.decode(encoding,'replace') for i in old_lines]
 
109
            self.new_lines = [i.decode(encoding,'replace') for i in new_lines]
107
110
 
108
111
    def html_diff_lines(self, html1, html2, inline=True):
109
112
        a = self.old_lines
110
113
        b = self.new_lines
111
114
        groups = self.groups
112
 
        # XXX more complicated encoding support needed
113
115
        a = [a.decode("utf-8", "replace").rstrip("\n") for a in a]
114
116
        b = [b.decode("utf-8", "replace").rstrip("\n") for b in b]
115
117
        for group in groups:
174
176
        tofiledate = self.new_date
175
177
        tofile = self.new_path
176
178
 
177
 
        a = [a.decode("utf-8", "replace").rstrip("\n") for a in a]
178
 
        b = [b.decode("utf-8", "replace").rstrip("\n") for b in b]
 
179
        a = [a.rstrip("\n") for a in a]
 
180
        b = [b.rstrip("\n") for b in b]
179
181
 
180
182
        started = False
181
183
        for group in self.groups:
252
254
                secs = 0
253
255
        return format_timestamp(secs)
254
256
 
255
 
    def _make_diff(self, old_tree, new_tree, specific_files=[], complete=False):
256
 
        delta = new_tree.changes_from(old_tree, specific_files=specific_files, require_versioned=True)
 
257
    def _make_diff(self, old_tree, new_tree, specific_files=[], complete=False,
 
258
                   encoding='utf-8'):
 
259
        delta = new_tree.changes_from(old_tree, specific_files=specific_files,
 
260
                                      require_versioned=True)
257
261
 
258
262
        for path, file_id, kind in delta.removed:
259
263
            diff = FileDiff(_('removed'), path)
263
267
            diff.old_path = path
264
268
            diff.new_path = path
265
269
            if diff.kind != 'directory':
266
 
                diff.make_diff(get_file_lines_from_tree(old_tree, file_id), [], complete)
 
270
                diff.make_diff(get_file_lines_from_tree(old_tree, file_id),
 
271
                               [], complete, encoding)
267
272
            self.append(diff)
268
273
 
269
274
        for path, file_id, kind in delta.added:
274
279
            diff.old_path = path
275
280
            diff.new_path = path
276
281
            if diff.kind != 'directory':
277
 
                diff.make_diff([], get_file_lines_from_tree(new_tree, file_id), complete)
 
282
                diff.make_diff([], get_file_lines_from_tree(new_tree, file_id),
 
283
                               complete, encoding)
278
284
            self.append(diff)
279
285
 
280
286
        for old_path, new_path, file_id, kind, text_modified, meta_modified in delta.renamed:
287
293
            if text_modified:
288
294
                old_lines = get_file_lines_from_tree(old_tree, file_id)
289
295
                new_lines = get_file_lines_from_tree(new_tree, file_id)
290
 
                diff.make_diff(old_lines, new_lines, complete)
 
296
                diff.make_diff(old_lines, new_lines, complete, encoding)
291
297
            self.append(diff)
292
298
 
293
299
        for path, file_id, kind, text_modified, meta_modified in delta.modified:
300
306
            if text_modified:
301
307
                old_lines = get_file_lines_from_tree(old_tree, file_id)
302
308
                new_lines = get_file_lines_from_tree(new_tree, file_id)
303
 
                diff.make_diff(old_lines, new_lines, complete)
 
309
                diff.make_diff(old_lines, new_lines, complete, encoding)
304
310
            self.append(diff)
305
311
 
306
 
    def __init__(self, old_tree, new_tree, specific_files=[], complete=False):
 
312
    def __init__(self, old_tree, new_tree, specific_files=[], complete=False,
 
313
                 encoding='utf-8'):
307
314
        self._metainfo_template = None
308
315
        old_tree.lock_read()
309
316
        new_tree.lock_read()
310
317
        try:
311
 
            self._make_diff(old_tree, new_tree, specific_files, complete)
 
318
            self._make_diff(old_tree, new_tree, specific_files, complete,
 
319
                            encoding)
312
320
        finally:
313
321
            old_tree.unlock()
314
322
            new_tree.unlock()
372
380
 
373
381
    def __init__(self, tree1=None, tree2=None, specific_files=None,
374
382
                 parent=None, custom_title=None, inline=False,
375
 
                 complete=False, branch=None):
 
383
                 complete=False, branch=None, encoding=None):
376
384
        title = [_("Diff")]
377
385
        if custom_title:
378
386
            title.append(custom_title)
384
392
            else:
385
393
                title.append(", ".join(specific_files))
386
394
 
 
395
        config = get_branch_config(branch)
 
396
 
387
397
        size = (780, 580)
388
398
        try:
389
 
            size_str = get_branch_config(branch).get_user_option("qdiff_window_size")
 
399
            size_str = config.get_user_option("qdiff_window_size")
390
400
            if size_str:
391
401
                size = map(int, size_str.split("x", 2))
392
402
        except:
393
403
            pass
394
404
 
 
405
        if encoding is None:
 
406
            encoding = config.get_user_option("encoding") or 'utf-8'
 
407
        else:
 
408
            config.set_user_option('encoding', encoding)
 
409
 
395
410
        QBzrWindow.__init__(self, title, size, parent)
396
411
 
397
412
        self.tree1 = tree1
398
413
        self.tree2 = tree2
399
414
        self.specific_files = specific_files
400
415
 
401
 
        treediff = TreeDiff(self.tree1, self.tree2, self.specific_files, complete)
 
416
        treediff = TreeDiff(self.tree1, self.tree2, self.specific_files,
 
417
                            complete, encoding)
402
418
        self.diffview = DiffView(treediff, self)
403
419
 
404
420
        self.sdiffview = SimpleDiffView(treediff, self)