def test_ssh_port_abspath_explicit(self): c, path = get_transport_and_path_from_url( 'git+ssh://foo.com:1234//bar/baz') self.assertTrue(isinstance(c, SSHGitClient)) self.assertEqual('foo.com', c.host) self.assertEqual(1234, c.port) self.assertEqual('/bar/baz', path)
def test_ssh_explicit(self): c, path = get_transport_and_path_from_url('git+ssh://foo.com/bar/baz') self.assertTrue(isinstance(c, SSHGitClient)) self.assertEqual('foo.com', c.host) self.assertEqual(None, c.port) self.assertEqual(None, c.username) self.assertEqual('/bar/baz', path)
def test_ssh_explicit(self): c, path = get_transport_and_path_from_url('git+ssh://foo.com/bar/baz') self.assertTrue(isinstance(c, SSHGitClient)) self.assertEqual('foo.com', c.host) self.assertEqual(None, c.port) self.assertEqual(None, c.username) self.assertEqual('bar/baz', path)
def test_ssh_port_homepath(self): c, path = get_transport_and_path_from_url( 'git+ssh://foo.com:1234/~/bar/baz') self.assertTrue(isinstance(c, SSHGitClient)) self.assertEqual('foo.com', c.host) self.assertEqual(1234, c.port) self.assertEqual('/~/bar/baz', path)
def get_git_branches(git_url): from dulwich.client import get_transport_and_path_from_url t, p = get_transport_and_path_from_url(git_url) branches = t.get_refs(p) res = {} for key, value in branches.items(): res[key.decode("utf-8")] = value.decode("utf-8") return res
def test_http_port(self): if '__pypy__' in sys.modules: self.skipTest('urllib3 not available for pypy in debian') url = 'https://github.com:9090/jelmer/dulwich' c, path = get_transport_and_path_from_url(url) self.assertEqual('https://github.com:9090', c.get_url(b'/')) self.assertTrue(isinstance(c, HttpGitClient)) self.assertEqual('/jelmer/dulwich', path)
def push(self, progress_func, username=None, password=None): """ This pushes updates to a remote repository. This code has been take from dulwich.porcelain. """ if not self.remote_url: return logger.info("Pushing to '%s' ..." % (self.remote_url)) # Get the client and path client, path = get_transport_and_path_from_url(self.remote_url) if password: client.ssh_vendor.ssh_kwargs["password"] = password selected_refs = [] refspecs = b"+refs/heads/master" def update_refs(refs): selected_refs.extend( parse_reftuples(self.git_repository.refs, refs, refspecs)) new_refs = {} # TODO: Handle selected_refs == {None: None} for (lh, rh, force) in selected_refs: if lh is None: new_refs[rh] = ZERO_SHA else: new_refs[rh] = self.git_repository.refs[lh] return new_refs try: client.send_pack( path.encode('utf-8'), update_refs, self.git_repository.object_store.generate_pack_contents, progress=progress_func) progress_func(b"Push successful.\n") except (UpdateRefsError, SendPackError) as e: progress_func(b"Push failed -> " + e.message.encode('utf8') + b"\n")
def test_file(self): c, path = get_transport_and_path_from_url('file:///home/jelmer/foo') self.assertTrue(isinstance(c, LocalGitClient)) self.assertEqual('/home/jelmer/foo', path)
def test_http(self): url = 'https://github.com/jelmer/dulwich' c, path = get_transport_and_path_from_url(url) self.assertTrue(isinstance(c, HttpGitClient)) self.assertEqual('/jelmer/dulwich', path)
def test_tcp_port(self): c, path = get_transport_and_path_from_url('git://foo.com:1234/bar/baz') self.assertTrue(isinstance(c, TCPGitClient)) self.assertEqual('foo.com', c._host) self.assertEqual(1234, c._port) self.assertEqual('/bar/baz', path)
def test_file(self): c, path = get_transport_and_path_from_url('file:///home/jelmer/foo') self.assertTrue(isinstance(c, SubprocessGitClient)) self.assertEqual('/home/jelmer/foo', path)
#!/usr/bin/python3 from dulwich.client import get_transport_and_path_from_url from dulwich.objects import ZERO_SHA from dulwich.pack import pack_objects_to_data import argparse parser = argparse.ArgumentParser() parser.add_argument('url', type=str) parser.add_argument('old_ref', type=str) parser.add_argument('new_ref', type=str) args = parser.parse_args() client, path = get_transport_and_path_from_url(args.url) def generate_pack_data(*args, **kwargs): return pack_objects_to_data([]) def update_refs(refs): sha = refs[args.old_ref.encode('utf-8')] return { args.old_ref.encode('utf-8'): ZERO_SHA, args.new_ref.encode('utf-8'): sha } client.send_pack(path, update_refs, generate_pack_data) print("Renamed %s to %s" % (args.old_ref, args.new_ref))
def get_git_branches(git_url): from dulwich.client import get_transport_and_path_from_url t, p = get_transport_and_path_from_url(git_url) branches = t.get_refs(p) return branches