/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/__init__.py

  • Committer: Jelmer Vernooij
  • Date: 2019-06-03 23:48:08 UTC
  • mfrom: (7316 work)
  • mto: This revision was merged to the branch mainline in revision 7328.
  • Revision ID: jelmer@jelmer.uk-20190603234808-15yk5c7054tj8e2b
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
    __version__ as breezy_version,
34
34
    errors as brz_errors,
35
35
    trace,
36
 
    urlutils,
37
36
    version_info,
38
37
    )
39
38
 
104
103
 
105
104
class LocalGitProber(Prober):
106
105
 
107
 
    @classmethod
108
 
    def priority(klass, transport):
109
 
        return 10
110
 
 
111
106
    def probe_transport(self, transport):
112
107
        try:
113
108
            external_url = transport.external_url()
117
112
                external_url.startswith("https:")):
118
113
            # Already handled by RemoteGitProber
119
114
            raise brz_errors.NotBranchError(path=transport.base)
 
115
        from .. import urlutils
120
116
        if urlutils.split(transport.base)[1] == ".git":
121
117
            raise brz_errors.NotBranchError(path=transport.base)
122
118
        if not transport.has_any(['objects', '.git/objects', '.git']):
146
142
    return "git/Breezy/%s" % breezy_version
147
143
 
148
144
 
149
 
def is_github_url(url):
150
 
    (scheme, user, password, host, port,
151
 
     path) = urlutils.parse_url(url)
152
 
    return host in ("github.com", "gopkg.in")
153
 
 
154
 
 
155
145
class RemoteGitProber(Prober):
156
146
 
157
 
    @classmethod
158
 
    def priority(klass, transport):
159
 
        # This is a surprisingly good heuristic to determine whether this
160
 
        # prober is more likely to succeed than the Bazaar one.
161
 
        if 'git' in transport.base:
162
 
            return -15
163
 
        return -10
164
 
 
165
147
    def probe_http_transport(self, transport):
166
148
        # This function intentionally doesn't use any of the support code under
167
149
        # breezy.git, since it's called for every repository that's
168
150
        # accessed over HTTP, whether it's Git, Bzr or something else.
169
151
        # Importing Dulwich and the other support code adds unnecessray slowdowns.
170
 
        base_url = urlutils.strip_segment_parameters(transport.external_url())
 
152
        from .. import urlutils
 
153
        base_url, _ = urlutils.split_segment_parameters(
 
154
            transport.external_url())
171
155
        url = urlutils.URL.from_string(base_url)
172
156
        url.user = url.quoted_user = None
173
157
        url.password = url.quoted_password = None
174
 
        host = url.host
175
158
        url = urlutils.join(str(url), "info/refs") + "?service=git-upload-pack"
176
159
        headers = {"Content-Type": "application/x-git-upload-pack-request",
177
160
                   "Accept": "application/x-git-upload-pack-result",
178
161
                   }
179
 
        if is_github_url(url):
 
162
        (scheme, user, password, host, port,
 
163
         path) = urlutils.parse_url(url)
 
164
        if host == "github.com":
180
165
            # GitHub requires we lie.
181
166
            # https://github.com/dulwich/dulwich/issues/562
182
 
            headers["User-Agent"] = user_agent_for_github()
 
167
            req.add_header("User-Agent", user_agent_for_github())
183
168
        elif host == "bazaar.launchpad.net":
184
169
            # Don't attempt Git probes against bazaar.launchpad.net; pad.lv/1744830
185
170
            raise brz_errors.NotBranchError(transport.base)
186
171
        resp = transport.request('GET', url, headers=headers)
187
172
        if resp.status in (404, 405):
188
173
            raise brz_errors.NotBranchError(transport.base)
189
 
        elif resp.status != 200:
190
 
            raise brz_errors.InvalidHttpResponse(
 
174
        else:
 
175
            raise errors.InvalidHttpResponse(
191
176
                url, 'Unable to handle http code %d' % resp.status)
192
177
 
193
 
        ct = resp.getheader("Content-Type")
194
 
        if ct and ct.startswith("application/x-git"):
 
178
        headers = resp.headers
 
179
        ct = headers.get("Content-Type")
 
180
        if ct is None:
 
181
            raise brz_errors.NotBranchError(transport.base)
 
182
        if ct.startswith("application/x-git"):
195
183
            from .remote import RemoteGitControlDirFormat
196
184
            return RemoteGitControlDirFormat()
197
 
        elif not ct:
 
185
        else:
198
186
            from .dir import (
199
187
                BareLocalGitControlDirFormat,
200
188
                )
201
189
            ret = BareLocalGitControlDirFormat()
202
190
            ret._refs_text = resp.read()
203
191
            return ret
204
 
        raise brz_errors.NotBranchError(transport.base)
205
192
 
206
193
    def probe_transport(self, transport):
207
194
        try:
233
220
 
234
221
 
235
222
ControlDirFormat.register_prober(LocalGitProber)
236
 
ControlDirFormat.register_prober(RemoteGitProber)
 
223
ControlDirFormat._server_probers.append(RemoteGitProber)
237
224
 
238
225
register_transport_proto(
239
226
    'git://', help="Access using the Git smart server protocol.")