~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/missing.py

  • Committer: Martin Pool
  • Date: 2008-07-03 10:44:34 UTC
  • mfrom: (3512.3.2 setlocale.mini)
  • mto: This revision was merged to the branch mainline in revision 3518.
  • Revision ID: mbp@sourcefrog.net-20080703104434-v4qgzvxd2wxg8etl
Set locale from environment for third party libs and day of week.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""Display what revisions are missing in 'other' from 'this' and vice verca."""
2
 
 
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
 
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
 
 
23
 
 
24
def iter_log_revisions(revisions, revision_source, verbose):
 
25
    last_tree = revision_source.revision_tree(None)
10
26
    last_rev_id = None
11
27
    for revno, rev_id in revisions:
12
28
        rev = revision_source.get_revision(rev_id)
20
36
            revision_tree = revision_source.revision_tree(rev_id)
21
37
            last_rev_id = rev_id
22
38
            last_tree = revision_tree
23
 
            delta = compare_trees(revision_tree, parent_tree)
 
39
            delta = revision_tree.changes_from(parent_tree)
24
40
        else:
25
41
            delta = None
26
 
        yield revno, rev, delta
27
 
 
28
 
 
29
 
def find_unmerged(local_branch, remote_branch):
30
 
    progress = ui.ui_factory.nested_progress_bar()
 
42
        yield LogRevision(rev, revno, delta=delta)
 
43
 
 
44
 
 
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
    """
31
59
    local_branch.lock_read()
32
60
    try:
33
61
        remote_branch.lock_read()
34
62
        try:
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
 
                    
 
63
            return _find_unmerged(local_branch,
 
64
                remote_branch, restrict=restrict)
60
65
        finally:
61
66
            remote_branch.unlock()
62
67
    finally:
63
68
        local_branch.unlock()
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:
 
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:
79
82
        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]))
 
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])
96
125
    else:
97
 
        ancestry = set()
98
 
    return ancestry
99
 
    
 
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
 
100
139
 
101
140
def sorted_revisions(revisions, history_map):
102
141
    revisions = [(history_map[r],r) for r in revisions]