~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/fetch.py

  • Committer: Martin Pool
  • Date: 2005-08-25 05:58:05 UTC
  • mfrom: (974.1.36)
  • Revision ID: mbp@sourcefrog.net-20050825055805-8c892bc3c2d75131
- merge aaron's merge improvements:

  * When merging, pull in all missing revisions from the source
    branch. 

  * Detect common ancestors by looking at the whole ancestry graph, 
    rather than just mainline history.

  Some changes to reconcile this with parallel updates to the test and
  trace code.

aaron.bentley@utoronto.ca-20050823052551-f3401a8b57d9126f

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