/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
1
# vim: expandtab
2
0.358.2 by Jelmer Vernooij
Refresh copyright headers, add my email.
3
# Copyright (C) 2011-2018 Jelmer Vernooij <jelmer@jelmer.uk>
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
4
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
0.358.1 by Jelmer Vernooij
Fix FSF address.
17
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
18
19
20
"""Remote helper for git for accessing bzr repositories."""
21
22
CAPABILITIES = ["fetch", "option", "push"]
23
24
import os
25
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
26
from ..controldir import ControlDir
27
from ..errors import NotBranchError, NoRepositoryPresent
28
from ..repository import InterRepository
29
from ..transport import get_transport_from_path
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
30
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
31
from . import (
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
32
    LocalGitProber,
33
    )
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
34
from .dir import (
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
35
    BareLocalGitControlDirFormat,
36
    LocalGitControlDirFormat,
37
    )
38
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
39
from .object_store import (
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
40
    get_object_store,
41
    )
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
42
from .refs import (
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
43
    get_refs_container,
44
    ref_to_branch_name,
45
    )
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
46
from .repository import (
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
47
    GitRepository,
48
    )
49
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
50
from ..plugins.fastimport import exporter as fastexporter
0.337.1 by Jelmer Vernooij
Various git remote helper fixes.
51
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
52
try:
7143.11.1 by Jelmer Vernooij
Remove some unused imports.
53
    import fastimport  # noqa: F401
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
54
except ImportError:
0.337.1 by Jelmer Vernooij
Various git remote helper fixes.
55
    pass
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
56
else:
57
    CAPABILITIES.append("import")
7463.3.1 by Jelmer Vernooij
Advertise refspec *:*, to stop warnings from git.
58
    CAPABILITIES.append("refspec *:*")
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
59
7143.15.2 by Jelmer Vernooij
Run autopep8.
60
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
61
def open_remote_dir(url):
62
    try:
63
        return ControlDir.open(url)
64
    except NotBranchError:
65
        return ControlDir.create(url)
66
67
68
def fetch(outf, wants, shortname, remote_dir, local_dir):
69
    remote_repo = remote_dir.find_repository()
70
    local_repo = local_dir.find_repository()
71
    inter = InterRepository.get(remote_repo, local_repo)
0.200.1586 by Jelmer Vernooij
Fixes duplicate tag warnings in 'git-remote-bzr' helper.
72
    revs = []
73
    for (sha1, ref) in wants:
74
        revs.append((sha1, None))
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
75
    if (isinstance(remote_repo, GitRepository) and
7143.15.2 by Jelmer Vernooij
Run autopep8.
76
            isinstance(local_repo, GitRepository)):
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
77
        lossy = False
78
    else:
79
        lossy = True
7490.148.1 by Jelmer Vernooij
Different names for functions with a different signature.:
80
    inter.fetch_revs(revs, lossy=lossy)
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
81
    outf.write(b"\n")
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
82
83
84
def push(outf, wants, shortname, remote_dir, local_dir):
85
    for (src_ref, dest_ref) in wants:
86
        local_branch = local_dir.open_branch(ref=src_ref)
87
        dest_branch_name = ref_to_branch_name(dest_ref)
88
        if dest_branch_name == "master":
89
            dest_branch_name = None
90
        try:
91
            remote_branch = remote_dir.open_branch(name=dest_branch_name)
92
        except NotBranchError:
93
            remote_branch = remote_dir.create_branch(name=dest_branch_name)
94
        local_branch.push(remote_branch)
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
95
        outf.write(b"ok %s\n" % dest_ref)
96
    outf.write(b"\n")
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
97
98
99
class RemoteHelper(object):
100
    """Git remote helper."""
101
102
    def __init__(self, local_dir, shortname, remote_dir):
103
        self.local_dir = local_dir
104
        self.shortname = shortname
105
        self.remote_dir = remote_dir
106
        self.batchcmd = None
107
        self.wants = []
108
109
    def cmd_capabilities(self, outf, argv):
7143.15.2 by Jelmer Vernooij
Run autopep8.
110
        outf.write(b"\n".join([c.encode() for c in CAPABILITIES]) + b"\n\n")
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
111
112
    def cmd_list(self, outf, argv):
113
        try:
114
            repo = self.remote_dir.find_repository()
115
        except NoRepositoryPresent:
