Esempio n. 1
0
    def rebase_to_hash(self, branch_name, hash_):
        """Perform a rebase from a specific reference to another.

           :param str branch_name: The name of the branch to rebase on
           :param str hash_: The commit hash or reference to rebase to
        """
        self.logger.debug(
            "Rebasing branch %s to hash %s. Repo currently at commit %s.",
            branch_name, hash_, self.git_repo.repo.head.commit
        )

        if self.git_repo.repo.is_dirty():
            msg = ("Repository {0} is dirty. Please clean workspace "
                   "before proceeding.".format(self.git_repo.repo.working_dir))
            raise exceptions.DirtyRepositoryException(msg)

        # Checkout
        try:
            self.git_repo.git.checkout(branch_name)
        except git.GitCommandError as ex:
            msg = "Could not checkout branch {name}. Error: {error}".format(name=branch_name, error=ex)
            raise_from(exceptions.CheckoutException(msg), ex)

        # Rebase
        try:
            self.git_repo.git.rebase(hash_)
        except git.GitCommandError as ex:
            msg = "Could not rebase hash {hash_} onto branch {name}. Error: {error}".format(hash_=hash_, name=branch_name, error=ex)
            raise_from(exceptions.RebaseException(msg), ex)

        self.logger.debug("Successfully rebased branch %s to %s", branch_name, hash_)
Esempio n. 2
0
    def rebase_to_hash(self, branch_name, hash_):
        """Perform a rebase from a specific reference to another.

           :param str branch_name: The name of the branch to rebase on
           :param str hash_: The commit hash or reference to rebase to
        """
        self.logger.debug(
            f"Rebasing branch {branch_name} to hash {hash_}. "
            f"Repo currently at commit {self.git_repo.repo.head.commit}.")

        if self.git_repo.repo.is_dirty():
            working_dir = self.git_repo.repo.working_dir
            msg = (
                f"Repository {working_dir} is dirty. Please clean workspace "
                "before proceeding.")
            raise exceptions.DirtyRepositoryException(msg)

        # Checkout
        try:
            self.git_repo.git.checkout(branch_name)
        except git.GitCommandError as ex:
            msg = f"Could not checkout branch {branch_name}. Error: {ex}"
            raise exceptions.CheckoutException(msg) from ex

        # Rebase
        try:
            self.git_repo.git.rebase(hash_)
        except git.GitCommandError as ex:
            msg = (f"Could not rebase hash {hash_} onto branch {branch_name}. "
                   f"Error: {ex}")
            raise exceptions.RebaseException(msg) from ex

        msg = f"Successfully rebased branch {branch_name} to {hash_}"
        self.logger.debug(msg)
Esempio n. 3
0
    def to_hash(self, branch_name, hash_):
        """Perform a rebase from a specific reference to another.

           :param str branch_name: The name of the branch or reference to start from
           :param str hash_: The commit hash or reference to rebase to
        """
        logger.debug(
            "Rebasing branch %s to hash %s. Repo currently at commit %s.",
            branch_name, hash_, self.repo.head.commit)

        if self.repo.is_dirty():
            msg = "Repository %s is dirty. Please clean workspace before proceeding." % self.repo.working_dir
            raise exceptions.DirtyRepositoryException(msg)

        # Does the branch exist?
        try:
            branch = git.repo.fun.name_to_object(self.repo, branch_name)
        except git.exc.BadName as ex:
            msg = "Could not find branch %s." % branch_name
            raise exceptions.ReferenceNotFoundException(msg) from ex

        # Does the hash exists?
        try:
            commit = git.repo.fun.name_to_object(self.repo, hash_)
        except git.exc.BadName as ex:
            msg = "Could not find hash %s." % hash_
            raise exceptions.ReferenceNotFoundException(msg) from ex

        # Checkout
        try:
            self.repo.git.checkout(branch.hexsha)
        except git.GitCommandError as ex:
            msg = "Could not checkout branch %s. Error: %s" % (branch_name, ex)
            raise exceptions.CheckoutException(msg) from ex

        # Rebase
        try:
            self.repo.git.rebase(commit.hexsha)
        except git.GitCommandError as ex:
            msg = "Could not rebase hash %s onto branch %s. Error: %s" % (
                hash_, branch_name, ex)
            raise exceptions.RebaseException(msg) from ex

        logger.debug("Successfully rebased branch %s (%s) to %s" %
                     (branch_name, branch.hexsha, hash_))
def test_rebase_exception_gitreview(mock_repo):
    """
    GIVEN Rebaser initialized correctly
    WHEN perform_rebase asserts with RebaseException
    THEN Rebaser calls the try_automated_rebase_fix method
    """
    mock_repo.branch.rebase_to_hash.side_effect = [
        exceptions.RebaseException('.gitreview failed to rebase'), True
    ]
    rebaser = Rebaser(mock_repo,
                      "my_branch",
                      "my_commit",
                      "my_remote",
                      "my_tstamp",
                      dev_mode=True)

    rebaser.try_automated_rebase_fix = Mock()
    rebaser.try_automated_rebase_fix.side_effect = [True]
    rebaser.perform_rebase()
    rebaser.try_automated_rebase_fix.assert_called()
def test_rebase_exception_not_gitreview(mock_repo):
    """
    GIVEN Rebaser initialized correctly
    WHEN perform_rebase asserts with RebaseException
    AND the exception message does not contain .gitreview
    THEN Rebaser calls the repo.branch.abort_rebase method
    """
    mock_repo.branch.rebase_to_hash.side_effect = [
        exceptions.RebaseException('Whatever other exception'), True
    ]
    rebaser = Rebaser(mock_repo,
                      "my_branch",
                      "my_commit",
                      "my_remote",
                      "my_tstamp",
                      dev_mode=True)

    rebaser.repo.branch.abort_rebase = Mock()
    with pytest.raises(exceptions.RebaseException):
        rebaser.perform_rebase()
    rebaser.repo.branch.abort_rebase.assert_called()
def test_try_automated_rebase_fix(reb_mock, mock_repo):
    """
    GIVEN Rebaser initialized correctly
    WHEN perform_rebase asserts with RebaseException
    AND the exception contains .gitreview in the exception message
    AND Rebaser calls the try_automated_rebase_fix method
    THEN try_automated_rebase_fix detects .gitreview in the exception message
    AND calls git.rebase('--skip') and _rebuild_gitreview
    """

    rebaser = Rebaser(mock_repo,
                      "my_branch",
                      "my_commit",
                      "my_remote",
                      "my_tstamp",
                      dev_mode=True)

    exception = exceptions.RebaseException('.gitreview failed to rebase')
    output = rebaser.try_automated_rebase_fix(exception)

    mock_repo.git.rebase.assert_called_with('--skip')
    reb_mock.assert_called()
    assert output is True