def test_new_commit(self): repo = self.repo message = 'New commit.\n\nMessage with non-ascii chars: ééé.\n' committer = Signature('John Doe', '*****@*****.**', 12346, 0) author = Signature('J. David Ibáñez', '*****@*****.**', 12345, 0, encoding='utf-8') tree = '967fce8df97cc71722d3c2a5930ef3e6f1d27b12' tree_prefix = tree[:5] too_short_prefix = tree[:3] parents = [COMMIT_SHA[:5]] self.assertRaises(ValueError, repo.create_commit, None, author, committer, message, too_short_prefix, parents) sha = repo.create_commit(None, author, committer, message, tree_prefix, parents) commit = repo[sha] self.assertEqual(GIT_OBJ_COMMIT, commit.type) self.assertEqual('98286caaab3f1fde5bf52c8369b2b0423bad743b', commit.hex) self.assertEqual(None, commit.message_encoding) self.assertEqual(message, commit.message) self.assertEqual(12346, commit.commit_time) self.assertEqualSignature(committer, commit.committer) self.assertEqualSignature(author, commit.author) self.assertEqual(tree, commit.tree.hex) self.assertEqual(Oid(hex=tree), commit.tree_id) self.assertEqual(1, len(commit.parents)) self.assertEqual(COMMIT_SHA, commit.parents[0].hex) self.assertEqual(Oid(hex=COMMIT_SHA), commit.parent_ids[0])
def test_new_commit(self): repo = self.repo message = 'New commit.\n\nMessage with non-ascii chars: ééé.\n' committer = Signature('John Doe', '*****@*****.**', 12346, 0) author = Signature('J. David Ibáñez', '*****@*****.**', 12345, 0, encoding='utf-8') tree = '967fce8df97cc71722d3c2a5930ef3e6f1d27b12' tree_prefix = tree[:5] too_short_prefix = tree[:3] parents = [COMMIT_SHA[:5]] with pytest.raises(ValueError): repo.create_commit(None, author, committer, message, too_short_prefix, parents) sha = repo.create_commit(None, author, committer, message, tree_prefix, parents) commit = repo[sha] assert GIT_OBJ_COMMIT == commit.type assert '98286caaab3f1fde5bf52c8369b2b0423bad743b' == commit.hex assert commit.message_encoding is None assert message == commit.message assert 12346 == commit.commit_time assert committer == commit.committer assert author == commit.author assert tree == commit.tree.hex assert Oid(hex=tree) == commit.tree_id assert 1 == len(commit.parents) assert COMMIT_SHA == commit.parents[0].hex assert Oid(hex=COMMIT_SHA) == commit.parent_ids[0]
def test_new_commit_encoding(self): repo = self.repo encoding = 'iso-8859-1' message = 'New commit.\n\nMessage with non-ascii chars: ééé.\n' committer = Signature('John Doe', '*****@*****.**', 12346, 0, encoding) author = Signature('J. David Ibáñez', '*****@*****.**', 12345, 0, encoding) tree = '967fce8df97cc71722d3c2a5930ef3e6f1d27b12' tree_prefix = tree[:5] parents = [COMMIT_SHA[:5]] sha = repo.create_commit(None, author, committer, message, tree_prefix, parents, encoding) commit = repo[sha] assert GIT_OBJ_COMMIT == commit.type assert 'iso-8859-1' == commit.message_encoding assert message.encode(encoding) == commit.raw_message assert 12346 == commit.commit_time assert committer == commit.committer assert author == commit.author assert tree == commit.tree.hex assert Oid(hex=tree) == commit.tree_id assert 1 == len(commit.parents) assert COMMIT_SHA == commit.parents[0].hex assert Oid(hex=COMMIT_SHA) == commit.parent_ids[0]
def test_new_commit_encoding(self): repo = self.repo encoding = 'iso-8859-1' message = 'New commit.\n\nMessage with non-ascii chars: ééé.\n' committer = Signature('John Doe', '*****@*****.**', 12346, 0, encoding) author = Signature('J. David Ibáñez', '*****@*****.**', 12345, 0, encoding) tree = '967fce8df97cc71722d3c2a5930ef3e6f1d27b12' tree_prefix = tree[:5] parents = [COMMIT_SHA[:5]] sha = repo.create_commit(None, author, committer, message, tree_prefix, parents, encoding) commit = repo[sha] self.assertEqual(GIT_OBJ_COMMIT, commit.type) self.assertEqual('iso-8859-1', commit.message_encoding) self.assertEqual(message.encode(encoding), commit.raw_message) self.assertEqual(12346, commit.commit_time) self.assertEqualSignature(committer, commit.committer) self.assertEqualSignature(author, commit.author) self.assertEqual(tree, commit.tree.hex) self.assertEqual(Oid(hex=tree), commit.tree_id) self.assertEqual(1, len(commit.parents)) self.assertEqual(COMMIT_SHA, commit.parents[0].hex) self.assertEqual(Oid(hex=COMMIT_SHA), commit.parent_ids[0])
def keys(self): table = self.items_table keys = [] for k in range(table.TABLE_SIZE): if table[k] != table.EMPTY_PAGE_ID: page = ItemPage(self.repo[Oid(table[k])].data) for key in page.keys(): keys.append(self.repo[Oid(key)].data) return keys
def test_hash(self): s = set() s.add(Oid(raw=RAW)) s.add(Oid(hex=HEX)) self.assertEqual(len(s), 1) s.add(Oid(hex="0000000000000000000000000000000000000000")) s.add(Oid(hex="0000000000000000000000000000000000000001")) self.assertEqual(len(s), 3)
def test_hash(self): s = set() s.add(Oid(raw=RAW)) s.add(Oid(hex=HEX)) assert len(s) == 1 s.add(Oid(hex="0000000000000000000000000000000000000000")) s.add(Oid(hex="0000000000000000000000000000000000000001")) assert len(s) == 3
def test_hex_bytes(self): if version_info[0] == 2: hex = bytes(HEX) oid = Oid(hex=hex) assert oid.raw == RAW assert str(oid) == HEX else: hex = bytes(HEX, "ascii") with pytest.raises(TypeError): Oid(hex=hex)
def test_update_tips(self): remote = self.repo.remotes[0] self.i = 0 self.tips = [('refs/remotes/origin/master', Oid(hex='0' * 40), Oid(hex='784855caf26449a1914d2cf62d12b9374d76ae78')), ('refs/tags/root', Oid(hex='0' * 40), Oid(hex='3d2962987c695a29f1f80b6c3aa4ec046ef44369'))] def ut_cb(name, old, new): self.assertEqual(self.tips[self.i], (name, old, new)) self.i += 1 remote.update_tips = ut_cb remote.fetch()
def _get_page(self, h_key, table=None): table = table or self.items_table entry_no = self._entry_no(h_key, 0) try: return ItemPage(self.repo[Oid(table[entry_no])].data) except TypeError: return ItemPage()
def _fetch_commit_ids(self): try: with open(f"{ROOT}/.gras-cache/{self.repo_name}_commits.txt", "rb") as fp: self.commits = pickle.load(fp) self.commits = [Oid(hex=x) for x in self.commits] logger.info(f"TOTAL COMMITS: {len(self.commits)}") return self.commits except FileNotFoundError: logger.error("Commits file not present, dumping...") self.commits = set() with concurrent.futures.ThreadPoolExecutor(max_workers=THREADS) as executor: process = {executor.submit(self.__fetch_branch_commits, branch_target): branch_target for branch_target in self.branches.items()} for future in concurrent.futures.as_completed(process): branch_target = process[future] logger.info(f"Fetched for {branch_target[0]}, Total: {len(self.commits)}") logger.info(f"TOTAL COMMITS: {len(self.commits)}") with open(f"{ROOT}/.gras-cache/{self.repo_name}_commits.txt", "wb") as fp: temp = [x.hex for x in self.commits] pickle.dump(temp, fp) del temp
def _read_replaces(self): prefix = 'refs/replace/' prefix_len = len(prefix) for ref in self._repository.references: if ref.startswith(prefix): self._replaces[ Oid(hex=ref[prefix_len:]) ] = self._repository.references[ref].target
def test_set_head(self): # Test setting a detatched HEAD. self.repo.set_head(Oid(hex=PARENT_SHA)) self.assertEqual(self.repo.head.target.hex, PARENT_SHA) # And test setting a normal HEAD. self.repo.set_head("refs/heads/master") self.assertEqual(self.repo.head.name, "refs/heads/master") self.assertEqual(self.repo.head.target.hex, HEAD_SHA)
def test_set_head(self): # Test setting a detatched HEAD. self.repo.set_head(Oid(hex=PARENT_SHA)) assert self.repo.head.target.hex == PARENT_SHA # And test setting a normal HEAD. self.repo.set_head("refs/heads/master") assert self.repo.head.name == "refs/heads/master" assert self.repo.head.target.hex == HEAD_SHA
def test_cmp(self): oid1 = Oid(raw=RAW) # Equal oid2 = Oid(hex=HEX) self.assertEqual(oid1, oid2) # Not equal oid2 = Oid(hex="15b648aec6ed045b5ca6f57f8b7831a8b4757299") self.assertNotEqual(oid1, oid2) # Other self.assertTrue(oid1 < oid2) self.assertTrue(oid1 <= oid2) self.assertFalse(oid1 == oid2) self.assertFalse(oid1 > oid2) self.assertFalse(oid1 >= oid2)
def test_cmp(self): oid1 = Oid(raw=RAW) # Equal oid2 = Oid(hex=HEX) assert oid1 == oid2 # Not equal oid2 = Oid(hex="15b648aec6ed045b5ca6f57f8b7831a8b4757299") assert oid1 != oid2 # Other assert oid1 < oid2 assert oid1 <= oid2 assert not oid1 == oid2 assert not oid1 > oid2 assert not oid1 >= oid2
def __getitem__(self, item): if not isinstance(item, Oid): item = Oid(hex=item) try: return self._replaces[item] except KeyError: return item
def test_hex_bytes(self): if version_info[0] == 2: hex = bytes(HEX) oid = Oid(hex=hex) self.assertEqual(oid.raw, RAW) self.assertEqual(str(oid), HEX) else: hex = bytes(HEX, "ascii") self.assertRaises(TypeError, Oid, hex=hex)
def test_replaces(self): replaces = GitReplaces.GitReplaces(self.repo) # non-replaced commit self.assertEqual( Oid(hex='640e5fddd05c5a6a113663ffa1555e9ef4176f77'), replaces[Oid(hex='640e5fddd05c5a6a113663ffa1555e9ef4176f77')]) self.assertEqual(Oid(hex='640e5fddd05c5a6a113663ffa1555e9ef4176f77'), replaces['640e5fddd05c5a6a113663ffa1555e9ef4176f77']) # replaced commit self.assertEqual( '640e5fddd05c5a6a113663ffa1555e9ef4176f77', replaces['5bcfecc3c7b13f6121cc97aed31bf226777b1b93'].hex) self.assertEqual(1, len(replaces)) self.assertEqual(['5bcfecc3c7b13f6121cc97aed31bf226777b1b93'], [i.hex for i in replaces])
def test_update_tips(emptyrepo): remote = emptyrepo.remotes[0] tips = [('refs/remotes/origin/master', Oid(hex='0'*40), Oid(hex='784855caf26449a1914d2cf62d12b9374d76ae78')), ('refs/tags/root', Oid(hex='0'*40), Oid(hex='3d2962987c695a29f1f80b6c3aa4ec046ef44369'))] class MyCallbacks(pygit2.RemoteCallbacks): def __init__(self, tips): self.tips = tips self.i = 0 def update_tips(self, name, old, new): assert self.tips[self.i] == (name, old, new) self.i += 1 callbacks = MyCallbacks(tips) remote.fetch(callbacks=callbacks) assert callbacks.i > 0
def test_grafts(self): grafts = GitGrafts(self.repo) # non-grafted commit self.assertEqual( (Oid(hex='5bcaacc7f74359ac1c8c4886dacb43c8840dd8a4'), ), grafts[Oid(hex='640e5fddd05c5a6a113663ffa1555e9ef4176f77')]) self.assertEqual( (Oid(hex='5bcaacc7f74359ac1c8c4886dacb43c8840dd8a4'), ), grafts['640e5fddd05c5a6a113663ffa1555e9ef4176f77']) # grafted commit self.assertEqual( (Oid(hex='640e5fddd05c5a6a113663ffa1555e9ef4176f77'), ), grafts['f21d222ddda26d99379257010bdc24536087e7cc']) self.assertEqual(1, len(grafts)) self.assertEqual(['f21d222ddda26d99379257010bdc24536087e7cc'], [i.hex for i in grafts])
def get_bug_introducing_commits(self, repo: Repository, commit: Commit, diff: Diff) -> list: buggy_commits = set() mapper = BugMap() for patch in diff: file = patch.delta.new_file.path for hunk in patch.hunks: for line in hunk.lines: if line.new_lineno != -1: buggy_commits.add(Oid(hex=mapper.map(repo, file, line.new_lineno, commit))) return buggy_commits
def get_commit_id_list(last_commit_id=None, first_commit_id=None): commit_list = [] last = last_commit_id if last_commit_id is not None else repo.head.target for commit in repo.walk(last, GIT_SORT_REVERSE): commit_list.append(commit.id) first_commit_id_Oid = Oid( hex=first_commit_id) if first_commit_id is not None else commit_list[0] modified_commit_list = commit_list[commit_list.index(first_commit_id_Oid):] return modified_commit_list
def test_entry_eq(testrepo): index = testrepo.index hello_entry = index['hello.txt'] entry = pygit2.IndexEntry(hello_entry.path, hello_entry.id, hello_entry.mode) assert hello_entry == entry entry = pygit2.IndexEntry("README.md", hello_entry.id, hello_entry.mode) assert hello_entry != entry oid = Oid(hex='0907563af06c7464d62a70cdd135a6ba7d2b41d8') entry = pygit2.IndexEntry(hello_entry.path, oid, hello_entry.mode) assert hello_entry != entry entry = pygit2.IndexEntry(hello_entry.path, hello_entry.id, pygit2.GIT_FILEMODE_BLOB_EXECUTABLE) assert hello_entry != entry
def save(self, *args, **kwargs): repository = self._transaction.repo current_branch = self._transaction.repo.branches.local.get( self.initial_pk, ) if current_branch is None: if isinstance(self.inited_head, str): commit = repository[Oid(hex=self.inited_head)] elif isinstance(self.inited_head, ProblemCommit): commit = repository[self.inited_head.commit_id] else: raise ValueError("Invalid head") current_branch = self._transaction.repo.branches.local.create( self.name, commit) if self.initial_pk != self.pk: current_branch.rename(self.pk) self._transaction = Transaction(repository_path=repository.path, branch_name=self.name)
def test_write_tree(self): self.check_writing(TREE_HASH) self.check_writing(Oid(hex=TREE_HASH)) self.check_writing(self.repo[TREE_HASH])
def test_write_commit(self): commit_timestamp = self.repo[COMMIT_HASH].committer.time self.check_writing(COMMIT_HASH, commit_timestamp) self.check_writing(Oid(hex=COMMIT_HASH), commit_timestamp) self.check_writing(self.repo[COMMIT_HASH], commit_timestamp)
def test_iterable(self): l = [obj for obj in self.repo] oid = Oid(hex=BLOB_HEX) assert oid in l
from pygit2 import init_repository, clone_repository, discover_repository from pygit2 import Oid, Reference, hashfile import pygit2 from . import utils try: import __pypy__ except ImportError: __pypy__ = None HEAD_SHA = '784855caf26449a1914d2cf62d12b9374d76ae78' PARENT_SHA = 'f5e5aa4e36ab0fe62ee1ccc6eb8f79b866863b87' # HEAD^ BLOB_HEX = 'af431f20fc541ed6d5afede3e2dc7160f6f01f16' BLOB_RAW = binascii.unhexlify(BLOB_HEX.encode('ascii')) BLOB_OID = Oid(raw=BLOB_RAW) class RepositoryTest(utils.BareRepoTestCase): def test_is_empty(self): assert not self.repo.is_empty def test_is_bare(self): assert self.repo.is_bare def test_head(self): head = self.repo.head assert HEAD_SHA == head.target.hex assert type(head) == Reference assert not self.repo.head_is_unborn
# Boston, MA 02110-1301, USA. """Tests for Blame objects.""" from __future__ import absolute_import from __future__ import unicode_literals import pytest from pygit2 import Signature, Oid from . import utils PATH = 'hello.txt' HUNKS = [ (Oid(hex='acecd5ea2924a4b900e7e149496e1f4b57976e51'), 1, Signature('J. David Ibañez', '*****@*****.**', 1297179898, 60, encoding='utf-8'), True), (Oid(hex='6aaa262e655dd54252e5813c8e5acd7780ed097d'), 2, Signature('J. David Ibañez', '*****@*****.**', 1297696877, 60, encoding='utf-8'), False), (Oid(hex='4ec4389a8068641da2d6578db0419484972284c8'), 3, Signature('J. David Ibañez', '*****@*****.**', 1297696908, 60, encoding='utf-8'), False) ] class BlameTest(utils.RepoTestCase): def test_blame_index(self): repo = self.repo blame = repo.blame(PATH)