~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/diff.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-01-02 04:25:42 UTC
  • mfrom: (3146.1.1 parents-provider-fix)
  • Revision ID: pqm@pqm.ubuntu.com-20080102042542-13ocbo4243mov89j
Fix handling of ghost revisions in KnitParentsProvider (abentley)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2014 Canonical Ltd.
 
1
# Copyright (C) 2004, 2005, 2006 Canonical Ltd.
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
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
 
from __future__ import absolute_import
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
16
 
19
17
import difflib
20
18
import os
21
19
import re
22
 
import string
 
20
import shutil
23
21
import sys
24
22
 
25
23
from bzrlib.lazy_import import lazy_import
27
25
import errno
28
26
import subprocess
29
27
import tempfile
 
28
import time
30
29
 
31
30
from bzrlib import (
32
 
    cleanup,
33
 
    cmdline,
34
 
    controldir,
 
31
    bzrdir,
 
32
    commands,
35
33
    errors,
36
34
    osutils,
37
35
    patiencediff,
38
36
    textfile,
39
37
    timestamp,
40
 
    views,
41
38
    )
42
 
 
43
 
from bzrlib.workingtree import WorkingTree
44
 
from bzrlib.i18n import gettext
45
39
""")
46
40
 
47
 
from bzrlib.registry import (
48
 
    Registry,
49
 
    )
50
 
from bzrlib.trace import mutter, note, warning
51
 
 
52
 
DEFAULT_CONTEXT_AMOUNT = 3
53
 
 
54
 
class AtTemplate(string.Template):
55
 
    """Templating class that uses @ instead of $."""
56
 
 
57
 
    delimiter = '@'
 
41
from bzrlib.symbol_versioning import (
 
42
        deprecated_function,
 
43
        one_zero,
 
44
        )
 
45
from bzrlib.trace import mutter, warning
58
46
 
59
47
 
60
48
# TODO: Rather than building a changeset object, we should probably
73
61
 
74
62
def internal_diff(old_filename, oldlines, new_filename, newlines, to_file,
75
63
                  allow_binary=False, sequence_matcher=None,
76
 
                  path_encoding='utf8', context_lines=DEFAULT_CONTEXT_AMOUNT):
 
64
                  path_encoding='utf8'):
77
65
    # FIXME: difflib is wrong if there is no trailing newline.
78
66
    # The syntax used by patch seems to be "\ No newline at
79
67
    # end of file" following the last diff line from that
89
77
    # both sequences are empty.
90
78
    if not oldlines and not newlines:
91
79
        return
92
 
 
 
80
    
93
81
    if allow_binary is False:
94
82
        textfile.check_text_lines(oldlines)
95
83
        textfile.check_text_lines(newlines)
97
85
    if sequence_matcher is None:
98
86
        sequence_matcher = patiencediff.PatienceSequenceMatcher
99
87
    ud = patiencediff.unified_diff(oldlines, newlines,
100
 
                      fromfile=old_filename.encode(path_encoding, 'replace'),
101
 
                      tofile=new_filename.encode(path_encoding, 'replace'),
102
 
                      n=context_lines, sequencematcher=sequence_matcher)
 
88
                      fromfile=old_filename.encode(path_encoding),
 
89
                      tofile=new_filename.encode(path_encoding),
 
90
                      sequencematcher=sequence_matcher)
103
91
 
104
92
    ud = list(ud)
105
93
    if len(ud) == 0: # Identical contents, nothing to do
110
98
        ud[2] = ud[2].replace('-1,0', '-0,0')
111
99
    elif not newlines:
112
100
        ud[2] = ud[2].replace('+1,0', '+0,0')
 
101
    # work around for difflib emitting random spaces after the label
 
102
    ud[0] = ud[0][:-2] + '\n'
 
103
    ud[1] = ud[1][:-2] + '\n'
113
104
 
114
105
    for line in ud:
115
106
        to_file.write(line)
119
110
 
120
111
 
121
112
def _spawn_external_diff(diffcmd, capture_errors=True):
122
 
    """Spawn the external diff process, and return the child handle.
 
113
    """Spawn the externall diff process, and return the child handle.
123
114
 
124
115
    :param diffcmd: The command list to spawn
125
116
    :param capture_errors: Capture stderr as well as setting LANG=C
154
145
 
155
146
    return pipe
156
147
 
157
 
# diff style options as of GNU diff v3.2
158
 
style_option_list = ['-c', '-C', '--context',
159
 
                     '-e', '--ed',
160
 
                     '-f', '--forward-ed',
161
 
                     '-q', '--brief',
162
 
                     '--normal',
163
 
                     '-n', '--rcs',
164
 
                     '-u', '-U', '--unified',
165
 
                     '-y', '--side-by-side',
166
 
                     '-D', '--ifdef']
167
 
 
168
 
def default_style_unified(diff_opts):
169
 
    """Default to unified diff style if alternative not specified in diff_opts.
170
 
 
171
 
        diff only allows one style to be specified; they don't override.
172
 
        Note that some of these take optargs, and the optargs can be
173
 
        directly appended to the options.
174
 
        This is only an approximate parser; it doesn't properly understand
175
 
        the grammar.
