You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1048 lines
39KB

  1. # -*- coding: utf-8 -*-
  2. # test_repo.py
  3. # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors
  4. #
  5. # This module is part of GitPython and is released under
  6. # the BSD License: http://www.opensource.org/licenses/bsd-license.php
  7. import glob
  8. import io
  9. from io import BytesIO
  10. import itertools
  11. import os
  12. import pickle
  13. import tempfile
  14. from unittest import skipIf, SkipTest
  15. try:
  16. import pathlib
  17. except ImportError:
  18. pathlib = None
  19. from git import (
  20. InvalidGitRepositoryError,
  21. Repo,
  22. NoSuchPathError,
  23. Head,
  24. Commit,
  25. Object,
  26. Tree,
  27. IndexFile,
  28. Git,
  29. Reference,
  30. GitDB,
  31. Submodule,
  32. GitCmdObjectDB,
  33. Remote,
  34. BadName,
  35. GitCommandError
  36. )
  37. from git.compat import (
  38. PY3,
  39. is_win,
  40. string_types,
  41. win_encode,
  42. )
  43. from git.exc import (
  44. BadObject,
  45. )
  46. from git.repo.fun import touch
  47. from git.test.lib import (
  48. patch,
  49. TestBase,
  50. with_rw_repo,
  51. fixture,
  52. assert_false,
  53. assert_equal,
  54. assert_true,
  55. raises
  56. )
  57. from git.util import HIDE_WINDOWS_KNOWN_ERRORS, cygpath
  58. from git.test.lib import with_rw_directory
  59. from git.util import join_path_native, rmtree, rmfile, bin_to_hex
  60. import functools as fnt
  61. import os.path as osp
  62. def iter_flatten(lol):
  63. for items in lol:
  64. for item in items:
  65. yield item
  66. def flatten(lol):
  67. return list(iter_flatten(lol))
  68. _tc_lock_fpaths = osp.join(osp.dirname(__file__), '../../.git/*.lock')
  69. def _rm_lock_files():
  70. for lfp in glob.glob(_tc_lock_fpaths):
  71. rmfile(lfp)
  72. class TestRepo(TestBase):
  73. def setUp(self):
  74. _rm_lock_files()
  75. def tearDown(self):
  76. for lfp in glob.glob(_tc_lock_fpaths):
  77. if osp.isfile(lfp):
  78. raise AssertionError('Previous TC left hanging git-lock file: %s', lfp)
  79. import gc
  80. gc.collect()
  81. @raises(InvalidGitRepositoryError)
  82. def test_new_should_raise_on_invalid_repo_location(self):
  83. Repo(tempfile.gettempdir())
  84. @raises(NoSuchPathError)
  85. def test_new_should_raise_on_non_existent_path(self):
  86. Repo("repos/foobar")
  87. @with_rw_repo('0.3.2.1')
  88. def test_repo_creation_from_different_paths(self, rw_repo):
  89. r_from_gitdir = Repo(rw_repo.git_dir)
  90. self.assertEqual(r_from_gitdir.git_dir, rw_repo.git_dir)
  91. assert r_from_gitdir.git_dir.endswith('.git')
  92. assert not rw_repo.git.working_dir.endswith('.git')
  93. self.assertEqual(r_from_gitdir.git.working_dir, rw_repo.git.working_dir)
  94. @with_rw_repo('0.3.2.1')
  95. def test_repo_creation_pathlib(self, rw_repo):
  96. if pathlib is None: # pythons bellow 3.4 don't have pathlib
  97. raise SkipTest("pathlib was introduced in 3.4")
  98. r_from_gitdir = Repo(pathlib.Path(rw_repo.git_dir))
  99. self.assertEqual(r_from_gitdir.git_dir, rw_repo.git_dir)
  100. def test_description(self):
  101. txt = "Test repository"
  102. self.rorepo.description = txt
  103. assert_equal(self.rorepo.description, txt)
  104. def test_heads_should_return_array_of_head_objects(self):
  105. for head in self.rorepo.heads:
  106. assert_equal(Head, head.__class__)
  107. def test_heads_should_populate_head_data(self):
  108. for head in self.rorepo.heads:
  109. assert head.name
  110. self.assertIsInstance(head.commit, Commit)
  111. # END for each head
  112. self.assertIsInstance(self.rorepo.heads.master, Head)
  113. self.assertIsInstance(self.rorepo.heads['master'], Head)
  114. def test_tree_from_revision(self):
  115. tree = self.rorepo.tree('0.1.6')
  116. self.assertEqual(len(tree.hexsha), 40)
  117. self.assertEqual(tree.type, "tree")
  118. self.assertEqual(self.rorepo.tree(tree), tree)
  119. # try from invalid revision that does not exist
  120. self.failUnlessRaises(BadName, self.rorepo.tree, 'hello world')
  121. def test_pickleable(self):
  122. pickle.loads(pickle.dumps(self.rorepo))
  123. def test_commit_from_revision(self):
  124. commit = self.rorepo.commit('0.1.4')
  125. self.assertEqual(commit.type, 'commit')
  126. self.assertEqual(self.rorepo.commit(commit), commit)
  127. def test_commits(self):
  128. mc = 10
  129. commits = list(self.rorepo.iter_commits('0.1.6', max_count=mc))
  130. self.assertEqual(len(commits), mc)
  131. c = commits[0]
  132. assert_equal('9a4b1d4d11eee3c5362a4152216376e634bd14cf', c.hexsha)
  133. assert_equal(["c76852d0bff115720af3f27acdb084c59361e5f6"], [p.hexsha for p in c.parents])
  134. assert_equal("ce41fc29549042f1aa09cc03174896cf23f112e3", c.tree.hexsha)
  135. assert_equal("Michael Trier", c.author.name)
  136. assert_equal("mtrier@gmail.com", c.author.email)
  137. assert_equal(1232829715, c.authored_date)
  138. assert_equal(5 * 3600, c.author_tz_offset)
  139. assert_equal("Michael Trier", c.committer.name)
  140. assert_equal("mtrier@gmail.com", c.committer.email)
  141. assert_equal(1232829715, c.committed_date)
  142. assert_equal(5 * 3600, c.committer_tz_offset)
  143. assert_equal("Bumped version 0.1.6\n", c.message)
  144. c = commits[1]
  145. self.assertIsInstance(c.parents, tuple)
  146. def test_trees(self):
  147. mc = 30
  148. num_trees = 0
  149. for tree in self.rorepo.iter_trees('0.1.5', max_count=mc):
  150. num_trees += 1
  151. self.assertIsInstance(tree, Tree)
  152. # END for each tree
  153. self.assertEqual(num_trees, mc)
  154. def _assert_empty_repo(self, repo):
  155. # test all kinds of things with an empty, freshly initialized repo.
  156. # It should throw good errors
  157. # entries should be empty
  158. self.assertEqual(len(repo.index.entries), 0)
  159. # head is accessible
  160. assert repo.head
  161. assert repo.head.ref
  162. assert not repo.head.is_valid()
  163. # we can change the head to some other ref
  164. head_ref = Head.from_path(repo, Head.to_full_path('some_head'))
  165. assert not head_ref.is_valid()
  166. repo.head.ref = head_ref
  167. # is_dirty can handle all kwargs
  168. for args in ((1, 0, 0), (0, 1, 0), (0, 0, 1)):
  169. assert not repo.is_dirty(*args)
  170. # END for each arg
  171. # we can add a file to the index ( if we are not bare )
  172. if not repo.bare:
  173. pass
  174. # END test repos with working tree
  175. @with_rw_directory
  176. def test_clone_from_keeps_env(self, rw_dir):
  177. original_repo = Repo.init(osp.join(rw_dir, "repo"))
  178. environment = {"entry1": "value", "another_entry": "10"}
  179. cloned = Repo.clone_from(original_repo.git_dir, osp.join(rw_dir, "clone"), env=environment)
  180. assert_equal(environment, cloned.git.environment())
  181. @with_rw_directory
  182. def test_clone_from_pathlib(self, rw_dir):
  183. if pathlib is None: # pythons bellow 3.4 don't have pathlib
  184. raise SkipTest("pathlib was introduced in 3.4")
  185. original_repo = Repo.init(osp.join(rw_dir, "repo"))
  186. Repo.clone_from(original_repo.git_dir, pathlib.Path(rw_dir) / "clone_pathlib")
  187. @with_rw_directory
  188. def test_clone_from_pathlib_withConfig(self, rw_dir):
  189. if pathlib is None: # pythons bellow 3.4 don't have pathlib
  190. raise SkipTest("pathlib was introduced in 3.4")
  191. original_repo = Repo.init(osp.join(rw_dir, "repo"))
  192. cloned = Repo.clone_from(original_repo.git_dir, pathlib.Path(rw_dir) / "clone_pathlib_withConfig",
  193. multi_options=["--recurse-submodules=repo",
  194. "--config core.filemode=false",
  195. "--config submodule.repo.update=checkout"])
  196. assert_equal(cloned.config_reader().get_value('submodule', 'active'), 'repo')
  197. assert_equal(cloned.config_reader().get_value('core', 'filemode'), False)
  198. assert_equal(cloned.config_reader().get_value('submodule "repo"', 'update'), 'checkout')
  199. @with_rw_repo('HEAD')
  200. def test_max_chunk_size(self, repo):
  201. class TestOutputStream(object):
  202. def __init__(self, max_chunk_size):
  203. self.max_chunk_size = max_chunk_size
  204. def write(self, b):
  205. assert_true(len(b) <= self.max_chunk_size)
  206. for chunk_size in [16, 128, 1024]:
  207. repo.git.status(output_stream=TestOutputStream(chunk_size), max_chunk_size=chunk_size)
  208. repo.git.log(n=100, output_stream=TestOutputStream(io.DEFAULT_BUFFER_SIZE), max_chunk_size=None)
  209. repo.git.log(n=100, output_stream=TestOutputStream(io.DEFAULT_BUFFER_SIZE), max_chunk_size=-10)
  210. repo.git.log(n=100, output_stream=TestOutputStream(io.DEFAULT_BUFFER_SIZE))
  211. def test_init(self):
  212. prev_cwd = os.getcwd()
  213. os.chdir(tempfile.gettempdir())
  214. git_dir_rela = "repos/foo/bar.git"
  215. del_dir_abs = osp.abspath("repos")
  216. git_dir_abs = osp.abspath(git_dir_rela)
  217. try:
  218. # with specific path
  219. for path in (git_dir_rela, git_dir_abs):
  220. r = Repo.init(path=path, bare=True)
  221. self.assertIsInstance(r, Repo)
  222. assert r.bare is True
  223. assert not r.has_separate_working_tree()
  224. assert osp.isdir(r.git_dir)
  225. self._assert_empty_repo(r)
  226. # test clone
  227. clone_path = path + "_clone"
  228. rc = r.clone(clone_path)
  229. self._assert_empty_repo(rc)
  230. try:
  231. rmtree(clone_path)
  232. except OSError:
  233. # when relative paths are used, the clone may actually be inside
  234. # of the parent directory
  235. pass
  236. # END exception handling
  237. # try again, this time with the absolute version
  238. rc = Repo.clone_from(r.git_dir, clone_path)
  239. self._assert_empty_repo(rc)
  240. rmtree(git_dir_abs)
  241. try:
  242. rmtree(clone_path)
  243. except OSError:
  244. # when relative paths are used, the clone may actually be inside
  245. # of the parent directory
  246. pass
  247. # END exception handling
  248. # END for each path
  249. os.makedirs(git_dir_rela)
  250. os.chdir(git_dir_rela)
  251. r = Repo.init(bare=False)
  252. assert r.bare is False
  253. assert not r.has_separate_working_tree()
  254. self._assert_empty_repo(r)
  255. finally:
  256. try:
  257. rmtree(del_dir_abs)
  258. except OSError:
  259. pass
  260. os.chdir(prev_cwd)
  261. # END restore previous state
  262. def test_bare_property(self):
  263. self.rorepo.bare
  264. def test_daemon_export(self):
  265. orig_val = self.rorepo.daemon_export
  266. self.rorepo.daemon_export = not orig_val
  267. self.assertEqual(self.rorepo.daemon_export, (not orig_val))
  268. self.rorepo.daemon_export = orig_val
  269. self.assertEqual(self.rorepo.daemon_export, orig_val)
  270. def test_alternates(self):
  271. cur_alternates = self.rorepo.alternates
  272. # empty alternates
  273. self.rorepo.alternates = []
  274. self.assertEqual(self.rorepo.alternates, [])
  275. alts = ["other/location", "this/location"]
  276. self.rorepo.alternates = alts
  277. self.assertEqual(alts, self.rorepo.alternates)
  278. self.rorepo.alternates = cur_alternates
  279. def test_repr(self):
  280. assert repr(self.rorepo).startswith('<git.Repo ')
  281. def test_is_dirty_with_bare_repository(self):
  282. orig_value = self.rorepo._bare
  283. self.rorepo._bare = True
  284. assert_false(self.rorepo.is_dirty())
  285. self.rorepo._bare = orig_value
  286. def test_is_dirty(self):
  287. self.rorepo._bare = False
  288. for index in (0, 1):
  289. for working_tree in (0, 1):
  290. for untracked_files in (0, 1):
  291. assert self.rorepo.is_dirty(index, working_tree, untracked_files) in (True, False)
  292. # END untracked files
  293. # END working tree
  294. # END index
  295. orig_val = self.rorepo._bare
  296. self.rorepo._bare = True
  297. assert self.rorepo.is_dirty() is False
  298. self.rorepo._bare = orig_val
  299. @with_rw_repo('HEAD')
  300. def test_is_dirty_with_path(self, rwrepo):
  301. assert rwrepo.is_dirty(path="git") is False
  302. with open(osp.join(rwrepo.working_dir, "git", "util.py"), "at") as f:
  303. f.write("junk")
  304. assert rwrepo.is_dirty(path="git") is True
  305. assert rwrepo.is_dirty(path="doc") is False
  306. rwrepo.git.add(Git.polish_url(osp.join("git", "util.py")))
  307. assert rwrepo.is_dirty(index=False, path="git") is False
  308. assert rwrepo.is_dirty(path="git") is True
  309. with open(osp.join(rwrepo.working_dir, "doc", "no-such-file.txt"), "wt") as f:
  310. f.write("junk")
  311. assert rwrepo.is_dirty(path="doc") is False
  312. assert rwrepo.is_dirty(untracked_files=True, path="doc") is True
  313. def test_head(self):
  314. self.assertEqual(self.rorepo.head.reference.object, self.rorepo.active_branch.object)
  315. def test_index(self):
  316. index = self.rorepo.index
  317. self.assertIsInstance(index, IndexFile)
  318. def test_tag(self):
  319. assert self.rorepo.tag('refs/tags/0.1.5').commit
  320. def test_archive(self):
  321. tmpfile = tempfile.mktemp(suffix='archive-test')
  322. with open(tmpfile, 'wb') as stream:
  323. self.rorepo.archive(stream, '0.1.6', path='doc')
  324. assert stream.tell()
  325. os.remove(tmpfile)
  326. @patch.object(Git, '_call_process')
  327. def test_should_display_blame_information(self, git):
  328. git.return_value = fixture('blame')
  329. b = self.rorepo.blame('master', 'lib/git.py')
  330. assert_equal(13, len(b))
  331. assert_equal(2, len(b[0]))
  332. # assert_equal(25, reduce(lambda acc, x: acc + len(x[-1]), b))
  333. assert_equal(hash(b[0][0]), hash(b[9][0]))
  334. c = b[0][0]
  335. assert_true(git.called)
  336. assert_equal('634396b2f541a9f2d58b00be1a07f0c358b999b3', c.hexsha)
  337. assert_equal('Tom Preston-Werner', c.author.name)
  338. assert_equal('tom@mojombo.com', c.author.email)
  339. assert_equal(1191997100, c.authored_date)
  340. assert_equal('Tom Preston-Werner', c.committer.name)
  341. assert_equal('tom@mojombo.com', c.committer.email)
  342. assert_equal(1191997100, c.committed_date)
  343. self.assertRaisesRegexp(ValueError, "634396b2f541a9f2d58b00be1a07f0c358b999b3 missing", lambda: c.message)
  344. # test the 'lines per commit' entries
  345. tlist = b[0][1]
  346. assert_true(tlist)
  347. assert_true(isinstance(tlist[0], string_types))
  348. assert_true(len(tlist) < sum(len(t) for t in tlist)) # test for single-char bug
  349. # BINARY BLAME
  350. git.return_value = fixture('blame_binary')
  351. blames = self.rorepo.blame('master', 'rps')
  352. self.assertEqual(len(blames), 2)
  353. def test_blame_real(self):
  354. c = 0
  355. nml = 0 # amount of multi-lines per blame
  356. for item in self.rorepo.head.commit.tree.traverse(
  357. predicate=lambda i, d: i.type == 'blob' and i.path.endswith('.py')):
  358. c += 1
  359. for b in self.rorepo.blame(self.rorepo.head, item.path):
  360. nml += int(len(b[1]) > 1)
  361. # END for each item to traverse
  362. assert c, "Should have executed at least one blame command"
  363. assert nml, "There should at least be one blame commit that contains multiple lines"
  364. @patch.object(Git, '_call_process')
  365. def test_blame_incremental(self, git):
  366. # loop over two fixtures, create a test fixture for 2.11.1+ syntax
  367. for git_fixture in ('blame_incremental', 'blame_incremental_2.11.1_plus'):
  368. git.return_value = fixture(git_fixture)
  369. blame_output = self.rorepo.blame_incremental('9debf6b0aafb6f7781ea9d1383c86939a1aacde3', 'AUTHORS')
  370. blame_output = list(blame_output)
  371. self.assertEqual(len(blame_output), 5)
  372. # Check all outputted line numbers
  373. ranges = flatten([entry.linenos for entry in blame_output])
  374. self.assertEqual(ranges, flatten([range(2, 3), range(14, 15), range(1, 2), range(3, 14), range(15, 17)]))
  375. commits = [entry.commit.hexsha[:7] for entry in blame_output]
  376. self.assertEqual(commits, ['82b8902', '82b8902', 'c76852d', 'c76852d', 'c76852d'])
  377. # Original filenames
  378. self.assertSequenceEqual([entry.orig_path for entry in blame_output], [u'AUTHORS'] * len(blame_output))
  379. # Original line numbers
  380. orig_ranges = flatten([entry.orig_linenos for entry in blame_output])
  381. self.assertEqual(orig_ranges, flatten([range(2, 3), range(14, 15), range(1, 2), range(2, 13), range(13, 15)])) # noqa E501
  382. @patch.object(Git, '_call_process')
  383. def test_blame_complex_revision(self, git):
  384. git.return_value = fixture('blame_complex_revision')
  385. res = self.rorepo.blame("HEAD~10..HEAD", "README.md")
  386. self.assertEqual(len(res), 1)
  387. self.assertEqual(len(res[0][1]), 83, "Unexpected amount of parsed blame lines")
  388. @skipIf(HIDE_WINDOWS_KNOWN_ERRORS and Git.is_cygwin(),
  389. """FIXME: File "C:\\projects\\gitpython\\git\\cmd.py", line 671, in execute
  390. raise GitCommandError(command, status, stderr_value, stdout_value)
  391. GitCommandError: Cmd('git') failed due to: exit code(128)
  392. cmdline: git add 1__��ava verb��ten 1_test _myfile 1_test_other_file
  393. 1_��ava-----verb��ten
  394. stderr: 'fatal: pathspec '"1__çava verböten"' did not match any files'
  395. """)
  396. @with_rw_repo('HEAD', bare=False)
  397. def test_untracked_files(self, rwrepo):
  398. for run, (repo_add, is_invoking_git) in enumerate((
  399. (rwrepo.index.add, False),
  400. (rwrepo.git.add, True),
  401. )):
  402. base = rwrepo.working_tree_dir
  403. files = (join_path_native(base, u"%i_test _myfile" % run),
  404. join_path_native(base, "%i_test_other_file" % run),
  405. join_path_native(base, u"%i__çava verböten" % run),
  406. join_path_native(base, u"%i_çava-----verböten" % run))
  407. num_recently_untracked = 0
  408. for fpath in files:
  409. with open(fpath, "wb"):
  410. pass
  411. untracked_files = rwrepo.untracked_files
  412. num_recently_untracked = len(untracked_files)
  413. # assure we have all names - they are relative to the git-dir
  414. num_test_untracked = 0
  415. for utfile in untracked_files:
  416. num_test_untracked += join_path_native(base, utfile) in files
  417. self.assertEqual(len(files), num_test_untracked)
  418. if is_win and not PY3 and is_invoking_git:
  419. ## On Windows, shell needed when passing unicode cmd-args.
  420. #
  421. repo_add = fnt.partial(repo_add, shell=True)
  422. untracked_files = [win_encode(f) for f in untracked_files]
  423. repo_add(untracked_files)
  424. self.assertEqual(len(rwrepo.untracked_files), (num_recently_untracked - len(files)))
  425. # end for each run
  426. def test_config_reader(self):
  427. reader = self.rorepo.config_reader() # all config files
  428. assert reader.read_only
  429. reader = self.rorepo.config_reader("repository") # single config file
  430. assert reader.read_only
  431. def test_config_writer(self):
  432. for config_level in self.rorepo.config_level:
  433. try:
  434. with self.rorepo.config_writer(config_level) as writer:
  435. self.assertFalse(writer.read_only)
  436. except IOError:
  437. # its okay not to get a writer for some configuration files if we
  438. # have no permissions
  439. pass
  440. def test_config_level_paths(self):
  441. for config_level in self.rorepo.config_level:
  442. assert self.rorepo._get_config_path(config_level)
  443. def test_creation_deletion(self):
  444. # just a very quick test to assure it generally works. There are
  445. # specialized cases in the test_refs module
  446. head = self.rorepo.create_head("new_head", "HEAD~1")
  447. self.rorepo.delete_head(head)
  448. try:
  449. tag = self.rorepo.create_tag("new_tag", "HEAD~2")
  450. finally:
  451. self.rorepo.delete_tag(tag)
  452. with self.rorepo.config_writer():
  453. pass
  454. try:
  455. remote = self.rorepo.create_remote("new_remote", "git@server:repo.git")
  456. finally:
  457. self.rorepo.delete_remote(remote)
  458. def test_comparison_and_hash(self):
  459. # this is only a preliminary test, more testing done in test_index
  460. self.assertEqual(self.rorepo, self.rorepo)
  461. self.assertFalse(self.rorepo != self.rorepo)
  462. self.assertEqual(len({self.rorepo, self.rorepo}), 1)
  463. @with_rw_directory
  464. def test_tilde_and_env_vars_in_repo_path(self, rw_dir):
  465. ph = os.environ.get('HOME')
  466. try:
  467. os.environ['HOME'] = rw_dir
  468. Repo.init(osp.join('~', 'test.git'), bare=True)
  469. os.environ['FOO'] = rw_dir
  470. Repo.init(osp.join('$FOO', 'test.git'), bare=True)
  471. finally:
  472. if ph:
  473. os.environ['HOME'] = ph
  474. del os.environ['FOO']
  475. # end assure HOME gets reset to what it was
  476. def test_git_cmd(self):
  477. # test CatFileContentStream, just to be very sure we have no fencepost errors
  478. # last \n is the terminating newline that it expects
  479. l1 = b"0123456789\n"
  480. l2 = b"abcdefghijklmnopqrstxy\n"
  481. l3 = b"z\n"
  482. d = l1 + l2 + l3 + b"\n"
  483. l1p = l1[:5]
  484. # full size
  485. # size is without terminating newline
  486. def mkfull():
  487. return Git.CatFileContentStream(len(d) - 1, BytesIO(d))
  488. ts = 5
  489. def mktiny():
  490. return Git.CatFileContentStream(ts, BytesIO(d))
  491. # readlines no limit
  492. s = mkfull()
  493. lines = s.readlines()
  494. self.assertEqual(len(lines), 3)
  495. self.assertTrue(lines[-1].endswith(b'\n'), lines[-1])
  496. self.assertEqual(s._stream.tell(), len(d)) # must have scrubbed to the end
  497. # realines line limit
  498. s = mkfull()
  499. lines = s.readlines(5)
  500. self.assertEqual(len(lines), 1)
  501. # readlines on tiny sections
  502. s = mktiny()
  503. lines = s.readlines()
  504. self.assertEqual(len(lines), 1)
  505. self.assertEqual(lines[0], l1p)
  506. self.assertEqual(s._stream.tell(), ts + 1)
  507. # readline no limit
  508. s = mkfull()
  509. self.assertEqual(s.readline(), l1)
  510. self.assertEqual(s.readline(), l2)
  511. self.assertEqual(s.readline(), l3)
  512. self.assertEqual(s.readline(), b'')
  513. self.assertEqual(s._stream.tell(), len(d))
  514. # readline limit
  515. s = mkfull()
  516. self.assertEqual(s.readline(5), l1p)
  517. self.assertEqual(s.readline(), l1[5:])
  518. # readline on tiny section
  519. s = mktiny()
  520. self.assertEqual(s.readline(), l1p)
  521. self.assertEqual(s.readline(), b'')
  522. self.assertEqual(s._stream.tell(), ts + 1)
  523. # read no limit
  524. s = mkfull()
  525. self.assertEqual(s.read(), d[:-1])
  526. self.assertEqual(s.read(), b'')
  527. self.assertEqual(s._stream.tell(), len(d))
  528. # read limit
  529. s = mkfull()
  530. self.assertEqual(s.read(5), l1p)
  531. self.assertEqual(s.read(6), l1[5:])
  532. self.assertEqual(s._stream.tell(), 5 + 6) # its not yet done
  533. # read tiny
  534. s = mktiny()
  535. self.assertEqual(s.read(2), l1[:2])
  536. self.assertEqual(s._stream.tell(), 2)
  537. self.assertEqual(s.read(), l1[2:ts])
  538. self.assertEqual(s._stream.tell(), ts + 1)
  539. def _assert_rev_parse_types(self, name, rev_obj):
  540. rev_parse = self.rorepo.rev_parse
  541. if rev_obj.type == 'tag':
  542. rev_obj = rev_obj.object
  543. # tree and blob type
  544. obj = rev_parse(name + '^{tree}')
  545. self.assertEqual(obj, rev_obj.tree)
  546. obj = rev_parse(name + ':CHANGES')
  547. self.assertEqual(obj.type, 'blob')
  548. self.assertEqual(obj.path, 'CHANGES')
  549. self.assertEqual(rev_obj.tree['CHANGES'], obj)
  550. def _assert_rev_parse(self, name):
  551. """tries multiple different rev-parse syntaxes with the given name
  552. :return: parsed object"""
  553. rev_parse = self.rorepo.rev_parse
  554. orig_obj = rev_parse(name)
  555. if orig_obj.type == 'tag':
  556. obj = orig_obj.object
  557. else:
  558. obj = orig_obj
  559. # END deref tags by default
  560. # try history
  561. rev = name + "~"
  562. obj2 = rev_parse(rev)
  563. self.assertEqual(obj2, obj.parents[0])
  564. self._assert_rev_parse_types(rev, obj2)
  565. # history with number
  566. ni = 11
  567. history = [obj.parents[0]]
  568. for pn in range(ni):
  569. history.append(history[-1].parents[0])
  570. # END get given amount of commits
  571. for pn in range(11):
  572. rev = name + "~%i" % (pn + 1)
  573. obj2 = rev_parse(rev)
  574. self.assertEqual(obj2, history[pn])
  575. self._assert_rev_parse_types(rev, obj2)
  576. # END history check
  577. # parent ( default )
  578. rev = name + "^"
  579. obj2 = rev_parse(rev)
  580. self.assertEqual(obj2, obj.parents[0])
  581. self._assert_rev_parse_types(rev, obj2)
  582. # parent with number
  583. for pn, parent in enumerate(obj.parents):
  584. rev = name + "^%i" % (pn + 1)
  585. self.assertEqual(rev_parse(rev), parent)
  586. self._assert_rev_parse_types(rev, parent)
  587. # END for each parent
  588. return orig_obj
  589. @with_rw_repo('HEAD', bare=False)
  590. def test_rw_rev_parse(self, rwrepo):
  591. # verify it does not confuse branches with hexsha ids
  592. ahead = rwrepo.create_head('aaaaaaaa')
  593. assert(rwrepo.rev_parse(str(ahead)) == ahead.commit)
  594. def test_rev_parse(self):
  595. rev_parse = self.rorepo.rev_parse
  596. # try special case: This one failed at some point, make sure its fixed
  597. self.assertEqual(rev_parse("33ebe").hexsha, "33ebe7acec14b25c5f84f35a664803fcab2f7781")
  598. # start from reference
  599. num_resolved = 0
  600. for ref_no, ref in enumerate(Reference.iter_items(self.rorepo)):
  601. path_tokens = ref.path.split("/")
  602. for pt in range(len(path_tokens)):
  603. path_section = '/'.join(path_tokens[-(pt + 1):])
  604. try:
  605. obj = self._assert_rev_parse(path_section)
  606. self.assertEqual(obj.type, ref.object.type)
  607. num_resolved += 1
  608. except (BadName, BadObject):
  609. print("failed on %s" % path_section)
  610. # is fine, in case we have something like 112, which belongs to remotes/rname/merge-requests/112
  611. pass
  612. # END exception handling
  613. # END for each token
  614. if ref_no == 3 - 1:
  615. break
  616. # END for each reference
  617. assert num_resolved
  618. # it works with tags !
  619. tag = self._assert_rev_parse('0.1.4')
  620. self.assertEqual(tag.type, 'tag')
  621. # try full sha directly ( including type conversion )
  622. self.assertEqual(tag.object, rev_parse(tag.object.hexsha))
  623. self._assert_rev_parse_types(tag.object.hexsha, tag.object)
  624. # multiple tree types result in the same tree: HEAD^{tree}^{tree}:CHANGES
  625. rev = '0.1.4^{tree}^{tree}'
  626. self.assertEqual(rev_parse(rev), tag.object.tree)
  627. self.assertEqual(rev_parse(rev + ':CHANGES'), tag.object.tree['CHANGES'])
  628. # try to get parents from first revision - it should fail as no such revision
  629. # exists
  630. first_rev = "33ebe7acec14b25c5f84f35a664803fcab2f7781"
  631. commit = rev_parse(first_rev)
  632. self.assertEqual(len(commit.parents), 0)
  633. self.assertEqual(commit.hexsha, first_rev)
  634. self.failUnlessRaises(BadName, rev_parse, first_rev + "~")
  635. self.failUnlessRaises(BadName, rev_parse, first_rev + "^")
  636. # short SHA1
  637. commit2 = rev_parse(first_rev[:20])
  638. self.assertEqual(commit2, commit)
  639. commit2 = rev_parse(first_rev[:5])
  640. self.assertEqual(commit2, commit)
  641. # todo: dereference tag into a blob 0.1.7^{blob} - quite a special one
  642. # needs a tag which points to a blob
  643. # ref^0 returns commit being pointed to, same with ref~0, and ^{}
  644. tag = rev_parse('0.1.4')
  645. for token in (('~0', '^0', '^{}')):
  646. self.assertEqual(tag.object, rev_parse('0.1.4%s' % token))
  647. # END handle multiple tokens
  648. # try partial parsing
  649. max_items = 40
  650. for i, binsha in enumerate(self.rorepo.odb.sha_iter()):
  651. self.assertEqual(rev_parse(bin_to_hex(binsha)[:8 - (i % 2)].decode('ascii')).binsha, binsha)
  652. if i > max_items:
  653. # this is rather slow currently, as rev_parse returns an object
  654. # which requires accessing packs, it has some additional overhead
  655. break
  656. # END for each binsha in repo
  657. # missing closing brace commit^{tree
  658. self.failUnlessRaises(ValueError, rev_parse, '0.1.4^{tree')
  659. # missing starting brace
  660. self.failUnlessRaises(ValueError, rev_parse, '0.1.4^tree}')
  661. # REVLOG
  662. #######
  663. head = self.rorepo.head
  664. # need to specify a ref when using the @ syntax
  665. self.failUnlessRaises(BadObject, rev_parse, "%s@{0}" % head.commit.hexsha)
  666. # uses HEAD.ref by default
  667. self.assertEqual(rev_parse('@{0}'), head.commit)
  668. if not head.is_detached:
  669. refspec = '%s@{0}' % head.ref.name
  670. self.assertEqual(rev_parse(refspec), head.ref.commit)
  671. # all additional specs work as well
  672. self.assertEqual(rev_parse(refspec + "^{tree}"), head.commit.tree)
  673. self.assertEqual(rev_parse(refspec + ":CHANGES").type, 'blob')
  674. # END operate on non-detached head
  675. # position doesn't exist
  676. self.failUnlessRaises(IndexError, rev_parse, '@{10000}')
  677. # currently, nothing more is supported
  678. self.failUnlessRaises(NotImplementedError, rev_parse, "@{1 week ago}")
  679. # the last position
  680. assert rev_parse('@{1}') != head.commit
  681. def test_repo_odbtype(self):
  682. target_type = GitCmdObjectDB
  683. self.assertIsInstance(self.rorepo.odb, target_type)
  684. def test_submodules(self):
  685. self.assertEqual(len(self.rorepo.submodules), 1) # non-recursive
  686. self.assertGreaterEqual(len(list(self.rorepo.iter_submodules())), 2)
  687. self.assertIsInstance(self.rorepo.submodule("gitdb"), Submodule)
  688. self.failUnlessRaises(ValueError, self.rorepo.submodule, "doesn't exist")
  689. @with_rw_repo('HEAD', bare=False)
  690. def test_submodule_update(self, rwrepo):
  691. # fails in bare mode
  692. rwrepo._bare = True
  693. self.failUnlessRaises(InvalidGitRepositoryError, rwrepo.submodule_update)
  694. rwrepo._bare = False
  695. # test create submodule
  696. sm = rwrepo.submodules[0]
  697. sm = rwrepo.create_submodule("my_new_sub", "some_path", join_path_native(self.rorepo.working_tree_dir, sm.path))
  698. self.assertIsInstance(sm, Submodule)
  699. # note: the rest of this functionality is tested in test_submodule
  700. @with_rw_repo('HEAD')
  701. def test_git_file(self, rwrepo):
  702. # Move the .git directory to another location and create the .git file.
  703. real_path_abs = osp.abspath(join_path_native(rwrepo.working_tree_dir, '.real'))
  704. os.rename(rwrepo.git_dir, real_path_abs)
  705. git_file_path = join_path_native(rwrepo.working_tree_dir, '.git')
  706. with open(git_file_path, 'wb') as fp:
  707. fp.write(fixture('git_file'))
  708. # Create a repo and make sure it's pointing to the relocated .git directory.
  709. git_file_repo = Repo(rwrepo.working_tree_dir)
  710. self.assertEqual(osp.abspath(git_file_repo.git_dir), real_path_abs)
  711. # Test using an absolute gitdir path in the .git file.
  712. with open(git_file_path, 'wb') as fp:
  713. fp.write(('gitdir: %s\n' % real_path_abs).encode('ascii'))
  714. git_file_repo = Repo(rwrepo.working_tree_dir)
  715. self.assertEqual(osp.abspath(git_file_repo.git_dir), real_path_abs)
  716. def test_file_handle_leaks(self):
  717. def last_commit(repo, rev, path):
  718. commit = next(repo.iter_commits(rev, path, max_count=1))
  719. commit.tree[path]
  720. # This is based on this comment
  721. # https://github.com/gitpython-developers/GitPython/issues/60#issuecomment-23558741
  722. # And we expect to set max handles to a low value, like 64
  723. # You should set ulimit -n X, see .travis.yml
  724. # The loops below would easily create 500 handles if these would leak (4 pipes + multiple mapped files)
  725. for _ in range(64):
  726. for repo_type in (GitCmdObjectDB, GitDB):
  727. repo = Repo(self.rorepo.working_tree_dir, odbt=repo_type)
  728. last_commit(repo, 'master', 'git/test/test_base.py')
  729. # end for each repository type
  730. # end for each iteration
  731. def test_remote_method(self):
  732. self.failUnlessRaises(ValueError, self.rorepo.remote, 'foo-blue')
  733. self.assertIsInstance(self.rorepo.remote(name='origin'), Remote)
  734. @with_rw_directory
  735. def test_empty_repo(self, rw_dir):
  736. """Assure we can handle empty repositories"""
  737. r = Repo.init(rw_dir, mkdir=False)
  738. # It's ok not to be able to iterate a commit, as there is none
  739. self.failUnlessRaises(ValueError, r.iter_commits)
  740. self.assertEqual(r.active_branch.name, 'master')
  741. assert not r.active_branch.is_valid(), "Branch is yet to be born"
  742. # actually, when trying to create a new branch without a commit, git itself fails
  743. # We should, however, not fail ungracefully
  744. self.failUnlessRaises(BadName, r.create_head, 'foo')
  745. self.failUnlessRaises(BadName, r.create_head, 'master')
  746. # It's expected to not be able to access a tree
  747. self.failUnlessRaises(ValueError, r.tree)
  748. new_file_path = osp.join(rw_dir, "new_file.ext")
  749. touch(new_file_path)
  750. r.index.add([new_file_path])
  751. r.index.commit("initial commit\nBAD MESSAGE 1\n")
  752. # Now a branch should be creatable
  753. nb = r.create_head('foo')
  754. assert nb.is_valid()
  755. with open(new_file_path, 'w') as f:
  756. f.write('Line 1\n')
  757. r.index.add([new_file_path])
  758. r.index.commit("add line 1\nBAD MESSAGE 2\n")
  759. with open('%s/.git/logs/refs/heads/master' % (rw_dir,), 'r') as f:
  760. contents = f.read()
  761. assert 'BAD MESSAGE' not in contents, 'log is corrupt'
  762. def test_merge_base(self):
  763. repo = self.rorepo
  764. c1 = 'f6aa8d1'
  765. c2 = repo.commit('d46e3fe')
  766. c3 = '763ef75'
  767. self.failUnlessRaises(ValueError, repo.merge_base)
  768. self.failUnlessRaises(ValueError, repo.merge_base, 'foo')
  769. # two commit merge-base
  770. res = repo.merge_base(c1, c2)
  771. self.assertIsInstance(res, list)
  772. self.assertEqual(len(res), 1)
  773. self.assertIsInstance(res[0], Commit)
  774. self.assertTrue(res[0].hexsha.startswith('3936084'))
  775. for kw in ('a', 'all'):
  776. res = repo.merge_base(c1, c2, c3, **{kw: True})
  777. self.assertIsInstance(res, list)
  778. self.assertEqual(len(res), 1)
  779. # end for each keyword signalling all merge-bases to be returned
  780. # Test for no merge base - can't do as we have
  781. self.failUnlessRaises(GitCommandError, repo.merge_base, c1, 'ffffff')
  782. def test_is_ancestor(self):
  783. git = self.rorepo.git
  784. if git.version_info[:3] < (1, 8, 0):
  785. raise SkipTest("git merge-base --is-ancestor feature unsupported")
  786. repo = self.rorepo
  787. c1 = 'f6aa8d1'
  788. c2 = '763ef75'
  789. self.assertTrue(repo.is_ancestor(c1, c1))
  790. self.assertTrue(repo.is_ancestor("master", "master"))
  791. self.assertTrue(repo.is_ancestor(c1, c2))
  792. self.assertTrue(repo.is_ancestor(c1, "master"))
  793. self.assertFalse(repo.is_ancestor(c2, c1))
  794. self.assertFalse(repo.is_ancestor("master", c1))
  795. for i, j in itertools.permutations([c1, 'ffffff', ''], r=2):
  796. self.assertRaises(GitCommandError, repo.is_ancestor, i, j)
  797. @with_rw_directory
  798. def test_git_work_tree_dotgit(self, rw_dir):
  799. """Check that we find .git as a worktree file and find the worktree
  800. based on it."""
  801. git = Git(rw_dir)
  802. if git.version_info[:3] < (2, 5, 1):
  803. raise SkipTest("worktree feature unsupported")
  804. rw_master = self.rorepo.clone(join_path_native(rw_dir, 'master_repo'))
  805. branch = rw_master.create_head('aaaaaaaa')
  806. worktree_path = join_path_native(rw_dir, 'worktree_repo')
  807. if Git.is_cygwin():
  808. worktree_path = cygpath(worktree_path)
  809. rw_master.git.worktree('add', worktree_path, branch.name)
  810. # this ensures that we can read the repo's gitdir correctly
  811. repo = Repo(worktree_path)
  812. self.assertIsInstance(repo, Repo)
  813. # this ensures we're able to actually read the refs in the tree, which
  814. # means we can read commondir correctly.
  815. commit = repo.head.commit
  816. self.assertIsInstance(commit, Object)
  817. # this ensures we can read the remotes, which confirms we're reading
  818. # the config correctly.
  819. origin = repo.remotes.origin
  820. self.assertIsInstance(origin, Remote)
  821. self.assertIsInstance(repo.heads['aaaaaaaa'], Head)
  822. @with_rw_directory
  823. def test_git_work_tree_env(self, rw_dir):
  824. """Check that we yield to GIT_WORK_TREE"""
  825. # clone a repo
  826. # move .git directory to a subdirectory
  827. # set GIT_DIR and GIT_WORK_TREE appropriately
  828. # check that repo.working_tree_dir == rw_dir
  829. self.rorepo.clone(join_path_native(rw_dir, 'master_repo'))
  830. repo_dir = join_path_native(rw_dir, 'master_repo')
  831. old_git_dir = join_path_native(repo_dir, '.git')
  832. new_subdir = join_path_native(repo_dir, 'gitdir')
  833. new_git_dir = join_path_native(new_subdir, 'git')
  834. os.mkdir(new_subdir)
  835. os.rename(old_git_dir, new_git_dir)
  836. oldenv = os.environ.copy()
  837. os.environ['GIT_DIR'] = new_git_dir
  838. os.environ['GIT_WORK_TREE'] = repo_dir
  839. try:
  840. r = Repo()
  841. self.assertEqual(r.working_tree_dir, repo_dir)
  842. self.assertEqual(r.working_dir, repo_dir)
  843. finally:
  844. os.environ = oldenv
  845. @with_rw_directory
  846. def test_rebasing(self, rw_dir):
  847. r = Repo.init(rw_dir)
  848. fp = osp.join(rw_dir, 'hello.txt')
  849. r.git.commit("--allow-empty", message="init",)
  850. with open(fp, 'w') as fs:
  851. fs.write("hello world")
  852. r.git.add(Git.polish_url(fp))
  853. r.git.commit(message="English")
  854. self.assertEqual(r.currently_rebasing_on(), None)
  855. r.git.checkout("HEAD^1")
  856. with open(fp, 'w') as fs:
  857. fs.write("Hola Mundo")
  858. r.git.add(Git.polish_url(fp))
  859. r.git.commit(message="Spanish")
  860. commitSpanish = r.commit()
  861. try:
  862. r.git.rebase("master")
  863. except GitCommandError:
  864. pass
  865. self.assertEqual(r.currently_rebasing_on(), commitSpanish)