~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/missing.py

  • Committer: Robert Collins
  • Date: 2006-07-20 13:00:31 UTC
  • mto: (1852.9.1 Tree.compare().)
  • mto: This revision was merged to the branch mainline in revision 1890.
  • Revision ID: robertc@robertcollins.net-20060720130031-d26103a427ea10f3
StartĀ treeĀ implementationĀ tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
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
1
"""Display what revisions are missing in 'other' from 'this' and vice versa."""
18
2
 
19
 
from bzrlib import (
20
 
    log,
21
 
    repository as _mod_repository,
22
 
    tsort,
23
 
    )
24
 
import bzrlib.revision as _mod_revision
25
 
 
26
 
 
27
 
def iter_log_revisions(revisions, revision_source, verbose):
28
 
    last_tree = revision_source.revision_tree(_mod_revision.NULL_REVISION)
 
3
import bzrlib.ui as ui
 
4
 
 
5
 
 
6
def iter_log_data(revisions, revision_source, verbose):
 
7
    from bzrlib.diff import compare_trees
 
8
    from bzrlib.tree import EmptyTree
 
9
    last_tree = EmptyTree
29
10
    last_rev_id = None
30
 
    for rev in revisions:
31
 
        # We need the following for backward compatibilty (hopefully
32
 
        # this will be deprecated soon :-/) -- vila 080911
33
 
        if len(rev) == 2:
34
 
            revno, rev_id = rev
35
 
            merge_depth = 0
36
 
        else:
37
 
            revno, rev_id, merge_depth = rev
 
11
    for revno, rev_id in revisions:
38
12
        rev = revision_source.get_revision(rev_id)
39
13
        if verbose:
40
 
            delta = revision_source.get_revision_delta(rev_id)
 
14
            remote_tree = revision_source.revision_tree(rev_id)
 
15
            parent_rev_id = rev.parent_ids[0]
 
16
            if last_rev_id == parent_rev_id:
 
17
                parent_tree = last_tree
 
18
            else:
 
19
                parent_tree = revision_source.revision_tree(parent_rev_id)
 
20
            revision_tree = revision_source.revision_tree(rev_id)
 
21
            last_rev_id = rev_id
 
22
            last_tree = revision_tree
 
23
            delta = compare_trees(revision_tree, parent_tree)
41
24
        else:
42
25
            delta = None
43
 
        yield log.LogRevision(rev, revno, merge_depth, delta=delta)
44
 
 
45
 
 
46
 
def find_unmerged(local_branch, remote_branch, restrict='all',
47
 
                  include_merges=False, backward=False,
48
 
                  local_revid_range=None, remote_revid_range=None):
49
 
    """Find revisions from each side that have not been merged.
50
 
 
51
 
    :param local_branch: Compare the history of local_branch
52
 
    :param remote_branch: versus the history of remote_branch, and determine
53
 
        mainline revisions which have not been merged.
54
 
    :param restrict: ('all', 'local', 'remote') If 'all', we will return the
55
 
        unique revisions from both sides. If 'local', we will return None
56
 
        for the remote revisions, similarly if 'remote' we will return None for
57
 
        the local revisions.
58
 
    :param include_merges: Show mainline revisions only if False,
59
 
        all revisions otherwise.
60
 
    :param backward: Show oldest versions first when True, newest versions
61
 
        first when False.
62
 
    :param local_revid_range: Revision-id range for filtering local_branch
63
 
        revisions (lower bound, upper bound)
64
 
    :param remote_revid_range: Revision-id range for filtering remote_branch
65
 
        revisions (lower bound, upper bound)
66
 
 
67
 
    :return: A list of [(revno, revision_id)] for the mainline revisions on
68
 
        each side.
69
 
    """
 
26
        yield revno, rev, delta
 
27
 
 
28
 
 
29
def find_unmerged(local_branch, remote_branch):
 
30
    progress = ui.ui_factory.nested_progress_bar()
70
31
    local_branch.lock_read()
71
32
    try:
72
33
        remote_branch.lock_read()
73
34
        try:
74
 
            return _find_unmerged(
75
 
                local_branch, remote_branch, restrict=restrict,
76
 
                include_merges=include_merges, backward=backward,
77
 
                local_revid_range=local_revid_range,
78
 
                remote_revid_range=remote_revid_range)
 
35
            local_rev_history, local_rev_history_map = \
 
36
                _get_history(local_branch, progress, "local", 0)
 
37
            remote_rev_history, remote_rev_history_map = \
 
38
                _get_history(remote_branch, progress, "remote", 1)
 