176
 
 
177
 
    :param diff_opts: List of options for external (GNU) diff.
178
 
    :return: List of options with default style=='unified'.
179
 
    """
180
 
    for s in style_option_list:
181
 
        for j in diff_opts:
182
 
            if j.startswith(s):
183
 
                break
184
 
        else:
185
 
            continue
186
 
        break
187
 
    else:
188
 
        diff_opts.append('-u')
189
 
    return diff_opts
190
 
 
191
148
 
192
149
def external_diff(old_filename, oldlines, new_filename, newlines, to_file,
193
150
                  diff_opts):
216
173
 
217
174
        if not diff_opts:
218
175
            diff_opts = []
219
 
        if sys.platform == 'win32':
220
 
            # Popen doesn't do the proper encoding for external commands
221
 
            # Since we are dealing with an ANSI api, use mbcs encoding
222
 
            old_filename = old_filename.encode('mbcs')
223
 
            new_filename = new_filename.encode('mbcs')
224
176
        diffcmd = ['diff',
225
177
                   '--label', old_filename,
226
178
                   old_abspath,
229
181
                   '--binary',
230
182
                  ]
231
183
 
232
 
        diff_opts = default_style_unified(diff_opts)
233
 
 
 
184
        # diff only allows one style to be specified; they don't override.
 
185
        # note that some of these take optargs, and the optargs can be
 
186
        # directly appended to the options.
 
187
        # this is only an approximate parser; it doesn't properly understand
 
188
        # the grammar.
 
189
        for s in ['-c', '-u', '-C', '-U',
 
190
                  '-e', '--ed',
 
191
                  '-q', '--brief',
 
192
                  '--normal',
 
193
                  '-n', '--rcs',
 
194
                  '-y', '--side-by-side',
 
195
                  '-D', '--ifdef']:
 
196
            for j in diff_opts:
 
197
                if j.startswith(s):
 
198
                    break
 
199
            else:
 
200
                continue
 
201
            break
 
202
        else:
 
203
            diffcmd.append('-u')
 
204
                  
234
205
        if diff_opts:
235
206
            diffcmd.extend(diff_opts)
236
207
 
237
208
        pipe = _spawn_external_diff(diffcmd, capture_errors=True)
238
209
        out,err = pipe.communicate()
239
210
        rc = pipe.returncode
240
 
 
 
211
        
241
212
        # internal_diff() adds a trailing newline, add one here for consistency
242
213
        out += '\n'
243
214
        if rc == 2:
278
249
                msg = 'signal %d' % (-rc)
279
250
            else:
280
251
                msg = 'exit code %d' % rc
281
 
 
282
 
            raise errors.BzrError('external diff failed with %s; command: %r'
283
 
                                  % (msg, diffcmd))
 
252
                
 
253
            raise errors.BzrError('external diff failed with %s; command: %r' 
 
254
                                  % (rc, diffcmd))
284
255
 
285
256
 
286
257
    finally:
287
258
        oldtmpf.close()                 # and delete
288
259
        newtmpf.close()
289
 
 
290
 
        def cleanup(path):
291
 
            # Warn in case the file couldn't be deleted (in case windows still
292
 
            # holds the file open, but not if the files have already been
293
 
            # deleted)
294
 
            try:
295
 
                os.remove(path)
296
 
            except OSError, e:
297
 
                if e.errno not in (errno.ENOENT,):
298
 
                    warning('Failed to delete temporary file: %s %s', path, e)
299
 
 
300
 
        cleanup(old_abspath)
301
 
        cleanup(new_abspath)
302
 
 
303
 
 
304
 
def get_trees_and_branches_to_diff_locked(
305
 
    path_list, revision_specs, old_url, new_url, add_cleanup, apply_view=True):
 
260
        # Clean up. Warn in case the files couldn't be deleted
 
261
        # (in case windows still holds the file open, but not
 
262
        # if the files have already been deleted)
 
263
        try:
 
264
            os.remove(old_abspath)
 
265
        except OSError, e:
 
266
            if e.errno not in (errno.ENOENT,):
 
267
                warning('Failed to delete temporary file: %s %s',
 
268
                        old_abspath, e)
 
269
        try:
 
270
            os.remove(new_abspath)
 
271
        except OSError:
 
272
            if e.errno not in (errno.ENOENT,):
 
273
                warning('Failed to delete temporary file: %s %s',
 
274
                        new_abspath, e)
 
275
 
 
276
 
 
277
@deprecated_function(one_zero)
 
278
def diff_cmd_helper(tree, specific_files, external_diff_options, 
 
279
                    old_revision_spec=None, new_revision_spec=None,
 
280
                    revision_specs=None,
 
281
                    old_label='a/', new_label='b/'):
 
282
    """Helper for cmd_diff.
 
283
 
 
284
    :param tree:
 
285
        A WorkingTree
 
286
 
 
287
    :param specific_files:
 
288
        The specific files to compare, or None
 
289
 
 
290
    :param external_diff_options:
 
291
        If non-None, run an external diff, and pass it these options
 
292
 
 
293
    :param old_revision_spec:
 
294
        If None, use basis tree as old revision, otherwise use the tree for
 
295
        the specified revision. 
 
