/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/git/git_remote_helper.py

  • Committer: Robert Collins
  • Date: 2005-10-19 10:11:57 UTC
  • mfrom: (1185.16.78)
  • mto: This revision was merged to the branch mainline in revision 1470.
  • Revision ID: robertc@robertcollins.net-20051019101157-17438d311e746b4f
mergeĀ fromĀ upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python3
2
 
# vim: expandtab
3
 
 
4
 
# Copyright (C) 2011-2018 Jelmer Vernooij <jelmer@jelmer.uk>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 
 
20
 
 
21
 
"""Remote helper for git for accessing bzr repositories."""
22
 
 
23
 
from __future__ import absolute_import
24
 
 
25
 
CAPABILITIES = ["fetch", "option", "push"]
26
 
 
27
 
import os
28
 
 
29
 
from ..controldir import ControlDir
30
 
from ..errors import NotBranchError, NoRepositoryPresent
31
 
from ..repository import InterRepository
32
 
from ..sixish import viewitems
33
 
from ..transport import get_transport_from_path
34
 
 
35
 
from . import (
36
 
    LocalGitProber,
37
 
    )
38
 
from .dir import (
39
 
    BareLocalGitControlDirFormat,
40
 
    LocalGitControlDirFormat,
41
 
    )
42
 
 
43
 
from .object_store import (
44
 
    get_object_store,
45
 
    )
46
 
from .refs import (
47
 
    get_refs_container,
48
 
    ref_to_branch_name,
49
 
    )
50
 
from .repository import (
51
 
    GitRepository,
52
 
    )
53
 
 
54
 
from ..plugins.fastimport import exporter as fastexporter
55
 
 
56
 
try:
57
 
    import fastimport  # noqa: F401
58
 
except ImportError:
59
 
    pass
60
 
else:
61
 
    CAPABILITIES.append("import")
62
 
    CAPABILITIES.append("refspec *:*")
63
 
 
64
 
 
65
 
def open_remote_dir(url):
66
 
    try:
67
 
        return ControlDir.open(url)
68
 
    except NotBranchError:
69
 
        return ControlDir.create(url)
70
 
 
71
 
 
72
 
def fetch(outf, wants, shortname, remote_dir, local_dir):
73
 
    remote_repo = remote_dir.find_repository()
74
 
    local_repo = local_dir.find_repository()
75
 
    inter = InterRepository.get(remote_repo, local_repo)
76
 
    revs = []
77
 
    for (sha1, ref) in wants:
78
 
        revs.append((sha1, None))
79
 
    if (isinstance(remote_repo, GitRepository) and
80
 
            isinstance(local_repo, GitRepository)):
81
 
        lossy = False
82
 
    else:
83
 
        lossy = True
84
 
    inter.fetch_objects(revs, lossy=lossy)
85
 
    outf.write(b"\n")
86
 
 
87
 
 
88
 
def push(outf, wants, shortname, remote_dir, local_dir):
89
 
    for (src_ref, dest_ref) in wants:
90
 
        local_branch = local_dir.open_branch(ref=src_ref)
91
 
        dest_branch_name = ref_to_branch_name(dest_ref)
92
 
        if dest_branch_name == "master":
93
 
            dest_branch_name = None
94
 
        try:
95
 
            remote_branch = remote_dir.open_branch(name=dest_branch_name)
96
 
        except NotBranchError:
97
 
            remote_branch = remote_dir.create_branch(name=dest_branch_name)
98
 
        local_branch.push(remote_branch)
99
 
        outf.write(b"ok %s\n" % dest_ref)
100
 
    outf.write(b"\n")
101
 
 
102
 
 
103
 
class RemoteHelper(object):
104
 
    """Git remote helper."""
105
 
 
106
 
    def __init__(self, local_dir, shortname, remote_dir):
107
 
        self.local_dir = local_dir
108
 
        self.shortname = shortname
109
 
        self.remote_dir = remote_dir
110
 
        self.batchcmd = None
111
 
        self.wants = []
112
 
 
113
 
    def cmd_capabilities(self, outf, argv):
114
 
        outf.write(b"\n".join([c.encode() for c in CAPABILITIES]) + b"\n\n")
115
 
 
116
 
    def cmd_list(self, outf, argv):
