def test_add_remote_adds_fails(mock_repo):
    """
    GIVEN GitRepo initialized with a path and repo
    WHEN remote.add is called with a name and url
    AND the remote create fails with an exception
    THEN a False status is returned
    """
    mock_repo.create_remote.side_effect = git.CommandError('create')
    git_util = GitRepo('./', mock_repo)

    assert git_util.remote.add('rdo', 'http://rdoproject.org') is False
Exemple #2
0
def test_describe_sha_with_describe_failure(mock_repo):
    """
    GIVEN GitRepo initialized with a path and repo
    WHEN git.describe fails
    THEN a DescribeException is raised
    """
    repo = GitRepo('./', mock_repo)
    repo.git.describe.side_effect = git.CommandError('describe')

    with patch('git.repo.fun.name_to_object'):
        with pytest.raises(exceptions.DescribeException):
            repo.commit.describe('12345')
def test_add_remote_update_fails(mock_repo):
    """
    GIVEN GitRepo initialized with a path and repo
    WHEN remote.add is called with a name and url
    AND the remote update fails with an exception
    THEN a False status is returned
    WITH delete_remote called
    """
    remote_mock = Mock()
    delete_mock = Mock()
    update_mock = Mock(side_effect=git.CommandError('update'))
    remote_mock.attach_mock(update_mock, 'update')

    mock_repo.attach_mock(delete_mock, 'delete_remote')
    mock_repo.create_remote.return_value = remote_mock

    git_util = GitRepo('./', mock_repo)

    assert git_util.remote.add('rdo', 'http://rdoproject.org') is False
    assert update_mock.called is True
    delete_mock.assert_called_once_with(remote_mock)
    def load_issue_branch(self):
        repo = self.obtain_repo()

        issue_path = "{}/{}".format(repo.working_dir, self.ISSUE_BRANCH)
        path = os.path.normpath(issue_path)

        if (self._is_issue_branch_loaded(repo)):
            if (Path.cwd().parts[-1] != self.ISSUE_BRANCH):
                os.chdir(path)
            return

        has_remote = len(repo.remotes) > 0 and hasattr(repo.refs,
                                                       self.ISSUE_BRANCH)
        has_local_branch = hasattr(repo.refs, self.ISSUE_BRANCH)
        if not has_local_branch and not has_remote:
            self._create_new_issue_branch(repo)

        # If current branch is the issue branch, we can just go from there
        if repo.active_branch.name == self.ISSUE_BRANCH:
            return

        worktree_path = Path(repo.git_dir).joinpath("worktrees/issue")

        if not os.path.exists(path) and self.is_worktree():
            shutil.rmtree(worktree_path)
        elif os.path.exists(path) and not self.is_worktree():
            print(f"An issue folder already exists at location {path}.")
            print(
                "This folder is required to create a worktree of the issue branch."
            )
            print(
                "As this could be a user-created folder, I need confirmation on if I can delete this folder."
            )
            print("\n\nPlease review the contents before proceeding.")

            proceed = input("\nConfirm deletion (Y/N): ").capitalize()

            while proceed != "Y" and proceed != "YES" and proceed != "N" and proceed != "NO":
                proceed = input(
                    "\nInvalid input, please try again (Y/N): ").capitalize()

            if proceed == "Y" or proceed == "YES":
                shutil.rmtree(path)
                print(f"\nDirectory {path} successfully deleted.")

            else:
                print("\nOperation cancelled.")
                print(
                    "Please change your configuration to use a new non-conflicting branch name."
                )
                print("Note that this requires manually migrating branches.")
                exit()

        if not os.path.exists(path) and not os.path.exists(worktree_path):
            if has_remote and not has_local_branch:
                repo.git.branch("-f", self.ISSUE_BRANCH,
                                f"origin/{self.ISSUE_BRANCH}")

            repo.git.worktree("add", path, self.ISSUE_BRANCH)

        if os.path.exists(path):
            os.chdir(path)

        else:
            raise git.CommandError(
                "Failed to add a work tree for branch {} at path {}".format(
                    self.ISSUE_BRANCH, path))