296
 
 
297
    :param new_revision_spec:
 
298
        If None, use working tree as new revision, otherwise use the tree for
 
299
        the specified revision.
 
300
    
 
301
    :param revision_specs: 
 
302
        Zero, one or two RevisionSpecs from the command line, saying what revisions 
 
303
        to compare.  This can be passed as an alternative to the old_revision_spec 
 
304
        and new_revision_spec parameters.
 
305
 
 
306
    The more general form is show_diff_trees(), where the caller
 
307
    supplies any two trees.
 
308
    """
 
309
 
 
310
    # TODO: perhaps remove the old parameters old_revision_spec and
 
311
    # new_revision_spec, since this is only really for use from cmd_diff and
 
312
    # it now always passes through a sequence of revision_specs -- mbp
 
313
    # 20061221
 
314
 
 
315
    def spec_tree(spec):
 
316
        if tree:
 
317
            revision = spec.in_store(tree.branch)
 
318
        else:
 
319
            revision = spec.in_store(None)
 
320
        revision_id = revision.rev_id
 
321
        branch = revision.branch
 
322
        return branch.repository.revision_tree(revision_id)
 
323
 
 
324
    if revision_specs is not None:
 
325
        assert (old_revision_spec is None
 
326
                and new_revision_spec is None)
 
327
        if len(revision_specs) > 0:
 
328
            old_revision_spec = revision_specs[0]
 
329
        if len(revision_specs) > 1:
 
330
            new_revision_spec = revision_specs[1]
 
331
 
 
332
    if old_revision_spec is None:
 
333
        old_tree = tree.basis_tree()
 
334
    else:
 
335
        old_tree = spec_tree(old_revision_spec)
 
336
 
 
337
    if (new_revision_spec is None
 
338
        or new_revision_spec.spec is None):
 
339
        new_tree = tree
 
340
    else:
 
341
        new_tree = spec_tree(new_revision_spec)
 
342
 
 
343
    if new_tree is not tree:
 
344
        extra_trees = (tree,)
 
345
    else:
 
346
        extra_trees = None
 
347
 
 
348
    return show_diff_trees(old_tree, new_tree, sys.stdout, specific_files,
 
349
                           external_diff_options,
 
350
                           old_label=old_label, new_label=new_label,
 
351
                           extra_trees=extra_trees)
 
352
 
 
353
 
 
354
def _get_trees_to_diff(path_list, revision_specs, old_url, new_url):
306
355
    """Get the trees and specific files to diff given a list of paths.
307
356
 
308
357
    This method works out the trees to be diff'ed and the files of
319
368
    :param new_url:
320
369
        The url of the new branch or tree. If None, the tree to use is
321
370
        taken from the first path, if any, or the current working tree.
322
 
    :param add_cleanup:
323
 
        a callable like Command.add_cleanup.  get_trees_and_branches_to_diff
324
 
        will register cleanups that must be run to unlock the trees, etc.
325
 
    :param apply_view:
326
 
        if True and a view is set, apply the view or check that the paths
327
 
        are within it
328
371
    :returns:
329
 
        a tuple of (old_tree, new_tree, old_branch, new_branch,
330
 
        specific_files, extra_trees) where extra_trees is a sequence of
331
 
        additional trees to search in for file-ids.  The trees and branches
332
 
        will be read-locked until the cleanups registered via the add_cleanup
333
 
        param are run.
 
372
        a tuple of (old_tree, new_tree, specific_files, extra_trees) where
 
373
        extra_trees is a sequence of additional trees to search in for
 
374
        file-ids.
