/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6651.3.1 by Jelmer Vernooij
Bundle the fetch-ghosts command as hidden 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.
6651.3.3 by Jelmer Vernooij
Fix source tests.
7
#
6651.3.1 by Jelmer Vernooij
Bundle the fetch-ghosts command as hidden command.
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.
6651.3.3 by Jelmer Vernooij
Fix source tests.
12
#
6651.3.1 by Jelmer Vernooij
Bundle the fetch-ghosts command as hidden command.
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
6651.3.3 by Jelmer Vernooij
Fix source tests.
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17
from __future__ import absolute_import
6651.3.1 by Jelmer Vernooij
Bundle the fetch-ghosts command as hidden command.
18
19
from .branch import Branch
20
from .trace import note
21
from .errors import NoSuchRevision, BzrCommandError
22
23
24
class GhostFetcher(object):
25
26
    @classmethod
6651.3.2 by Jelmer Vernooij
Move fetch_ghosts body, avoid print.
27
    def from_cmdline(cls, other):
6651.3.1 by Jelmer Vernooij
Bundle the fetch-ghosts command as hidden command.
28
        this_branch = Branch.open_containing('.')[0]
29
        if other is None:
30
            other = this_branch.get_parent()
31
            if other is None:
32
                raise BzrCommandError('No branch specified and no location'
33
                                      ' saved.')
34
            else:
35
                note("Using saved location %s.", other)
36
        other_branch = Branch.open_containing(other)[0]
6651.3.2 by Jelmer Vernooij
Move fetch_ghosts body, avoid print.
37
        return cls(this_branch, other_branch)
6651.3.1 by Jelmer Vernooij
Bundle the fetch-ghosts command as hidden command.
38
39
    def __init__(self, this_branch, other_branch):
40
        self.this_branch = this_branch
41
        self.other_branch = other_branch
42
43
    def run(self):
44
        lock_other = self.this_branch.base != self.other_branch.base
45
        self.this_branch.lock_write()
46
        try:
47
            if lock_other:
48
                self.other_branch.lock_read()
49
            try:
50
                return self._run_locked()
51
            finally:
52
                if lock_other:
53
                    self.other_branch.unlock()
54
        finally:
55
            self.this_branch.unlock()
56
57
    def iter_ghosts(self):
58
        """Find all ancestors that aren't stored in this branch."""
59
        seen = set()
60
        lines = [self.this_branch.last_revision()]
61
        if lines[0] is None:
62
            return
63
        while len(lines) > 0:
64
            new_lines = []
65
            for line in lines:
66
                if line in seen:
67
                    continue
68
                seen.add(line)
69
                try:
70
                    revision = self.this_branch.repository.get_revision(line)
71
                    new_lines.extend(revision.parent_ids)
72
                except NoSuchRevision:
73
                    yield line
74
            lines = new_lines
75
76
    def _run_locked(self):
77
        installed = []
78
        failed = []
79
        if self.this_branch.last_revision() is None:
80
            print("No revisions in branch.")
81
            return
82
        # Because iter_ghosts tests for existence after our last fetch
83
        # is complete, it won't falsely report an ancestor as a ghost.
84
        # Yay iterators!
85
        ghosts = self.iter_ghosts()
86
        for revision in ghosts:
87
            try:
88
                self.this_branch.fetch(self.other_branch, revision)
89
                installed.append(revision)
90
            except NoSuchRevision:
91
                failed.append(revision)
92
        return installed, failed