/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to diff.py

  • Committer: John Arbash Meinel
  • Date: 2007-10-02 23:08:12 UTC
  • mto: (322.1.1 trunk) (330.3.3 trunk)
  • mto: This revision was merged to the branch mainline in revision 368.
  • Revision ID: john@arbash-meinel.com-20071002230812-h8i6pq8fwvodute4
Start testing with Unicode data.
It seems there is some brokenness with serializing Unicode messages.
But otherwise everything seems to be working.

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
except ImportError:
31
31
    have_gconf = False
32
32
 
33
 
import bzrlib
34
 
 
35
 
from bzrlib.diff import show_diff_trees
 
33
from bzrlib import osutils
 
34
from bzrlib.diff import show_diff_trees, internal_diff
36
35
from bzrlib.errors import NoSuchFile
37
36
from bzrlib.trace import warning
38
37
 
210
209
    def set_trees(self, rev_tree, parent_tree):
211
210
        self.rev_tree = rev_tree
212
211
        self.parent_tree = parent_tree
 
212
#        self._build_delta()
 
213
 
 
214
#    def _build_delta(self):
 
215
#        self.parent_tree.lock_read()
 
216
#        self.rev_tree.lock_read()
 
217
#        try:
 
218
#            self.delta = _iter_changes_to_status(self.parent_tree, self.rev_tree)
 
219
#            self.path_to_status = {}
 
220
#            self.path_to_diff = {}
 
221
#            source_inv = self.parent_tree.inventory
 
222
#            target_inv = self.rev_tree.inventory
 
223
#            for (file_id, real_path, change_type, display_path) in self.delta:
 
224
#                self.path_to_status[real_path] = u'=== %s %s' % (change_type, display_path)
 
225
#                if change_type in ('modified', 'renamed and modified'):
 
226
#                    source_ie = source_inv[file_id]
 
227
#                    target_ie = target_inv[file_id]
 
228
#                    sio = StringIO()
 
229
#                    source_ie.diff(internal_diff, *old path, *old_tree,
 
230
#                                   *new_path, target_ie, self.rev_tree,
 
231
#                                   sio)
 
232
#                    self.path_to_diff[real_path] = 
 
233
#
 
234
#        finally:
 
235
#            self.rev_tree.unlock()
 
236
#            self.parent_tree.unlock()
213
237
 
214
238
    def show_diff(self, specific_files):
215
239
        s = StringIO()
220
244
                        # contents as getdefaultencoding(), so we should
221
245
                        # probably try to make the paths in the same encoding.
222
246
                        )
223
 
        self.buffer.set_text(s.getvalue().decode(sys.getdefaultencoding(), 'replace'))
 
247
        # str.decode(encoding, 'replace') doesn't do anything. Because if a
 
248
        # character is not valid in 'encoding' there is nothing to replace, the
 
249
        # 'replace' is for 'str.encode()'
 
250
        try:
 
251
            decoded = s.getvalue().decode(sys.getdefaultencoding())
 
252
        except UnicodeDecodeError:
 
253
            try:
 
254
                decoded = s.getvalue().decode('UTF-8')
 
255
            except UnicodeDecodeError:
 
256
                decoded = s.getvalue().decode('iso-8859-1')
 
257
                # This always works, because every byte has a valid
 
258
                # mapping from iso-8859-1 to Unicode
 
259
        # TextBuffer must contain pure UTF-8 data
 
260
        self.buffer.set_text(decoded.encode('UTF-8'))
224
261
 
225
262
 
226
263
class DiffWindow(gtk.Window):
342
379
            specific_files = None
343
380
 
344
381
        self.diff_view.show_diff(specific_files)
 
382
 
 
383
 
 
384
def _iter_changes_to_status(source, target):
 
385
    """Determine the differences between trees.
 
386
 
 
387
    This is a wrapper around _iter_changes which just yields more
 
388
    understandable results.
 
389
 
 
390
    :param source: The source tree (basis tree)
 
391
    :param target: The target tree
 
392
    :return: A list of (file_id, real_path, change_type, display_path)
 
393
    """
 
394
    added = 'added'
 
395
    removed = 'removed'
 
396
    renamed = 'renamed'
 
397
    renamed_and_modified = 'renamed and modified'
 
398
    modified = 'modified'
 
399
    kind_changed = 'kind changed'
 
400
 
 
401
    # TODO: Handle metadata changes
 
402
 
 
403
    status = []
 
404
    target.lock_read()
 
405
    try:
 
406
        source.lock_read()
 
407
        try:
 
408
            for (file_id, paths, changed_content, versioned, parent_ids, names,
 
409
                 kinds, executables) in target._iter_changes(source):
 
410
 
 
411
                # Skip the root entry if it isn't very interesting
 
412
                if parent_ids == (None, None):
 
413
                    continue
 
414
 
 
415
                change_type = None
 
416
                if kinds[0] is None:
 
417
                    source_marker = ''
 
418
                else:
 
419
                    source_marker = osutils.kind_marker(kinds[0])
 
420
                if kinds[1] is None:
 
421
                    assert kinds[0] is not None
 
422
                    marker = osutils.kind_marker(kinds[0])
 
423
                else:
 
424
                    marker = osutils.kind_marker(kinds[1])
 
425
 
 
426
                real_path = paths[1]
 
427
                if real_path is None:
 
428
                    real_path = paths[0]
 
429
                assert real_path is not None
 
430
                display_path = real_path + marker
 
431
 
 
432
                present_source = versioned[0] and kinds[0] is not None
 
433
                present_target = versioned[1] and kinds[1] is not None
 
434
 
 
435
                if present_source != present_target:
 
436
                    if present_target:
 
437
                        change_type = added
 
438
                    else:
 
439
                        assert present_source
 
440
                        change_type = removed
 
441
                elif names[0] != names[1] or parent_ids[0] != parent_ids[1]:
 
442
                    # Renamed
 
443
                    if changed_content or executables[0] != executables[1]:
 
444
                        # and modified
 
445
                        change_type = renamed_and_modified
 
446
                    else:
 
447
                        change_type = renamed
 
448
                    display_path = (paths[0] + source_marker
 
449
                                    + ' => ' + paths[1] + marker)
 
450
                elif kinds[0] != kinds[1]:
 
451
                    change_type = kind_changed
 
452
                    display_path = (paths[0] + source_marker
 
453
                                    + ' => ' + paths[1] + marker)
 
454
                elif changed_content is True or executables[0] != executables[1]:
 
455
                    change_type = modified
 
456
                else:
 
457
                    assert False, "How did we get here?"
 
458
 
 
459
                status.append((file_id, real_path, change_type, display_path))
 
460
        finally:
 
461
            source.unlock()
 
462
    finally:
 
463
        target.unlock()
 
464
 
 
465
    return status