39
            result = _shortcut(local_rev_history, remote_rev_history)
 
40
            if result is not None:
 
41
                local_extra, remote_extra = result
 
42
                local_extra = sorted_revisions(local_extra, 
 
43
                                               local_rev_history_map)
 
44
                remote_extra = sorted_revisions(remote_extra, 
 
45
                                                remote_rev_history_map)
 
46
                return local_extra, remote_extra
 
47
 
 
48
            local_ancestry = _get_ancestry(local_branch.repository, progress, 
 
49
                                           "local", 2, local_rev_history)
 
50
            remote_ancestry = _get_ancestry(remote_branch.repository, progress,
 
51
                                            "remote", 3, remote_rev_history)
 
52
            progress.update('pondering', 4, 5)
 
53
            extras = local_ancestry.symmetric_difference(remote_ancestry) 
 
54
            local_extra = extras.intersection(set(local_rev_history))
 
55
            remote_extra = extras.intersection(set(remote_rev_history))
 
56
            local_extra = sorted_revisions(local_extra, local_rev_history_map)
 
57
            remote_extra = sorted_revisions(remote_extra, 
 
58
                                            remote_rev_history_map)
 
59
                    
79
60
        finally:
80
61
            remote_branch.unlock()
81
62
    finally:
82
63
        local_branch.unlock()
83
 
 
84
 
 
85
 
def _enumerate_mainline(ancestry, graph, tip_revno, tip, backward=True):
86
 
    """Enumerate the mainline revisions for these revisions.
87
 
 
88
 
    :param ancestry: A set of revisions that we care about
89
 
    :param graph: A Graph which lets us find the parents for a revision
90
 
    :param tip_revno: The revision number for the tip revision
91
 
    :param tip: The tip of mainline
92
 
    :param backward: Show oldest versions first when True, newest versions
93
 
        first when False.
94
 
    :return: [(revno, revision_id)] for all revisions in ancestry that
95
 
        are left-hand parents from tip, or None if ancestry is None.
96
 
    """
97
 
    if ancestry is None:
98
 
        return None
99
 
    if not ancestry: #Empty ancestry, no need to do any work
100
 
        return []
101
 
 
102
 
    # Optionally, we could make 1 call to graph.get_parent_map with all
103
 
    # ancestors. However that will often check many more parents than we
104
 
    # actually need, and the Graph is likely to already have the parents cached
105
 
    # anyway.
106
 
    mainline = []
107
 
    cur = tip
108
 
    cur_revno = tip_revno
109
 
    while cur in ancestry:
110
 
        parent_map = graph.get_parent_map([cur])
111
 
        parents = parent_map.get(cur)
112
 
        if not parents:
113
 
            break # Ghost, we are done
114
 
        mainline.append((str(cur_revno), cur))
115
 
        cur = parents[0]
116
 
        cur_revno -= 1
117
 
    if not backward:
118
 
        mainline.reverse()
119
 
    return mainline
120
 
 
121
 
 
122
 
def _enumerate_with_merges(branch, ancestry, graph, tip_revno, tip,
123
 
                           backward=True):
124
 
    """Enumerate the revisions for the ancestry.
125
 
 
126
 
    :param branch: The branch we care about
127
 
    :param ancestry: A set of revisions that we care about
128
 
    :param graph: A Graph which lets us find the parents for a revision
129
 
    :param tip_revno: The revision number for the tip revision
130
 
    :param tip: The tip of the ancsetry
131
 
    :param backward: Show oldest versions first when True, newest versions
132
 
        first when False.
133
 
    :return: [(revno, revision_id)] for all revisions in ancestry that
134
 
        are parents from tip, or None if ancestry is None.
135
 
    """
136
 
    if ancestry is None:
137
 
        return None
138
 
    if not ancestry: #Empty ancestry, no need to do any work
139
 
        return []
140
 
 
141
 
    merge_sorted_revisions = branch.iter_merge_sorted_revisions()
142
 
    # Now that we got the correct revnos, keep only the relevant
143
 
    # revisions.
144
 
    merge_sorted_revisions = [
145
 
        # log.reverse_by_depth expects seq_num to be present, but it is
146
 
        # stripped by iter_merge_sorted_revisions()
147
 
        (0, revid, n, d, e) for revid, n, d, e in merge_sorted_revisions
148
 
        if revid in ancestry]
149
 
    if not backward:
150
 
        merge_sorted_revisions = log.reverse_by_depth(merge_sorted_revisions)
151
 
    revline = []
