def test_garbage_collect(self): '''Tests that git gc doesn't prune FETCH_HEAD''' parent_path = os.path.join(self.upstream_root, 'org/project1') repo = git.Repo(parent_path) change_ref = 'refs/changes/1/1' self.log.info('Creating a commit on %s', change_ref) repo.head.reference = repo.head.commit files = {"README": "creating fake commit\n"} for name, content in files.items(): file_name = os.path.join(parent_path, name) with open(file_name, 'a') as f: f.write(content) repo.index.add([file_name]) commit = repo.index.commit('Test commit') ref = git.refs.Reference(repo, change_ref) ref.set_commit(commit) self.log.info('Cloning parent repo') work_repo = Repo(parent_path, self.workspace_root, '*****@*****.**', 'User Name', '0', '0') self.log.info('Fetch %s', change_ref) work_repo.fetch(change_ref) self.log.info('Checkout master and run garbage collection') work_repo_object = work_repo.createRepoObject(None) work_repo.checkout('master') result = work_repo_object.git.gc('--prune=now') self.log.info(result) self.log.info('Dereferencing FETCH_HEAD') commit = work_repo_object.commit('FETCH_HEAD') self.assertIsNotNone(commit)
def test_repo_reset_branch_conflict(self): """Test correct reset with conflicting branch names""" parent_path = os.path.join(self.upstream_root, 'org/project1') parent_repo = git.Repo(parent_path) parent_repo.create_head("foobar") work_repo = Repo(parent_path, self.workspace_root, '*****@*****.**', 'User Name', '0', '0') # Checkout branch that will be deleted from the remote repo work_repo.checkout("foobar") # Delete remote branch and create a branch that conflicts with # the branch checked out locally. parent_repo.delete_head("foobar") parent_repo.create_head("foobar/sub") work_repo.reset() work_repo.checkout("foobar/sub") # Try the reverse conflict parent_path = os.path.join(self.upstream_root, 'org/project2') parent_repo = git.Repo(parent_path) parent_repo.create_head("foobar/sub") work_repo = Repo(parent_path, self.workspace_root, '*****@*****.**', 'User Name', '0', '0') # Checkout branch that will be deleted from the remote repo work_repo.checkout("foobar/sub") # Delete remote branch and create a branch that conflicts with # the branch checked out locally. parent_repo.delete_head("foobar/sub") # Note: Before git 2.13 deleting a a ref foo/bar leaves an empty # directory foo behind that will block creating the reference foo # in the future. As a workaround we must clean up empty directories # in .git/refs. if parent_repo.git.version_info[:2] < (2, 13): Repo._cleanup_leaked_ref_dirs(parent_path, None, []) parent_repo.create_head("foobar") work_repo.reset() work_repo.checkout("foobar")