1
# Copyright (C) 2005, 2006 Canonical Ltd
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.
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.
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
17
"""Display what revisions are missing in 'other' from 'this' and vice versa."""
19
from __future__ import absolute_import
24
from . import revision as _mod_revision
27
def iter_log_revisions(revisions, revision_source, verbose, rev_tag_dict=None):
28
last_tree = revision_source.revision_tree(_mod_revision.NULL_REVISION)
31
if rev_tag_dict is None:
33
for revno, rev_id, merge_depth in revisions:
34
rev = revision_source.get_revision(rev_id)
36
delta = revision_source.get_revision_delta(rev_id)
39
yield log.LogRevision(rev, revno, merge_depth, delta=delta,
40
tags=rev_tag_dict.get(rev_id))
43
def find_unmerged(local_branch, remote_branch, restrict='all',
44
include_merged=None, backward=False,
45
local_revid_range=None, remote_revid_range=None):
46
"""Find revisions from each side that have not been merged.
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
55
:param include_merged: Show mainline revisions only if False,
56
all revisions otherwise.
57
:param backward: Show oldest versions first when True, newest versions
59
:param local_revid_range: Revision-id range for filtering local_branch
60
revisions (lower bound, upper bound)
61
:param remote_revid_range: Revision-id range for filtering remote_branch
62
revisions (lower bound, upper bound)
64
:return: A list of [(revno, revision_id)] for the mainline revisions on
67
if include_merged is None:
68
include_merged = False
69
local_branch.lock_read()
71
remote_branch.lock_read()
73
return _find_unmerged(
74
local_branch, remote_branch, restrict=restrict,
75
include_merged=include_merged, backward=backward,
76
local_revid_range=local_revid_range,
77
remote_revid_range=remote_revid_range)
79
remote_branch.unlock()
84
def _enumerate_mainline(ancestry, graph, tip_revno, tip, backward=True):
85
"""Enumerate the mainline revisions for these revisions.
87
:param ancestry: A set of revisions that we care about
88
:param graph: A Graph which lets us find the parents for a revision
89
:param tip_revno: The revision number for the tip revision
90
:param tip: The tip of mainline
91
:param backward: Show oldest versions first when True, newest versions
93
:return: [(revno, revision_id, 0)] for all revisions in ancestry that
94
are left-hand parents from tip, or None if ancestry is None.
98
if not ancestry: #Empty ancestry, no need to do any work
101
# Optionally, we could make 1 call to graph.get_parent_map with all
102
# ancestors. However that will often check many more parents than we
103
# actually need, and the Graph is likely to already have the parents cached
107
cur_revno = tip_revno
108
while cur in ancestry:
109
parent_map = graph.get_parent_map([cur])
110
parents = parent_map.get(cur)
112
break # Ghost, we are done
113
mainline.append((str(cur_revno), cur, 0))
121
def _enumerate_with_merges(branch, ancestry, graph, tip_revno, tip,
123
"""Enumerate the revisions for the ancestry.
125
:param branch: The branch we care about
126
:param ancestry: A set of revisions that we care about
127
:param graph: A Graph which lets us find the parents for a revision
128
:param tip_revno: The revision number for the tip revision
129
:param tip: The tip of the ancsetry
130
:param backward: Show oldest versions first when True, newest versions
132
:return: [(revno, revision_id)] for all revisions in ancestry that
133
are parents from tip, or None if ancestry is None.
137
if not ancestry: #Empty ancestry, no need to do any work
140
merge_sorted_revisions = branch.iter_merge_sorted_revisions()
141
# Now that we got the correct revnos, keep only the relevant
143
merge_sorted_revisions = [
144
# log.reverse_by_depth expects seq_num to be present, but it is
145
# stripped by iter_merge_sorted_revisions()
146
(0, revid, n, d, e) for revid, n, d, e in merge_sorted_revisions
147
if revid in ancestry]
149
merge_sorted_revisions = log.reverse_by_depth(merge_sorted_revisions)
151
for seq, rev_id, merge_depth, revno, end_of_merge in merge_sorted_revisions:
152
revline.append(('.'.join(map(str, revno)), rev_id, merge_depth))
156
def _filter_revs(graph, revs, revid_range):
157
if revid_range is None or revs is None:
159
return [rev for rev in revs
160
if graph.is_between(rev[1], revid_range[0], revid_range[1])]
163
def _find_unmerged(local_branch, remote_branch, restrict,
164
include_merged, backward,
165
local_revid_range=None, remote_revid_range=None):
166
"""See find_unmerged.
168
The branches should already be locked before entering.
170
local_revno, local_revision_id = local_branch.last_revision_info()
171
remote_revno, remote_revision_id = remote_branch.last_revision_info()
172
if local_revno == remote_revno and local_revision_id == remote_revision_id:
173
# A simple shortcut when the tips are at the same point
175
graph = local_branch.repository.get_graph(remote_branch.repository)
176
if restrict == 'remote':
178
remote_extra = graph.find_unique_ancestors(remote_revision_id,
180
elif restrict == 'local':
182
local_extra = graph.find_unique_ancestors(local_revision_id,
183
[remote_revision_id])
185
if restrict != 'all':
186
raise ValueError('param restrict not one of "all", "local",'
187
' "remote": %r' % (restrict,))
188
local_extra, remote_extra = graph.find_difference(local_revision_id,
191
locals = _enumerate_with_merges(local_branch, local_extra,
193
local_revision_id, backward)
194
remotes = _enumerate_with_merges(remote_branch, remote_extra,
196
remote_revision_id, backward)
198
# Now that we have unique ancestors, compute just the mainline, and
199
# generate revnos for them.
200
locals = _enumerate_mainline(local_extra, graph, local_revno,
201
local_revision_id, backward)
202
remotes = _enumerate_mainline(remote_extra, graph, remote_revno,
203
remote_revision_id, backward)
204
return _filter_revs(graph, locals, local_revid_range), _filter_revs(graph,
205
remotes, remote_revid_range)
208
def sorted_revisions(revisions, history_map):
209
revisions = sorted([(history_map[r], r) for r in revisions])