def test_checkout_commit_and_master(self): git_url = 'https://github.com/polyaxon/empty.git' # Create repo repo = ExternalRepo(project=self.project, git_url=git_url) repo.save() # Check last commit assert len(repo.last_commit) == 2 # Add new file file_path = os.path.join(repo.path, 'file1.dat') open(file_path, 'w+') assert git.get_status(repo.path) is not None git.commit(repo.path, '*****@*****.**', 'username') # Check last commit commit1 = repo.last_commit[0] assert commit1 is not None # Add new file file_path = os.path.join(repo.path, 'file2.dat') open(file_path, 'w+') assert git.get_status(repo.path) is not None git.commit(repo.path, '*****@*****.**', 'username') # Check last commit commit2 = repo.last_commit[0] assert commit2 is not None # Commits are different assert commit1 != commit2 # Checkout to commit1 git.checkout_commit(repo_path=repo.path, commit=commit1) assert repo.last_commit[0] == commit1 # Checkout to master git.checkout_commit(repo_path=repo.path) assert repo.last_commit[0] == commit2
def handle_new_files(user_id, repo_id, tar_file_name): if not tarfile.is_tarfile(tar_file_name): raise ValueError('Received wrong file format.') User = get_user_model() # noqa try: user = User.objects.get(id=user_id) except User.DoesNotExist: _logger.warning('User with id `%s` does not exist anymore.', user_id) return try: repo = Repo.objects.get(id=repo_id) # Checkout to master git.checkout_commit(repo.path) except User.DoesNotExist: _logger.warning('Repo with id `%s` does not exist anymore.', repo_id) return # Destination files new_repo_path = repo.get_tmp_tar_path() # clean the current path from all files path_files = os.listdir(repo.path) for member in path_files: if member == '.git': continue member = os.path.join(repo.path, member) if os.path.isfile(member): os.remove(member) else: delete_path(member) # Move the tar inside the repo path shutil.move(tar_file_name, new_repo_path) # Untar the file with tarfile.open(new_repo_path) as tar: tar.extractall(repo.path) # Delete the current tar os.remove(new_repo_path) # Get the git repo if not git.get_status(repo.path): return # commit changes git.commit(repo.path, user.email, user.username) auditor.record(event_type=REPO_NEW_COMMIT, instance=repo, actor_id=user.id, actor_name=user.username)
def handle_new_files(user_id, repo_id, tar_file_name): if not tarfile.is_tarfile(tar_file_name): raise ValueError('Received wrong file format.') User = get_user_model() # noqa try: user = User.objects.get(id=user_id) except User.DoesNotExist: logger.warning('User with id `%s` does not exist anymore.', user_id) return try: repo = Repo.objects.get(id=repo_id) # Checkout to master git.checkout_commit(repo.path) except User.DoesNotExist: logger.warning('Repo with id `%s` does not exist anymore.', repo_id) return # Destination files new_repo_path = repo.get_tmp_tar_path() # clean the current path from all files path_files = os.listdir(repo.path) for member in path_files: if member == '.git': continue member = os.path.join(repo.path, member) if os.path.isfile(member): os.remove(member) else: delete_path(member) # Move the tar inside the repo path shutil.move(tar_file_name, new_repo_path) # Untar the file with tarfile.open(new_repo_path) as tar: tar.extractall(repo.path) # Delete the current tar os.remove(new_repo_path) # Get the git repo if not git.get_status(repo.path): return # commit changes git.commit(repo.path, user.email, user.username) auditor.record(event_type=REPO_NEW_COMMIT, instance=repo, actor_id=user.id)