~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/fetch.py

  • Committer: aaron.bentley at utoronto
  • Date: 2005-08-20 05:10:18 UTC
  • mto: (1092.1.41) (1185.3.4)
  • mto: This revision was merged to the branch mainline in revision 1110.
  • Revision ID: aaron.bentley@utoronto.ca-20050820051018-13c5ecdd70a6fcdc
Got greedy fetch working when the desired revision's not in the revision history

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
import sys
22
22
import os
23
23
 
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
 
27
    revision history.
 
28
    """
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]
 
31
        try:
 
32
            rev_index = from_history.index(last_revision)
 
33
        except ValueError:
 
34
            rev_index = None
 
35
        if rev_index is not None:
 
36
            from_history = from_history[:rev_index + 1]
 
37
        else:
 
38
            from_history = [last_revision]
28
39
    to_history = to_branch.revision_history()
29
40
    missing = []
30
41
    for rev_id in from_history:
31
42
        if not has_revision(to_branch, rev_id):
32
43
            missing.append(rev_id)
33
 
 
 
44
    
 
45
    count = 0
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)
36
48
        new_missing = []
37
49
        for rev_id in missing:
38
50
            try:
43
55
                else:
44
56
                    continue
45
57
            for parent in [p.revision_id for p in revision.parents]:
 
58
                print >> sys.stderr, "adding %s" % parent
46
59
                if not has_revision(to_branch, parent):
47
60
                    new_missing.append(parent)
48
61
        missing = new_missing
 
62
    return count
49
63
 
50
64
 
51
65
from testsweet import InTempDir
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
71
85
 
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])
75
89
 
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])
84
98
 
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])
90
104
 
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])
95
109
 
 
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
 
117
 
96
118
 
97
119
 
98
120
if __name__ == '__main__':