1
# Copyright (C) 2005 by 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
from bzrlib.selftest.testrevision import make_branches
18
from bzrlib.trace import mutter
19
from bzrlib.branch import Branch
20
from bzrlib.progress import ProgressBar
24
def greedy_fetch(from_branch, to_branch, last_revision=None):
25
from_history = from_branch.revision_history()
26
if last_revision is not None:
27
from_history = from_history[:from_history.index(last_revision)+1]
28
to_history = to_branch.revision_history()
30
for rev_id in from_history:
31
if not has_revision(to_branch, rev_id):
32
missing.append(rev_id)
34
while len(missing) > 0:
35
to_branch.install_revisions(from_branch, revision_ids=missing)
37
for rev_id in missing:
39
revision = from_branch.get_revision(rev_id)
40
except bzrlib.errors.NoSuchRevision:
41
if revision in from_history:
45
for parent in [p.revision_id for p in revision.parents]:
46
if not has_revision(to_branch, parent):
47
new_missing.append(parent)
51
from testsweet import InTempDir
52
def has_revision(branch, revision_id):
54
branch.get_revision_xml(revision_id)
56
except bzrlib.errors.NoSuchRevision:
59
class TestFetch(InTempDir):
63
return Branch(name, init=True)
65
#highest indices a: 3, b: 4
66
br_a, br_b = make_branches()
67
assert not has_revision(br_b, br_a.revision_history()[3])
68
assert has_revision(br_b, br_a.revision_history()[2])
69
assert len(br_b.revision_history()) == 5
70
greedy_fetch(br_a, br_b, br_a.revision_history()[2])
72
# greedy_fetch is not supposed to alter the revision history
73
assert len(br_b.revision_history()) == 5
74
assert not has_revision(br_b, br_a.revision_history()[3])
76
assert len(br_b.revision_history()) == 5
77
greedy_fetch(br_a, br_b, br_a.revision_history()[3])
78
assert has_revision(br_b, br_a.revision_history()[3])
79
assert not has_revision(br_a, br_b.revision_history()[3])
80
assert not has_revision(br_a, br_b.revision_history()[4])
81
greedy_fetch(br_b, br_a)
82
assert has_revision(br_a, br_b.revision_history()[3])
83
assert has_revision(br_a, br_b.revision_history()[4])
85
br_b2 = new_branch('br_b2')
86
greedy_fetch(br_b, br_b2)
87
assert has_revision(br_b2, br_b.revision_history()[4])
88
assert has_revision(br_b2, br_a.revision_history()[2])
89
assert not has_revision(br_b2, br_a.revision_history()[3])
91
br_a2 = new_branch('br_a2')
92
greedy_fetch(br_a, br_a2)
93
assert has_revision(br_a2, br_b.revision_history()[4])
94
assert has_revision(br_a2, br_a.revision_history()[3])
98
if __name__ == '__main__':
100
sys.exit(run_suite(unittest.makeSuite()))