117
 
        try:
118
 
            repo = self.remote_dir.find_repository()
119
 
        except NoRepositoryPresent:
120
 
            repo = self.remote_dir.create_repository()
121
 
        object_store = get_object_store(repo)
122
 
        with object_store.lock_read():
123
 
            refs = get_refs_container(self.remote_dir, object_store)
124
 
            for ref, git_sha1 in viewitems(refs.as_dict()):
125
 
                ref = ref.replace(b"~", b"_")
126
 
                outf.write(b"%s %s\n" % (git_sha1, ref))
127
 
            outf.write(b"\n")
128
 
 
129
 
    def cmd_option(self, outf, argv):
130
 
        outf.write(b"unsupported\n")
131
 
 
132
 
    def cmd_fetch(self, outf, argv):
133
 
        if self.batchcmd not in (None, "fetch"):
134
 
            raise Exception("fetch command inside other batch command")
135
 
        self.wants.append(tuple(argv[1:]))
136
 
        self.batchcmd = "fetch"
137
 
 
138
 
    def cmd_push(self, outf, argv):
139
 
        if self.batchcmd not in (None, "push"):
140
 
            raise Exception("push command inside other batch command")
141
 
        self.wants.append(tuple(argv[1].split(":", 1)))
142
 
        self.batchcmd = "push"
143
 
 
144
 
    def cmd_import(self, outf, argv):
145
 
        if "fastimport" in CAPABILITIES:
146
 
            raise Exception("install fastimport for 'import' command support")
147
 
        ref = argv[1].encode('utf-8')
148
 
        dest_branch_name = ref_to_branch_name(ref)
149
 
        if dest_branch_name == "master":
150
 
            dest_branch_name = None
151
 
        remote_branch = self.remote_dir.open_branch(name=dest_branch_name)
152
 
        exporter = fastexporter.BzrFastExporter(
153
 
            remote_branch, outf=outf, ref=ref, checkpoint=None,
154
 
            import_marks_file=None, export_marks_file=None, revision=None,
155
 
            verbose=None, plain_format=True, rewrite_tags=False)
156
 
        exporter.run()
157
 
 
158
 
    commands = {
159
 
        "capabilities": cmd_capabilities,
160
 
        "list": cmd_list,
161
 
        "option": cmd_option,
162
 
        "fetch": cmd_fetch,
163
 
        "push": cmd_push,
164
 
        "import": cmd_import,
165
 
        }
166
 
 
167
 
    def process(self, inf, outf):
168
 
        while True:
169
 
            line = inf.readline()
170
 
            if not line:
171
 
                break
172
 
            self.process_line(line, outf)
173
 
 
174
 
    def process_line(self, l, outf):
175
 
        argv = l.strip().split()
176
 
        if argv == []:
177
 
            if self.batchcmd == "fetch":
178
 
                fetch(outf, self.wants, self.shortname,
179
 
                      self.remote_dir, self.local_dir)
180
 
            elif self.batchcmd == "push":
181
 
                push(outf, self.wants, self.shortname,
182
 
                     self.remote_dir, self.local_dir)
183
 
            elif self.batchcmd is None:
184
 
                return
185
 
            else:
186
 
                raise AssertionError("invalid batch %r" % self.batchcmd)
187
 
            self.batchcmd = None
188
 
        else:
189
 
            try:
190
 
                self.commands[argv[0].decode()](self, outf, argv)
191
 
            except KeyError:
192
 
                raise Exception("Unknown remote command %r" % argv)
193
 
        outf.flush()
194
 
 
195
 
 
196
 
def open_local_dir():
197
 
    try:
198
 
        git_path = os.environ["GIT_DIR"]
199
 
    except KeyError:
200
 
        git_transport = get_transport_from_path(".")
201
 
        git_format = LocalGitProber().probe_transport(git_transport)
202
 
    else:
203
 
        if git_path.endswith("/.git"):
204
 
            git_format = LocalGitControlDirFormat()
205
 
            git_path = git_path[:-4]
206
 
        else:
207
 
            git_format = BareLocalGitControlDirFormat()
208
 
        git_transport = get_transport_from_path(git_path)
209
 
 
210
 
    return git_format.open(git_transport)