test_repository.py 49 KB

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