1
# Copyright (C) 2005 by Canonical Ltd
 
 
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.
 
 
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.
 
 
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
 
 
17
from bzrlib.trace import mutter, note
 
 
18
from bzrlib.branch import Branch
 
 
19
from bzrlib.progress import ProgressBar
 
 
23
def has_revision(branch, revision_id):
 
 
25
        branch.get_revision_xml(revision_id)
 
 
27
    except bzrlib.errors.NoSuchRevision:
 
 
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
 
 
36
    from_history = from_branch.revision_history()
 
 
37
    required_revisions = set(from_history)
 
 
39
    if revision is not None:
 
 
40
        required_revisions.add(revision)
 
 
42
            rev_index = from_history.index(revision)
 
 
45
        if rev_index is not None:
 
 
46
            from_history = from_history[:rev_index + 1]
 
 
48
            from_history = [revision]
 
 
49
    to_history = to_branch.revision_history()
 
 
51
    for rev_id in from_history:
 
 
52
        if not has_revision(to_branch, rev_id):
 
 
53
            missing.append(rev_id)
 
 
56
    while len(missing) > 0:
 
 
57
        installed, failed = to_branch.install_revisions(from_branch, 
 
 
61
        required_failed = failed.intersection(required_revisions)
 
 
62
        if len(required_failed) > 0:
 
 
63
            raise bzrlib.errors.InstallFailed(required_failed)
 
 
65
            note("Failed to install %s" % rev_id)
 
 
66
        all_failed.update(failed)
 
 
68
        for rev_id in missing:
 
 
70
                revision = from_branch.get_revision(rev_id)
 
 
71
            except bzrlib.errors.NoSuchRevision:
 
 
72
                if revision in from_history:
 
 
76
            for parent in [p.revision_id for p in revision.parents]:
 
 
77
                if not has_revision(to_branch, parent):
 
 
78
                    new_missing.add(parent)
 
 
80
    return count, all_failed