167
160
:param url: Git URL
168
161
:return: Tuple with host, port, username, path.
170
parsed_url = urlparse.urlparse(url)
171
path = urlparse.unquote(parsed_url.path)
163
(scheme, netloc, loc, _, _) = urlparse.urlsplit(url)
164
path = urlparse.unquote(loc)
172
165
if path.startswith("/~"):
174
return ((parsed_url.hostname or '', parsed_url.port, parsed_url.username, path))
167
(username, hostport) = splituser(netloc)
168
(host, port) = splitnport(hostport, None)
169
return (host, port, username, path)
177
172
class RemoteGitError(BzrError):
202
197
message.endswith(' not found.'))):
203
198
return NotBranchError(url, message)
204
199
if message == "HEAD failed to update":
205
base_url = urlutils.strip_segment_parameters(url)
200
base_url, _ = urlutils.split_segment_parameters(url)
206
201
return HeadUpdateFailed(base_url)
207
202
if message.startswith('access denied or repository not exported:'):
208
extra, path = message.split(':', 1)
209
return PermissionDenied(path.strip(), extra)
203
extra, path = message.split(': ', 1)
204
return PermissionDenied(path, extra)
210
205
if message.endswith('You are not allowed to push code to this project.'):
211
206
return PermissionDenied(url, message)
212
207
if message.endswith(' does not appear to be a git repository'):
213
208
return NotBranchError(url, message)
214
if re.match('(.+) is not a valid repository name',
215
message.splitlines()[0]):
216
return NotBranchError(url, message)
217
209
m = re.match(r'Permission to ([^ ]+) denied to ([^ ]+)\.', message)
219
211
return PermissionDenied(m.group(1), 'denied to %s' % m.group(2))
456
def get_changed_refs_wrapper(remote_refs):
457
if self._refs is not None:
458
update_refs_container(self._refs, remote_refs)
459
return get_changed_refs(remote_refs)
448
def get_changed_refs_wrapper(refs):
449
# TODO(jelmer): This drops symref information
450
self._refs = remote_refs_dict_to_container(refs)
451
return get_changed_refs(refs)
461
453
return self._client.send_pack(
462
454
self._client_path, get_changed_refs_wrapper,
472
464
refname = self._get_selected_ref(name, ref)
473
465
if refname != b'HEAD' and refname in self.get_refs_container():
474
466
raise AlreadyBranchError(self.user_url)
475
ref_chain, unused_sha = self.get_refs_container().follow(
476
self._get_selected_ref(name))
477
if ref_chain and ref_chain[0] == b'HEAD':
478
refname = ref_chain[1]
467
if refname in self.get_refs_container():
468
ref_chain, unused_sha = self.get_refs_container().follow(
469
self._get_selected_ref(None))
470
if ref_chain[0] == b'HEAD':
471
refname = ref_chain[1]
479
472
repo = self.open_repository()
480
473
return RemoteGitBranch(self, repo, refname)
570
563
push_result.branch_push_result = None
571
564
repo = self.find_repository()
572
565
refname = self._get_selected_ref(name)
573
ref_chain, old_sha = self.get_refs_container().follow(refname)
575
actual_refname = ref_chain[-1]
577
actual_refname = refname
578
566
if isinstance(source, GitBranch) and lossy:
579
567
raise errors.LossyPushToSameVCS(source.controldir, self)
580
568
source_store = get_object_store(source.repository)
581
fetch_tags = source.get_config_stack().get('branch.fetch_tags')
582
def get_changed_refs(remote_refs):
583
if self._refs is not None:
584
update_refs_container(self._refs, remote_refs)
586
# TODO(jelmer): Unpeel if necessary
587
push_result.new_original_revid = revision_id
589
new_sha = source_store._lookup_revision_sha1(revision_id)
592
new_sha = repo.lookup_bzr_revision_id(revision_id)[0]
593
except errors.NoSuchRevision:
594
raise errors.NoRoundtrippingSupport(
595
source, self.open_branch(name=name, nascent_ok=True))
597
if remote_divergence(old_sha, new_sha, source_store):
598
raise DivergedBranches(
599
source, self.open_branch(name, nascent_ok=True))
600
ret[actual_refname] = new_sha
602
for tagname, revid in viewitems(source.tags.get_tag_dict()):
603
if tag_selector and not tag_selector(tagname):
607
new_sha = source_store._lookup_revision_sha1(revid)
609
if source.repository.has_revision(revid):
613
new_sha = repo.lookup_bzr_revision_id(revid)[0]
614
except errors.NoSuchRevision:
616
ret[tag_name_to_ref(tagname)] = new_sha
618
569
with source_store.lock_read():
570
def get_changed_refs(refs):
571
self._refs = remote_refs_dict_to_container(refs)
573
# TODO(jelmer): Unpeel if necessary
574
push_result.new_original_revid = revision_id
576
new_sha = source_store._lookup_revision_sha1(revision_id)
579
new_sha = repo.lookup_bzr_revision_id(revision_id)[0]
580
except errors.NoSuchRevision:
581
raise errors.NoRoundtrippingSupport(
582
source, self.open_branch(name=name, nascent_ok=True))
584
if remote_divergence(ret.get(refname), new_sha,
586
raise DivergedBranches(
587
source, self.open_branch(name, nascent_ok=True))
588
ret[refname] = new_sha
620
591
generate_pack_data = source_store.generate_lossy_pack_data
622
593
generate_pack_data = source_store.generate_pack_data
623
594
new_refs = self.send_pack(get_changed_refs, generate_pack_data)
624
595
push_result.new_revid = repo.lookup_foreign_revision_id(
625
new_refs[actual_refname])
626
if old_sha is not None:
627
push_result.old_revid = repo.lookup_foreign_revision_id(old_sha)
629
push_result.old_revid = NULL_REVISION
630
if self._refs is not None:
631
update_refs_container(self._refs, new_refs)
598
old_remote = self._refs[refname]
600
old_remote = ZERO_SHA
601
push_result.old_revid = repo.lookup_foreign_revision_id(old_remote)
602
self._refs = remote_refs_dict_to_container(new_refs)
632
603
push_result.target_branch = self.open_branch(name)
633
if old_sha is not None:
604
if old_remote != ZERO_SHA:
634
605
push_result.branch_push_result = GitBranchPushResult()
635
606
push_result.branch_push_result.source_branch = source
636
607
push_result.branch_push_result.target_branch = (
667
638
def _idx_load_or_generate(self, path):
668
639
if not os.path.exists(path):
669
with ui.ui_factory.nested_progress_bar() as pb:
640
pb = ui.ui_factory.nested_progress_bar()
670
642
def report_progress(cur, total):
671
643
pb.update("generating index", cur, total)
672
self.data.create_index(path, progress=report_progress)
644
self.data.create_index(path,
645
progress=report_progress)
673
648
return load_pack_index(path)
675
650
def __del__(self):
686
661
def __init__(self, transport, *args, **kwargs):
687
662
self.transport = transport
688
url = urlutils.URL.from_string(transport.external_url())
689
url.user = url.quoted_user = None
690
url.password = url.quoted_password = None
691
url = urlutils.strip_segment_parameters(str(url))
692
super(BzrGitHttpClient, self).__init__(url, *args, **kwargs)
663
super(BzrGitHttpClient, self).__init__(
664
transport.external_url(), *args, **kwargs)
694
666
def _http_request(self, url, headers=None, data=None,
695
667
allow_compression=False):
704
676
`redirect_location` properties, and `read` is a consumable read
705
677
method for the response data.
707
if is_github_url(url):
708
headers['User-agent'] = user_agent_for_github()
679
from breezy.transport.http._urllib2_wrappers import Request
680
headers['User-agent'] = user_agent_for_github()
709
681
headers["Pragma"] = "no-cache"
710
682
if allow_compression:
711
683
headers["Accept-Encoding"] = "gzip"
713
685
headers["Accept-Encoding"] = "identity"
715
response = self.transport.request(
716
688
('GET' if data is None else 'POST'),
719
headers=headers, retries=8)
721
if response.status == 404:
690
accepted_errors=[200, 404])
691
request.follow_redirections = True
693
response = self.transport._perform(request)
695
if response.code == 404:
722
696
raise NotGitRepository()
723
elif response.status != 200:
697
elif response.code != 200:
724
698
raise GitProtocolError("unexpected http resp %d for %s" %
725
(response.status, url))
699
(response.code, url))
727
701
# TODO: Optimization available by adding `preload_content=False` to the
728
702
# request and just passing the `read` method on instead of going via
739
713
def __init__(self, response):
740
714
self._response = response
741
self.status = response.status
715
self.status = response.code
742
716
self.content_type = response.getheader("Content-Type")
743
self.redirect_location = response._actual.geturl()
717
self.redirect_location = response.geturl()
745
719
def readlines(self):
746
720
return self._response.readlines()
723
self._response.close()
751
725
return WrapResponse(response), read
754
def _git_url_and_path_from_transport(external_url):
755
url = urlutils.strip_segment_parameters(external_url)
756
return urlparse.urlsplit(url)
759
728
class RemoteGitControlDirFormat(GitControlDirFormat):
760
729
"""The .git directory control format."""
782
747
"""Open this directory.
785
split_url = _git_url_and_path_from_transport(transport.external_url())
750
# we dont grok readonly - git isn't integrated with transport.
752
if url.startswith('readonly+'):
753
url = url[len('readonly+'):]
754
scheme = urlparse.urlsplit(transport.external_url())[0]
786
755
if isinstance(transport, GitSmartTransport):
787
756
client = transport._get_client()
788
elif split_url.scheme in ("http", "https"):
757
client_path = transport._get_path()
758
elif scheme in ("http", "https"):
789
759
client = BzrGitHttpClient(transport)
790
elif split_url.scheme in ('file', ):
760
client_path, _ = urlutils.split_segment_parameters(transport._path)
761
elif scheme == 'file':
791
762
client = dulwich.client.LocalGitClient()
763
client_path = transport.local_abspath('.')
793
765
raise NotBranchError(transport.base)
795
767
pass # TODO(jelmer): Actually probe for something
796
return RemoteGitDir(transport, self, client, split_url.path)
768
return RemoteGitDir(transport, self, client, client_path)
798
770
def get_format_description(self):
799
771
return "Remote Git Repository"
845
817
return osutils.file_iterator(f)
847
def is_versioned(self, path):
819
def is_versioned(self, path, file_id=None):
848
820
raise GitSmartRemoteNotSupported(self.is_versioned, self)
850
822
def has_filename(self, path):
851
823
raise GitSmartRemoteNotSupported(self.has_filename, self)
853
def get_file_text(self, path):
825
def get_file_text(self, path, file_id=None):
854
826
raise GitSmartRemoteNotSupported(self.get_file_text, self)
856
def list_files(self, include_root=False, from_dir=None, recursive=True):
857
raise GitSmartRemoteNotSupported(self.list_files, self)
860
829
class RemoteGitRepository(GitRepository):
862
supports_random_access = False
865
832
def user_url(self):
866
833
return self.control_url