334
375
    """
335
376
    # Get the old and new revision specs
336
377
    old_revision_spec = None
347
388
 
348
389
    other_paths = []
349
390
    make_paths_wt_relative = True
350
 
    consider_relpath = True
351
391
    if path_list is None or len(path_list) == 0:
352
 
        # If no path is given, the current working tree is used
 
392
        # If no path is given, assume the current directory
353
393
        default_location = u'.'
354
 
        consider_relpath = False
355
394
    elif old_url is not None and new_url is not None:
356
395
        other_paths = path_list
357
396
        make_paths_wt_relative = False
359
398
        default_location = path_list[0]
360
399
        other_paths = path_list[1:]
361
400
 
362
 
    def lock_tree_or_branch(wt, br):
363
 
        if wt is not None:
364
 
            wt.lock_read()
365
 
            add_cleanup(wt.unlock)
366
 
        elif br is not None:
367
 
            br.lock_read()
368
 
            add_cleanup(br.unlock)
369
 
 
370
401
    # Get the old location
371
402
    specific_files = []
372
403
    if old_url is None:
373
404
        old_url = default_location
374
405
    working_tree, branch, relpath = \
375
 
        controldir.ControlDir.open_containing_tree_or_branch(old_url)
376
 
    lock_tree_or_branch(working_tree, branch)
377
 
    if consider_relpath and relpath != '':
378
 
        if working_tree is not None and apply_view:
379
 
            views.check_path_in_view(working_tree, relpath)
 
406
        bzrdir.BzrDir.open_containing_tree_or_branch(old_url)
 
407
    if relpath != '':
380
408
        specific_files.append(relpath)
381
409
    old_tree = _get_tree_to_diff(old_revision_spec, working_tree, branch)
382
 
    old_branch = branch
383
410
 
384
411
    # Get the new location
385
412
    if new_url is None:
386
413
        new_url = default_location
387
414
    if new_url != old_url:
388
415
        working_tree, branch, relpath = \
389
 
            controldir.ControlDir.open_containing_tree_or_branch(new_url)
390
 
        lock_tree_or_branch(working_tree, branch)
391
 
        if consider_relpath and relpath != '':
392
 
            if working_tree is not None and apply_view:
393
 
                views.check_path_in_view(working_tree, relpath)
 
416
            bzrdir.BzrDir.open_containing_tree_or_branch(new_url)
 
417
        if relpath != '':
394
418
            specific_files.append(relpath)
395
419
    new_tree = _get_tree_to_diff(new_revision_spec, working_tree, branch,
396
420
        basis_is_default=working_tree is None)
397
 
    new_branch = branch
398
421
 
399
422
    # Get the specific files (all files is None, no files is [])
400
423
    if make_paths_wt_relative and working_tree is not None:
401
 
        other_paths = working_tree.safe_relpath_files(
402
 
            other_paths,
403
 
            apply_view=apply_view)
 
424
        other_paths = _relative_paths_in_tree(working_tree, other_paths)
404
425
    specific_files.extend(other_paths)
405
426
    if len(specific_files) == 0:
406
427
        specific_files = None
407
 
        if (working_tree is not None and working_tree.supports_views()
408
 
            and apply_view):
409
 
            view_files = working_tree.views.lookup_view()
410
 
            if view_files:
411
 
                specific_files = view_files
412
 
                view_str = views.view_display_str(view_files)
413
 
                note(gettext("*** Ignoring files outside view. View is %s") % view_str)
414
428
 
415
429
    # Get extra trees that ought to be searched for file-ids
416
430
    extra_trees = None
417
431
    if working_tree is not None and working_tree not in (old_tree, new_tree):
418
432
        extra_trees = (working_tree,)
419
 
    return (old_tree, new_tree, old_branch, new_branch,
420
 
            specific_files, extra_trees)
 
433
    return old_tree, new_tree, specific_files, extra_trees
421
434
 
422
435
 
423
436
def _get_tree_to_diff(spec, tree=None, branch=None, basis_is_default=True):
431
444
                return branch.basis_tree()
432
445
        else:
433
446
            return tree
434
 
    return spec.as_tree(branch)
 
447
    revision = spec.in_store(branch)
 
448
    revision_id = revision.rev_id
 
449
    rev_branch = revision.branch
 
450
    return rev_branch.repository.revision_tree(revision_id)
 
451
 
 
452
 
 
453
def _relative_paths_in_tree(tree, paths):
 
454
    """Get the relative paths within a working tree.
 
455
 
 
456
    Each path may be either an absolute path or a path relative to the
 
457
    current working directory.
 
458
    """
 
459
    result = []
 
460
    for filename in paths:
 
461
        try:
 
462
            result.append(tree.relpath(osutils.dereference_path(filename)))
 
463
        except errors.PathNotChild:
 
464
            raise errors.BzrCommandError("Files are in different branches")
 
465
    return result
435
466
 
436
467
 
437
468
def show_diff_trees(old_tree, new_tree, to_file, specific_files=None,
439
470
                    old_label='a/', new_label='b/',
440
471
                    extra_trees=None,
441
472
                    path_encoding='utf8',
442
 
                    using=None,
443
 
                    format_cls=None,
444
 
                    context=DEFAULT_CONTEXT_AMOUNT):
 
473
                    using=None):
445
474
    """Show in text form the changes from one tree to another.
446
475
 
447
 
    :param to_file: The output stream.
448
 
    :param specific_files: Include only changes to these files - None for all
449
 
        changes.
450
 
    :param external_diff_options: If set, use an external GNU diff and pass 
451
 
        these options.
452
 
    :param extra_trees: If set, more Trees to use for looking up file ids
453
 
    :param path_encoding: If set, the path will be encoded as specified, 
454
 
        otherwise is supposed to be utf8
455
 
    :param format_cls: Formatter class (DiffTree subclass)
 
476
    to_file
 
477
        The output stream.
 
478
 
 
479
    specific_files
 
480
        Include only changes to these files - None for all changes.
 
481
 
 
482
    external_diff_options
 
483
        If set, use an external GNU diff and pass these options.
 
484
 
 
485
    extra_trees
 
486
        If set, more Trees to use for looking up file ids
 
487
 
 
488
    path_encoding
 
489
        If set, the path will be encoded as specified, otherwise is supposed
 
490
        to be utf8
