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