~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/fetch.py

  • Committer: Robert Collins
  • Date: 2005-08-25 06:08:36 UTC
  • mto: (974.1.50) (1185.1.10) (1092.3.1)
  • mto: This revision was merged to the branch mainline in revision 1139.
  • Revision ID: robertc@robertcollins.net-20050825060836-40b430abb9d341d9
unbreak cmd_branch now that something tests the core of it..

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.trace import mutter, note
 
17
from bzrlib.selftest.testrevision import make_branches
 
18
from bzrlib.trace import mutter
18
19
from bzrlib.branch import Branch
19
 
from bzrlib.progress import ProgressBar
20
20
import sys
21
21
import os
22
22
 
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
 
    """
 
23
def greedy_fetch(from_branch, to_branch, last_revision=None):
36
24
    from_history = from_branch.revision_history()
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]
 
25
    if last_revision is not None:
 
26
        from_history = from_history[:from_history.index(last_revision)+1]
49
27
    to_history = to_branch.revision_history()
50
28
    missing = []
51
29
    for rev_id in from_history:
52
30
        if not has_revision(to_branch, rev_id):
53
31
            missing.append(rev_id)
54
 
    
55
 
    count = 0
 
32
 
56
33
    while len(missing) > 0:
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() 
 
34
        to_branch.install_revisions(from_branch, revision_ids=missing)
 
35
        new_missing = []
68
36
        for rev_id in missing:
69
37
            try:
70
38
                revision = from_branch.get_revision(rev_id)
75
43
                    continue
76
44
            for parent in [p.revision_id for p in revision.parents]:
77
45
                if not has_revision(to_branch, parent):
78
 
                    new_missing.add(parent)
 
46
                    new_missing.append(parent)
79
47
        missing = new_missing
80
 
    return count, all_failed
81
 
 
82
 
 
 
48
 
 
49
 
 
50
from testsweet import InTempDir
 
51
def has_revision(branch, revision_id):
 
52
    try:
 
53
        branch.get_revision_xml(revision_id)
 
54
        return True
 
55
    except bzrlib.errors.NoSuchRevision:
 
56
        return False
 
57
 
 
58
class TestFetch(InTempDir):
 
59
    def runTest(self):
 
60
        def new_branch(name):
 
61
            os.mkdir(name)
 
62
            return Branch(name, init=True)
 
63
            
 
64
        #highest indices a: 3, b: 4
 
65
        br_a, br_b = make_branches()
 
66
        assert not has_revision(br_b, br_a.revision_history()[3])
 
67
        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])
 
70
 
 
71
        # greedy_fetch is not supposed to alter the revision history
 
72
        assert len(br_b.revision_history()) == 5
 
73
        assert not has_revision(br_b, br_a.revision_history()[3])
 
74
 
 
75
        assert len(br_b.revision_history()) == 5
 
76
        greedy_fetch(br_a, br_b, br_a.revision_history()[3])
 
77
        assert has_revision(br_b, br_a.revision_history()[3])
 
78
        assert not has_revision(br_a, br_b.revision_history()[3])
 
79
        assert not has_revision(br_a, br_b.revision_history()[4])
 
80
        greedy_fetch(br_b, br_a)
 
81
        assert has_revision(br_a, br_b.revision_history()[3])
 
82
        assert has_revision(br_a, br_b.revision_history()[4])
 
83
 
 
84
        br_b2 = new_branch('br_b2')
 
85
        greedy_fetch(br_b, br_b2)
 
86
        assert has_revision(br_b2, br_b.revision_history()[4])
 
87
        assert has_revision(br_b2, br_a.revision_history()[2])
 
88
        assert not has_revision(br_b2, br_a.revision_history()[3])
 
89
 
 
90
        br_a2 = new_branch('br_a2')
 
91
        greedy_fetch(br_a, br_a2)
 
92
        assert has_revision(br_a2, br_b.revision_history()[4])
 
93
        assert has_revision(br_a2, br_a.revision_history()[3])
 
94
 
 
95
 
 
96
 
 
97
if __name__ == '__main__':
 
98
    import sys
 
99
    sys.exit(run_suite(unittest.makeSuite()))