~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/switch.py

  • Committer: Ian Clatworthy
  • Date: 2007-12-07 05:31:54 UTC
  • mto: (3092.1.1 ianc-integration)
  • mto: This revision was merged to the branch mainline in revision 3093.
  • Revision ID: ian.clatworthy@internode.on.net-20071207053154-k9tmyczcf8niwonm
fix efficiency of local commit detection as recommended by jameinel's review

Show diffs side-by-side

added added

removed removed

Lines of Context:
47
47
    try:
48
48
        tree = control.open_workingtree()
49
49
    except errors.NotBranchError, ex:
 
50
        # Lightweight checkout and branch is no longer there
50
51
        if force:
51
52
            return
52
53
        else:
77
78
            # have been pushed to the current bound branch then
78
79
            # synchronise the local branch with the new remote branch
79
80
            # and bind to it
80
 
            if not force and _any_local_commits(b, bound_branch):
 
81
            possible_transports = []
 
82
            if not force and _any_local_commits(b, possible_transports):
81
83
                raise errors.BzrCommandError(
82
84
                    'Cannot switch as local commits found in the checkout. '
83
85
                    'Commit these to the bound branch or use --force to '
84
86
                    'throw them away.')
85
87
            b.set_bound_location(None)
86
 
            b.pull(to_branch, overwrite=True)
 
88
            b.pull(to_branch, overwrite=True,
 
89
                possible_transports=possible_transports)
87
90
            b.set_bound_location(to_branch.base)
88
91
        else:
89
92
            raise errors.BzrCommandError('Cannot switch a branch, '
90
93
                'only a checkout.')
91
94
 
92
95
 
93
 
def _any_local_commits(this_branch, other_branch_url):
94
 
    """Does this branch have any commits not in the other branch?"""
 
96
def _any_local_commits(this_branch, possible_transports):
 
97
    """Does this branch have any commits not in the master branch?"""
95
98
    last_rev = revision.ensure_null(this_branch.last_revision())
96
99
    if last_rev != revision.NULL_REVISION:
97
 
        a_bzrdir, relpath = BzrDir.open_containing(other_branch_url)
98
 
        other_branch = a_bzrdir.open_branch()
 
100
        other_branch = this_branch.get_master_branch(possible_transports)
 
101
        this_branch.lock_read()
99
102
        other_branch.lock_read()
100
103
        try:
101
104
            other_last_rev = other_branch.last_revision()
102
 
            remote_graph = other_branch.repository.get_revision_graph(
103
 
                other_last_rev)
104
 
            if last_rev not in remote_graph:
 
105
            graph = this_branch.repository.get_graph(
 
106
                other_branch.repository)
 
107
            if not graph.is_ancestor(last_rev, other_last_rev):
105
108
                return True
106
109
        finally:
107
110
            other_branch.unlock()
 
111
            this_branch.unlock()
108
112
    return False
109
113
 
110
114