/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
4
# Copyright (C) 2011 Jelmer Vernooij <jelmer@apache.org>
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
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
29
from ...controldir import ControlDir
30
from ...errors import NotBranchError, NoRepositoryPresent
31
from ...repository import InterRepository
32
from ...transport import get_transport_from_path
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
33
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
34
from . import (
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
35
    LocalGitProber,
36
    )
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
37
from .dir import (
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
38
    BareLocalGitControlDirFormat,
39
    LocalGitControlDirFormat,
40
    )
41
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
42
from .object_store import (
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
43
    get_object_store,
44
    )
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
45
from .refs import (
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
46
    get_refs_container,
47
    ref_to_branch_name,
48
    )
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
49
from .repository import (
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
50
    GitRepository,
51
    )
52
53
try:
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
54
    from ..fastimport import exporter as fastexporter
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
55
except ImportError:
0.200.1582 by Jelmer Vernooij
Print error when bzr-fastimport is not installed.
56
    fastexporter = None
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
57
else:
58
    CAPABILITIES.append("import")
59
60
def open_remote_dir(url):
61
    try:
62
        return ControlDir.open(url)
63
    except NotBranchError:
64
        return ControlDir.create(url)
65
66
67
def fetch(outf, wants, shortname, remote_dir, local_dir):
68
    remote_repo = remote_dir.find_repository()
69
    local_repo = local_dir.find_repository()
70
    inter = InterRepository.get(remote_repo, local_repo)
0.200.1586 by Jelmer Vernooij
Fixes duplicate tag warnings in 'git-remote-bzr' helper.
71
    revs = []
72
    for (sha1, ref) in wants:
73
        revs.append((sha1, None))
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
74
    if (isinstance(remote_repo, GitRepository) and
75
        isinstance(local_repo, GitRepository)):
76
        lossy = False
77
    else:
78
        lossy = True
0.200.1586 by Jelmer Vernooij
Fixes duplicate tag warnings in 'git-remote-bzr' helper.
79
    inter.fetch_objects(revs, lossy=lossy)
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
80
    outf.write("\n")
81
82
83
def push(outf, wants, shortname, remote_dir, local_dir):
84
    for (src_ref, dest_ref) in wants:
85
        local_branch = local_dir.open_branch(ref=src_ref)
86
        dest_branch_name = ref_to_branch_name(dest_ref)
87
        if dest_branch_name == "master":
88
            dest_branch_name = None
89
        try:
90
            remote_branch = remote_dir.open_branch(name=dest_branch_name)
91
        except NotBranchError:
92
            remote_branch = remote_dir.create_branch(name=dest_branch_name)
93
        local_branch.push(remote_branch)
94
        outf.write("ok %s\n" % dest_ref)
95
    outf.write("\n")
96
97
98
class RemoteHelper(object):
99
    """Git remote helper."""
100
101
    def __init__(self, local_dir, shortname, remote_dir):
102
        self.local_dir = local_dir
103
        self.shortname = shortname
104
        self.remote_dir = remote_dir
105
        self.batchcmd = None
106
        self.wants = []
107
108
    def cmd_capabilities(self, outf, argv):
109
        outf.write("\n".join(CAPABILITIES)+"\n\n")
110
111
    def cmd_list(self, outf, argv):
112
        try:
113
            repo = self.remote_dir.find_repository()
114
        except NoRepositoryPresent:
115
            repo = self.remote_dir.create_repository()
116
        object_store = get_object_store(repo)
0.200.1788 by Jelmer Vernooij
Use context managers.
117
        with object_store.lock_read():
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
118
            refs = get_refs_container(self.remote_dir, object_store)
119
            for ref, git_sha1 in refs.as_dict().iteritems():
0.200.1610 by Jelmer Vernooij
Convert ~ to _ in tag names.
120
                ref = ref.replace("~", "_")
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
121
                outf.write("%s %s\n" % (git_sha1, ref))
122
            outf.write("\n")
123
124
    def cmd_option(self, outf, argv):
125
        outf.write("unsupported\n")
126
127
    def cmd_fetch(self, outf, argv):
128
        if self.batchcmd not in (None, "fetch"):
129
            raise Exception("fetch command inside other batch command")
130
        self.wants.append(tuple(argv[1:]))
131
        self.batchcmd = "fetch"
132
133
    def cmd_push(self, outf, argv):
134
        if self.batchcmd not in (None, "push"):
135
            raise Exception("push command inside other batch command")
136
        self.wants.append(tuple(argv[1].split(":", 1)))
137
        self.batchcmd = "push"
138
139
    def cmd_import(self, outf, argv):
0.200.1582 by Jelmer Vernooij
Print error when bzr-fastimport is not installed.
140
        if fastexporter is None:
141
            raise Exception("install bzr-fastimport for 'import' command support")
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
142
        dest_branch_name = ref_to_branch_name(argv[1])
143
        if dest_branch_name == "master":
144
            dest_branch_name = None
145
        remote_branch = self.remote_dir.open_branch(name=dest_branch_name)
146
        exporter = fastexporter.BzrFastExporter(remote_branch,
0.200.1581 by Jelmer Vernooij
Fix support for newer versions of bzr-fastimport, add more tests.
147
            outf=outf, ref=argv[1],
0.200.1514 by Jelmer Vernooij
Move git_remote_helper to a python module.
148
            checkpoint=None, import_marks_file=None,
149
            export_marks_file=None, revision=None,
150
            verbose=None, plain_format=True,
151
            rewrite_tags=False)
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:
165
            l = inf.readline()
166
            if not l:
167
                break
168
            self.process_line(l, outf)
169
170
    def process_line(self, l, outf):
171
        argv = l.strip().split()
172
        if argv == []:
173
            if self.batchcmd == "fetch":
174
                fetch(outf, self.wants, self.shortname, self.remote_dir, self.local_dir)
175
            elif self.batchcmd == "push":
176
                push(outf, self.wants, self.shortname, self.remote_dir, self.local_dir)
177
            elif self.batchcmd is None:
178
                return
179
            else:
180
                raise AssertionError("invalid batch %r" % self.batchcmd)
181
            self.batchcmd = None
182
        else:
183
            try:
184
               self.commands[argv[0]](self, outf, argv)
185
            except KeyError:
186
               raise Exception("Unknown remote command %r" % argv)
187
        outf.flush()
188
189
190
def open_local_dir():
191
    try:
192
        git_path = os.environ["GIT_DIR"]
193
    except KeyError:
194
        git_transport = get_transport_from_path(".")
195
        git_format = LocalGitProber().probe_transport(git_transport)
196
    else:
197
        if git_path.endswith("/.git"):
198
            git_format = LocalGitControlDirFormat()
199
            git_path = git_path[:-4]
200
        else:
201
            git_format = BareLocalGitControlDirFormat()
202
        git_transport = get_transport_from_path(git_path)
203
204
    return git_format.open(git_transport)