test_client.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. # test_client.py -- Tests for the git protocol, client side
  2. # Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; version 2
  7. # or (at your option) any later version of the License.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. # MA 02110-1301, USA.
  18. from contextlib import closing
  19. from io import BytesIO
  20. import sys
  21. import shutil
  22. import tempfile
  23. from dulwich import (
  24. client,
  25. )
  26. from dulwich.client import (
  27. LocalGitClient,
  28. TraditionalGitClient,
  29. TCPGitClient,
  30. SubprocessGitClient,
  31. SSHGitClient,
  32. HttpGitClient,
  33. ReportStatusParser,
  34. SendPackError,
  35. UpdateRefsError,
  36. get_transport_and_path,
  37. get_transport_and_path_from_url,
  38. )
  39. from dulwich.tests import (
  40. TestCase,
  41. )
  42. from dulwich.protocol import (
  43. TCP_GIT_PORT,
  44. Protocol,
  45. )
  46. from dulwich.pack import (
  47. write_pack_objects,
  48. )
  49. from dulwich.objects import (
  50. Commit,
  51. Tree
  52. )
  53. from dulwich.repo import (
  54. MemoryRepo,
  55. Repo,
  56. )
  57. from dulwich.tests import skipIf
  58. from dulwich.tests.utils import (
  59. open_repo,
  60. tear_down_repo,
  61. )
  62. class DummyClient(TraditionalGitClient):
  63. def __init__(self, can_read, read, write):
  64. self.can_read = can_read
  65. self.read = read
  66. self.write = write
  67. TraditionalGitClient.__init__(self)
  68. def _connect(self, service, path):
  69. return Protocol(self.read, self.write), self.can_read
  70. # TODO(durin42): add unit-level tests of GitClient
  71. class GitClientTests(TestCase):
  72. def setUp(self):
  73. super(GitClientTests, self).setUp()
  74. self.rout = BytesIO()
  75. self.rin = BytesIO()
  76. self.client = DummyClient(lambda x: True, self.rin.read,
  77. self.rout.write)
  78. def test_caps(self):
  79. self.assertEqual(set([b'multi_ack', b'side-band-64k', b'ofs-delta',
  80. b'thin-pack', b'multi_ack_detailed']),
  81. set(self.client._fetch_capabilities))
  82. self.assertEqual(set([b'ofs-delta', b'report-status', b'side-band-64k']),
  83. set(self.client._send_capabilities))
  84. def test_archive_ack(self):
  85. self.rin.write(
  86. b'0009NACK\n'
  87. b'0000')
  88. self.rin.seek(0)
  89. self.client.archive(b'bla', b'HEAD', None, None)
  90. self.assertEqual(self.rout.getvalue(), b'0011argument HEAD0000')
  91. def test_fetch_empty(self):
  92. self.rin.write(b'0000')
  93. self.rin.seek(0)
  94. self.client.fetch_pack(b'/', lambda heads: [], None, None)
  95. def test_fetch_pack_none(self):
  96. self.rin.write(
  97. b'008855dcc6bf963f922e1ed5c4bbaaefcfacef57b1d7 HEAD.multi_ack '
  98. b'thin-pack side-band side-band-64k ofs-delta shallow no-progress '
  99. b'include-tag\n'
  100. b'0000')
  101. self.rin.seek(0)
  102. self.client.fetch_pack(b'bla', lambda heads: [], None, None, None)
  103. self.assertEqual(self.rout.getvalue(), b'0000')
  104. def test_send_pack_no_sideband64k_with_update_ref_error(self):
  105. # No side-bank-64k reported by server shouldn't try to parse
  106. # side band data
  107. pkts = [b'55dcc6bf963f922e1ed5c4bbaaefcfacef57b1d7 capabilities^{}'
  108. b'\x00 report-status delete-refs ofs-delta\n',
  109. b'',
  110. b"unpack ok",
  111. b"ng refs/foo/bar pre-receive hook declined",
  112. b'']
  113. for pkt in pkts:
  114. if pkt == b'':
  115. self.rin.write(b"0000")
  116. else:
  117. self.rin.write(("%04x" % (len(pkt)+4)).encode('ascii') + pkt)
  118. self.rin.seek(0)
  119. tree = Tree()
  120. commit = Commit()
  121. commit.tree = tree
  122. commit.parents = []
  123. commit.author = commit.committer = b'test user'
  124. commit.commit_time = commit.author_time = 1174773719
  125. commit.commit_timezone = commit.author_timezone = 0
  126. commit.encoding = b'UTF-8'
  127. commit.message = b'test message'
  128. def determine_wants(refs):
  129. return {b'refs/foo/bar': commit.id, }
  130. def generate_pack_contents(have, want):
  131. return [(commit, None), (tree, ''), ]
  132. self.assertRaises(UpdateRefsError,
  133. self.client.send_pack, "blah",
  134. determine_wants, generate_pack_contents)
  135. def test_send_pack_none(self):
  136. self.rin.write(
  137. b'0078310ca9477129b8586fa2afc779c1f57cf64bba6c '
  138. b'refs/heads/master\x00 report-status delete-refs '
  139. b'side-band-64k quiet ofs-delta\n'
  140. b'0000')
  141. self.rin.seek(0)
  142. def determine_wants(refs):
  143. return {
  144. b'refs/heads/master': b'310ca9477129b8586fa2afc779c1f57cf64bba6c'
  145. }
  146. def generate_pack_contents(have, want):
  147. return {}
  148. self.client.send_pack(b'/', determine_wants, generate_pack_contents)
  149. self.assertEqual(self.rout.getvalue(), b'0000')
  150. def test_send_pack_delete_only(self):
  151. self.rin.write(
  152. b'0063310ca9477129b8586fa2afc779c1f57cf64bba6c '
  153. b'refs/heads/master\x00report-status delete-refs ofs-delta\n'
  154. b'0000000eunpack ok\n'
  155. b'0019ok refs/heads/master\n'
  156. b'0000')
  157. self.rin.seek(0)
  158. def determine_wants(refs):
  159. return {b'refs/heads/master': b'0' * 40}
  160. def generate_pack_contents(have, want):
  161. return {}
  162. self.client.send_pack(b'/', determine_wants, generate_pack_contents)
  163. self.assertIn(
  164. self.rout.getvalue(),
  165. [b'007f310ca9477129b8586fa2afc779c1f57cf64bba6c '
  166. b'0000000000000000000000000000000000000000 '
  167. b'refs/heads/master\x00report-status ofs-delta0000',
  168. b'007f310ca9477129b8586fa2afc779c1f57cf64bba6c '
  169. b'0000000000000000000000000000000000000000 '
  170. b'refs/heads/master\x00ofs-delta report-status0000'])
  171. def test_send_pack_new_ref_only(self):
  172. self.rin.write(
  173. b'0063310ca9477129b8586fa2afc779c1f57cf64bba6c '
  174. b'refs/heads/master\x00report-status delete-refs ofs-delta\n'
  175. b'0000000eunpack ok\n'
  176. b'0019ok refs/heads/blah12\n'
  177. b'0000')
  178. self.rin.seek(0)
  179. def determine_wants(refs):
  180. return {
  181. b'refs/heads/blah12':
  182. b'310ca9477129b8586fa2afc779c1f57cf64bba6c',
  183. b'refs/heads/master': b'310ca9477129b8586fa2afc779c1f57cf64bba6c'
  184. }
  185. def generate_pack_contents(have, want):
  186. return {}
  187. f = BytesIO()
  188. write_pack_objects(f, {})
  189. self.client.send_pack('/', determine_wants, generate_pack_contents)
  190. self.assertIn(
  191. self.rout.getvalue(),
  192. [b'007f0000000000000000000000000000000000000000 '
  193. b'310ca9477129b8586fa2afc779c1f57cf64bba6c '
  194. b'refs/heads/blah12\x00report-status ofs-delta0000' +
  195. f.getvalue(),
  196. b'007f0000000000000000000000000000000000000000 '
  197. b'310ca9477129b8586fa2afc779c1f57cf64bba6c '
  198. b'refs/heads/blah12\x00ofs-delta report-status0000' +
  199. f.getvalue()])
  200. def test_send_pack_new_ref(self):
  201. self.rin.write(
  202. b'0064310ca9477129b8586fa2afc779c1f57cf64bba6c '
  203. b'refs/heads/master\x00 report-status delete-refs ofs-delta\n'
  204. b'0000000eunpack ok\n'
  205. b'0019ok refs/heads/blah12\n'
  206. b'0000')
  207. self.rin.seek(0)
  208. tree = Tree()
  209. commit = Commit()
  210. commit.tree = tree
  211. commit.parents = []
  212. commit.author = commit.committer = b'test user'
  213. commit.commit_time = commit.author_time = 1174773719
  214. commit.commit_timezone = commit.author_timezone = 0
  215. commit.encoding = b'UTF-8'
  216. commit.message = b'test message'
  217. def determine_wants(refs):
  218. return {
  219. b'refs/heads/blah12': commit.id,
  220. b'refs/heads/master': b'310ca9477129b8586fa2afc779c1f57cf64bba6c'
  221. }
  222. def generate_pack_contents(have, want):
  223. return [(commit, None), (tree, b''), ]
  224. f = BytesIO()
  225. write_pack_objects(f, generate_pack_contents(None, None))
  226. self.client.send_pack(b'/', determine_wants, generate_pack_contents)
  227. self.assertIn(
  228. self.rout.getvalue(),
  229. [b'007f0000000000000000000000000000000000000000 ' + commit.id +
  230. b' refs/heads/blah12\x00report-status ofs-delta0000' + f.getvalue(),
  231. b'007f0000000000000000000000000000000000000000 ' + commit.id +
  232. b' refs/heads/blah12\x00ofs-delta report-status0000' + f.getvalue()])
  233. def test_send_pack_no_deleteref_delete_only(self):
  234. pkts = [b'310ca9477129b8586fa2afc779c1f57cf64bba6c refs/heads/master'
  235. b'\x00 report-status ofs-delta\n',
  236. b'',
  237. b'']
  238. for pkt in pkts:
  239. if pkt == b'':
  240. self.rin.write(b"0000")
  241. else:
  242. self.rin.write(("%04x" % (len(pkt)+4)).encode('ascii') + pkt)
  243. self.rin.seek(0)
  244. def determine_wants(refs):
  245. return {b'refs/heads/master': b'0' * 40}
  246. def generate_pack_contents(have, want):
  247. return {}
  248. self.assertRaises(UpdateRefsError,
  249. self.client.send_pack, b"/",
  250. determine_wants, generate_pack_contents)
  251. self.assertEqual(self.rout.getvalue(), b'0000')
  252. class TestGetTransportAndPath(TestCase):
  253. def test_tcp(self):
  254. c, path = get_transport_and_path('git://foo.com/bar/baz')
  255. self.assertTrue(isinstance(c, TCPGitClient))
  256. self.assertEqual('foo.com', c._host)
  257. self.assertEqual(TCP_GIT_PORT, c._port)
  258. self.assertEqual('/bar/baz', path)
  259. def test_tcp_port(self):
  260. c, path = get_transport_and_path('git://foo.com:1234/bar/baz')
  261. self.assertTrue(isinstance(c, TCPGitClient))
  262. self.assertEqual('foo.com', c._host)
  263. self.assertEqual(1234, c._port)
  264. self.assertEqual('/bar/baz', path)
  265. def test_ssh_explicit(self):
  266. c, path = get_transport_and_path('git+ssh://foo.com/bar/baz')
  267. self.assertTrue(isinstance(c, SSHGitClient))
  268. self.assertEqual('foo.com', c.host)
  269. self.assertEqual(None, c.port)
  270. self.assertEqual(None, c.username)
  271. self.assertEqual('bar/baz', path)
  272. def test_ssh_port_explicit(self):
  273. c, path = get_transport_and_path(
  274. 'git+ssh://foo.com:1234/bar/baz')
  275. self.assertTrue(isinstance(c, SSHGitClient))
  276. self.assertEqual('foo.com', c.host)
  277. self.assertEqual(1234, c.port)
  278. self.assertEqual('bar/baz', path)
  279. def test_ssh_abspath_explicit(self):
  280. c, path = get_transport_and_path('git+ssh://foo.com//bar/baz')
  281. self.assertTrue(isinstance(c, SSHGitClient))
  282. self.assertEqual('foo.com', c.host)
  283. self.assertEqual(None, c.port)
  284. self.assertEqual(None, c.username)
  285. self.assertEqual('/bar/baz', path)
  286. def test_ssh_port_abspath_explicit(self):
  287. c, path = get_transport_and_path(
  288. 'git+ssh://foo.com:1234//bar/baz')
  289. self.assertTrue(isinstance(c, SSHGitClient))
  290. self.assertEqual('foo.com', c.host)
  291. self.assertEqual(1234, c.port)
  292. self.assertEqual('/bar/baz', path)
  293. def test_ssh_implicit(self):
  294. c, path = get_transport_and_path('foo:/bar/baz')
  295. self.assertTrue(isinstance(c, SSHGitClient))
  296. self.assertEqual('foo', c.host)
  297. self.assertEqual(None, c.port)
  298. self.assertEqual(None, c.username)
  299. self.assertEqual('/bar/baz', path)
  300. def test_ssh_host(self):
  301. c, path = get_transport_and_path('foo.com:/bar/baz')
  302. self.assertTrue(isinstance(c, SSHGitClient))
  303. self.assertEqual('foo.com', c.host)
  304. self.assertEqual(None, c.port)
  305. self.assertEqual(None, c.username)
  306. self.assertEqual('/bar/baz', path)
  307. def test_ssh_user_host(self):
  308. c, path = get_transport_and_path('user@foo.com:/bar/baz')
  309. self.assertTrue(isinstance(c, SSHGitClient))
  310. self.assertEqual('foo.com', c.host)
  311. self.assertEqual(None, c.port)
  312. self.assertEqual('user', c.username)
  313. self.assertEqual('/bar/baz', path)
  314. def test_ssh_relpath(self):
  315. c, path = get_transport_and_path('foo:bar/baz')
  316. self.assertTrue(isinstance(c, SSHGitClient))
  317. self.assertEqual('foo', c.host)
  318. self.assertEqual(None, c.port)
  319. self.assertEqual(None, c.username)
  320. self.assertEqual('bar/baz', path)
  321. def test_ssh_host_relpath(self):
  322. c, path = get_transport_and_path('foo.com:bar/baz')
  323. self.assertTrue(isinstance(c, SSHGitClient))
  324. self.assertEqual('foo.com', c.host)
  325. self.assertEqual(None, c.port)
  326. self.assertEqual(None, c.username)
  327. self.assertEqual('bar/baz', path)
  328. def test_ssh_user_host_relpath(self):
  329. c, path = get_transport_and_path('user@foo.com:bar/baz')
  330. self.assertTrue(isinstance(c, SSHGitClient))
  331. self.assertEqual('foo.com', c.host)
  332. self.assertEqual(None, c.port)
  333. self.assertEqual('user', c.username)
  334. self.assertEqual('bar/baz', path)
  335. def test_local(self):
  336. c, path = get_transport_and_path('foo.bar/baz')
  337. self.assertTrue(isinstance(c, LocalGitClient))
  338. self.assertEqual('foo.bar/baz', path)
  339. @skipIf(sys.platform != 'win32', 'Behaviour only happens on windows.')
  340. def test_local_abs_windows_path(self):
  341. c, path = get_transport_and_path('C:\\foo.bar\\baz')
  342. self.assertTrue(isinstance(c, LocalGitClient))
  343. self.assertEqual('C:\\foo.bar\\baz', path)
  344. def test_error(self):
  345. # Need to use a known urlparse.uses_netloc URL scheme to get the
  346. # expected parsing of the URL on Python versions less than 2.6.5
  347. c, path = get_transport_and_path('prospero://bar/baz')
  348. self.assertTrue(isinstance(c, SSHGitClient))
  349. def test_http(self):
  350. url = 'https://github.com/jelmer/dulwich'
  351. c, path = get_transport_and_path(url)
  352. self.assertTrue(isinstance(c, HttpGitClient))
  353. self.assertEqual('/jelmer/dulwich', path)
  354. class TestGetTransportAndPathFromUrl(TestCase):
  355. def test_tcp(self):
  356. c, path = get_transport_and_path_from_url('git://foo.com/bar/baz')
  357. self.assertTrue(isinstance(c, TCPGitClient))
  358. self.assertEqual('foo.com', c._host)
  359. self.assertEqual(TCP_GIT_PORT, c._port)
  360. self.assertEqual('/bar/baz', path)
  361. def test_tcp_port(self):
  362. c, path = get_transport_and_path_from_url('git://foo.com:1234/bar/baz')
  363. self.assertTrue(isinstance(c, TCPGitClient))
  364. self.assertEqual('foo.com', c._host)
  365. self.assertEqual(1234, c._port)
  366. self.assertEqual('/bar/baz', path)
  367. def test_ssh_explicit(self):
  368. c, path = get_transport_and_path_from_url('git+ssh://foo.com/bar/baz')
  369. self.assertTrue(isinstance(c, SSHGitClient))
  370. self.assertEqual('foo.com', c.host)
  371. self.assertEqual(None, c.port)
  372. self.assertEqual(None, c.username)
  373. self.assertEqual('bar/baz', path)
  374. def test_ssh_port_explicit(self):
  375. c, path = get_transport_and_path_from_url(
  376. 'git+ssh://foo.com:1234/bar/baz')
  377. self.assertTrue(isinstance(c, SSHGitClient))
  378. self.assertEqual('foo.com', c.host)
  379. self.assertEqual(1234, c.port)
  380. self.assertEqual('bar/baz', path)
  381. def test_ssh_abspath_explicit(self):
  382. c, path = get_transport_and_path_from_url('git+ssh://foo.com//bar/baz')
  383. self.assertTrue(isinstance(c, SSHGitClient))
  384. self.assertEqual('foo.com', c.host)
  385. self.assertEqual(None, c.port)
  386. self.assertEqual(None, c.username)
  387. self.assertEqual('/bar/baz', path)
  388. def test_ssh_port_abspath_explicit(self):
  389. c, path = get_transport_and_path_from_url(
  390. 'git+ssh://foo.com:1234//bar/baz')
  391. self.assertTrue(isinstance(c, SSHGitClient))
  392. self.assertEqual('foo.com', c.host)
  393. self.assertEqual(1234, c.port)
  394. self.assertEqual('/bar/baz', path)
  395. def test_ssh_host_relpath(self):
  396. self.assertRaises(ValueError, get_transport_and_path_from_url,
  397. 'foo.com:bar/baz')
  398. def test_ssh_user_host_relpath(self):
  399. self.assertRaises(ValueError, get_transport_and_path_from_url,
  400. 'user@foo.com:bar/baz')
  401. def test_local_path(self):
  402. self.assertRaises(ValueError, get_transport_and_path_from_url,
  403. 'foo.bar/baz')
  404. def test_error(self):
  405. # Need to use a known urlparse.uses_netloc URL scheme to get the
  406. # expected parsing of the URL on Python versions less than 2.6.5
  407. self.assertRaises(ValueError, get_transport_and_path_from_url,
  408. 'prospero://bar/baz')
  409. def test_http(self):
  410. url = 'https://github.com/jelmer/dulwich'
  411. c, path = get_transport_and_path_from_url(url)
  412. self.assertTrue(isinstance(c, HttpGitClient))
  413. self.assertEqual('/jelmer/dulwich', path)
  414. def test_file(self):
  415. c, path = get_transport_and_path_from_url('file:///home/jelmer/foo')
  416. self.assertTrue(isinstance(c, LocalGitClient))
  417. self.assertEqual('/home/jelmer/foo', path)
  418. class TestSSHVendor(object):
  419. def __init__(self):
  420. self.host = None
  421. self.command = ""
  422. self.username = None
  423. self.port = None
  424. def run_command(self, host, command, username=None, port=None):
  425. self.host = host
  426. self.command = command
  427. self.username = username
  428. self.port = port
  429. class Subprocess: pass
  430. setattr(Subprocess, 'read', lambda: None)
  431. setattr(Subprocess, 'write', lambda: None)
  432. setattr(Subprocess, 'close', lambda: None)
  433. setattr(Subprocess, 'can_read', lambda: None)
  434. return Subprocess()
  435. class SSHGitClientTests(TestCase):
  436. def setUp(self):
  437. super(SSHGitClientTests, self).setUp()
  438. self.server = TestSSHVendor()
  439. self.real_vendor = client.get_ssh_vendor
  440. client.get_ssh_vendor = lambda: self.server
  441. self.client = SSHGitClient('git.samba.org')
  442. def tearDown(self):
  443. super(SSHGitClientTests, self).tearDown()
  444. client.get_ssh_vendor = self.real_vendor
  445. def test_default_command(self):
  446. self.assertEqual('git-upload-pack',
  447. self.client._get_cmd_path(b'upload-pack'))
  448. def test_alternative_command_path(self):
  449. self.client.alternative_paths['upload-pack'] = (
  450. '/usr/lib/git/git-upload-pack')
  451. self.assertEqual('/usr/lib/git/git-upload-pack',
  452. self.client._get_cmd_path(b'upload-pack'))
  453. def test_connect(self):
  454. server = self.server
  455. client = self.client
  456. client.username = b"username"
  457. client.port = 1337
  458. client._connect(b"command", "/path/to/repo")
  459. self.assertEqual(b"username", server.username)
  460. self.assertEqual(1337, server.port)
  461. self.assertEqual(["git-command", "/path/to/repo"], server.command)
  462. client._connect(b"relative-command", "/~/path/to/repo")
  463. self.assertEqual(["git-relative-command", "~/path/to/repo"],
  464. server.command)
  465. class ReportStatusParserTests(TestCase):
  466. def test_invalid_pack(self):
  467. parser = ReportStatusParser()
  468. parser.handle_packet(b"unpack error - foo bar")
  469. parser.handle_packet(b"ok refs/foo/bar")
  470. parser.handle_packet(None)
  471. self.assertRaises(SendPackError, parser.check)
  472. def test_update_refs_error(self):
  473. parser = ReportStatusParser()
  474. parser.handle_packet(b"unpack ok")
  475. parser.handle_packet(b"ng refs/foo/bar need to pull")
  476. parser.handle_packet(None)
  477. self.assertRaises(UpdateRefsError, parser.check)
  478. def test_ok(self):
  479. parser = ReportStatusParser()
  480. parser.handle_packet(b"unpack ok")
  481. parser.handle_packet(b"ok refs/foo/bar")
  482. parser.handle_packet(None)
  483. parser.check()
  484. class LocalGitClientTests(TestCase):
  485. def test_fetch_into_empty(self):
  486. c = LocalGitClient()
  487. t = MemoryRepo()
  488. s = open_repo('a.git')
  489. self.addCleanup(tear_down_repo, s)
  490. self.assertEqual(s.get_refs(), c.fetch(s.path, t))
  491. def test_fetch_empty(self):
  492. c = LocalGitClient()
  493. s = open_repo('a.git')
  494. self.addCleanup(tear_down_repo, s)
  495. out = BytesIO()
  496. walker = {}
  497. c.fetch_pack(s.path, lambda heads: [], graph_walker=walker,
  498. pack_data=out.write)
  499. self.assertEqual(b"PACK\x00\x00\x00\x02\x00\x00\x00\x00\x02\x9d\x08"
  500. b"\x82;\xd8\xa8\xea\xb5\x10\xadj\xc7\\\x82<\xfd>\xd3\x1e", out.getvalue())
  501. def test_fetch_pack_none(self):
  502. c = LocalGitClient()
  503. s = open_repo('a.git')
  504. self.addCleanup(tear_down_repo, s)
  505. out = BytesIO()
  506. walker = MemoryRepo().get_graph_walker()
  507. c.fetch_pack(s.path,
  508. lambda heads: [b"a90fa2d900a17e99b433217e988c4eb4a2e9a097"],
  509. graph_walker=walker, pack_data=out.write)
  510. # Hardcoding is not ideal, but we'll fix that some other day..
  511. self.assertTrue(out.getvalue().startswith(b'PACK\x00\x00\x00\x02\x00\x00\x00\x07'))
  512. def test_send_pack_without_changes(self):
  513. local = open_repo('a.git')
  514. self.addCleanup(tear_down_repo, local)
  515. target = open_repo('a.git')
  516. self.addCleanup(tear_down_repo, target)
  517. self.send_and_verify(b"master", local, target)
  518. def test_send_pack_with_changes(self):
  519. local = open_repo('a.git')
  520. self.addCleanup(tear_down_repo, local)
  521. target_path = tempfile.mkdtemp()
  522. self.addCleanup(shutil.rmtree, target_path)
  523. with closing(Repo.init_bare(target_path)) as target:
  524. self.send_and_verify(b"master", local, target)
  525. def send_and_verify(self, branch, local, target):
  526. client = LocalGitClient()
  527. ref_name = b"refs/heads/" + branch
  528. new_refs = client.send_pack(target.path,
  529. lambda _: { ref_name: local.refs[ref_name] },
  530. local.object_store.generate_pack_contents)
  531. self.assertEqual(local.refs[ref_name], new_refs[ref_name])
  532. for name, sha in new_refs.items():
  533. self.assertEqual(new_refs[name], target.refs[name])
  534. obj_local = local.get_object(new_refs[ref_name])
  535. obj_target = target.get_object(new_refs[ref_name])
  536. self.assertEqual(obj_local, obj_target)