/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/diff.py

  • Committer: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2019-07-27 23:53:10 UTC
  • mfrom: (7356.1.10 exitstack)
  • Revision ID: breezy.the.bot@gmail.com-20190727235310-ccs69jhu4pq55qsi
Use contextlib.ExitStack rather than our homegrown OperationWithCleanups.

Merged from https://code.launchpad.net/~jelmer/brz/exitstack/+merge/369203

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
import tempfile
30
30
 
31
31
from breezy import (
 
32
    cleanup,
32
33
    controldir,
33
34
    errors,
34
35
    osutils,
362
363
 
363
364
 
364
365
def get_trees_and_branches_to_diff_locked(
365
 
        path_list, revision_specs, old_url, new_url, add_cleanup, apply_view=True):
 
366
        path_list, revision_specs, old_url, new_url, exit_stack, apply_view=True):
366
367
    """Get the trees and specific files to diff given a list of paths.
367
368
 
368
369
    This method works out the trees to be diff'ed and the files of
379
380
    :param new_url:
380
381
        The url of the new branch or tree. If None, the tree to use is
381
382
        taken from the first path, if any, or the current working tree.
382
 
    :param add_cleanup:
383
 
        a callable like Command.add_cleanup.  get_trees_and_branches_to_diff
 
383
    :param exit_stack:
 
384
        an ExitStack object. get_trees_and_branches_to_diff
384
385
        will register cleanups that must be run to unlock the trees, etc.
385
386
    :param apply_view:
386
387
        if True and a view is set, apply the view or check that the paths
389
390
        a tuple of (old_tree, new_tree, old_branch, new_branch,
390
391
        specific_files, extra_trees) where extra_trees is a sequence of
391
392
        additional trees to search in for file-ids.  The trees and branches
392
 
        will be read-locked until the cleanups registered via the add_cleanup
 
393
        will be read-locked until the cleanups registered via the exit_stack
393
394
        param are run.
394
395
    """
395
396
    # Get the old and new revision specs
421
422
 
422
423
    def lock_tree_or_branch(wt, br):
423
424
        if wt is not None:
424
 
            wt.lock_read()
425
 
            add_cleanup(wt.unlock)
 
425
            exit_stack.enter_context(wt.lock_read())
426
426
        elif br is not None:
427
 
            br.lock_read()
428
 
            add_cleanup(br.unlock)
 
427
            exit_stack.enter_context(br.lock_read())
429
428
 
430
429
    # Get the old location
431
430
    specific_files = []
518
517
        context = DEFAULT_CONTEXT_AMOUNT
519
518
    if format_cls is None:
520
519
        format_cls = DiffTree
521
 
    with old_tree.lock_read():
 
520
    with cleanup.ExitStack() as exit_stack:
 
521
        exit_stack.enter_context(old_tree.lock_read())
522
522
        if extra_trees is not None:
523
523
            for tree in extra_trees:
524
 
                tree.lock_read()
525
 
        new_tree.lock_read()
526
 
        try:
527
 
            differ = format_cls.from_trees_options(old_tree, new_tree, to_file,
528
 
                                                   path_encoding,
529
 
                                                   external_diff_options,
530
 
                                                   old_label, new_label, using,
531
 
                                                   context_lines=context)
532
 
            return differ.show_diff(specific_files, extra_trees)
533
 
        finally:
534
 
            new_tree.unlock()
535
 
            if extra_trees is not None:
536
 
                for tree in extra_trees:
537
 
                    tree.unlock()
 
524
                exit_stack.enter_context(tree.lock_read())
 
525
        exit_stack.enter_context(new_tree.lock_read())
 
526
        differ = format_cls.from_trees_options(old_tree, new_tree, to_file,
 
527
                                               path_encoding,
 
528
                                               external_diff_options,
 
529
                                               old_label, new_label, using,
 
530
                                               context_lines=context)
 
531
        return differ.show_diff(specific_files, extra_trees)
538
532
 
539
533
 
540
534
def _patch_header_date(tree, path):
881
875
        except OSError as e:
882
876
            if e.errno != errno.EEXIST:
883
877
                raise
884
 
        source = tree.get_file(relpath)
885
 
        try:
886
 
            with open(full_path, 'wb') as target:
887
 
                osutils.pumpfile(source, target)
888
 
        finally:
889
 
            source.close()
 
878
        with tree.get_file(relpath) as source, \
 
879
                open(full_path, 'wb') as target:
 
880
            osutils.pumpfile(source, target)
890
881
        try:
891
882
            mtime = tree.get_file_mtime(relpath)
892
883
        except FileTimestampUnavailable: