~bzr-pqm/bzr/bzr.dev

974.1.27 by aaron.bentley at utoronto
Initial greedy fetch work
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
974.1.30 by aaron.bentley at utoronto
Changed copy_multi to permit failure and return a tuple, tested missing required revisions
17
from bzrlib.trace import mutter, note
974.1.27 by aaron.bentley at utoronto
Initial greedy fetch work
18
from bzrlib.branch import Branch
974.1.28 by aaron.bentley at utoronto
factored install_revisions out of update_revisions, updated test cases for greedy_fetch
19
from bzrlib.progress import ProgressBar
974.1.27 by aaron.bentley at utoronto
Initial greedy fetch work
20
import sys
21
import os
22
1139 by Martin Pool
- merge in merge improvements and additional tests
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
974.1.33 by aaron.bentley at utoronto
Added greedy_fetch to update_revisions
31
def greedy_fetch(to_branch, from_branch, revision=None, pb=None):
974.1.29 by aaron.bentley at utoronto
Got greedy fetch working when the desired revision's not in the revision history
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
    """
974.1.27 by aaron.bentley at utoronto
Initial greedy fetch work
36
    from_history = from_branch.revision_history()
974.1.30 by aaron.bentley at utoronto
Changed copy_multi to permit failure and return a tuple, tested missing required revisions
37
    required_revisions = set(from_history)
38
    all_failed = set()
39
    if revision is not None:
40
        required_revisions.add(revision)
974.1.29 by aaron.bentley at utoronto
Got greedy fetch working when the desired revision's not in the revision history
41
        try:
974.1.30 by aaron.bentley at utoronto
Changed copy_multi to permit failure and return a tuple, tested missing required revisions
42
            rev_index = from_history.index(revision)
974.1.29 by aaron.bentley at utoronto
Got greedy fetch working when the desired revision's not in the revision history
43
        except ValueError:
44
            rev_index = None
45
        if rev_index is not None:
46
            from_history = from_history[:rev_index + 1]
47
        else:
974.1.30 by aaron.bentley at utoronto
Changed copy_multi to permit failure and return a tuple, tested missing required revisions
48
            from_history = [revision]
974.1.27 by aaron.bentley at utoronto
Initial greedy fetch work
49
    to_history = to_branch.revision_history()
50
    missing = []
51
    for rev_id in from_history:
52
        if not has_revision(to_branch, rev_id):
53
            missing.append(rev_id)
974.1.29 by aaron.bentley at utoronto
Got greedy fetch working when the desired revision's not in the revision history
54
    
55
    count = 0
974.1.27 by aaron.bentley at utoronto
Initial greedy fetch work
56
    while len(missing) > 0:
974.1.30 by aaron.bentley at utoronto
Changed copy_multi to permit failure and return a tuple, tested missing required revisions
57
        installed, failed = to_branch.install_revisions(from_branch, 
974.1.33 by aaron.bentley at utoronto
Added greedy_fetch to update_revisions
58
                                                        revision_ids=missing,
59
                                                        pb=pb)
974.1.30 by aaron.bentley at utoronto
Changed copy_multi to permit failure and return a tuple, tested missing required revisions
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)
974.1.27 by aaron.bentley at utoronto
Initial greedy fetch work
67
        new_missing = []
68
        for rev_id in missing:
69
            try:
70
                revision = from_branch.get_revision(rev_id)
71
            except bzrlib.errors.NoSuchRevision:
72
                if revision in from_history:
73
                    raise
74
                else:
75
                    continue
76
            for parent in [p.revision_id for p in revision.parents]:
77
                if not has_revision(to_branch, parent):
78
                    new_missing.append(parent)
79
        missing = new_missing
974.1.30 by aaron.bentley at utoronto
Changed copy_multi to permit failure and return a tuple, tested missing required revisions
80
    return count, all_failed
974.1.27 by aaron.bentley at utoronto
Initial greedy fetch work
81
82