~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/missing.py

  • Committer: John Arbash Meinel
  • Date: 2008-05-28 21:53:46 UTC
  • mfrom: (3453 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3458.
  • Revision ID: john@arbash-meinel.com-20080528215346-l8plys9h9ps624pn
merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Display what revisions are missing in 'other' from 'this' and vice versa."""
18
18
 
19
 
from bzrlib import ui
20
19
from bzrlib.log import (
21
20
    LogRevision,
22
21
    )
43
42
        yield LogRevision(rev, revno, delta=delta)
44
43
 
45
44
 
46
 
def find_unmerged(local_branch, remote_branch):
47
 
    progress = ui.ui_factory.nested_progress_bar()
 
45
def find_unmerged(local_branch, remote_branch, restrict='all'):
 
46
    """Find revisions from each side that have not been merged.
 
47
 
 
48
    :param local_branch: Compare the history of local_branch
 
49
    :param remote_branch: versus the history of remote_branch, and determine
 
50
        mainline revisions which have not been merged.
 
51
    :param restrict: ('all', 'local', 'remote') If 'all', we will return the
 
52
        unique revisions from both sides. If 'local', we will return None
 
53
        for the remote revisions, similarly if 'remote' we will return None for
 
54
        the local revisions.
 
55
 
 
56
    :return: A list of [(revno, revision_id)] for the mainline revisions on
 
57
        each side.
 
58
    """
48
59
    local_branch.lock_read()
49
60
    try:
50
61
        remote_branch.lock_read()
51
62
        try:
52
 
            # check for special case: both branches are equivalent
53
 
            if (local_branch.last_revision_info() ==
54
 
                remote_branch.last_revision_info()):
55
 
                return [], []
56
 
            local_rev_history, local_rev_history_map = \
57
 
                _get_history(local_branch, progress, "local", 0)
58
 
            remote_rev_history, remote_rev_history_map = \
59
 
                _get_history(remote_branch, progress, "remote", 1)
60
 
            result = _shortcut(local_rev_history, remote_rev_history)
61
 
            if result is not None:
62
 
                local_extra, remote_extra = result
63
 
                local_extra = sorted_revisions(local_extra, 
64
 
                                               local_rev_history_map)
65
 
                remote_extra = sorted_revisions(remote_extra, 
66
 
                                                remote_rev_history_map)
67
 
                return local_extra, remote_extra
68
 
 
69
 
            local_ancestry = _get_ancestry(local_branch.repository, progress, 
70
 
                                           "local", 2, local_rev_history)
71
 
            remote_ancestry = _get_ancestry(remote_branch.repository, progress,
72
 
                                            "remote", 3, remote_rev_history)
73
 
            progress.update('pondering', 4, 5)
74
 
            extras = local_ancestry.symmetric_difference(remote_ancestry) 
75
 
            local_extra = extras.intersection(set(local_rev_history))
76
 
            remote_extra = extras.intersection(set(remote_rev_history))
77
 
            local_extra = sorted_revisions(local_extra, local_rev_history_map)
78
 
            remote_extra = sorted_revisions(remote_extra, 
79
 
                                            remote_rev_history_map)
80
 
                    
 
63
            return _find_unmerged(local_branch,
 
64
                remote_branch, restrict=restrict)
81
65
        finally:
82
66
            remote_branch.unlock()
83
67
    finally:
84
68
        local_branch.unlock()
85
 
        progress.finished()
86
 
    return (local_extra, remote_extra)
87
 
 
88
 
def _shortcut(local_rev_history, remote_rev_history):
89
 
    local_history = set(local_rev_history)
90
 
    remote_history = set(remote_rev_history)
91
 
    if len(local_rev_history) == 0:
92
 
        return set(), remote_history
93
 
    elif len(remote_rev_history) == 0:
94
 
        return local_history, set()
95
 
    elif local_rev_history[-1] in remote_history:
96
 
        return set(), _after(remote_rev_history, local_rev_history)
97
 
    elif remote_rev_history[-1] in local_history:
98
 
        return _after(local_rev_history, remote_rev_history), set()
99
 
    else:
 
69
 
 
70
 
 
71
def _enumerate_mainline(ancestry, graph, tip_revno, tip):
 
72
    """Enumerate the mainline revisions for these revisions.
 
73
 
 
74
    :param ancestry: A set of revisions that we care about
 
75
    :param graph: A Graph which lets us find the parents for a revision
 
76
    :param tip_revno: The revision number for the tip revision
 
77
    :param tip: The tip of mainline
 
78
    :return: [(revno, revision_id)] for all revisions in ancestry that
 
79
        are left-hand parents from tip, or None if ancestry is None.
 
80
    """
 
81
    if ancestry is None:
100
82
        return None
101
 
 
102
 
def _after(larger_history, smaller_history):
103
 
    return set(larger_history[larger_history.index(smaller_history[-1])+1:])
104
 
 
105
 
def _get_history(branch, progress, label, step):
106
 
    progress.update('%s history' % label, step, 5)
107
 
    rev_history = branch.revision_history()
108
 
    rev_history_map = dict(
109
 
        [(rev, rev_history.index(rev) + 1)
110
 
         for rev in rev_history])
111
 
    return rev_history, rev_history_map
112
 
 
113
 
def _get_ancestry(repository, progress, label, step, rev_history):
114
 
    progress.update('%s ancestry' % label, step, 5)
115
 
    if len(rev_history) > 0:
116
 
        ancestry = set(repository.get_ancestry(rev_history[-1],
117
 
                       topo_sorted=False))
 
83
    if not ancestry: #Empty ancestry, no need to do any work
 
84
        return []
 
85
 
 
86
    # Optionally, we could make 1 call to graph.get_parent_map with all
 
87
    # ancestors. However that will often check many more parents than we
 
88
    # actually need, and the Graph is likely to already have the parents cached
 
89
    # anyway.
 
90
    mainline = []
 
91
    cur = tip
 
92
    cur_revno = tip_revno
 
93
    while cur in ancestry:
 
94
        parent_map = graph.get_parent_map([cur])
 
95
        parents = parent_map.get(cur)
 
96
        if not parents:
 
97
            break # Ghost, we are done
 
98
        mainline.append((cur_revno, cur))
 
99
        cur = parents[0]
 
100
        cur_revno -= 1
 
101
    mainline.reverse()
 
102
    return mainline
 
103
 
 
104
 
 
105
def _find_unmerged(local_branch, remote_branch, restrict):
 
106
    """See find_unmerged.
 
107
 
 
108
    The branches should already be locked before entering.
 
109
    """
 
110
    local_revno, local_revision_id = local_branch.last_revision_info()
 
111
    remote_revno, remote_revision_id = remote_branch.last_revision_info()
 
112
    if local_revno == remote_revno and local_revision_id == remote_revision_id:
 
113
        # A simple shortcut when the tips are at the same point
 
114
        return [], []
 
115
    graph = local_branch.repository.get_graph(
 
116
                remote_branch.repository)
 
117
    if restrict == 'remote':
 
118
        local_extra = None
 
119
        remote_extra = graph.find_unique_ancestors(
 
120
            remote_revision_id, [local_revision_id])
 
121
    elif restrict == 'local':
 
122
        remote_extra = None
 
123
        local_extra = graph.find_unique_ancestors(
 
124
            local_revision_id, [remote_revision_id])
118
125
    else:
119
 
        ancestry = set()
120
 
    return ancestry
121
 
    
 
126
        if restrict != 'all':
 
127
            raise ValueError('param restrict not one of "all", "local",'
 
128
                             ' "remote": %r' % (restrict,))
 
129
        local_extra, remote_extra = graph.find_difference(
 
130
            local_revision_id, remote_revision_id)
 
131
    # Now that we have unique ancestors, compute just the mainline, and
 
132
    # generate revnos for them.
 
133
    local_mainline = _enumerate_mainline(local_extra, graph, local_revno,
 
134
                                         local_revision_id)
 
135
    remote_mainline = _enumerate_mainline(remote_extra, graph, remote_revno,
 
136
                                          remote_revision_id)
 
137
    return local_mainline, remote_mainline
 
138
 
122
139
 
123
140
def sorted_revisions(revisions, history_map):
124
141
    revisions = [(history_map[r],r) for r in revisions]