/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: Canonical.com Patch Queue Manager
  • Date: 2008-09-06 10:25:39 UTC
  • mfrom: (3690.1.2 nicer-error)
  • Revision ID: pqm@pqm.ubuntu.com-20080906102539-ss1fkx2csdcalqlc
Do not traceback on unexpected error responses from a smart server.
        (Andrew Bennetts)

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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Display what revisions are missing in 'other' from 'this' and vice versa."""
 
18
 
 
19
from bzrlib.log import (
 
20
    LogRevision,
 
21
    )
 
22
import bzrlib.revision as _mod_revision
 
23
 
 
24
 
 
25
def iter_log_revisions(revisions, revision_source, verbose):
 
26
    last_tree = revision_source.revision_tree(_mod_revision.NULL_REVISION)
 
27
    last_rev_id = None
 
28
    for revno, rev_id in revisions:
 
29
        rev = revision_source.get_revision(rev_id)
 
30
        if verbose:
 
31
            remote_tree = revision_source.revision_tree(rev_id)
 
32
            parent_rev_id = rev.parent_ids[0]
 
33
            if last_rev_id == parent_rev_id:
 
34
                parent_tree = last_tree
 
35
            else:
 
36
                parent_tree = revision_source.revision_tree(parent_rev_id)
 
37
            revision_tree = revision_source.revision_tree(rev_id)
 
38
            last_rev_id = rev_id
 
39
            last_tree = revision_tree
 
40
            delta = revision_tree.changes_from(parent_tree)
 
41
        else:
 
42
            delta = None
 
43
        yield LogRevision(rev, revno, delta=delta)
 
44
 
 
45
 
 
46
def find_unmerged(local_branch, remote_branch, restrict='all'):
 
47
    """Find revisions from each side that have not been merged.
 
48
 
 
49
    :param local_branch: Compare the history of local_branch
 
50
    :param remote_branch: versus the history of remote_branch, and determine
 
51
        mainline revisions which have not been merged.
 
52
    :param restrict: ('all', 'local', 'remote') If 'all', we will return the
 
53
        unique revisions from both sides. If 'local', we will return None
 
54
        for the remote revisions, similarly if 'remote' we will return None for
 
55
        the local revisions.
 
56
 
 
57
    :return: A list of [(revno, revision_id)] for the mainline revisions on
 
58
        each side.
 
59
    """
 
60
    local_branch.lock_read()
 
61
    try:
 
62
        remote_branch.lock_read()
 
63
        try:
 
64
            return _find_unmerged(local_branch,
 
65
                remote_branch, restrict=restrict)
 
66
        finally:
 
67
            remote_branch.unlock()
 
68
    finally:
 
69
        local_branch.unlock()
 
70
 
 
71
 
 
72
def _enumerate_mainline(ancestry, graph, tip_revno, tip):
 
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
    :return: [(revno, revision_id)] for all revisions in ancestry that
 
80
        are left-hand parents from tip, or None if ancestry is None.
 
81
    """
 
82
    if ancestry is None:
 
83
        return None
 
84
    if not ancestry: #Empty ancestry, no need to do any work
 
85
        return []
 
86
 
 
87
    # Optionally, we could make 1 call to graph.get_parent_map with all
 
88
    # ancestors. However that will often check many more parents than we
 
89
    # actually need, and the Graph is likely to already have the parents cached
 
90
    # anyway.
 
91
    mainline = []
 
92
    cur = tip
 
93
    cur_revno = tip_revno
 
94
    while cur in ancestry:
 
95
        parent_map = graph.get_parent_map([cur])
 
96
        parents = parent_map.get(cur)
 
97
        if not parents:
 
98
            break # Ghost, we are done
 
99
        mainline.append((cur_revno, cur))
 
100
        cur = parents[0]
 
101
        cur_revno -= 1
 
102
    mainline.reverse()
 
103
    return mainline
 
104
 
 
105
 
 
106
def _find_unmerged(local_branch, remote_branch, restrict):
 
107
    """See find_unmerged.
 
108
 
 
109
    The branches should already be locked before entering.
 
110
    """
 
111
    local_revno, local_revision_id = local_branch.last_revision_info()
 
112
    remote_revno, remote_revision_id = remote_branch.last_revision_info()
 
113
    if local_revno == remote_revno and local_revision_id == remote_revision_id:
 
114
        # A simple shortcut when the tips are at the same point
 
115
        return [], []
 
116
    graph = local_branch.repository.get_graph(
 
117
                remote_branch.repository)
 
118
    if restrict == 'remote':
 
119
        local_extra = None
 
120
        remote_extra = graph.find_unique_ancestors(
 
121
            remote_revision_id, [local_revision_id])
 
122
    elif restrict == 'local':
 
123
        remote_extra = None
 
124
        local_extra = graph.find_unique_ancestors(
 
125
            local_revision_id, [remote_revision_id])
 
126
    else:
 
127
        if restrict != 'all':
 
128
            raise ValueError('param restrict not one of "all", "local",'
 
129
                             ' "remote": %r' % (restrict,))
 
130
        local_extra, remote_extra = graph.find_difference(
 
131
            local_revision_id, remote_revision_id)
 
132
    # Now that we have unique ancestors, compute just the mainline, and
 
133
    # generate revnos for them.
 
134
    local_mainline = _enumerate_mainline(local_extra, graph, local_revno,
 
135
                                         local_revision_id)
 
136
    remote_mainline = _enumerate_mainline(remote_extra, graph, remote_revno,
 
137
                                          remote_revision_id)
 
138
    return local_mainline, remote_mainline
 
139
 
 
140
 
 
141
def sorted_revisions(revisions, history_map):
 
142
    revisions = [(history_map[r],r) for r in revisions]
 
143
    revisions.sort()
 
144
    return revisions