24
def greedy_fetch(from_branch, to_branch, last_revision=None):
24
def greedy_fetch(to_branch, from_branch, last_revision=None):
25
"""Copy a revision and all available ancestors from one branch to another
26
If no revision is specified, uses the last revision in the source branch's
25
29
from_history = from_branch.revision_history()
26
30
if last_revision is not None:
27
from_history = from_history[:from_history.index(last_revision)+1]
32
rev_index = from_history.index(last_revision)
35
if rev_index is not None:
36
from_history = from_history[:rev_index + 1]
38
from_history = [last_revision]
28
39
to_history = to_branch.revision_history()
30
41
for rev_id in from_history:
31
42
if not has_revision(to_branch, rev_id):
32
43
missing.append(rev_id)
34
46
while len(missing) > 0:
35
to_branch.install_revisions(from_branch, revision_ids=missing)
47
count += to_branch.install_revisions(from_branch, revision_ids=missing)
37
49
for rev_id in missing:
67
81
assert not has_revision(br_b, br_a.revision_history()[3])
68
82
assert has_revision(br_b, br_a.revision_history()[2])
69
83
assert len(br_b.revision_history()) == 5
70
greedy_fetch(br_a, br_b, br_a.revision_history()[2])
84
assert greedy_fetch(br_b, br_a, br_a.revision_history()[2]) == 0
72
86
# greedy_fetch is not supposed to alter the revision history
73
87
assert len(br_b.revision_history()) == 5
74
88
assert not has_revision(br_b, br_a.revision_history()[3])
76
90
assert len(br_b.revision_history()) == 5
77
greedy_fetch(br_a, br_b, br_a.revision_history()[3])
91
assert greedy_fetch(br_b, br_a, br_a.revision_history()[3]) == 1
78
92
assert has_revision(br_b, br_a.revision_history()[3])
79
93
assert not has_revision(br_a, br_b.revision_history()[3])
80
94
assert not has_revision(br_a, br_b.revision_history()[4])
81
greedy_fetch(br_b, br_a)
95
assert greedy_fetch(br_a, br_b) == 2
82
96
assert has_revision(br_a, br_b.revision_history()[3])
83
97
assert has_revision(br_a, br_b.revision_history()[4])
85
99
br_b2 = new_branch('br_b2')
86
greedy_fetch(br_b, br_b2)
100
assert greedy_fetch(br_b2, br_b) == 5
87
101
assert has_revision(br_b2, br_b.revision_history()[4])
88
102
assert has_revision(br_b2, br_a.revision_history()[2])
89
103
assert not has_revision(br_b2, br_a.revision_history()[3])
91
105
br_a2 = new_branch('br_a2')
92
greedy_fetch(br_a, br_a2)
106
assert greedy_fetch(br_a2, br_a) == 6
93
107
assert has_revision(br_a2, br_b.revision_history()[4])
94
108
assert has_revision(br_a2, br_a.revision_history()[3])
110
br_a3 = new_branch('br_a3')
111
assert greedy_fetch(br_a3, br_a2) == 0
112
for revno in range(4):
113
assert not has_revision(br_a3, br_a.revision_history()[revno])
114
assert greedy_fetch(br_a3, br_a2, br_a.revision_history()[2]) == 3
115
fetched = greedy_fetch(br_a3, br_a2, br_a.revision_history()[3])
116
assert fetched == 3, "fetched %d instead of 3" % fetched
98
120
if __name__ == '__main__':