test_repository.py 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  1. # -*- coding: utf-8 -*-
  2. # test_repository.py -- tests for repository.py
  3. # Copyright (C) 2007 James Westby <jw+debian@jameswestby.net>
  4. #
  5. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  6. # General Public License as public by the Free Software Foundation; version 2.0
  7. # or (at your option) any later version. You can redistribute it and/or
  8. # modify it under the terms of either of these two licenses.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # You should have received a copy of the licenses; if not, see
  17. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  18. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  19. # License, Version 2.0.
  20. #
  21. """Tests for the repository."""
  22. import locale
  23. import os
  24. import stat
  25. import shutil
  26. import sys
  27. import tempfile
  28. import warnings
  29. from dulwich import errors
  30. from dulwich.object_store import (
  31. tree_lookup_path,
  32. )
  33. from dulwich import objects
  34. from dulwich.config import Config
  35. from dulwich.errors import NotGitRepository
  36. from dulwich.repo import (
  37. InvalidUserIdentity,
  38. Repo,
  39. MemoryRepo,
  40. check_user_identity,
  41. )
  42. from dulwich.tests import (
  43. TestCase,
  44. skipIf,
  45. )
  46. from dulwich.tests.utils import (
  47. open_repo,
  48. tear_down_repo,
  49. setup_warning_catcher,
  50. )
  51. missing_sha = b'b91fa4d900e17e99b433218e988c4eb4a3e9a097'
  52. class CreateRepositoryTests(TestCase):
  53. def assertFileContentsEqual(self, expected, repo, path):
  54. f = repo.get_named_file(path)
  55. if not f:
  56. self.assertEqual(expected, None)
  57. else:
  58. with f:
  59. self.assertEqual(expected, f.read())
  60. def _check_repo_contents(self, repo, expect_bare):
  61. self.assertEqual(expect_bare, repo.bare)
  62. self.assertFileContentsEqual(
  63. b'Unnamed repository', repo, 'description')
  64. self.assertFileContentsEqual(
  65. b'', repo, os.path.join('info', 'exclude'))
  66. self.assertFileContentsEqual(None, repo, 'nonexistent file')
  67. barestr = b'bare = ' + str(expect_bare).lower().encode('ascii')
  68. with repo.get_named_file('config') as f:
  69. config_text = f.read()
  70. self.assertTrue(barestr in config_text, "%r" % config_text)
  71. expect_filemode = sys.platform != 'win32'
  72. barestr = b'filemode = ' + str(expect_filemode).lower().encode('ascii')
  73. with repo.get_named_file('config') as f:
  74. config_text = f.read()
  75. self.assertTrue(barestr in config_text, "%r" % config_text)
  76. def test_create_memory(self):
  77. repo = MemoryRepo.init_bare([], {})
  78. self._check_repo_contents(repo, True)
  79. def test_create_disk_bare(self):
  80. tmp_dir = tempfile.mkdtemp()
  81. self.addCleanup(shutil.rmtree, tmp_dir)
  82. repo = Repo.init_bare(tmp_dir)
  83. self.assertEqual(tmp_dir, repo._controldir)
  84. self._check_repo_contents(repo, True)
  85. def test_create_disk_non_bare(self):
  86. tmp_dir = tempfile.mkdtemp()
  87. self.addCleanup(shutil.rmtree, tmp_dir)
  88. repo = Repo.init(tmp_dir)
  89. self.assertEqual(os.path.join(tmp_dir, '.git'), repo._controldir)
  90. self._check_repo_contents(repo, False)
  91. def test_create_disk_non_bare_mkdir(self):
  92. tmp_dir = tempfile.mkdtemp()
  93. target_dir = os.path.join(tmp_dir, "target")
  94. self.addCleanup(shutil.rmtree, tmp_dir)
  95. repo = Repo.init(target_dir, mkdir=True)
  96. self.assertEqual(os.path.join(target_dir, '.git'), repo._controldir)
  97. self._check_repo_contents(repo, False)
  98. def test_create_disk_bare_mkdir(self):
  99. tmp_dir = tempfile.mkdtemp()
  100. target_dir = os.path.join(tmp_dir, "target")
  101. self.addCleanup(shutil.rmtree, tmp_dir)
  102. repo = Repo.init_bare(target_dir, mkdir=True)
  103. self.assertEqual(target_dir, repo._controldir)
  104. self._check_repo_contents(repo, True)
  105. class MemoryRepoTests(TestCase):
  106. def test_set_description(self):
  107. r = MemoryRepo.init_bare([], {})
  108. description = b"Some description"
  109. r.set_description(description)
  110. self.assertEqual(description, r.get_description())
  111. class RepositoryRootTests(TestCase):
  112. def mkdtemp(self):
  113. return tempfile.mkdtemp()
  114. def open_repo(self, name):
  115. temp_dir = self.mkdtemp()
  116. repo = open_repo(name, temp_dir)
  117. self.addCleanup(tear_down_repo, repo)
  118. return repo
  119. def test_simple_props(self):
  120. r = self.open_repo('a.git')
  121. self.assertEqual(r.controldir(), r.path)
  122. def test_setitem(self):
  123. r = self.open_repo('a.git')
  124. r[b"refs/tags/foo"] = b'a90fa2d900a17e99b433217e988c4eb4a2e9a097'
  125. self.assertEqual(b'a90fa2d900a17e99b433217e988c4eb4a2e9a097',
  126. r[b"refs/tags/foo"].id)
  127. def test_getitem_unicode(self):
  128. r = self.open_repo('a.git')
  129. test_keys = [
  130. (b'refs/heads/master', True),
  131. (b'a90fa2d900a17e99b433217e988c4eb4a2e9a097', True),
  132. (b'11' * 19 + b'--', False),
  133. ]
  134. for k, contained in test_keys:
  135. self.assertEqual(k in r, contained)
  136. # Avoid deprecation warning under Py3.2+
  137. if getattr(self, 'assertRaisesRegex', None):
  138. assertRaisesRegexp = self.assertRaisesRegex
  139. else:
  140. assertRaisesRegexp = self.assertRaisesRegexp
  141. for k, _ in test_keys:
  142. assertRaisesRegexp(
  143. TypeError, "'name' must be bytestring, not int",
  144. r.__getitem__, 12
  145. )
  146. def test_delitem(self):
  147. r = self.open_repo('a.git')
  148. del r[b'refs/heads/master']
  149. self.assertRaises(KeyError, lambda: r[b'refs/heads/master'])
  150. del r[b'HEAD']
  151. self.assertRaises(KeyError, lambda: r[b'HEAD'])
  152. self.assertRaises(ValueError, r.__delitem__, b'notrefs/foo')
  153. def test_get_refs(self):
  154. r = self.open_repo('a.git')
  155. self.assertEqual({
  156. b'HEAD': b'a90fa2d900a17e99b433217e988c4eb4a2e9a097',
  157. b'refs/heads/master': b'a90fa2d900a17e99b433217e988c4eb4a2e9a097',
  158. b'refs/tags/mytag': b'28237f4dc30d0d462658d6b937b08a0f0b6ef55a',
  159. b'refs/tags/mytag-packed':
  160. b'b0931cadc54336e78a1d980420e3268903b57a50',
  161. }, r.get_refs())
  162. def test_head(self):
  163. r = self.open_repo('a.git')
  164. self.assertEqual(r.head(), b'a90fa2d900a17e99b433217e988c4eb4a2e9a097')
  165. def test_get_object(self):
  166. r = self.open_repo('a.git')
  167. obj = r.get_object(r.head())
  168. self.assertEqual(obj.type_name, b'commit')
  169. def test_get_object_non_existant(self):
  170. r = self.open_repo('a.git')
  171. self.assertRaises(KeyError, r.get_object, missing_sha)
  172. def test_contains_object(self):
  173. r = self.open_repo('a.git')
  174. self.assertTrue(r.head() in r)
  175. def test_contains_ref(self):
  176. r = self.open_repo('a.git')
  177. self.assertTrue(b"HEAD" in r)
  178. def test_get_no_description(self):
  179. r = self.open_repo('a.git')
  180. self.assertIs(None, r.get_description())
  181. def test_get_description(self):
  182. r = self.open_repo('a.git')
  183. with open(os.path.join(r.path, 'description'), 'wb') as f:
  184. f.write(b"Some description")
  185. self.assertEqual(b"Some description", r.get_description())
  186. def test_set_description(self):
  187. r = self.open_repo('a.git')
  188. description = b"Some description"
  189. r.set_description(description)
  190. self.assertEqual(description, r.get_description())
  191. def test_contains_missing(self):
  192. r = self.open_repo('a.git')
  193. self.assertFalse(b"bar" in r)
  194. def test_get_peeled(self):
  195. # unpacked ref
  196. r = self.open_repo('a.git')
  197. tag_sha = b'28237f4dc30d0d462658d6b937b08a0f0b6ef55a'
  198. self.assertNotEqual(r[tag_sha].sha().hexdigest(), r.head())
  199. self.assertEqual(r.get_peeled(b'refs/tags/mytag'), r.head())
  200. # packed ref with cached peeled value
  201. packed_tag_sha = b'b0931cadc54336e78a1d980420e3268903b57a50'
  202. parent_sha = r[r.head()].parents[0]
  203. self.assertNotEqual(r[packed_tag_sha].sha().hexdigest(), parent_sha)
  204. self.assertEqual(r.get_peeled(b'refs/tags/mytag-packed'), parent_sha)
  205. # TODO: add more corner cases to test repo
  206. def test_get_peeled_not_tag(self):
  207. r = self.open_repo('a.git')
  208. self.assertEqual(r.get_peeled(b'HEAD'), r.head())
  209. def test_get_walker(self):
  210. r = self.open_repo('a.git')
  211. # include defaults to [r.head()]
  212. self.assertEqual(
  213. [e.commit.id for e in r.get_walker()],
  214. [r.head(), b'2a72d929692c41d8554c07f6301757ba18a65d91'])
  215. self.assertEqual(
  216. [e.commit.id for e in
  217. r.get_walker([b'2a72d929692c41d8554c07f6301757ba18a65d91'])],
  218. [b'2a72d929692c41d8554c07f6301757ba18a65d91'])
  219. self.assertEqual(
  220. [e.commit.id for e in
  221. r.get_walker(b'2a72d929692c41d8554c07f6301757ba18a65d91')],
  222. [b'2a72d929692c41d8554c07f6301757ba18a65d91'])
  223. def assertFilesystemHidden(self, path):
  224. if sys.platform != 'win32':
  225. return
  226. import ctypes
  227. from ctypes.wintypes import DWORD, LPCWSTR
  228. GetFileAttributesW = ctypes.WINFUNCTYPE(DWORD, LPCWSTR)(
  229. ('GetFileAttributesW', ctypes.windll.kernel32))
  230. self.assertTrue(2 & GetFileAttributesW(path))
  231. def test_init_existing(self):
  232. tmp_dir = self.mkdtemp()
  233. self.addCleanup(shutil.rmtree, tmp_dir)
  234. t = Repo.init(tmp_dir)
  235. self.addCleanup(t.close)
  236. self.assertEqual(os.listdir(tmp_dir), ['.git'])
  237. self.assertFilesystemHidden(os.path.join(tmp_dir, '.git'))
  238. def test_init_mkdir(self):
  239. tmp_dir = self.mkdtemp()
  240. self.addCleanup(shutil.rmtree, tmp_dir)
  241. repo_dir = os.path.join(tmp_dir, 'a-repo')
  242. t = Repo.init(repo_dir, mkdir=True)
  243. self.addCleanup(t.close)
  244. self.assertEqual(os.listdir(repo_dir), ['.git'])
  245. self.assertFilesystemHidden(os.path.join(repo_dir, '.git'))
  246. def test_init_mkdir_unicode(self):
  247. repo_name = u'\xa7'
  248. try:
  249. os.fsencode(repo_name)
  250. except UnicodeEncodeError:
  251. self.skipTest('filesystem lacks unicode support')
  252. tmp_dir = self.mkdtemp()
  253. self.addCleanup(shutil.rmtree, tmp_dir)
  254. repo_dir = os.path.join(tmp_dir, repo_name)
  255. t = Repo.init(repo_dir, mkdir=True)
  256. self.addCleanup(t.close)
  257. self.assertEqual(os.listdir(repo_dir), ['.git'])
  258. self.assertFilesystemHidden(os.path.join(repo_dir, '.git'))
  259. @skipIf(sys.platform == 'win32', 'fails on Windows')
  260. def test_fetch(self):
  261. r = self.open_repo('a.git')
  262. tmp_dir = self.mkdtemp()
  263. self.addCleanup(shutil.rmtree, tmp_dir)
  264. t = Repo.init(tmp_dir)
  265. self.addCleanup(t.close)
  266. r.fetch(t)
  267. self.assertIn(b'a90fa2d900a17e99b433217e988c4eb4a2e9a097', t)
  268. self.assertIn(b'a90fa2d900a17e99b433217e988c4eb4a2e9a097', t)
  269. self.assertIn(b'a90fa2d900a17e99b433217e988c4eb4a2e9a097', t)
  270. self.assertIn(b'28237f4dc30d0d462658d6b937b08a0f0b6ef55a', t)
  271. self.assertIn(b'b0931cadc54336e78a1d980420e3268903b57a50', t)
  272. @skipIf(sys.platform == 'win32', 'fails on Windows')
  273. def test_fetch_ignores_missing_refs(self):
  274. r = self.open_repo('a.git')
  275. missing = b'1234566789123456789123567891234657373833'
  276. r.refs[b'refs/heads/blah'] = missing
  277. tmp_dir = self.mkdtemp()
  278. self.addCleanup(shutil.rmtree, tmp_dir)
  279. t = Repo.init(tmp_dir)
  280. self.addCleanup(t.close)
  281. r.fetch(t)
  282. self.assertIn(b'a90fa2d900a17e99b433217e988c4eb4a2e9a097', t)
  283. self.assertIn(b'a90fa2d900a17e99b433217e988c4eb4a2e9a097', t)
  284. self.assertIn(b'a90fa2d900a17e99b433217e988c4eb4a2e9a097', t)
  285. self.assertIn(b'28237f4dc30d0d462658d6b937b08a0f0b6ef55a', t)
  286. self.assertIn(b'b0931cadc54336e78a1d980420e3268903b57a50', t)
  287. self.assertNotIn(missing, t)
  288. def test_clone(self):
  289. r = self.open_repo('a.git')
  290. tmp_dir = self.mkdtemp()
  291. self.addCleanup(shutil.rmtree, tmp_dir)
  292. with r.clone(tmp_dir, mkdir=False) as t:
  293. self.assertEqual({
  294. b'HEAD': b'a90fa2d900a17e99b433217e988c4eb4a2e9a097',
  295. b'refs/remotes/origin/master':
  296. b'a90fa2d900a17e99b433217e988c4eb4a2e9a097',
  297. b'refs/heads/master':
  298. b'a90fa2d900a17e99b433217e988c4eb4a2e9a097',
  299. b'refs/tags/mytag':
  300. b'28237f4dc30d0d462658d6b937b08a0f0b6ef55a',
  301. b'refs/tags/mytag-packed':
  302. b'b0931cadc54336e78a1d980420e3268903b57a50',
  303. }, t.refs.as_dict())
  304. shas = [e.commit.id for e in r.get_walker()]
  305. self.assertEqual(shas, [t.head(),
  306. b'2a72d929692c41d8554c07f6301757ba18a65d91'])
  307. c = t.get_config()
  308. encoded_path = r.path
  309. if not isinstance(encoded_path, bytes):
  310. encoded_path = os.fsencode(encoded_path)
  311. self.assertEqual(encoded_path, c.get((b'remote', b'origin'), b'url'))
  312. self.assertEqual(
  313. b'+refs/heads/*:refs/remotes/origin/*',
  314. c.get((b'remote', b'origin'), b'fetch'))
  315. def test_clone_no_head(self):
  316. temp_dir = self.mkdtemp()
  317. self.addCleanup(shutil.rmtree, temp_dir)
  318. repo_dir = os.path.join(os.path.dirname(__file__), 'data', 'repos')
  319. dest_dir = os.path.join(temp_dir, 'a.git')
  320. shutil.copytree(os.path.join(repo_dir, 'a.git'),
  321. dest_dir, symlinks=True)
  322. r = Repo(dest_dir)
  323. del r.refs[b"refs/heads/master"]
  324. del r.refs[b"HEAD"]
  325. t = r.clone(os.path.join(temp_dir, 'b.git'), mkdir=True)
  326. self.assertEqual({
  327. b'refs/tags/mytag': b'28237f4dc30d0d462658d6b937b08a0f0b6ef55a',
  328. b'refs/tags/mytag-packed':
  329. b'b0931cadc54336e78a1d980420e3268903b57a50',
  330. }, t.refs.as_dict())
  331. def test_clone_empty(self):
  332. """Test clone() doesn't crash if HEAD points to a non-existing ref.
  333. This simulates cloning server-side bare repository either when it is
  334. still empty or if user renames master branch and pushes private repo
  335. to the server.
  336. Non-bare repo HEAD always points to an existing ref.
  337. """
  338. r = self.open_repo('empty.git')
  339. tmp_dir = self.mkdtemp()
  340. self.addCleanup(shutil.rmtree, tmp_dir)
  341. r.clone(tmp_dir, mkdir=False, bare=True)
  342. def test_clone_bare(self):
  343. r = self.open_repo('a.git')
  344. tmp_dir = self.mkdtemp()
  345. self.addCleanup(shutil.rmtree, tmp_dir)
  346. t = r.clone(tmp_dir, mkdir=False)
  347. t.close()
  348. def test_clone_checkout_and_bare(self):
  349. r = self.open_repo('a.git')
  350. tmp_dir = self.mkdtemp()
  351. self.addCleanup(shutil.rmtree, tmp_dir)
  352. self.assertRaises(ValueError, r.clone, tmp_dir, mkdir=False,
  353. checkout=True, bare=True)
  354. def test_merge_history(self):
  355. r = self.open_repo('simple_merge.git')
  356. shas = [e.commit.id for e in r.get_walker()]
  357. self.assertEqual(shas, [b'5dac377bdded4c9aeb8dff595f0faeebcc8498cc',
  358. b'ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd',
  359. b'4cffe90e0a41ad3f5190079d7c8f036bde29cbe6',
  360. b'60dacdc733de308bb77bb76ce0fb0f9b44c9769e',
  361. b'0d89f20333fbb1d2f3a94da77f4981373d8f4310'])
  362. def test_out_of_order_merge(self):
  363. """Test that revision history is ordered by date, not parent order."""
  364. r = self.open_repo('ooo_merge.git')
  365. shas = [e.commit.id for e in r.get_walker()]
  366. self.assertEqual(shas, [b'7601d7f6231db6a57f7bbb79ee52e4d462fd44d1',
  367. b'f507291b64138b875c28e03469025b1ea20bc614',
  368. b'fb5b0425c7ce46959bec94d54b9a157645e114f5',
  369. b'f9e39b120c68182a4ba35349f832d0e4e61f485c'])
  370. def test_get_tags_empty(self):
  371. r = self.open_repo('ooo_merge.git')
  372. self.assertEqual({}, r.refs.as_dict(b'refs/tags'))
  373. def test_get_config(self):
  374. r = self.open_repo('ooo_merge.git')
  375. self.assertIsInstance(r.get_config(), Config)
  376. def test_get_config_stack(self):
  377. r = self.open_repo('ooo_merge.git')
  378. self.assertIsInstance(r.get_config_stack(), Config)
  379. @skipIf(not getattr(os, 'symlink', None), 'Requires symlink support')
  380. def test_submodule(self):
  381. temp_dir = self.mkdtemp()
  382. self.addCleanup(shutil.rmtree, temp_dir)
  383. repo_dir = os.path.join(os.path.dirname(__file__), 'data', 'repos')
  384. shutil.copytree(os.path.join(repo_dir, 'a.git'),
  385. os.path.join(temp_dir, 'a.git'), symlinks=True)
  386. rel = os.path.relpath(os.path.join(repo_dir, 'submodule'), temp_dir)
  387. os.symlink(os.path.join(rel, 'dotgit'), os.path.join(temp_dir, '.git'))
  388. with Repo(temp_dir) as r:
  389. self.assertEqual(r.head(),
  390. b'a90fa2d900a17e99b433217e988c4eb4a2e9a097')
  391. def test_common_revisions(self):
  392. """
  393. This test demonstrates that ``find_common_revisions()`` actually
  394. returns common heads, not revisions; dulwich already uses
  395. ``find_common_revisions()`` in such a manner (see
  396. ``Repo.fetch_objects()``).
  397. """
  398. expected_shas = set([b'60dacdc733de308bb77bb76ce0fb0f9b44c9769e'])
  399. # Source for objects.
  400. r_base = self.open_repo('simple_merge.git')
  401. # Re-create each-side of the merge in simple_merge.git.
  402. #
  403. # Since the trees and blobs are missing, the repository created is
  404. # corrupted, but we're only checking for commits for the purpose of
  405. # this test, so it's immaterial.
  406. r1_dir = self.mkdtemp()
  407. self.addCleanup(shutil.rmtree, r1_dir)
  408. r1_commits = [b'ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd', # HEAD
  409. b'60dacdc733de308bb77bb76ce0fb0f9b44c9769e',
  410. b'0d89f20333fbb1d2f3a94da77f4981373d8f4310']
  411. r2_dir = self.mkdtemp()
  412. self.addCleanup(shutil.rmtree, r2_dir)
  413. r2_commits = [b'4cffe90e0a41ad3f5190079d7c8f036bde29cbe6', # HEAD
  414. b'60dacdc733de308bb77bb76ce0fb0f9b44c9769e',
  415. b'0d89f20333fbb1d2f3a94da77f4981373d8f4310']
  416. r1 = Repo.init_bare(r1_dir)
  417. for c in r1_commits:
  418. r1.object_store.add_object(r_base.get_object(c))
  419. r1.refs[b'HEAD'] = r1_commits[0]
  420. r2 = Repo.init_bare(r2_dir)
  421. for c in r2_commits:
  422. r2.object_store.add_object(r_base.get_object(c))
  423. r2.refs[b'HEAD'] = r2_commits[0]
  424. # Finally, the 'real' testing!
  425. shas = r2.object_store.find_common_revisions(r1.get_graph_walker())
  426. self.assertEqual(set(shas), expected_shas)
  427. shas = r1.object_store.find_common_revisions(r2.get_graph_walker())
  428. self.assertEqual(set(shas), expected_shas)
  429. def test_shell_hook_pre_commit(self):
  430. if os.name != 'posix':
  431. self.skipTest('shell hook tests requires POSIX shell')
  432. pre_commit_fail = """#!/bin/sh
  433. exit 1
  434. """
  435. pre_commit_success = """#!/bin/sh
  436. exit 0
  437. """
  438. repo_dir = os.path.join(self.mkdtemp())
  439. self.addCleanup(shutil.rmtree, repo_dir)
  440. r = Repo.init(repo_dir)
  441. self.addCleanup(r.close)
  442. pre_commit = os.path.join(r.controldir(), 'hooks', 'pre-commit')
  443. with open(pre_commit, 'w') as f:
  444. f.write(pre_commit_fail)
  445. os.chmod(pre_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  446. self.assertRaises(errors.CommitError, r.do_commit, 'failed commit',
  447. committer='Test Committer <test@nodomain.com>',
  448. author='Test Author <test@nodomain.com>',
  449. commit_timestamp=12345, commit_timezone=0,
  450. author_timestamp=12345, author_timezone=0)
  451. with open(pre_commit, 'w') as f:
  452. f.write(pre_commit_success)
  453. os.chmod(pre_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  454. commit_sha = r.do_commit(
  455. b'empty commit',
  456. committer=b'Test Committer <test@nodomain.com>',
  457. author=b'Test Author <test@nodomain.com>',
  458. commit_timestamp=12395, commit_timezone=0,
  459. author_timestamp=12395, author_timezone=0)
  460. self.assertEqual([], r[commit_sha].parents)
  461. def test_shell_hook_commit_msg(self):
  462. if os.name != 'posix':
  463. self.skipTest('shell hook tests requires POSIX shell')
  464. commit_msg_fail = """#!/bin/sh
  465. exit 1
  466. """
  467. commit_msg_success = """#!/bin/sh
  468. exit 0
  469. """
  470. repo_dir = self.mkdtemp()
  471. self.addCleanup(shutil.rmtree, repo_dir)
  472. r = Repo.init(repo_dir)
  473. self.addCleanup(r.close)
  474. commit_msg = os.path.join(r.controldir(), 'hooks', 'commit-msg')
  475. with open(commit_msg, 'w') as f:
  476. f.write(commit_msg_fail)
  477. os.chmod(commit_msg, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  478. self.assertRaises(errors.CommitError, r.do_commit, b'failed commit',
  479. committer=b'Test Committer <test@nodomain.com>',
  480. author=b'Test Author <test@nodomain.com>',
  481. commit_timestamp=12345, commit_timezone=0,
  482. author_timestamp=12345, author_timezone=0)
  483. with open(commit_msg, 'w') as f:
  484. f.write(commit_msg_success)
  485. os.chmod(commit_msg, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  486. commit_sha = r.do_commit(
  487. b'empty commit',
  488. committer=b'Test Committer <test@nodomain.com>',
  489. author=b'Test Author <test@nodomain.com>',
  490. commit_timestamp=12395, commit_timezone=0,
  491. author_timestamp=12395, author_timezone=0)
  492. self.assertEqual([], r[commit_sha].parents)
  493. def test_shell_hook_post_commit(self):
  494. if os.name != 'posix':
  495. self.skipTest('shell hook tests requires POSIX shell')
  496. repo_dir = self.mkdtemp()
  497. self.addCleanup(shutil.rmtree, repo_dir)
  498. r = Repo.init(repo_dir)
  499. self.addCleanup(r.close)
  500. (fd, path) = tempfile.mkstemp(dir=repo_dir)
  501. os.close(fd)
  502. post_commit_msg = """#!/bin/sh
  503. rm """ + path + """
  504. """
  505. root_sha = r.do_commit(
  506. b'empty commit',
  507. committer=b'Test Committer <test@nodomain.com>',
  508. author=b'Test Author <test@nodomain.com>',
  509. commit_timestamp=12345, commit_timezone=0,
  510. author_timestamp=12345, author_timezone=0)
  511. self.assertEqual([], r[root_sha].parents)
  512. post_commit = os.path.join(r.controldir(), 'hooks', 'post-commit')
  513. with open(post_commit, 'wb') as f:
  514. f.write(post_commit_msg.encode(locale.getpreferredencoding()))
  515. os.chmod(post_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  516. commit_sha = r.do_commit(
  517. b'empty commit',
  518. committer=b'Test Committer <test@nodomain.com>',
  519. author=b'Test Author <test@nodomain.com>',
  520. commit_timestamp=12345, commit_timezone=0,
  521. author_timestamp=12345, author_timezone=0)
  522. self.assertEqual([root_sha], r[commit_sha].parents)
  523. self.assertFalse(os.path.exists(path))
  524. post_commit_msg_fail = """#!/bin/sh
  525. exit 1
  526. """
  527. with open(post_commit, 'w') as f:
  528. f.write(post_commit_msg_fail)
  529. os.chmod(post_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  530. warnings.simplefilter("always", UserWarning)
  531. self.addCleanup(warnings.resetwarnings)
  532. warnings_list, restore_warnings = setup_warning_catcher()
  533. self.addCleanup(restore_warnings)
  534. commit_sha2 = r.do_commit(
  535. b'empty commit',
  536. committer=b'Test Committer <test@nodomain.com>',
  537. author=b'Test Author <test@nodomain.com>',
  538. commit_timestamp=12345, commit_timezone=0,
  539. author_timestamp=12345, author_timezone=0)
  540. expected_warning = UserWarning(
  541. 'post-commit hook failed: Hook post-commit exited with '
  542. 'non-zero status',)
  543. for w in warnings_list:
  544. if (type(w) == type(expected_warning) and
  545. w.args == expected_warning.args):
  546. break
  547. else:
  548. raise AssertionError(
  549. 'Expected warning %r not in %r' %
  550. (expected_warning, warnings_list))
  551. self.assertEqual([commit_sha], r[commit_sha2].parents)
  552. def test_as_dict(self):
  553. def check(repo):
  554. self.assertEqual(
  555. repo.refs.subkeys(b'refs/tags'),
  556. repo.refs.subkeys(b'refs/tags/'))
  557. self.assertEqual(
  558. repo.refs.as_dict(b'refs/tags'),
  559. repo.refs.as_dict(b'refs/tags/'))
  560. self.assertEqual(
  561. repo.refs.as_dict(b'refs/heads'),
  562. repo.refs.as_dict(b'refs/heads/'))
  563. bare = self.open_repo('a.git')
  564. tmp_dir = self.mkdtemp()
  565. self.addCleanup(shutil.rmtree, tmp_dir)
  566. with bare.clone(tmp_dir, mkdir=False) as nonbare:
  567. check(nonbare)
  568. check(bare)
  569. def test_working_tree(self):
  570. temp_dir = tempfile.mkdtemp()
  571. self.addCleanup(shutil.rmtree, temp_dir)
  572. worktree_temp_dir = tempfile.mkdtemp()
  573. self.addCleanup(shutil.rmtree, worktree_temp_dir)
  574. r = Repo.init(temp_dir)
  575. self.addCleanup(r.close)
  576. root_sha = r.do_commit(
  577. b'empty commit',
  578. committer=b'Test Committer <test@nodomain.com>',
  579. author=b'Test Author <test@nodomain.com>',
  580. commit_timestamp=12345, commit_timezone=0,
  581. author_timestamp=12345, author_timezone=0)
  582. r.refs[b'refs/heads/master'] = root_sha
  583. w = Repo._init_new_working_directory(worktree_temp_dir, r)
  584. self.addCleanup(w.close)
  585. new_sha = w.do_commit(
  586. b'new commit',
  587. committer=b'Test Committer <test@nodomain.com>',
  588. author=b'Test Author <test@nodomain.com>',
  589. commit_timestamp=12345, commit_timezone=0,
  590. author_timestamp=12345, author_timezone=0)
  591. w.refs[b'HEAD'] = new_sha
  592. self.assertEqual(os.path.abspath(r.controldir()),
  593. os.path.abspath(w.commondir()))
  594. self.assertEqual(r.refs.keys(), w.refs.keys())
  595. self.assertNotEqual(r.head(), w.head())
  596. class BuildRepoRootTests(TestCase):
  597. """Tests that build on-disk repos from scratch.
  598. Repos live in a temp dir and are torn down after each test. They start with
  599. a single commit in master having single file named 'a'.
  600. """
  601. def get_repo_dir(self):
  602. return os.path.join(tempfile.mkdtemp(), 'test')
  603. def setUp(self):
  604. super(BuildRepoRootTests, self).setUp()
  605. self._repo_dir = self.get_repo_dir()
  606. os.makedirs(self._repo_dir)
  607. r = self._repo = Repo.init(self._repo_dir)
  608. self.addCleanup(tear_down_repo, r)
  609. self.assertFalse(r.bare)
  610. self.assertEqual(b'ref: refs/heads/master', r.refs.read_ref(b'HEAD'))
  611. self.assertRaises(KeyError, lambda: r.refs[b'refs/heads/master'])
  612. with open(os.path.join(r.path, 'a'), 'wb') as f:
  613. f.write(b'file contents')
  614. r.stage(['a'])
  615. commit_sha = r.do_commit(
  616. b'msg',
  617. committer=b'Test Committer <test@nodomain.com>',
  618. author=b'Test Author <test@nodomain.com>',
  619. commit_timestamp=12345, commit_timezone=0,
  620. author_timestamp=12345, author_timezone=0)
  621. self.assertEqual([], r[commit_sha].parents)
  622. self._root_commit = commit_sha
  623. def test_get_shallow(self):
  624. self.assertEqual(set(), self._repo.get_shallow())
  625. with open(os.path.join(self._repo.path, '.git', 'shallow'), 'wb') as f:
  626. f.write(b'a90fa2d900a17e99b433217e988c4eb4a2e9a097\n')
  627. self.assertEqual({b'a90fa2d900a17e99b433217e988c4eb4a2e9a097'},
  628. self._repo.get_shallow())
  629. def test_update_shallow(self):
  630. self._repo.update_shallow(None, None) # no op
  631. self.assertEqual(set(), self._repo.get_shallow())
  632. self._repo.update_shallow(
  633. [b'a90fa2d900a17e99b433217e988c4eb4a2e9a097'],
  634. None)
  635. self.assertEqual(
  636. {b'a90fa2d900a17e99b433217e988c4eb4a2e9a097'},
  637. self._repo.get_shallow())
  638. self._repo.update_shallow(
  639. [b'a90fa2d900a17e99b433217e988c4eb4a2e9a097'],
  640. [b'f9e39b120c68182a4ba35349f832d0e4e61f485c'])
  641. self.assertEqual({b'a90fa2d900a17e99b433217e988c4eb4a2e9a097'},
  642. self._repo.get_shallow())
  643. def test_build_repo(self):
  644. r = self._repo
  645. self.assertEqual(b'ref: refs/heads/master', r.refs.read_ref(b'HEAD'))
  646. self.assertEqual(self._root_commit, r.refs[b'refs/heads/master'])
  647. expected_blob = objects.Blob.from_string(b'file contents')
  648. self.assertEqual(expected_blob.data, r[expected_blob.id].data)
  649. actual_commit = r[self._root_commit]
  650. self.assertEqual(b'msg', actual_commit.message)
  651. def test_commit_modified(self):
  652. r = self._repo
  653. with open(os.path.join(r.path, 'a'), 'wb') as f:
  654. f.write(b'new contents')
  655. r.stage(['a'])
  656. commit_sha = r.do_commit(
  657. b'modified a',
  658. committer=b'Test Committer <test@nodomain.com>',
  659. author=b'Test Author <test@nodomain.com>',
  660. commit_timestamp=12395, commit_timezone=0,
  661. author_timestamp=12395, author_timezone=0)
  662. self.assertEqual([self._root_commit], r[commit_sha].parents)
  663. a_mode, a_id = tree_lookup_path(r.get_object, r[commit_sha].tree, b'a')
  664. self.assertEqual(stat.S_IFREG | 0o644, a_mode)
  665. self.assertEqual(b'new contents', r[a_id].data)
  666. @skipIf(not getattr(os, 'symlink', None), 'Requires symlink support')
  667. def test_commit_symlink(self):
  668. r = self._repo
  669. os.symlink('a', os.path.join(r.path, 'b'))
  670. r.stage(['a', 'b'])
  671. commit_sha = r.do_commit(
  672. b'Symlink b',
  673. committer=b'Test Committer <test@nodomain.com>',
  674. author=b'Test Author <test@nodomain.com>',
  675. commit_timestamp=12395, commit_timezone=0,
  676. author_timestamp=12395, author_timezone=0)
  677. self.assertEqual([self._root_commit], r[commit_sha].parents)
  678. b_mode, b_id = tree_lookup_path(r.get_object, r[commit_sha].tree, b'b')
  679. self.assertTrue(stat.S_ISLNK(b_mode))
  680. self.assertEqual(b'a', r[b_id].data)
  681. def test_commit_merge_heads_file(self):
  682. tmp_dir = tempfile.mkdtemp()
  683. self.addCleanup(shutil.rmtree, tmp_dir)
  684. r = Repo.init(tmp_dir)
  685. with open(os.path.join(r.path, 'a'), 'w') as f:
  686. f.write('initial text')
  687. c1 = r.do_commit(
  688. b'initial commit',
  689. committer=b'Test Committer <test@nodomain.com>',
  690. author=b'Test Author <test@nodomain.com>',
  691. commit_timestamp=12395, commit_timezone=0,
  692. author_timestamp=12395, author_timezone=0)
  693. with open(os.path.join(r.path, 'a'), 'w') as f:
  694. f.write('merged text')
  695. with open(os.path.join(r.path, '.git', 'MERGE_HEADS'), 'w') as f:
  696. f.write('c27a2d21dd136312d7fa9e8baabb82561a1727d0\n')
  697. r.stage(['a'])
  698. commit_sha = r.do_commit(
  699. b'deleted a',
  700. committer=b'Test Committer <test@nodomain.com>',
  701. author=b'Test Author <test@nodomain.com>',
  702. commit_timestamp=12395, commit_timezone=0,
  703. author_timestamp=12395, author_timezone=0)
  704. self.assertEqual([
  705. c1,
  706. b'c27a2d21dd136312d7fa9e8baabb82561a1727d0'],
  707. r[commit_sha].parents)
  708. def test_commit_deleted(self):
  709. r = self._repo
  710. os.remove(os.path.join(r.path, 'a'))
  711. r.stage(['a'])
  712. commit_sha = r.do_commit(
  713. b'deleted a',
  714. committer=b'Test Committer <test@nodomain.com>',
  715. author=b'Test Author <test@nodomain.com>',
  716. commit_timestamp=12395, commit_timezone=0,
  717. author_timestamp=12395, author_timezone=0)
  718. self.assertEqual([self._root_commit], r[commit_sha].parents)
  719. self.assertEqual([], list(r.open_index()))
  720. tree = r[r[commit_sha].tree]
  721. self.assertEqual([], list(tree.iteritems()))
  722. def test_commit_follows(self):
  723. r = self._repo
  724. r.refs.set_symbolic_ref(b'HEAD', b'refs/heads/bla')
  725. commit_sha = r.do_commit(
  726. b'commit with strange character',
  727. committer=b'Test Committer <test@nodomain.com>',
  728. author=b'Test Author <test@nodomain.com>',
  729. commit_timestamp=12395, commit_timezone=0,
  730. author_timestamp=12395, author_timezone=0,
  731. ref=b'HEAD')
  732. self.assertEqual(commit_sha, r[b'refs/heads/bla'].id)
  733. def test_commit_encoding(self):
  734. r = self._repo
  735. commit_sha = r.do_commit(
  736. b'commit with strange character \xee',
  737. committer=b'Test Committer <test@nodomain.com>',
  738. author=b'Test Author <test@nodomain.com>',
  739. commit_timestamp=12395, commit_timezone=0,
  740. author_timestamp=12395, author_timezone=0,
  741. encoding=b"iso8859-1")
  742. self.assertEqual(b"iso8859-1", r[commit_sha].encoding)
  743. def test_compression_level(self):
  744. r = self._repo
  745. c = r.get_config()
  746. c.set(('core',), 'compression', '3')
  747. c.set(('core',), 'looseCompression', '4')
  748. c.write_to_path()
  749. r = Repo(self._repo_dir)
  750. self.assertEqual(r.object_store.loose_compression_level, 4)
  751. def test_commit_encoding_from_config(self):
  752. r = self._repo
  753. c = r.get_config()
  754. c.set(('i18n',), 'commitEncoding', 'iso8859-1')
  755. c.write_to_path()
  756. commit_sha = r.do_commit(
  757. b'commit with strange character \xee',
  758. committer=b'Test Committer <test@nodomain.com>',
  759. author=b'Test Author <test@nodomain.com>',
  760. commit_timestamp=12395, commit_timezone=0,
  761. author_timestamp=12395, author_timezone=0)
  762. self.assertEqual(b"iso8859-1", r[commit_sha].encoding)
  763. def test_commit_config_identity(self):
  764. # commit falls back to the users' identity if it wasn't specified
  765. r = self._repo
  766. c = r.get_config()
  767. c.set((b"user", ), b"name", b"Jelmer")
  768. c.set((b"user", ), b"email", b"jelmer@apache.org")
  769. c.write_to_path()
  770. commit_sha = r.do_commit(b'message')
  771. self.assertEqual(
  772. b"Jelmer <jelmer@apache.org>",
  773. r[commit_sha].author)
  774. self.assertEqual(
  775. b"Jelmer <jelmer@apache.org>",
  776. r[commit_sha].committer)
  777. def test_commit_config_identity_strips_than(self):
  778. # commit falls back to the users' identity if it wasn't specified,
  779. # and strips superfluous <>
  780. r = self._repo
  781. c = r.get_config()
  782. c.set((b"user", ), b"name", b"Jelmer")
  783. c.set((b"user", ), b"email", b"<jelmer@apache.org>")
  784. c.write_to_path()
  785. commit_sha = r.do_commit(b'message')
  786. self.assertEqual(
  787. b"Jelmer <jelmer@apache.org>",
  788. r[commit_sha].author)
  789. self.assertEqual(
  790. b"Jelmer <jelmer@apache.org>",
  791. r[commit_sha].committer)
  792. def test_commit_config_identity_in_memoryrepo(self):
  793. # commit falls back to the users' identity if it wasn't specified
  794. r = MemoryRepo.init_bare([], {})
  795. c = r.get_config()
  796. c.set((b"user", ), b"name", b"Jelmer")
  797. c.set((b"user", ), b"email", b"jelmer@apache.org")
  798. commit_sha = r.do_commit(b'message', tree=objects.Tree().id)
  799. self.assertEqual(
  800. b"Jelmer <jelmer@apache.org>",
  801. r[commit_sha].author)
  802. self.assertEqual(
  803. b"Jelmer <jelmer@apache.org>",
  804. r[commit_sha].committer)
  805. def overrideEnv(self, name, value):
  806. def restore():
  807. if oldval is not None:
  808. os.environ[name] = oldval
  809. else:
  810. del os.environ[name]
  811. oldval = os.environ.get(name)
  812. os.environ[name] = value
  813. self.addCleanup(restore)
  814. def test_commit_config_identity_from_env(self):
  815. # commit falls back to the users' identity if it wasn't specified
  816. self.overrideEnv('GIT_COMMITTER_NAME', 'joe')
  817. self.overrideEnv('GIT_COMMITTER_EMAIL', 'joe@example.com')
  818. r = self._repo
  819. c = r.get_config()
  820. c.set((b"user", ), b"name", b"Jelmer")
  821. c.set((b"user", ), b"email", b"jelmer@apache.org")
  822. c.write_to_path()
  823. commit_sha = r.do_commit(b'message')
  824. self.assertEqual(
  825. b"Jelmer <jelmer@apache.org>",
  826. r[commit_sha].author)
  827. self.assertEqual(
  828. b"joe <joe@example.com>",
  829. r[commit_sha].committer)
  830. def test_commit_fail_ref(self):
  831. r = self._repo
  832. def set_if_equals(name, old_ref, new_ref, **kwargs):
  833. return False
  834. r.refs.set_if_equals = set_if_equals
  835. def add_if_new(name, new_ref, **kwargs):
  836. self.fail('Unexpected call to add_if_new')
  837. r.refs.add_if_new = add_if_new
  838. old_shas = set(r.object_store)
  839. self.assertRaises(errors.CommitError, r.do_commit, b'failed commit',
  840. committer=b'Test Committer <test@nodomain.com>',
  841. author=b'Test Author <test@nodomain.com>',
  842. commit_timestamp=12345, commit_timezone=0,
  843. author_timestamp=12345, author_timezone=0)
  844. new_shas = set(r.object_store) - old_shas
  845. self.assertEqual(1, len(new_shas))
  846. # Check that the new commit (now garbage) was added.
  847. new_commit = r[new_shas.pop()]
  848. self.assertEqual(r[self._root_commit].tree, new_commit.tree)
  849. self.assertEqual(b'failed commit', new_commit.message)
  850. def test_commit_branch(self):
  851. r = self._repo
  852. commit_sha = r.do_commit(
  853. b'commit to branch',
  854. committer=b'Test Committer <test@nodomain.com>',
  855. author=b'Test Author <test@nodomain.com>',
  856. commit_timestamp=12395, commit_timezone=0,
  857. author_timestamp=12395, author_timezone=0,
  858. ref=b"refs/heads/new_branch")
  859. self.assertEqual(self._root_commit, r[b"HEAD"].id)
  860. self.assertEqual(commit_sha, r[b"refs/heads/new_branch"].id)
  861. self.assertEqual([], r[commit_sha].parents)
  862. self.assertTrue(b"refs/heads/new_branch" in r)
  863. new_branch_head = commit_sha
  864. commit_sha = r.do_commit(
  865. b'commit to branch 2',
  866. committer=b'Test Committer <test@nodomain.com>',
  867. author=b'Test Author <test@nodomain.com>',
  868. commit_timestamp=12395, commit_timezone=0,
  869. author_timestamp=12395, author_timezone=0,
  870. ref=b"refs/heads/new_branch")
  871. self.assertEqual(self._root_commit, r[b"HEAD"].id)
  872. self.assertEqual(commit_sha, r[b"refs/heads/new_branch"].id)
  873. self.assertEqual([new_branch_head], r[commit_sha].parents)
  874. def test_commit_merge_heads(self):
  875. r = self._repo
  876. merge_1 = r.do_commit(
  877. b'commit to branch 2',
  878. committer=b'Test Committer <test@nodomain.com>',
  879. author=b'Test Author <test@nodomain.com>',
  880. commit_timestamp=12395, commit_timezone=0,
  881. author_timestamp=12395, author_timezone=0,
  882. ref=b"refs/heads/new_branch")
  883. commit_sha = r.do_commit(
  884. b'commit with merge',
  885. committer=b'Test Committer <test@nodomain.com>',
  886. author=b'Test Author <test@nodomain.com>',
  887. commit_timestamp=12395, commit_timezone=0,
  888. author_timestamp=12395, author_timezone=0,
  889. merge_heads=[merge_1])
  890. self.assertEqual(
  891. [self._root_commit, merge_1],
  892. r[commit_sha].parents)
  893. def test_commit_dangling_commit(self):
  894. r = self._repo
  895. old_shas = set(r.object_store)
  896. old_refs = r.get_refs()
  897. commit_sha = r.do_commit(
  898. b'commit with no ref',
  899. committer=b'Test Committer <test@nodomain.com>',
  900. author=b'Test Author <test@nodomain.com>',
  901. commit_timestamp=12395, commit_timezone=0,
  902. author_timestamp=12395, author_timezone=0,
  903. ref=None)
  904. new_shas = set(r.object_store) - old_shas
  905. # New sha is added, but no new refs
  906. self.assertEqual(1, len(new_shas))
  907. new_commit = r[new_shas.pop()]
  908. self.assertEqual(r[self._root_commit].tree, new_commit.tree)
  909. self.assertEqual([], r[commit_sha].parents)
  910. self.assertEqual(old_refs, r.get_refs())
  911. def test_commit_dangling_commit_with_parents(self):
  912. r = self._repo
  913. old_shas = set(r.object_store)
  914. old_refs = r.get_refs()
  915. commit_sha = r.do_commit(
  916. b'commit with no ref',
  917. committer=b'Test Committer <test@nodomain.com>',
  918. author=b'Test Author <test@nodomain.com>',
  919. commit_timestamp=12395, commit_timezone=0,
  920. author_timestamp=12395, author_timezone=0,
  921. ref=None, merge_heads=[self._root_commit])
  922. new_shas = set(r.object_store) - old_shas
  923. # New sha is added, but no new refs
  924. self.assertEqual(1, len(new_shas))
  925. new_commit = r[new_shas.pop()]
  926. self.assertEqual(r[self._root_commit].tree, new_commit.tree)
  927. self.assertEqual([self._root_commit], r[commit_sha].parents)
  928. self.assertEqual(old_refs, r.get_refs())
  929. def test_stage_absolute(self):
  930. r = self._repo
  931. os.remove(os.path.join(r.path, 'a'))
  932. self.assertRaises(ValueError, r.stage, [os.path.join(r.path, 'a')])
  933. def test_stage_deleted(self):
  934. r = self._repo
  935. os.remove(os.path.join(r.path, 'a'))
  936. r.stage(['a'])
  937. r.stage(['a']) # double-stage a deleted path
  938. def test_stage_directory(self):
  939. r = self._repo
  940. os.mkdir(os.path.join(r.path, 'c'))
  941. r.stage(['c'])
  942. self.assertEqual([b'a'], list(r.open_index()))
  943. @skipIf(sys.platform == 'win32',
  944. 'tries to implicitly decode as utf8')
  945. def test_commit_no_encode_decode(self):
  946. r = self._repo
  947. repo_path_bytes = os.fsencode(r.path)
  948. encodings = ('utf8', 'latin1')
  949. names = [u'À'.encode(encoding) for encoding in encodings]
  950. for name, encoding in zip(names, encodings):
  951. full_path = os.path.join(repo_path_bytes, name)
  952. with open(full_path, 'wb') as f:
  953. f.write(encoding.encode('ascii'))
  954. # These files are break tear_down_repo, so cleanup these files
  955. # ourselves.
  956. self.addCleanup(os.remove, full_path)
  957. r.stage(names)
  958. commit_sha = r.do_commit(
  959. b'Files with different encodings',
  960. committer=b'Test Committer <test@nodomain.com>',
  961. author=b'Test Author <test@nodomain.com>',
  962. commit_timestamp=12395, commit_timezone=0,
  963. author_timestamp=12395, author_timezone=0,
  964. ref=None, merge_heads=[self._root_commit])
  965. for name, encoding in zip(names, encodings):
  966. mode, id = tree_lookup_path(r.get_object, r[commit_sha].tree, name)
  967. self.assertEqual(stat.S_IFREG | 0o644, mode)
  968. self.assertEqual(encoding.encode('ascii'), r[id].data)
  969. def test_discover_intended(self):
  970. path = os.path.join(self._repo_dir, 'b/c')
  971. r = Repo.discover(path)
  972. self.assertEqual(r.head(), self._repo.head())
  973. def test_discover_isrepo(self):
  974. r = Repo.discover(self._repo_dir)
  975. self.assertEqual(r.head(), self._repo.head())
  976. def test_discover_notrepo(self):
  977. with self.assertRaises(NotGitRepository):
  978. Repo.discover('/')
  979. class CheckUserIdentityTests(TestCase):
  980. def test_valid(self):
  981. check_user_identity(b'Me <me@example.com>')
  982. def test_invalid(self):
  983. self.assertRaises(InvalidUserIdentity,
  984. check_user_identity, b'No Email')
  985. self.assertRaises(InvalidUserIdentity,
  986. check_user_identity, b'Fullname <missing')
  987. self.assertRaises(InvalidUserIdentity,
  988. check_user_identity, b'Fullname missing>')
  989. self.assertRaises(InvalidUserIdentity,
  990. check_user_identity, b'Fullname >order<>')