456
491
    """
457
 
    if context is None:
458
 
        context = DEFAULT_CONTEXT_AMOUNT
459
 
    if format_cls is None:
460
 
        format_cls = DiffTree
461
492
    old_tree.lock_read()
462
493
    try:
463
494
        if extra_trees is not None:
465
496
                tree.lock_read()
466
497
        new_tree.lock_read()
467
498
        try:
468
 
            differ = format_cls.from_trees_options(old_tree, new_tree, to_file,
469
 
                                                   path_encoding,
470
 
                                                   external_diff_options,
471
 
                                                   old_label, new_label, using,
472
 
                                                   context_lines=context)
 
499
            differ = DiffTree.from_trees_options(old_tree, new_tree, to_file,
 
500
                                                 path_encoding,
 
501
                                                 external_diff_options,
 
502
                                                 old_label, new_label, using)
473
503
            return differ.show_diff(specific_files, extra_trees)
474
504
        finally:
475
505
            new_tree.unlock()
482
512
 
483
513
def _patch_header_date(tree, file_id, path):
484
514
    """Returns a timestamp suitable for use in a patch header."""
485
 
    try:
486
 
        mtime = tree.get_file_mtime(file_id, path)
487
 
    except errors.FileTimestampUnavailable:
488
 
        mtime = 0
 
515
    mtime = tree.get_file_mtime(file_id, path)
 
516
    assert mtime is not None, \
 
517
        "got an mtime of None for file-id %s, path %s in tree %s" % (
 
518
                file_id, path, tree)
489
519
    return timestamp.format_patch_date(mtime)
490
520
 
491
521
 
492
 
def get_executable_change(old_is_x, new_is_x):
493
 
    descr = { True:"+x", False:"-x", None:"??" }
494
 
    if old_is_x != new_is_x:
495
 
        return ["%s to %s" % (descr[old_is_x], descr[new_is_x],)]
 
522
def _raise_if_nonexistent(paths, old_tree, new_tree):
 
523
    """Complain if paths are not in either inventory or tree.
 
524
 
 
525
    It's OK with the files exist in either tree's inventory, or 
 
526
    if they exist in the tree but are not versioned.
 
527
    
 
528
    This can be used by operations such as bzr status that can accept
 
529
    unknown or ignored files.
 
530
    """
 
531
    mutter("check paths: %r", paths)
 
532
    if not paths:
 
533
        return
 
534
    s = old_tree.filter_unversioned_files(paths)
 
535
    s = new_tree.filter_unversioned_files(s)
 
536
    s = [path for path in s if not new_tree.has_filename(path)]
 
537
    if s:
 
538
        raise errors.PathsDoNotExist(sorted(s))
 
539
 
 
540
 
 
541
def get_prop_change(meta_modified):
 
542
    if meta_modified:
 
543
        return " (properties changed)"
496
544
    else:
497
 
        return []
 
545
        return  ""
498
546
 
499
547
 
500
548
class DiffPath(object):
633
681
    # or removed in a diff.
634
682
    EPOCH_DATE = '1970-01-01 00:00:00 +0000'
635
683
 
636
 
    def __init__(self, old_tree, new_tree, to_file, path_encoding='utf-8', 
637
 
                 old_label='', new_label='', text_differ=internal_diff, 
638
 
                 context_lines=DEFAULT_CONTEXT_AMOUNT):
 
684
    def __init__(self, old_tree, new_tree, to_file, path_encoding='utf-8',
 
685
                 old_label='', new_label='', text_differ=internal_diff):
639
686
        DiffPath.__init__(self, old_tree, new_tree, to_file, path_encoding)
640
687
        self.text_differ = text_differ
641
688
        self.old_label = old_label
642
689
        self.new_label = new_label
643
690
        self.path_encoding = path_encoding
644
 
        self.context_lines = context_lines
645
691
 
646
692
    def diff(self, file_id, old_path, new_path, old_kind, new_kind):
647
693
        """Compare two files in unified diff format
671
717
            return self.CANNOT_DIFF
672
718
        from_label = '%s%s\t%s' % (self.old_label, old_path, old_date)
673
719
        to_label = '%s%s\t%s' % (self.new_label, new_path, new_date)
674
 
        return self.diff_text(from_file_id, to_file_id, from_label, to_label,
675
 
            old_path, new_path)
 
720
        return self.diff_text(from_file_id, to_file_id, from_label, to_label)
676
721
 
677
 
    def diff_text(self, from_file_id, to_file_id, from_label, to_label,
678
 
        from_path=None, to_path=None):
 
722
    def diff_text(self, from_file_id, to_file_id, from_label, to_label):
679
723
        """Diff the content of given files in two trees
680
724
 
681
725
        :param from_file_id: The id of the file in the from tree.  If None,
683
727
        :param to_file_id: The id of the file in the to tree.  This may refer
684
728
            to a different file from from_file_id.  If None,
685
729
            the file is not present in the to tree.
686
 
        :param from_path: The path in the from tree or None if unknown.
687
 
        :param to_path: The path in the to tree or None if unknown.
688
730
        """
689
 
        def _get_text(tree, file_id, path):
 
731
        def _get_text(tree, file_id):
690
732
            if file_id is not None:
691
 
                return tree.get_file_lines(file_id, path)
 
733
                return tree.get_file(file_id).readlines()
692
734
            else:
693
735
                return []
694
736
        try:
695
 
            from_text = _get_text(self.old_tree, from_file_id, from_path)
696
 
            to_text = _get_text(self.new_tree, to_file_id, to_path)
 
737
            from_text = _get_text(self.old_tree, from_file_id)
 
738
            to_text = _get_text(self.new_tree, to_file_id)
