1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
|
# Copyright (C) 2004, 2005, 2006 Canonical Ltd.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import difflib
import os
import re
import sys
from bzrlib.lazy_import import lazy_import
lazy_import(globals(), """
import errno
import subprocess
import tempfile
import time
from bzrlib import (
errors,
osutils,
patiencediff,
textfile,
timestamp,
)
""")
from bzrlib.symbol_versioning import (
deprecated_function,
)
from bzrlib.trace import mutter, warning
# TODO: Rather than building a changeset object, we should probably
# invoke callbacks on an object. That object can either accumulate a
# list, write them out directly, etc etc.
class _PrematchedMatcher(difflib.SequenceMatcher):
"""Allow SequenceMatcher operations to use predetermined blocks"""
def __init__(self, matching_blocks):
difflib.SequenceMatcher(self, None, None)
self.matching_blocks = matching_blocks
self.opcodes = None
def internal_diff(old_filename, oldlines, new_filename, newlines, to_file,
allow_binary=False, sequence_matcher=None,
path_encoding='utf8'):
# FIXME: difflib is wrong if there is no trailing newline.
# The syntax used by patch seems to be "\ No newline at
# end of file" following the last diff line from that
# file. This is not trivial to insert into the
# unified_diff output and it might be better to just fix
# or replace that function.
# In the meantime we at least make sure the patch isn't
# mangled.
# Special workaround for Python2.3, where difflib fails if
# both sequences are empty.
if not oldlines and not newlines:
return
if allow_binary is False:
textfile.check_text_lines(oldlines)
textfile.check_text_lines(newlines)
if sequence_matcher is None:
sequence_matcher = patiencediff.PatienceSequenceMatcher
ud = patiencediff.unified_diff(oldlines, newlines,
fromfile=old_filename.encode(path_encoding),
tofile=new_filename.encode(path_encoding),
sequencematcher=sequence_matcher)
ud = list(ud)
# work-around for difflib being too smart for its own good
# if /dev/null is "1,0", patch won't recognize it as /dev/null
if not oldlines:
ud[2] = ud[2].replace('-1,0', '-0,0')
elif not newlines:
ud[2] = ud[2].replace('+1,0', '+0,0')
# work around for difflib emitting random spaces after the label
ud[0] = ud[0][:-2] + '\n'
ud[1] = ud[1][:-2] + '\n'
for line in ud:
to_file.write(line)
if not line.endswith('\n'):
to_file.write("\n\\ No newline at end of file\n")
to_file.write('\n')
def _spawn_external_diff(diffcmd, capture_errors=True):
"""Spawn the externall diff process, and return the child handle.
:param diffcmd: The command list to spawn
:param capture_errors: Capture stderr as well as setting LANG=C
and LC_ALL=C. This lets us read and understand the output of diff,
and respond to any errors.
:return: A Popen object.
"""
if capture_errors:
# construct minimal environment
env = {}
path = os.environ.get('PATH')
if path is not None:
env['PATH'] = path
env['LANGUAGE'] = 'C' # on win32 only LANGUAGE has effect
env['LANG'] = 'C'
env['LC_ALL'] = 'C'
stderr = subprocess.PIPE
else:
env = None
stderr = None
try:
pipe = subprocess.Popen(diffcmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=stderr,
env=env)
except OSError, e:
if e.errno == errno.ENOENT:
raise errors.NoDiff(str(e))
raise
return pipe
def external_diff(old_filename, oldlines, new_filename, newlines, to_file,
diff_opts):
"""Display a diff by calling out to the external diff program."""
# make sure our own output is properly ordered before the diff
to_file.flush()
oldtmp_fd, old_abspath = tempfile.mkstemp(prefix='bzr-diff-old-')
newtmp_fd, new_abspath = tempfile.mkstemp(prefix='bzr-diff-new-')
oldtmpf = os.fdopen(oldtmp_fd, 'wb')
newtmpf = os.fdopen(newtmp_fd, 'wb')
try:
# TODO: perhaps a special case for comparing to or from the empty
# sequence; can just use /dev/null on Unix
# TODO: if either of the files being compared already exists as a
# regular named file (e.g. in the working directory) then we can
# compare directly to that, rather than copying it.
oldtmpf.writelines(oldlines)
newtmpf.writelines(newlines)
oldtmpf.close()
newtmpf.close()
if not diff_opts:
diff_opts = []
diffcmd = ['diff',
'--label', old_filename,
old_abspath,
'--label', new_filename,
new_abspath,
'--binary',
]
# diff only allows one style to be specified; they don't override.
# note that some of these take optargs, and the optargs can be
# directly appended to the options.
# this is only an approximate parser; it doesn't properly understand
# the grammar.
for s in ['-c', '-u', '-C', '-U',
'-e', '--ed',
'-q', '--brief',
'--normal',
'-n', '--rcs',
'-y', '--side-by-side',
'-D', '--ifdef']:
for j in diff_opts:
if j.startswith(s):
break
else:
continue
break
else:
diffcmd.append('-u')
if diff_opts:
diffcmd.extend(diff_opts)
pipe = _spawn_external_diff(diffcmd, capture_errors=True)
out,err = pipe.communicate()
rc = pipe.returncode
# internal_diff() adds a trailing newline, add one here for consistency
out += '\n'
if rc == 2:
# 'diff' gives retcode == 2 for all sorts of errors
# one of those is 'Binary files differ'.
# Bad options could also be the problem.
# 'Binary files' is not a real error, so we suppress that error.
lang_c_out = out
# Since we got here, we want to make sure to give an i18n error
pipe = _spawn_external_diff(diffcmd, capture_errors=False)
out, err = pipe.communicate()
# Write out the new i18n diff response
to_file.write(out+'\n')
if pipe.returncode != 2:
raise errors.BzrError(
'external diff failed with exit code 2'
' when run with LANG=C and LC_ALL=C,'
' but not when run natively: %r' % (diffcmd,))
first_line = lang_c_out.split('\n', 1)[0]
# Starting with diffutils 2.8.4 the word "binary" was dropped.
m = re.match('^(binary )?files.*differ$', first_line, re.I)
if m is None:
raise errors.BzrError('external diff failed with exit code 2;'
' command: %r' % (diffcmd,))
else:
# Binary files differ, just return
return
# If we got to here, we haven't written out the output of diff
# do so now
to_file.write(out)
if rc not in (0, 1):
# returns 1 if files differ; that's OK
if rc < 0:
msg = 'signal %d' % (-rc)
else:
msg = 'exit code %d' % rc
raise errors.BzrError('external diff failed with %s; command: %r'
% (rc, diffcmd))
finally:
oldtmpf.close() # and delete
newtmpf.close()
# Clean up. Warn in case the files couldn't be deleted
# (in case windows still holds the file open, but not
# if the files have already been deleted)
try:
os.remove(old_abspath)
except OSError, e:
if e.errno not in (errno.ENOENT,):
warning('Failed to delete temporary file: %s %s',
old_abspath, e)
try:
os.remove(new_abspath)
except OSError:
if e.errno not in (errno.ENOENT,):
warning('Failed to delete temporary file: %s %s',
new_abspath, e)
def diff_cmd_helper(tree, specific_files, external_diff_options,
old_revision_spec=None, new_revision_spec=None,
revision_specs=None,
old_label='a/', new_label='b/'):
"""Helper for cmd_diff.
:param tree:
A WorkingTree
:param specific_files:
The specific files to compare, or None
:param external_diff_options:
If non-None, run an external diff, and pass it these options
:param old_revision_spec:
If None, use basis tree as old revision, otherwise use the tree for
the specified revision.
:param new_revision_spec:
If None, use working tree as new revision, otherwise use the tree for
the specified revision.
:param revision_specs:
Zero, one or two RevisionSpecs from the command line, saying what revisions
to compare. This can be passed as an alternative to the old_revision_spec
and new_revision_spec parameters.
The more general form is show_diff_trees(), where the caller
supplies any two trees.
"""
# TODO: perhaps remove the old parameters old_revision_spec and
# new_revision_spec, since this is only really for use from cmd_diff and
# it now always passes through a sequence of revision_specs -- mbp
# 20061221
def spec_tree(spec):
if tree:
revision = spec.in_store(tree.branch)
else:
revision = spec.in_store(None)
revision_id = revision.rev_id
branch = revision.branch
return branch.repository.revision_tree(revision_id)
if revision_specs is not None:
assert (old_revision_spec is None
and new_revision_spec is None)
if len(revision_specs) > 0:
old_revision_spec = revision_specs[0]
if len(revision_specs) > 1:
new_revision_spec = revision_specs[1]
if old_revision_spec is None:
old_tree = tree.basis_tree()
else:
old_tree = spec_tree(old_revision_spec)
if (new_revision_spec is None
or new_revision_spec.spec is None):
new_tree = tree
else:
new_tree = spec_tree(new_revision_spec)
if new_tree is not tree:
extra_trees = (tree,)
else:
extra_trees = None
return show_diff_trees(old_tree, new_tree, sys.stdout, specific_files,
external_diff_options,
old_label=old_label, new_label=new_label,
extra_trees=extra_trees)
def show_diff_trees(old_tree, new_tree, to_file, specific_files=None,
external_diff_options=None,
old_label='a/', new_label='b/',
extra_trees=None,
path_encoding='utf8'):
"""Show in text form the changes from one tree to another.
to_files
If set, include only changes to these files.
external_diff_options
If set, use an external GNU diff and pass these options.
extra_trees
If set, more Trees to use for looking up file ids
path_encoding
If set, the path will be encoded as specified, otherwise is supposed
to be utf8
"""
old_tree.lock_read()
try:
if extra_trees is not None:
for tree in extra_trees:
tree.lock_read()
new_tree.lock_read()
try:
differ = DiffTree.from_trees_options(old_tree, new_tree, to_file,
path_encoding,
external_diff_options,
old_label, new_label)
return differ.show_diff(specific_files, extra_trees)
finally:
new_tree.unlock()
if extra_trees is not None:
for tree in extra_trees:
tree.unlock()
finally:
old_tree.unlock()
def _patch_header_date(tree, file_id, path):
"""Returns a timestamp suitable for use in a patch header."""
mtime = tree.get_file_mtime(file_id, path)
assert mtime is not None, \
"got an mtime of None for file-id %s, path %s in tree %s" % (
file_id, path, tree)
return timestamp.format_patch_date(mtime)
def _raise_if_nonexistent(paths, old_tree, new_tree):
"""Complain if paths are not in either inventory or tree.
It's OK with the files exist in either tree's inventory, or
if they exist in the tree but are not versioned.
This can be used by operations such as bzr status that can accept
unknown or ignored files.
"""
mutter("check paths: %r", paths)
if not paths:
return
s = old_tree.filter_unversioned_files(paths)
s = new_tree.filter_unversioned_files(s)
s = [path for path in s if not new_tree.has_filename(path)]
if s:
raise errors.PathsDoNotExist(sorted(s))
def get_prop_change(meta_modified):
if meta_modified:
return " (properties changed)"
else:
return ""
class DiffPath(object):
"""Base type for command object that compare files"""
# The type or contents of the file were unsuitable for diffing
CANNOT_DIFF = 'CANNOT_DIFF'
# The file has changed in a semantic way
CHANGED = 'CHANGED'
# The file content may have changed, but there is no semantic change
UNCHANGED = 'UNCHANGED'
def __init__(self, old_tree, new_tree, to_file, path_encoding='utf-8'):
"""Constructor.
:param old_tree: The tree to show as the old tree in the comparison
:param new_tree: The tree to show as new in the comparison
:param to_file: The file to write comparison data to
:param path_encoding: The character encoding to write paths in
"""
self.old_tree = old_tree
self.new_tree = new_tree
self.to_file = to_file
self.path_encoding = path_encoding
@classmethod
def from_diff_tree(klass, diff_tree):
return klass(diff_tree.old_tree, diff_tree.new_tree,
diff_tree.to_file, diff_tree.path_encoding)
@staticmethod
def _diff_many(differs, file_id, old_path, new_path, old_kind, new_kind):
for file_differ in differs:
result = file_differ.diff(file_id, old_path, new_path, old_kind,
new_kind)
if result is not DiffPath.CANNOT_DIFF:
return result
else:
return DiffPath.CANNOT_DIFF
class DiffKindChange(object):
"""Special differ for file kind changes.
Represents kind change as deletion + creation. Uses the other differs
to do this.
"""
def __init__(self, differs):
self.differs = differs
@classmethod
def from_diff_tree(klass, diff_tree):
return klass(diff_tree.differs)
def diff(self, file_id, old_path, new_path, old_kind, new_kind):
"""Perform comparison
:param file_id: The file_id of the file to compare
:param old_path: Path of the file in the old tree
:param new_path: Path of the file in the new tree
:param old_kind: Old file-kind of the file
:param new_kind: New file-kind of the file
"""
if None in (old_kind, new_kind):
return DiffPath.CANNOT_DIFF
result = DiffPath._diff_many(self.differs, file_id, old_path,
new_path, old_kind, None)
if result is DiffPath.CANNOT_DIFF:
return result
return DiffPath._diff_many(self.differs, file_id, old_path, new_path,
None, new_kind)
class DiffDirectory(DiffPath):
def diff(self, file_id, old_path, new_path, old_kind, new_kind):
"""Perform comparison between two directories. (dummy)
"""
if 'directory' not in (old_kind, new_kind):
return self.CANNOT_DIFF
if old_kind not in ('directory', None):
return self.CANNOT_DIFF
if new_kind not in ('directory', None):
return self.CANNOT_DIFF
return self.CHANGED
class DiffSymlink(DiffPath):
def diff(self, file_id, old_path, new_path, old_kind, new_kind):
"""Perform comparison between two symlinks
:param file_id: The file_id of the file to compare
:param old_path: Path of the file in the old tree
:param new_path: Path of the file in the new tree
:param old_kind: Old file-kind of the file
:param new_kind: New file-kind of the file
"""
if 'symlink' not in (old_kind, new_kind):
return self.CANNOT_DIFF
if old_kind == 'symlink':
old_target = self.old_tree.get_symlink_target(file_id)
elif old_kind is None:
old_target = None
else:
return self.CANNOT_DIFF
if new_kind == 'symlink':
new_target = self.new_tree.get_symlink_target(file_id)
elif new_kind is None:
new_target = None
else:
return self.CANNOT_DIFF
return self.diff_symlink(old_target, new_target)
def diff_symlink(self, old_target, new_target):
if old_target is None:
self.to_file.write('=== target is %r\n' % new_target)
elif new_target is None:
self.to_file.write('=== target was %r\n' % old_target)
else:
self.to_file.write('=== target changed %r => %r\n' %
(old_target, new_target))
return self.CHANGED
class DiffText(DiffPath):
# GNU Patch uses the epoch date to detect files that are being added
# or removed in a diff.
EPOCH_DATE = '1970-01-01 00:00:00 +0000'
def __init__(self, old_tree, new_tree, to_file, path_encoding='utf-8',
old_label='', new_label='', text_differ=internal_diff):
DiffPath.__init__(self, old_tree, new_tree, to_file, path_encoding)
self.text_differ = text_differ
self.old_label = old_label
self.new_label = new_label
self.path_encoding = path_encoding
def diff(self, file_id, old_path, new_path, old_kind, new_kind):
"""Compare two files in unified diff format
:param file_id: The file_id of the file to compare
:param old_path: Path of the file in the old tree
:param new_path: Path of the file in the new tree
:param old_kind: Old file-kind of the file
:param new_kind: New file-kind of the file
"""
if 'file' not in (old_kind, new_kind):
return self.CANNOT_DIFF
from_file_id = to_file_id = file_id
if old_kind == 'file':
old_date = _patch_header_date(self.old_tree, file_id, old_path)
elif old_kind is None:
old_date = self.EPOCH_DATE
from_file_id = None
else:
return self.CANNOT_DIFF
if new_kind == 'file':
new_date = _patch_header_date(self.new_tree, file_id, new_path)
elif new_kind is None:
new_date = self.EPOCH_DATE
to_file_id = None
else:
return self.CANNOT_DIFF
from_label = '%s%s\t%s' % (self.old_label, old_path, old_date)
to_label = '%s%s\t%s' % (self.new_label, new_path, new_date)
return self.diff_text(from_file_id, to_file_id, from_label, to_label)
def diff_text(self, from_file_id, to_file_id, from_label, to_label):
"""Diff the content of given files in two trees
:param from_file_id: The id of the file in the from tree. If None,
the file is not present in the from tree.
:param to_file_id: The id of the file in the to tree. This may refer
to a different file from from_file_id. If None,
the file is not present in the to tree.
"""
def _get_text(tree, file_id):
if file_id is not None:
return tree.get_file(file_id).readlines()
else:
return []
try:
from_text = _get_text(self.old_tree, from_file_id)
to_text = _get_text(self.new_tree, to_file_id)
self.text_differ(from_label, from_text, to_label, to_text,
self.to_file)
except errors.BinaryFile:
self.to_file.write(
("Binary files %s and %s differ\n" %
(from_label, to_label)).encode(self.path_encoding))
return self.CHANGED
class DiffTree(object):
"""Provides textual representations of the difference between two trees.
A DiffTree examines two trees and where a file-id has altered
between them, generates a textual representation of the difference.
DiffTree uses a sequence of DiffPath objects which are each
given the opportunity to handle a given altered fileid. The list
of DiffPath objects can be extended globally by appending to
DiffTree.diff_factories, or for a specific diff operation by
supplying the extra_factories option to the appropriate method.
"""
# list of factories that can provide instances of DiffPath objects
# may be extended by plugins.
diff_factories = [DiffSymlink.from_diff_tree,
DiffDirectory.from_diff_tree]
def __init__(self, old_tree, new_tree, to_file, path_encoding='utf-8',
diff_text=None, extra_factories=None):
"""Constructor
:param old_tree: Tree to show as old in the comparison
:param new_tree: Tree to show as new in the comparison
:param to_file: File to write comparision to
:param path_encoding: Character encoding to write paths in
:param diff_text: DiffPath-type object to use as a last resort for
diffing text files.
:param extra_factories: Factories of DiffPaths to try before any other
DiffPaths"""
if diff_text is None:
diff_text = DiffText(old_tree, new_tree, to_file, path_encoding,
'', '', internal_diff)
self.old_tree = old_tree
self.new_tree = new_tree
self.to_file = to_file
self.path_encoding = path_encoding
self.differs = []
if extra_factories is not None:
self.differs.extend(f(self) for f in extra_factories)
self.differs.extend(f(self) for f in self.diff_factories)
self.differs.extend([diff_text, DiffKindChange.from_diff_tree(self)])
@classmethod
def from_trees_options(klass, old_tree, new_tree, to_file,
path_encoding, external_diff_options, old_label,
new_label):
"""Factory for producing a DiffTree.
Designed to accept options used by show_diff_trees.
:param old_tree: The tree to show as old in the comparison
:param new_tree: The tree to show as new in the comparison
:param to_file: File to write comparisons to
:param path_encoding: Character encoding to use for writing paths
:param external_diff_options: If supplied, use the installed diff
binary to perform file comparison, using supplied options.
:param old_label: Prefix to use for old file labels
:param new_label: Prefix to use for new file labels
"""
if external_diff_options:
assert isinstance(external_diff_options, basestring)
opts = external_diff_options.split()
def diff_file(olab, olines, nlab, nlines, to_file):
external_diff(olab, olines, nlab, nlines, to_file, opts)
else:
diff_file = internal_diff
diff_text = DiffText(old_tree, new_tree, to_file, path_encoding,
old_label, new_label, diff_file)
return klass(old_tree, new_tree, to_file, path_encoding, diff_text)
def show_diff(self, specific_files, extra_trees=None):
"""Write tree diff to self.to_file
:param sepecific_files: the specific files to compare (recursive)
:param extra_trees: extra trees to use for mapping paths to file_ids
"""
# TODO: Generation of pseudo-diffs for added/deleted files could
# be usefully made into a much faster special case.
delta = self.new_tree.changes_from(self.old_tree,
specific_files=specific_files,
extra_trees=extra_trees, require_versioned=True)
has_changes = 0
for path, file_id, kind in delta.removed:
has_changes = 1
path_encoded = path.encode(self.path_encoding, "replace")
self.to_file.write("=== removed %s '%s'\n" % (kind, path_encoded))
self.diff(file_id, path, path)
for path, file_id, kind in delta.added:
has_changes = 1
path_encoded = path.encode(self.path_encoding, "replace")
self.to_file.write("=== added %s '%s'\n" % (kind, path_encoded))
self.diff(file_id, path, path)
for (old_path, new_path, file_id, kind,
text_modified, meta_modified) in delta.renamed:
has_changes = 1
prop_str = get_prop_change(meta_modified)
oldpath_encoded = old_path.encode(self.path_encoding, "replace")
newpath_encoded = new_path.encode(self.path_encoding, "replace")
self.to_file.write("=== renamed %s '%s' => '%s'%s\n" % (kind,
oldpath_encoded, newpath_encoded, prop_str))
if text_modified:
self.diff(file_id, old_path, new_path)
for path, file_id, kind, text_modified, meta_modified in\
delta.modified:
has_changes = 1
prop_str = get_prop_change(meta_modified)
path_encoded = path.encode(self.path_encoding, "replace")
self.to_file.write("=== modified %s '%s'%s\n" % (kind,
path_encoded, prop_str))
# The file may be in a different location in the old tree (because
# the containing dir was renamed, but the file itself was not)
if text_modified:
old_path = self.old_tree.id2path(file_id)
self.diff(file_id, old_path, path)
return has_changes
def diff(self, file_id, old_path, new_path):
"""Perform a diff of a single file
:param file_id: file-id of the file
:param old_path: The path of the file in the old tree
:param new_path: The path of the file in the new tree
"""
try:
old_kind = self.old_tree.kind(file_id)
except errors.NoSuchId:
old_kind = None
try:
new_kind = self.new_tree.kind(file_id)
except errors.NoSuchId:
new_kind = None
result = DiffPath._diff_many(self.differs, file_id, old_path,
new_path, old_kind, new_kind)
if result is DiffPath.CANNOT_DIFF:
error_path = new_path
if error_path is None:
error_path = old_path
raise errors.NoDiffFound(error_path)
|