def test_add_files(self): file_name = "absurd_file" file_path = os.path.join(self.main_repo, file_name) file_name2 = "absurd_file2" file_path2 = os.path.join(self.main_repo, file_name2) with open(file_path, "w") as file: file_content = "Absurd content" file.write(file_content) with open(file_path2, "w") as file: file_content = "Absurd content2" file.write(file_content) def get_status(): git = GitCmd(self.main_repo) status = {} for f in git('status', porcelain=True, _iter=True): s, path = f.strip().split(maxsplit=1) status[path] = s return status status = get_status() self.assertTrue(file_name in status) self.assertTrue(file_name2 in status) self.assertEquals(status[file_name], '??') self.assertEquals(status[file_name2], '??') gitrepo = Repository(self.main_repo) gitrepo.add([file_name, file_name2]) status = get_status() self.assertEquals(status[file_name], 'A') self.assertEquals(status[file_name2], 'A') with self.assertRaises(RepositoryError): gitrepo.add("nonexistentfile")
def test_add_files(self): file_name = "absurd_file" file_path = os.path.join(self.main_repo, file_name) file_name2 = "absurd_file2" file_path2 = os.path.join(self.main_repo, file_name2) with open(file_path, "w") as file: file_content = "Absurd content" file.write(file_content) with open(file_path2, "w") as file: file_content = "Absurd content2" file.write(file_content) repo = pygit2.Repository(self.main_repo) status = repo.status() self.assertTrue(file_name in status) self.assertTrue(file_name2 in status) self.assertEquals(status[file_name], pygit2.GIT_STATUS_WT_NEW) self.assertEquals(status[file_name2], pygit2.GIT_STATUS_WT_NEW) gitrepo = Repository(self.main_repo) gitrepo.add([file_name, file_name2]) status = repo.status() self.assertEquals(status[file_name], pygit2.GIT_STATUS_INDEX_NEW) self.assertEquals(status[file_name2], pygit2.GIT_STATUS_INDEX_NEW) with self.assertRaises(RepositoryError): gitrepo.add("nonexistentfile")
def test_add_files(self): file_name = "absurd_file" file_path = os.path.join(self.main_repo, file_name) file_name2 = "absurd_file2" file_path2 = os.path.join(self.main_repo, file_name2) with open(file_path, "w") as file: file_content = "Absurd content" file.write(file_content) with open(file_path2, "w") as file: file_content = "Absurd content2" file.write(file_content) def get_status(): git = GitCmd(self.main_repo) status = {} for f in git('status', porcelain=True, _iter=True): s, path = f.split() status[path] = s return status status = get_status() self.assertTrue(file_name in status) self.assertTrue(file_name2 in status) self.assertEquals(status[file_name], '??') self.assertEquals(status[file_name2], '??') gitrepo = Repository(self.main_repo) gitrepo.add([file_name, file_name2]) status = get_status() self.assertEquals(status[file_name], 'A') self.assertEquals(status[file_name2], 'A') with self.assertRaises(RepositoryError): gitrepo.add("nonexistentfile")
def test_merge_with_conflict(self): gitrepo = Repository(self.cloned_from_repo) # Checkout gitrepo.update('newbranch') file_to_conflict_name = 'test1.txt' file_to_conflict = os.path.join(self.cloned_from_repo, file_to_conflict_name) with open(file_to_conflict, "w") as file: file_content = "Absurd content" file.write(file_content) gitrepo.add(file_to_conflict_name) conflict_cs = gitrepo.commit("Provoking conflict") gitrepo.update('master') try: gitrepo.merge(other_rev=conflict_cs) self.fail() except MergeConflictError as exp: self.assertTrue('Conflicts found: merging test1.txt failed' in exp)
def test_merge_fastforward_no_ff(self): git = GitCmd(self.cloned_from_repo) gitrepo = Repository(self.cloned_from_repo) gitrepo.update('master') gitrepo.branch('ff-branch') ff_file_name = 'ff-file.txt' ff_file = os.path.join(self.cloned_from_repo, ff_file_name) with open(ff_file, "w") as file: file_content = "Absurd content" file.write(file_content) gitrepo.add(ff_file_name) ff_head = gitrepo.commit(message="commit ff file") gitrepo.update('master') cs = gitrepo.merge(other_rev=ff_head, other_branch_name='test') self.assertEquals(len(git('log', '-1', pretty='%P').split()), 2) self.assertEquals(git('rev-parse', 'HEAD'), cs.hash) # We want a commit in fastforward merges, hashes must be different self.assertNotEquals(ff_head.hash, cs.hash) self.assertTrue(os.path.isfile(ff_file))
def test_merge_fastforward_no_ff(self): repo = pygit2.Repository(self.cloned_from_repo) gitrepo = Repository(self.cloned_from_repo) gitrepo.update('master') gitrepo.branch('ff-branch') ff_file_name = 'ff-file.txt' ff_file = os.path.join(self.cloned_from_repo, ff_file_name) with open(ff_file, "w") as file: file_content = "Absurd content" file.write(file_content) gitrepo.add(ff_file_name) ff_head = gitrepo.commit(message="commit ff file") gitrepo.update('master') cs = gitrepo.merge(other_rev=ff_head, other_branch_name='test') self.assertEquals(len(repo.head.get_object().parents), 2) self.assertEquals(repo.head.get_object().hex, cs.hash) # We want a commit in fastforward merges, hashes must be different self.assertNotEquals(ff_head.hash, cs.hash) self.assertTrue(os.path.isfile(ff_file))
def test_commit(self): file_name = "test_file" file_path = os.path.join(self.main_repo, file_name) with open(file_path, "a") as file: file.write('test content') commit_msg = "Test message" git = GitCmd(self.main_repo) initial_len = len(list(git('log', 'HEAD', pretty='oneline', _iter=True))) gitrepo = Repository(self.main_repo) gitrepo.add(file_name) commit = gitrepo.commit(commit_msg) final_len = len(list(git('log', 'HEAD', pretty='oneline', _iter=True))) self.assertEquals(final_len, initial_len + 1) self.assertEquals(git('log', '-1', pretty='%B'), commit_msg) self.assertEquals(commit.desc, commit_msg) self.assertIsNone(gitrepo.commit(commit_msg))
def test_commit(self): file_name = "test_file" file_path = os.path.join(self.main_repo, file_name) with open(file_path, "a") as file: file.write('test content') commit_msg = "Test message" git = GitCmd(self.main_repo) initial_len = len( list(git('log', 'HEAD', pretty='oneline', _iter=True))) gitrepo = Repository(self.main_repo) gitrepo.add(file_name) commit = gitrepo.commit(commit_msg) final_len = len(list(git('log', 'HEAD', pretty='oneline', _iter=True))) self.assertEquals(final_len, initial_len + 1) self.assertEquals(git('log', '-1', pretty='%B'), commit_msg) self.assertEquals(commit.desc, commit_msg) self.assertIsNone(gitrepo.commit(commit_msg))
def test_merge_with_conflict(self): gitrepo = Repository(self.cloned_from_repo) # Checkout gitrepo.update('newbranch') file_to_conflict_name = 'test1.txt' file_to_conflict = os.path.join(self.cloned_from_repo, file_to_conflict_name) with open(file_to_conflict, "w") as file: file_content = "Absurd content" file.write(file_content) gitrepo.add(file_to_conflict_name) conflict_cs = gitrepo.commit("Provoking conflict") gitrepo.update('master') try: gitrepo.merge(other_rev=conflict_cs) self.fail('Merge with conflict should have failed') except MergeConflictError as exp: print exp self.assertTrue('Conflicts found: merging test1.txt failed' in exp)
def test_commit(self): file_name = "test_file" file_path = os.path.join(self.main_repo, file_name) with open(file_path, "a") as file: file.write('test content') commit_msg = "Test message" repo = pygit2.Repository(self.main_repo) initial_len = len(list(repo.walk(repo.head.target, pygit2.GIT_SORT_TOPOLOGICAL))) gitrepo = Repository(self.main_repo) gitrepo.add(file_name) commit = gitrepo.commit(commit_msg) final_len = len(list(repo.walk(repo.head.target, pygit2.GIT_SORT_TOPOLOGICAL))) self.assertEquals(final_len, initial_len + 1) self.assertEquals(repo.head.get_object().message, commit_msg) self.assertEquals(commit.desc, commit_msg) self.assertIsNone(gitrepo.commit(commit_msg))
def test_merge_fastforward(self): repo = pygit2.Repository(self.cloned_from_repo) gitrepo = Repository(self.cloned_from_repo) gitrepo.update('master') gitrepo.branch('ff-branch') ff_file_name = 'ff-file.txt' ff_file = os.path.join(self.cloned_from_repo, ff_file_name) with open(ff_file, "w") as file: file_content = "Absurd content" file.write(file_content) gitrepo.add(ff_file_name) signature = Signature(user="******") ff_head = gitrepo.commit(message="commit ff file", signature=signature) gitrepo.update('master') cs = gitrepo.merge_fastforward( signature, other_rev=ff_head, other_branch_name='test') self.assertEquals(len(repo.head.get_object().parents), 1) self.assertEquals(repo.head.get_object().hex, cs.hash) self.assertEquals(ff_head.hash, cs.hash) self.assertTrue(os.path.isfile(ff_file))