~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-21 00:08:08 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-20050821000808-2a0e6ef95b1bca59
Changed copy_multi to permit failure and return a tuple, tested missing required revisions

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
import bzrlib.errors
17
17
from bzrlib.selftest.testrevision import make_branches
18
 
from bzrlib.trace import mutter
 
18
from bzrlib.trace import mutter, note
19
19
from bzrlib.branch import Branch
20
20
from bzrlib.progress import ProgressBar
21
21
import sys
22
22
import os
23
23
 
24
 
def greedy_fetch(to_branch, from_branch, last_revision=None):
 
24
def greedy_fetch(to_branch, from_branch, revision=None):
25
25
    """Copy a revision and all available ancestors from one branch to another
26
26
    If no revision is specified, uses the last revision in the source branch's
27
27
    revision history.
28
28
    """
29
29
    from_history = from_branch.revision_history()
30
 
    if last_revision is not None:
 
30
    required_revisions = set(from_history)
 
31
    all_failed = set()
 
32
    if revision is not None:
 
33
        required_revisions.add(revision)
31
34
        try:
32
 
            rev_index = from_history.index(last_revision)
 
35
            rev_index = from_history.index(revision)
33
36
        except ValueError:
34
37
            rev_index = None
35
38
        if rev_index is not None:
36
39
            from_history = from_history[:rev_index + 1]
37
40
        else:
38
 
            from_history = [last_revision]
 
41
            from_history = [revision]
39
42
    to_history = to_branch.revision_history()
40
43
    missing = []
41
44
    for rev_id in from_history:
44
47
    
45
48
    count = 0
46
49
    while len(missing) > 0:
47
 
        count += to_branch.install_revisions(from_branch, revision_ids=missing)
 
50
        installed, failed = to_branch.install_revisions(from_branch, 
 
51
                                                        revision_ids=missing)
 
52
        count += installed
 
53
        required_failed = failed.intersection(required_revisions)
 
54
        if len(required_failed) > 0:
 
55
            raise bzrlib.errors.InstallFailed(required_failed)
 
56
        for rev_id in failed:
 
57
            note("Failed to install %s" % rev_id)
 
58
        all_failed.update(failed)
48
59
        new_missing = []
49
60
        for rev_id in missing:
50
61
            try:
59
70
                if not has_revision(to_branch, parent):
60
71
                    new_missing.append(parent)
61
72
        missing = new_missing
62
 
    return count
 
73
    return count, all_failed
63
74
 
64
75
 
65
76
from testsweet import InTempDir
 
77
from bzrlib.commit import commit
66
78
def has_revision(branch, revision_id):
67
79
    try:
68
80
        branch.get_revision_xml(revision_id)
81
93
        assert not has_revision(br_b, br_a.revision_history()[3])
82
94
        assert has_revision(br_b, br_a.revision_history()[2])
83
95
        assert len(br_b.revision_history()) == 5
84
 
        assert greedy_fetch(br_b, br_a, br_a.revision_history()[2]) == 0
 
96
        assert greedy_fetch(br_b, br_a, br_a.revision_history()[2])[0] == 0
85
97
 
86
98
        # greedy_fetch is not supposed to alter the revision history
87
99
        assert len(br_b.revision_history()) == 5
88
100
        assert not has_revision(br_b, br_a.revision_history()[3])
89
101
 
90
102
        assert len(br_b.revision_history()) == 5
91
 
        assert greedy_fetch(br_b, br_a, br_a.revision_history()[3]) == 1
 
103
        assert greedy_fetch(br_b, br_a, br_a.revision_history()[3])[0] == 1
92
104
        assert has_revision(br_b, br_a.revision_history()[3])
93
105
        assert not has_revision(br_a, br_b.revision_history()[3])
94
106
        assert not has_revision(br_a, br_b.revision_history()[4])
95
 
        assert greedy_fetch(br_a, br_b) == 2
 
107
 
 
108
        # When a non-branch ancestor is missing, it should be a failure, not
 
109
        # exception
 
110
        br_a4 = new_branch('br_a4')
 
111
        count, failures = greedy_fetch(br_a4, br_a)
 
112
        assert count == 4
 
113
        assert failures == set((br_b.revision_history()[4],)) 
 
114
 
 
115
        assert greedy_fetch(br_a, br_b)[0] == 2
96
116
        assert has_revision(br_a, br_b.revision_history()[3])
97
117
        assert has_revision(br_a, br_b.revision_history()[4])
98
118
 
99
119
        br_b2 = new_branch('br_b2')
100
 
        assert greedy_fetch(br_b2, br_b) == 5
 
120
        assert greedy_fetch(br_b2, br_b)[0] == 5
101
121
        assert has_revision(br_b2, br_b.revision_history()[4])
102
122
        assert has_revision(br_b2, br_a.revision_history()[2])
103
123
        assert not has_revision(br_b2, br_a.revision_history()[3])
104
124
 
105
125
        br_a2 = new_branch('br_a2')
106
 
        assert greedy_fetch(br_a2, br_a) == 6
 
126
        assert greedy_fetch(br_a2, br_a)[0] == 6
107
127
        assert has_revision(br_a2, br_b.revision_history()[4])
108
128
        assert has_revision(br_a2, br_a.revision_history()[3])
109
129
 
110
130
        br_a3 = new_branch('br_a3')
111
 
        assert greedy_fetch(br_a3, br_a2) == 0
 
131
        assert greedy_fetch(br_a3, br_a2)[0] == 0
112
132
        for revno in range(4):
113
133
            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])
 
134
        assert greedy_fetch(br_a3, br_a2, br_a.revision_history()[2])[0] == 3
 
135
        fetched = greedy_fetch(br_a3, br_a2, br_a.revision_history()[3])[0]
116
136
        assert fetched == 3, "fetched %d instead of 3" % fetched
 
137
        # InstallFailed should be raised if the branch is missing the revision
 
138
        # that was requested.
 
139
        self.assertRaises(bzrlib.errors.InstallFailed, greedy_fetch, br_a3,
 
140
                          br_a2, 'pizza')
 
141
        # InstallFailed should be raised if the branch is missing a revision
 
142
        # from its own revision history
 
143
        br_a2.append_revision('a-b-c')
 
144
        self.assertRaises(bzrlib.errors.InstallFailed, greedy_fetch, br_a3,
 
145
                          br_a2)
117
146
 
118
147
 
119
148