~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-19 12:06:01 UTC
  • mto: (1092.1.41) (1185.3.4) (974.1.47)
  • mto: This revision was merged to the branch mainline in revision 1110.
  • Revision ID: aaron.bentley@utoronto.ca-20050819120601-58525b75283a9c1c
Initial greedy fetch work

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005 by Canonical Ltd
 
2
 
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
 
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
 
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
import bzrlib.errors
 
17
from bzrlib.selftest.testrevision import make_branches
 
18
from bzrlib.trace import mutter
 
19
from bzrlib.branch import Branch
 
20
import sys
 
21
import os
 
22
 
 
23
def greedy_fetch(from_branch, to_branch, last_revision=None):
 
24
    from_history = from_branch.revision_history()
 
25
    if last_revision is not None:
 
26
        from_history = from_history[:from_history.index(last_revision)+1]
 
27
    to_history = to_branch.revision_history()
 
28
    missing = []
 
29
    for rev_id in from_history:
 
30
        if not has_revision(to_branch, rev_id):
 
31
            missing.append(rev_id)
 
32
 
 
33
    while len(missing) > 0:
 
34
        to_branch.update_revisions(from_branch, revision_ids=missing)
 
35
        new_missing = []
 
36
        for rev_id in missing:
 
37
            try:
 
38
                revision = from_branch.get_revision(rev_id)
 
39
            except bzrlib.errors.NoSuchRevision:
 
40
                if revision in from_history:
 
41
                    raise
 
42
                else:
 
43
                    continue
 
44
            for parent in [p.revision_id for p in revision.parents]:
 
45
                if not has_revision(to_branch, parent):
 
46
                    new_missing.append(parent)
 
47
        missing = new_missing
 
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
        br_a, br_b = make_branches()
 
61
        assert not has_revision(br_b, br_a.revision_history()[3])
 
62
        assert has_revision(br_b, br_a.revision_history()[2])
 
63
        greedy_fetch(br_a, br_b, br_a.revision_history()[2])
 
64
        assert not has_revision(br_b, br_a.revision_history()[3])
 
65
        greedy_fetch(br_a, br_b, br_a.revision_history()[3])
 
66
        assert has_revision(br_b, br_a.revision_history()[3])
 
67
        assert not has_revision(br_a, br_b.revision_history()[3])
 
68
        assert not has_revision(br_a, br_b.revision_history()[4])
 
69
        greedy_fetch(br_b, br_a)
 
70
        assert has_revision(br_a, br_b.revision_history()[3])
 
71
        assert has_revision(br_a, br_b.revision_history()[4])
 
72
        os.mkdir('branchc')
 
73
        br_c = Branch("branchc", init=True)
 
74
        greedy_fetch(br_b, br_c)
 
75
        assert has_revision(br_c, br_b.revision_history()[5])
 
76
        assert has_revision(br_c, br_a.revision_history()[4])
 
77
        os.mkdir('branchd')
 
78
        br_d = Branch("branchd", init=True)
 
79
        greedy_fetch(br_a, br_d)
 
80
        assert has_revision(br_d, br_b.revision_history()[5])
 
81
        assert has_revision(br_d, br_a.revision_history()[4])
 
82
 
 
83
 
 
84
 
 
85
if __name__ == '__main__':
 
86
    import sys
 
87
    sys.exit(run_suite(unittest.makeSuite()))