116
            repo = self.remote_dir.create_repository()
117
        object_store = get_object_store(repo)
0.200.1788 by Jelmer Vernooij
Use context managers.
118
        with object_store.lock_read():
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
119
            refs = get_refs_container(self.remote_dir, object_store)
7479.2.1 by Jelmer Vernooij
Drop python2 support.
120
            for ref, git_sha1 in refs.as_dict().items():
7018.3.8 by Jelmer Vernooij
Disable some flaky tests on python3, allow running without fastimport.
121
                ref = ref.replace(b"~", b"_")
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
122
                outf.write(b"%s %s\n" % (git_sha1, ref))
123
            outf.write(b"\n")
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
124
125
    def cmd_option(self, outf, argv):
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
126
        outf.write(b"unsupported\n")
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
127
128
    def cmd_fetch(self, outf, argv):
129
        if self.batchcmd not in (None, "fetch"):
130
            raise Exception("fetch command inside other batch command")
131
        self.wants.append(tuple(argv[1:]))
132
        self.batchcmd = "fetch"
133
134
    def cmd_push(self, outf, argv):
135
        if self.batchcmd not in (None, "push"):
136
            raise Exception("push command inside other batch command")
137
        self.wants.append(tuple(argv[1].split(":", 1)))
138
        self.batchcmd = "push"
139
140
    def cmd_import(self, outf, argv):
0.337.1 by Jelmer Vernooij
Various git remote helper fixes.
141
        if "fastimport" in CAPABILITIES:
142
            raise Exception("install fastimport for 'import' command support")
7045.4.8 by Jelmer Vernooij
Fix another 128 tests on python 3.
143
        ref = argv[1].encode('utf-8')
144
        dest_branch_name = ref_to_branch_name(ref)
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
145
        if dest_branch_name == "master":
146
            dest_branch_name = None
147
        remote_branch = self.remote_dir.open_branch(name=dest_branch_name)
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
148
        exporter = fastexporter.BzrFastExporter(
149
            remote_branch, outf=outf, ref=ref, checkpoint=None,
150
            import_marks_file=None, export_marks_file=None, revision=None,
151
            verbose=None, plain_format=True, rewrite_tags=False)
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
152
        exporter.run()
153
154
    commands = {
155
        "capabilities": cmd_capabilities,
156
        "list": cmd_list,
157
        "option": cmd_option,
158
        "fetch": cmd_fetch,
159
        "push": cmd_push,
160
        "import": cmd_import,
161
        }
162
163
    def process(self, inf, outf):
164
        while True:
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
165
            line = inf.readline()
166
            if not line:
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
167
                break
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
168
            self.process_line(line, outf)
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
169
170
    def process_line(self, l, outf):
171
        argv = l.strip().split()
172
        if argv == []:
173
            if self.batchcmd == "fetch":
7143.15.2 by Jelmer Vernooij
Run autopep8.
174
                fetch(outf, self.wants, self.shortname,
175
                      self.remote_dir, self.local_dir)
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
176
            elif self.batchcmd == "push":
7143.15.2 by Jelmer Vernooij
Run autopep8.
177
                push(outf, self.wants, self.shortname,
178
                     self.remote_dir, self.local_dir)
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
179
            elif self.batchcmd is None:
180
                return
181
            else:
182
                raise AssertionError("invalid batch %r" % self.batchcmd)
183
            self.batchcmd = None
184
        else:
185
            try:
7290.11.8 by Jelmer Vernooij
Fix tests on Python 3.
186
                self.commands[argv[0].decode()](self, outf, argv)
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
187
            except KeyError:
7143.15.2 by Jelmer Vernooij
Run autopep8.
188
                raise Exception("Unknown remote command %r" % argv)
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
189
        outf.flush()
190
191
192
def open_local_dir():
193
    try:
194
        git_path = os.environ["GIT_DIR"]
195
    except KeyError:
196
        git_transport = get_transport_from_path(".")
197
        git_format = LocalGitProber().probe_transport(git_transport)
198
    else:
199
        if git_path.endswith("/.git"):
200
            git_format = LocalGitControlDirFormat()
201
            git_path = git_path[:-4]
202
        else:
203
            git_format = BareLocalGitControlDirFormat()
204
        git_transport = get_transport_from_path(git_path)
205
206
    return git_format.open(git_transport)