def test_checkout(self): """Test whether Git checkout commands are correctly executed""" new_path = os.path.join(self.tmp_path, 'testworktree') repo = GraalRepository('http://example.git', self.git_path) self.assertIsNone(repo.worktreepath) repo.worktree(new_path) repo.checkout("075f0c6161db5a3b1c8eca45e08b88469bb148b9") current_commit_hash = self.__git_show_hash(repo) self.assertEqual("075f0c6161db5a3b1c8eca45e08b88469bb148b9", current_commit_hash) repo.checkout("4f3b403d47fb291a9a942a62d62c24faa79244c8") current_commit_hash = self.__git_show_hash(repo) self.assertEqual("4f3b403d47fb291a9a942a62d62c24faa79244c8", current_commit_hash) repo.checkout("825b4da7ca740f7f2abbae1b3402908a44d130cd") current_commit_hash = self.__git_show_hash(repo) self.assertEqual("825b4da7ca740f7f2abbae1b3402908a44d130cd", current_commit_hash) repo.prune() self.assertFalse(os.path.exists(repo.worktreepath))
def test_files(self): """Test whether all files in a directory and its sub-directories are shown""" new_path = os.path.join(self.tmp_path, 'testworktree') repo = GraalRepository('http://example.git', self.git_path) repo.worktree(new_path) expected = [ os.path.join(new_path, 'perceval/archive.py'), os.path.join(new_path, 'perceval/backend.py'), os.path.join(new_path, 'perceval/client.py'), os.path.join(new_path, 'perceval/__init__.py'), os.path.join(new_path, 'perceval/_version.py'), os.path.join(new_path, 'perceval/utils.py'), os.path.join(new_path, 'perceval/errors.py'), os.path.join(new_path, 'perceval/backends/__init__.py'), os.path.join(new_path, 'perceval/backends/core/__init__.py'), os.path.join(new_path, 'perceval/backends/core/github.py'), os.path.join(new_path, 'perceval/backends/core/mbox.py'), os.path.join(new_path, 'perceval/backends/core/git.py') ] files = repo.files(new_path) self.assertEqual(len(files), len(expected)) for f in files: self.assertIn(f, expected) repo.prune() self.assertFalse(os.path.exists(repo.worktreepath))
def test_prune(self): """Test whether a working tree is deleted""" new_path = os.path.join(self.tmp_path, 'testworktree') repo = GraalRepository('http://example.git', self.git_path) repo.worktreepath = new_path repo.worktree(new_path) self.assertEqual(repo.worktreepath, new_path) self.assertTrue(os.path.exists(repo.worktreepath)) repo.prune() self.assertFalse(os.path.exists(repo.worktreepath))
def test_worktree_from_branch(self): """Test whether a working tree is created from a target branch""" new_path = os.path.join(self.tmp_path, 'testworktree') repo = GraalRepository('http://example.git', self.git_path) self.assertIsNone(repo.worktreepath) repo.worktree(new_path, branch='master') self.assertEqual(repo.worktreepath, new_path) self.assertTrue(os.path.exists(repo.worktreepath)) repo.prune() self.assertFalse(os.path.exists(repo.worktreepath))
def test_worktree_already_exists(self): """Test whether a debug info is logged when the worktree already exists""" new_path = os.path.join(self.tmp_path, 'testworktree') repo = GraalRepository('http://example.git', self.git_path) self.assertIsNone(repo.worktreepath) repo.worktree(new_path, branch='master') with self.assertLogs(logger, level='DEBUG') as cm: repo.worktree(new_path, branch='master') self.assertRegex(cm.output[0], 'DEBUG:graal.graal:Git worktree.*not created.*already exists.*') repo.prune() self.assertFalse(os.path.exists(repo.worktreepath))
def test_delete(self): """Test whether files and directories are deleted""" new_path = os.path.join(self.tmp_path, 'testworktree') repo = GraalRepository('http://example.git', self.git_path) repo.worktree(new_path) target_file = os.path.join(new_path, 'perceval/_version.py') target_folder = os.path.join(new_path, 'perceval/backends') self.assertTrue(os.path.exists(target_file)) repo.delete(target_file) self.assertFalse(os.path.exists(target_file)) self.assertTrue(os.path.exists(target_folder)) repo.delete(target_folder) self.assertFalse(os.path.exists(target_folder)) repo.prune() self.assertFalse(os.path.exists(repo.worktreepath))
# path of repository cloned repo_dir = '/tmp/repo' print("Cloning stated..") # Cloning the repo if it is not present if not GraalRepository.exists(repo_dir): repo = GraalRepository.clone(repo_uri, repo_dir) elif os.path.isdir(repo_dir): repo = GraalRepository(repo_uri, repo_dir) # creating a new worktree if not already present if GraalRepository.exists(worktreepath): shutil.rmtree(worktreepath) repo.worktree(worktreepath) # preforming checkout at given SHA repo.checkout(sha) print("cloning done") # Using flake8 to find errors if any flk_args = { 'module_path': os.path.join(os.sep, 'tmp', 'fossology'), 'worktree_path': '/tmp/worktrees', 'details': True } # instantiating flake8
worktree_path = input("Enter worktree path: ") # Git object, pointing to repo_url and repo_dir for cloning ggit = Git(uri=repo_url , gitpath=repo_dir) # clone the repository (if it doesn't exist locally) ggit.fetch_items(category='commit') commits = list(ggit.fetch()) # hash of random commit commit = random.choice(commits) _hash = commit['data']['commit'] print(_hash) # or input the hash of certain commit # _hash = input("Enter hash: ") gral_repo = GraalRepository(uri=repo_url, dirpath=repo_dir) gral_repo.worktree(worktree_path) # checkout the commit gral_repo.checkout(_hash) style_guide = flake8.get_style_guide() files = worktree_path # generate report by flake8 report = style_guide.check_files([files]) print(report.get_statistics(('E', 'W', 'F'))) print(report.total_errors) # repo_url = https://github.com/chaoss/grimoirelab-perceval # repo_dir = git/grimoirelab-perceval.git # worktree_path = "/Users/tanxin/PycharmProjects/git"