~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/fetch.py

merge merge tweaks from aaron, which includes latest .dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
import bzrlib.errors
17
 
from bzrlib.selftest.testrevision import make_branches
18
 
from bzrlib.trace import mutter
 
17
from bzrlib.trace import mutter, note
19
18
from bzrlib.branch import Branch
20
19
from bzrlib.progress import ProgressBar
21
20
import sys
22
21
import os
23
22
 
24
 
def greedy_fetch(from_branch, to_branch, last_revision=None):
 
23
def has_revision(branch, revision_id):
 
24
    try:
 
25
        branch.get_revision_xml(revision_id)
 
26
        return True
 
27
    except bzrlib.errors.NoSuchRevision:
 
28
        return False
 
29
 
 
30
 
 
31
def greedy_fetch(to_branch, from_branch, revision=None, pb=None):
 
32
    """Copy a revision and all available ancestors from one branch to another
 
33
    If no revision is specified, uses the last revision in the source branch's
 
34
    revision history.
 
35
    """
25
36
    from_history = from_branch.revision_history()
26
 
    if last_revision is not None:
27
 
        from_history = from_history[:from_history.index(last_revision)+1]
 
37
    required_revisions = set(from_history)
 
38
    all_failed = set()
 
39
    if revision is not None:
 
40
        required_revisions.add(revision)
 
41
        try:
 
42
            rev_index = from_history.index(revision)
 
43
        except ValueError:
 
44
            rev_index = None
 
45
        if rev_index is not None:
 
46
            from_history = from_history[:rev_index + 1]
 
47
        else:
 
48
            from_history = [revision]
28
49
    to_history = to_branch.revision_history()
29
50
    missing = []
30
51
    for rev_id in from_history:
31
52
        if not has_revision(to_branch, rev_id):
32
53
            missing.append(rev_id)
33
 
 
 
54
    
 
55
    count = 0
34
56
    while len(missing) > 0:
35
 
        to_branch.install_revisions(from_branch, revision_ids=missing)
36
 
        new_missing = []
 
57
        installed, failed = to_branch.install_revisions(from_branch, 
 
58
                                                        revision_ids=missing,
 
59
                                                        pb=pb)
 
60
        count += installed
 
61
        required_failed = failed.intersection(required_revisions)
 
62
        if len(required_failed) > 0:
 
63
            raise bzrlib.errors.InstallFailed(required_failed)
 
64
        for rev_id in failed:
 
65
            note("Failed to install %s" % rev_id)
 
66
        all_failed.update(failed)
 
67
        new_missing = set() 
37
68
        for rev_id in missing:
38
69
            try:
39
70
                revision = from_branch.get_revision(rev_id)
44
75
                    continue
45
76
            for parent in [p.revision_id for p in revision.parents]:
46
77
                if not has_revision(to_branch, parent):
47
 
                    new_missing.append(parent)
 
78
                    new_missing.add(parent)
48
79
        missing = new_missing
49
 
 
50
 
 
51
 
from testsweet import InTempDir
52
 
def has_revision(branch, revision_id):
53
 
    try:
54
 
        branch.get_revision_xml(revision_id)
55
 
        return True
56
 
    except bzrlib.errors.NoSuchRevision:
57
 
        return False
58
 
 
59
 
class TestFetch(InTempDir):
60
 
    def runTest(self):
61
 
        def new_branch(name):
62
 
            os.mkdir(name)
63
 
            return Branch(name, init=True)
64
 
            
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])
71
 
 
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])
75
 
 
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])
84
 
 
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])
90
 
 
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])
95
 
 
96
 
 
97
 
 
98
 
if __name__ == '__main__':
99
 
    import sys
100
 
    sys.exit(run_suite(unittest.makeSuite()))
 
80
    return count, all_failed
 
81
 
 
82