/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
7479.2.1 by Jelmer Vernooij
Drop python2 support.
17
import contextlib
18
6651.3.1 by Jelmer Vernooij
Bundle the fetch-ghosts command as hidden command.
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
7479.2.1 by Jelmer Vernooij
Drop python2 support.
45
        with contextlib.ExitStack() as exit_stack:
7356.1.5 by Jelmer Vernooij
Use more ExitStacks.
46
            exit_stack.enter_context(self.this_branch.lock_write())
6651.3.1 by Jelmer Vernooij
Bundle the fetch-ghosts command as hidden command.
47
            if lock_other:
7356.1.5 by Jelmer Vernooij
Use more ExitStacks.
48
                exit_stack.enter_context(self.other_branch.lock_read())
49
            return self._run_locked()
6651.3.1 by Jelmer Vernooij
Bundle the fetch-ghosts command as hidden command.
50
51
    def iter_ghosts(self):
52
        """Find all ancestors that aren't stored in this branch."""
53
        seen = set()
54
        lines = [self.this_branch.last_revision()]
55
        if lines[0] is None:
56
            return
57
        while len(lines) > 0:
58
            new_lines = []
59
            for line in lines:
60
                if line in seen:
61
                    continue
62
                seen.add(line)
63
                try:
64
                    revision = self.this_branch.repository.get_revision(line)
65
                    new_lines.extend(revision.parent_ids)
66
                except NoSuchRevision:
67
                    yield line
68
            lines = new_lines
69
70
    def _run_locked(self):
71
        installed = []
72
        failed = []
73
        if self.this_branch.last_revision() is None:
74
            print("No revisions in branch.")
75
            return
76
        # Because iter_ghosts tests for existence after our last fetch
77
        # is complete, it won't falsely report an ancestor as a ghost.
78
        # Yay iterators!
79
        ghosts = self.iter_ghosts()
80
        for revision in ghosts:
81
            try:
82
                self.this_branch.fetch(self.other_branch, revision)
83
                installed.append(revision)
84
            except NoSuchRevision:
85
                failed.append(revision)
86
        return installed, failed