/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 bzrlib/missing.py

  • Committer: Robert Collins
  • Date: 2005-12-24 02:20:45 UTC
  • mto: (1185.50.57 bzr-jam-integration)
  • mto: This revision was merged to the branch mainline in revision 1550.
  • Revision ID: robertc@robertcollins.net-20051224022045-14efc8dfa0e1a4e9
Start tests for api usage.

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