697
739
            self.text_differ(from_label, from_text, to_label, to_text,
698
 
                             self.to_file, path_encoding=self.path_encoding,
699
 
                             context_lines=self.context_lines)
 
740
                             self.to_file)
700
741
        except errors.BinaryFile:
701
742
            self.to_file.write(
702
743
                  ("Binary files %s and %s differ\n" %
703
 
                  (from_label, to_label)).encode(self.path_encoding,'replace'))
 
744
                  (from_label, to_label)).encode(self.path_encoding))
704
745
        return self.CHANGED
705
746
 
706
747
 
710
751
                 path_encoding='utf-8'):
711
752
        DiffPath.__init__(self, old_tree, new_tree, to_file, path_encoding)
712
753
        self.command_template = command_template
713
 
        self._root = osutils.mkdtemp(prefix='bzr-diff-')
 
754
        self._root = tempfile.mkdtemp(prefix='bzr-diff-')
714
755
 
715
756
    @classmethod
716
757
    def from_string(klass, command_string, old_tree, new_tree, to_file,
717
758
                    path_encoding='utf-8'):
718
 
        command_template = cmdline.split(command_string)
719
 
        if '@' not in command_string:
720
 
            command_template.extend(['@old_path', '@new_path'])
 
759
        command_template = commands.shlex_split_unicode(command_string)
 
760
        command_template.extend(['%(old_path)s', '%(new_path)s'])
721
761
        return klass(command_template, old_tree, new_tree, to_file,
722
762
                     path_encoding)
723
763
 
724
764
    @classmethod
725
 
    def make_from_diff_tree(klass, command_string, external_diff_options=None):
 
765
    def make_from_diff_tree(klass, command_string):
726
766
        def from_diff_tree(diff_tree):
727
 
            full_command_string = [command_string]
728
 
            if external_diff_options is not None:
729
 
                full_command_string += ' ' + external_diff_options
730
 
            return klass.from_string(full_command_string, diff_tree.old_tree,
 
767
            return klass.from_string(command_string, diff_tree.old_tree,
731
768
                                     diff_tree.new_tree, diff_tree.to_file)
732
769
        return from_diff_tree
733
770
 
734
771
    def _get_command(self, old_path, new_path):
735
772
        my_map = {'old_path': old_path, 'new_path': new_path}
736
 
        command = [AtTemplate(t).substitute(my_map) for t in
737
 
                   self.command_template]
738
 
        if sys.platform == 'win32': # Popen doesn't accept unicode on win32
739
 
            command_encoded = []
740
 
            for c in command:
741
 
                if isinstance(c, unicode):
742
 
                    command_encoded.append(c.encode('mbcs'))
743
 
                else:
744
 
                    command_encoded.append(c)
745
 
            return command_encoded
746
 
        else:
747
 
            return command
 
773
        return [t % my_map for t in self.command_template]
748
774
 
749
775
    def _execute(self, old_path, new_path):
750
 
        command = self._get_command(old_path, new_path)
751
 
        try:
752
 
            proc = subprocess.Popen(command, stdout=subprocess.PIPE,
753
 
                                    cwd=self._root)
754
 
        except OSError, e:
755
 
            if e.errno == errno.ENOENT:
756
 
                raise errors.ExecutableMissing(command[0])
757
 
            else:
758
 
                raise
 
776
        proc = subprocess.Popen(self._get_command(old_path, new_path),
 
777
                                stdout=subprocess.PIPE, cwd=self._root)
759
778
        self.to_file.write(proc.stdout.read())
760
779
        return proc.wait()
761
780
 
762
 
    def _try_symlink_root(self, tree, prefix):
763
 
        if (getattr(tree, 'abspath', None) is None
764
 
            or not osutils.host_os_dereferences_symlinks()):
765
 
            return False
766
 
        try:
767
 
            os.symlink(tree.abspath(''), osutils.pathjoin(self._root, prefix))
768
 
        except OSError, e:
769
 
            if e.errno != errno.EEXIST:
770
 
                raise
771
 
        return True
772
 
 
773
 
    @staticmethod
774
 
    def _fenc():
775
 
        """Returns safe encoding for passing file path to diff tool"""
776
 
        if sys.platform == 'win32':
777
 
            return 'mbcs'
778
 
        else:
779
 
            # Don't fallback to 'utf-8' because subprocess may not be able to
780
 
            # handle utf-8 correctly when locale is not utf-8.
781
 
            return sys.getfilesystemencoding() or 'ascii'
782
 
 
783
 
    def _is_safepath(self, path):
784
 
        """Return true if `path` may be able to pass to subprocess."""
785
 
        fenc = self._fenc()
786
 
        try:
787
 
            return path == path.encode(fenc).decode(fenc)
788
 
        except UnicodeError:
789
 
            return False
790
 
 
791
 
    def _safe_filename(self, prefix, relpath):
792
 
        """Replace unsafe character in `relpath` then join `self._root`,
793
 
        `prefix` and `relpath`."""
794
 
        fenc = self._fenc()
795
 
        # encoded_str.replace('?', '_') may break multibyte char.
796
 
        # So we should encode, decode, then replace(u'?', u'_')
797
 
        relpath_tmp = relpath.encode(fenc, 'replace').decode(fenc, 'replace')
798
 
        relpath_tmp = relpath_tmp.replace(u'?', u'_')
799
 
        return osutils.pathjoin(self._root, prefix, relpath_tmp)
800
 
 
801
 
    def _write_file(self, file_id, tree, prefix, relpath, force_temp=False,
802
 
                    allow_write=False):
803
 
        if not force_temp and isinstance(tree, WorkingTree):
804
 
            full_path = tree.abspath(tree.id2path(file_id))
805
 
            if self._is_safepath(full_path):
806
 
                return full_path
807
 
 
808
 
        full_path = self._safe_filename(prefix, relpath)
809
 
        if not force_temp and self._try_symlink_root(tree, prefix):
810
 
            return full_path
811
 
        parent_dir = osutils.dirname(full_path)
 
781
    def _write_file(self, file_id, tree, prefix, old_path):
 
782
        full_old_path = osutils.pathjoin(self._root, prefix, old_path)
 
783
        parent_dir = osutils.dirname(full_old_path)
812
784
        try:
813
785
            os.makedirs(parent_dir)
814
786
        except OSError, e:
815
787
            if e.errno != errno.EEXIST:
816
788
                raise
817
 
        source = tree.get_file(file_id, relpath)
 
789
        source = tree.get_file(file_id)
818
790
        try:
819
 
            target = open(full_path, 'wb')
 
791
            target = open(full_old_path, 'wb')
820
792
            try:
821
793
                osutils.pumpfile(source, target)
822
794
            finally:
823
795
                target.close()
824
796
        finally:
825
797
            source.close()
826
 
        try:
827
 
            mtime = tree.get_file_mtime(file_id)
828
 
        except errors.FileTimestampUnavailable:
829
 
            pass
830
 
        else:
831
 
            os.utime(full_path, (mtime, mtime))
832
 
        if not allow_write:
833
 
            osutils.make_readonly(full_path)
834
 
        return full_path
 
798
        return full_old_path
835
799
 
836
 
    def _prepare_files(self, file_id, old_path, new_path, force_temp=False,
837
 
                       allow_write_new=False):
 
800
    def _prepare_files(self, file_id, old_path, new_path):
838
801
        old_disk_path = self._write_file(file_id, self.old_tree, 'old',
839
 
                                         old_path, force_temp)
 
802
                                         old_path)
