test_client.py 22 KB

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