51
49
from ..revision import NULL_REVISION
52
50
from ..revisiontree import RevisionTree
53
from ..sixish import (
57
51
from ..transport import (
59
53
register_urlparse_netloc_protocol,
127
import urllib.parse as urlparse
128
from urllib.parse import splituser
131
from urllib import splituser
120
import urllib.parse as urlparse
121
from urllib.parse import splituser
133
123
# urlparse only supports a limited number of schemes by default
134
124
register_urlparse_netloc_protocol('git')
211
201
return PermissionDenied(url, message)
212
202
if message.endswith(' does not appear to be a git repository'):
213
203
return NotBranchError(url, message)
204
if message == 'pre-receive hook declined':
205
return PermissionDenied(url, message)
214
206
if re.match('(.+) is not a valid repository name',
215
207
message.splitlines()[0]):
216
208
return NotBranchError(url, message)
210
'GitLab: You are not allowed to push code to protected branches '
212
return PermissionDenied(url, message)
217
213
m = re.match(r'Permission to ([^ ]+) denied to ([^ ]+)\.', message)
219
215
return PermissionDenied(m.group(1), 'denied to %s' % m.group(2))
221
217
return RemoteGitError(message)
220
def parse_git_hangup(url, e):
221
"""Parse the error lines from a git servers stderr on hangup.
223
:param url: URL of the remote repository
224
:param e: A HangupException
226
stderr_lines = getattr(e, 'stderr_lines', None)
229
if all(line.startswith(b'remote: ') for line in stderr_lines):
231
line[len(b'remote: '):] for line in stderr_lines]
232
interesting_lines = [
233
line for line in stderr_lines
234
if line and line.replace(b'=', b'')]
235
if len(interesting_lines) == 1:
236
interesting_line = interesting_lines[0]
237
return parse_git_error(
238
url, interesting_line.decode('utf-8', 'surrogateescape'))
239
return RemoteGitError(
240
b'\n'.join(stderr_lines).decode('utf-8', 'surrogateescape'))
224
243
class GitSmartTransport(Transport):
226
245
def __init__(self, url, _client=None):
418
437
format=(format.encode('ascii') if format else None),
420
prefix=(prefix.encode('utf-8') if prefix else None))
439
prefix=(encode_git_path(prefix) if prefix else None))
440
except HangupException as e:
441
raise parse_git_hangup(self.transport.external_url(), e)
421
442
except GitProtocolError as e:
422
443
raise parse_git_error(self.transport.external_url(), e)
440
461
self._refs = remote_refs_dict_to_container(
441
462
result.refs, result.symrefs)
464
except HangupException as e:
465
raise parse_git_hangup(self.transport.external_url(), e)
443
466
except GitProtocolError as e:
444
467
raise parse_git_error(self.transport.external_url(), e)
461
484
return self._client.send_pack(
462
485
self._client_path, get_changed_refs_wrapper,
463
486
generate_pack_data, progress)
487
except HangupException as e:
488
raise parse_git_hangup(self.transport.external_url(), e)
464
489
except GitProtocolError as e:
465
490
raise parse_git_error(self.transport.external_url(), e)
492
517
def generate_pack_data(have, want, ofs_delta=False):
493
518
return pack_objects_to_data([])
494
self.send_pack(get_changed_refs, generate_pack_data)
519
result = self.send_pack(get_changed_refs, generate_pack_data)
520
if result is not None and not isinstance(result, dict):
521
error = result.ref_status.get(refname)
523
raise RemoteGitError(error)
497
526
def user_url(self):
561
590
# No revision supplied by the user, default to the branch
563
592
revision_id = source.last_revision()
594
if not source.repository.has_revision(revision_id):
595
raise NoSuchRevision(source, revision_id)
565
597
push_result = GitPushResult()
566
598
push_result.workingtree_updated = None
570
602
push_result.branch_push_result = None
571
603
repo = self.find_repository()
572
604
refname = self._get_selected_ref(name)
573
ref_chain, old_sha = self.get_refs_container().follow(refname)
575
actual_refname = ref_chain[-1]
606
ref_chain, old_sha = self.get_refs_container().follow(refname)
607
except NotBranchError:
577
608
actual_refname = refname
612
actual_refname = ref_chain[-1]
614
actual_refname = refname
578
615
if isinstance(source, GitBranch) and lossy:
579
616
raise errors.LossyPushToSameVCS(source.controldir, self)
580
617
source_store = get_object_store(source.repository)
594
631
raise errors.NoRoundtrippingSupport(
595
632
source, self.open_branch(name=name, nascent_ok=True))
596
633
if not overwrite:
634
old_sha = remote_refs.get(actual_refname)
597
635
if remote_divergence(old_sha, new_sha, source_store):
598
636
raise DivergedBranches(
599
637
source, self.open_branch(name, nascent_ok=True))
600
638
ret[actual_refname] = new_sha
602
for tagname, revid in viewitems(source.tags.get_tag_dict()):
640
for tagname, revid in source.tags.get_tag_dict().items():
603
641
if tag_selector and not tag_selector(tagname):
613
651
new_sha = repo.lookup_bzr_revision_id(revid)[0]
614
652
except errors.NoSuchRevision:
655
if not source.repository.has_revision(revid):
616
657
ret[tag_name_to_ref(tagname)] = new_sha
618
659
with source_store.lock_read():
620
generate_pack_data = source_store.generate_lossy_pack_data
622
generate_pack_data = source_store.generate_pack_data
623
new_refs = self.send_pack(get_changed_refs, generate_pack_data)
660
def generate_pack_data(have, want, progress=None,
662
git_repo = getattr(source.repository, '_git', None)
664
shallow = git_repo.get_shallow()
668
return source_store.generate_lossy_pack_data(
669
have, want, shallow=shallow,
670
progress=progress, ofs_delta=ofs_delta)
672
return source_store.generate_pack_data(
673
have, want, shallow=shallow,
674
progress=progress, ofs_delta=ofs_delta)
676
return source_store.generate_pack_data(
677
have, want, progress=progress, ofs_delta=ofs_delta)
678
dw_result = self.send_pack(get_changed_refs, generate_pack_data)
679
if not isinstance(dw_result, dict):
680
new_refs = dw_result.refs
681
error = dw_result.ref_status.get(actual_refname)
683
raise RemoteGitError(error)
684
for ref, error in dw_result.ref_status.items():
686
trace.warning('unable to open ref %s: %s',
688
else: # dulwich < 0.20.4
624
690
push_result.new_revid = repo.lookup_foreign_revision_id(
625
691
new_refs[actual_refname])
626
692
if old_sha is not None:
882
949
def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
884
952
fd, path = tempfile.mkstemp(suffix=".pack")
886
954
self.fetch_pack(determine_wants, graph_walker,
940
1008
def generate_pack_data(have, want, ofs_delta=False):
941
1009
return pack_objects_to_data([])
942
self.repository.send_pack(get_changed_refs, generate_pack_data)
1010
result = self.repository.send_pack(
1011
get_changed_refs, generate_pack_data)
1012
if result and not isinstance(result, dict):
1013
error = result.ref_status.get(ref)
1015
raise RemoteGitError(error)
945
1018
class RemoteGitBranch(GitBranch):
1009
1082
if peeled is None:
1010
1083
# Let's just hope it's a commit
1011
1084
peeled = unpeeled
1012
if not isinstance(tag_name, text_type):
1085
if not isinstance(tag_name, str):
1013
1086
raise TypeError(tag_name)
1014
1087
yield (ref_name, tag_name, peeled, unpeeled)
1023
1096
return {self.ref: sha}
1024
1097
def generate_pack_data(have, want, ofs_delta=False):
1025
1098
return pack_objects_to_data([])
1026
self.repository.send_pack(get_changed_refs, generate_pack_data)
1099
result = self.repository.send_pack(
1100
get_changed_refs, generate_pack_data)
1101
if result is not None and not isinstance(result, dict):
1102
error = result.ref_status.get(self.ref)
1104
raise RemoteGitError(error)
1027
1105
self._sha = sha