~abentley/bzrtools/bzrtools.dev

144 by aaron.bentley at utoronto
Added fetch-missing command
1
# Copyright (C) 2005 by Aaron Bentley
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
from bzrlib.branch import find_branch
17
from bzrlib.fetch import greedy_fetch
18
from bzrlib.errors import NoSuchRevision, InstallFailed
19
def fetch_missing(branch):
20
    """Install the revisions of missing ancestors from another branch."""
21
    this_branch = find_branch('.')
150 by abentley
Made fetch_missing use the x-pull file
22
    if branch is None:
23
        try:
24
            branch = this_branch.controlfile("x-pull", "rb").read().rstrip('\n')
25
            print "Using last location: %s" % branch
26
        except IOError, e:
27
            if e.errno != errno.ENOENT:
28
                raise
29
            else:
30
                raise BzrCommandError("No pull location known or specified.")
31
144 by aaron.bentley at utoronto
Added fetch-missing command
32
    other_branch = find_branch(branch)
33
    installed = []
34
    failed = []
35
36
    # Because iter_missing_ancestors tests for existence after our last fetch
37
    # is complete, it won't falsely report an ancestor as missing.
38
    # Yay iterators!
39
    missing = iter_missing_ancestors(this_branch)
40
    for revision in missing:
41
        try:
42
            greedy_fetch(this_branch, other_branch, revision)
43
            installed.append(revision)
44
        except InstallFailed:
45
            failed.append(revision)
46
    if len(installed) > 0:
47
        print "Installed:"
48
    for rev in installed:
49
        print rev
50
    if len(failed) > 0:
51
        print "Still missing:"
52
    for rev in failed:
53
        print rev
54
55
def iter_missing_ancestors(branch):
56
    """Find all ancestors that aren't stored in this branch."""
57
    seen = set()
58
    lines = [branch.last_patch()]
59
    while len(lines) > 0:
60
        new_lines = []
61
        for line in lines:
62
            if line in seen:
63
                continue
64
            seen.add(line)
65
            try:
66
                revision = branch.get_revision(line)
67
                new_lines.extend([r.revision_id for r in revision.parents])
68
            except NoSuchRevision:
69
                yield line
70
        lines = new_lines