test_client.py 24 KB

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