840
803
        new_disk_path = self._write_file(file_id, self.new_tree, 'new',
841
 
                                         new_path, force_temp,
842
 
                                         allow_write=allow_write_new)
 
804
                                         new_path)
843
805
        return old_disk_path, new_disk_path
844
806
 
845
807
    def finish(self):
846
 
        try:
847
 
            osutils.rmtree(self._root)
848
 
        except OSError, e:
849
 
            if e.errno != errno.ENOENT:
850
 
                mutter("The temporary directory \"%s\" was not "
851
 
                        "cleanly removed: %s." % (self._root, e))
 
808
        shutil.rmtree(self._root)
852
809
 
853
810
    def diff(self, file_id, old_path, new_path, old_kind, new_kind):
854
811
        if (old_kind, new_kind) != ('file', 'file'):
855
812
            return DiffPath.CANNOT_DIFF
856
 
        (old_disk_path, new_disk_path) = self._prepare_files(
857
 
                                                file_id, old_path, new_path)
858
 
        self._execute(old_disk_path, new_disk_path)
859
 
 
860
 
    def edit_file(self, file_id):
861
 
        """Use this tool to edit a file.
862
 
 
863
 
        A temporary copy will be edited, and the new contents will be
864
 
        returned.
865
 
 
866
 
        :param file_id: The id of the file to edit.
867
 
        :return: The new contents of the file.
868
 
        """
869
 
        old_path = self.old_tree.id2path(file_id)
870
 
        new_path = self.new_tree.id2path(file_id)
871
 
        old_abs_path, new_abs_path = self._prepare_files(
872
 
                                            file_id, old_path, new_path,
873
 
                                            allow_write_new=True,
874
 
                                            force_temp=True)
875
 
        command = self._get_command(old_abs_path, new_abs_path)
876
 
        subprocess.call(command, cwd=self._root)
877
 
        new_file = open(new_abs_path, 'rb')
878
 
        try:
879
 
            return new_file.read()
880
 
        finally:
881
 
            new_file.close()
 
813
        self._prepare_files(file_id, old_path, new_path)
 
814
        self._execute(osutils.pathjoin('old', old_path),
 
815
                      osutils.pathjoin('new', new_path))
882
816
 
883
817
 
884
818
class DiffTree(object):
926
860
    @classmethod
927
861
    def from_trees_options(klass, old_tree, new_tree, to_file,
928
862
                           path_encoding, external_diff_options, old_label,
929
 
                           new_label, using, context_lines):
 
863
                           new_label, using):
930
864
        """Factory for producing a DiffTree.
931
865
 
932
866
        Designed to accept options used by show_diff_trees.
933
 
 
934
867
        :param old_tree: The tree to show as old in the comparison
935
868
        :param new_tree: The tree to show as new in the comparison
936
869
        :param to_file: File to write comparisons to
942
875
        :param using: Commandline to use to invoke an external diff tool
943
876
        """