152
 
    for seq, rev_id, merge_depth, revno, end_of_merge in merge_sorted_revisions:
153
 
        revline.append(('.'.join(map(str, revno)), rev_id, merge_depth))
154
 
    return revline
155
 
 
156
 
 
157
 
def _filter_revs(graph, revs, revid_range):
158
 
    if revid_range is None or revs is None:
159
 
        return revs
160
 
    return [rev for rev in revs
161
 
        if graph.is_between(rev[1], revid_range[0], revid_range[1])]
162
 
 
163
 
 
164
 
def _find_unmerged(local_branch, remote_branch, restrict,
165
 
                   include_merges, backward,
166
 
                   local_revid_range=None, remote_revid_range=None):
167
 
    """See find_unmerged.
168
 
 
169
 
    The branches should already be locked before entering.
170
 
    """
171
 
    local_revno, local_revision_id = local_branch.last_revision_info()
172
 
    remote_revno, remote_revision_id = remote_branch.last_revision_info()
173
 
    if local_revno == remote_revno and local_revision_id == remote_revision_id:
174
 
        # A simple shortcut when the tips are at the same point
175
 
        return [], []
176
 
    graph = local_branch.repository.get_graph(remote_branch.repository)
177
 
    if restrict == 'remote':
178
 
        local_extra = None
179
 
        remote_extra = graph.find_unique_ancestors(remote_revision_id,
180
 
                                                   [local_revision_id])
181
 
    elif restrict == 'local':
182
 
        remote_extra = None
183
 
        local_extra = graph.find_unique_ancestors(local_revision_id,
184
 
                                                  [remote_revision_id])
185
 
    else:
186
 
        if restrict != 'all':
187
 
            raise ValueError('param restrict not one of "all", "local",'
188
 
                             ' "remote": %r' % (restrict,))
189
 
        local_extra, remote_extra = graph.find_difference(local_revision_id,
190
 
                                                          remote_revision_id)
191
 
    if include_merges:
192
 
        locals = _enumerate_with_merges(local_branch, local_extra,
193
 
                                        graph, local_revno,
194
 
                                        local_revision_id, backward)
195
 
        remotes = _enumerate_with_merges(remote_branch, remote_extra,
196
 
                                         graph, remote_revno,
197
 
                                         remote_revision_id, backward)
198
 
    else:
199
 
        # Now that we have unique ancestors, compute just the mainline, and
200
 
        # generate revnos for them.
201
 
        locals = _enumerate_mainline(local_extra, graph, local_revno,
202
 
                                     local_revision_id, backward)
203
 
        remotes = _enumerate_mainline(remote_extra, graph, remote_revno,
204
 
                                      remote_revision_id, backward)
205
 
    return _filter_revs(graph, locals, local_revid_range), _filter_revs(graph,
206
 
        remotes, remote_revid_range)
207
 
 
 
64
        progress.finished()
 
65
    return (local_extra, remote_extra)
 
66
 
 
67
def _shortcut(local_rev_history, remote_rev_history):
 
68
    local_history = set(local_rev_history)
 
69
    remote_history = set(remote_rev_history)
 
70
    if len(local_rev_history) == 0:
 
71
        return set(), remote_history
 
72
    elif len(remote_rev_history) == 0:
 
73
        return local_history, set()
 
74
    elif local_rev_history[-1] in remote_history:
 
75
        return set(), _after(remote_rev_history, local_rev_history)
 
76
    elif remote_rev_history[-1] in local_history:
 
77
        return _after(local_rev_history, remote_rev_history), set()
 
78
    else:
 
79
        return None
 
80
 
 
81
def _after(larger_history, smaller_history):
 
82
    return set(larger_history[larger_history.index(smaller_history[-1])+1:])
 
83
 
 
84
def _get_history(branch, progress, label, step):
 
85
    progress.update('%s history' % label, step, 5)
 
86
    rev_history = branch.revision_history()
 
87
    rev_history_map = dict(
 
88
        [(rev, rev_history.index(rev) + 1)
 
89
         for rev in rev_history])
 
90
    return rev_history, rev_history_map
 
91
 
 
92
def _get_ancestry(repository, progress, label, step, rev_history):
 
93
    progress.update('%s ancestry' % label, step, 5)
 
94
    if len(rev_history) > 0:
 
95
        ancestry = set(repository.get_ancestry(rev_history[-1]))
 
96
    else:
 
97
        ancestry = set()
 
98
    return ancestry
 
99
    
208
100
 
209
101
def sorted_revisions(revisions, history_map):
210
102
    revisions = [(history_map[r],r) for r in revisions]