944
877
        if using is not None:
945
 
            extra_factories = [DiffFromTool.make_from_diff_tree(using, external_diff_options)]
 
878
            extra_factories = [DiffFromTool.make_from_diff_tree(using)]
946
879
        else:
947
880
            extra_factories = []
948
881
        if external_diff_options:
 
882
            assert isinstance(external_diff_options, basestring)
949
883
            opts = external_diff_options.split()
950
 
            def diff_file(olab, olines, nlab, nlines, to_file, path_encoding=None, context_lines=None):
951
 
                """:param path_encoding: not used but required
952
 
                        to match the signature of internal_diff.
953
 
                """
 
884
            def diff_file(olab, olines, nlab, nlines, to_file):
954
885
                external_diff(olab, olines, nlab, nlines, to_file, opts)
955
886
        else:
956
887
            diff_file = internal_diff
957
888
        diff_text = DiffText(old_tree, new_tree, to_file, path_encoding,
958
 
                             old_label, new_label, diff_file, context_lines=context_lines)
 
889
                             old_label, new_label, diff_file)
959
890
        return klass(old_tree, new_tree, to_file, path_encoding, diff_text,
960
891
                     extra_factories)
961
892
 
962
893
    def show_diff(self, specific_files, extra_trees=None):
963
894
        """Write tree diff to self.to_file
964
895
 
965
 
        :param specific_files: the specific files to compare (recursive)
 
896
        :param sepecific_files: the specific files to compare (recursive)
966
897
        :param extra_trees: extra trees to use for mapping paths to file_ids
967
898
        """
968
899
        try:
974
905
    def _show_diff(self, specific_files, extra_trees):
975
906
        # TODO: Generation of pseudo-diffs for added/deleted files could
976
907
        # be usefully made into a much faster special case.
977
 
        iterator = self.new_tree.iter_changes(self.old_tree,
 
908
        iterator = self.new_tree._iter_changes(self.old_tree,
978
909
                                               specific_files=specific_files,
979
910
                                               extra_trees=extra_trees,
980
911
                                               require_versioned=True)
990
921
                return path.encode(self.path_encoding, "replace")
991
922
        for (file_id, paths, changed_content, versioned, parent, name, kind,
992
923
             executable) in sorted(iterator, key=changes_key):
993
 
            # The root does not get diffed, and items with no known kind (that
994
 
            # is, missing) in both trees are skipped as well.
995
 
            if parent == (None, None) or kind == (None, None):
 
924
            if parent == (None, None):
996
925
                continue
997
926
            oldpath, newpath = paths
998
927
            oldpath_encoded = get_encoded_path(paths[0])
1000
929
            old_present = (kind[0] is not None and versioned[0])
1001
930
            new_present = (kind[1] is not None and versioned[1])
1002
931
            renamed = (parent[0], name[0]) != (parent[1], name[1])
1003
 
 
1004
 
            properties_changed = []
1005
 
            properties_changed.extend(get_executable_change(executable[0], executable[1]))
1006
 
 
1007
 
            if properties_changed:
1008
 
                prop_str = " (properties changed: %s)" % (", ".join(properties_changed),)
1009
 
            else:
1010
 
                prop_str = ""
1011
 
 
 
932
            prop_str = get_prop_change(executable[0] != executable[1])
1012
933
            if (old_present, new_present) == (True, False):
1013
934
                self.to_file.write("=== removed %s '%s'\n" %
1014
935
                                   (kind[0], oldpath_encoded))
1021
942
                self.to_file.write("=== renamed %s '%s' => '%s'%s\n" %
1022
943
                    (kind[0], oldpath_encoded, newpath_encoded, prop_str))
1023
944
            else:
1024
 
                # if it was produced by iter_changes, it must be
 
945
                # if it was produced by _iter_changes, it must be
1025
946
                # modified *somehow*, either content or execute bit.
1026
947
                self.to_file.write("=== modified %s '%s'%s\n" % (kind[0],
1027
948
                                   newpath_encoded, prop_str))
1028
949
            if changed_content:
1029
 
                self._diff(file_id, oldpath, newpath, kind[0], kind[1])
 
950
                self.diff(file_id, oldpath, newpath)
1030
951
                has_changes = 1
1031
952
            if renamed:
1032
953
                has_changes = 1
1047
968
            new_kind = self.new_tree.kind(file_id)
1048
969
        except (errors.NoSuchId, errors.NoSuchFile):
1049
970
            new_kind = None
1050
 
        self._diff(file_id, old_path, new_path, old_kind, new_kind)
1051
 
 
1052
 
 
1053
 
    def _diff(self, file_id, old_path, new_path, old_kind, new_kind):
 
971
 
1054
972
        result = DiffPath._diff_many(self.differs, file_id, old_path,
1055
973
                                       new_path, old_kind, new_kind)
1056
974
        if result is DiffPath.CANNOT_DIFF:
1058
976
            if error_path is None:
1059
977
                error_path = old_path
1060
978
            raise errors.NoDiffFound(error_path)
1061
 
 
1062
 
 
1063
 
format_registry = Registry()
1064
 
format_registry.register('default', DiffTree)