Ejemplo n.º 1
0
def squash(ctx, ref, no_verify, allow_empty):
    """
    Squash commits from ref commit into a single commit.

    If ``:github/pr`` is provided as the ref, the base branch of the pull
    request will be used (e.g. ``origin/develop``).
    """
    commit_result = core.squash(
        ref, no_verify=no_verify, allow_empty=allow_empty
    )
    ctx.exit(commit_result.returncode)
Ejemplo n.º 2
0
def test_squash_error_rollback(mocker):
    """Tests core.squash rolls back resets on commit error"""
    mocker.patch.object(
        formaldict.Schema,
        'prompt',
        autospec=True,
        side_effect=[
            {
                # The first commit
                'type': 'bug',
                'summary': 'summary',
                'description': 'description',
                'jira': 'WEB-9999',
            },
            # Throw an exception when trying to do the squash commit
            Exception,
        ],
    )

    with open('file_to_commit', 'w+') as f:
        f.write('Hello World')

    utils.shell('git add .')
    assert core.commit().returncode == 0

    # The first squash call throws an unexpected error and rolls back the reset
    with pytest.raises(Exception):
        core.squash('HEAD~1')

    assert utils.shell_stdout('git diff --cached') == ''

    # Make commit return a non-zero exit code on next squash commit
    mocker.patch(
        'tidy.core.commit',
        autospec=True,
        return_value=mocker.Mock(returncode=1),
    )

    # The next squash call has a commit error and rolls back the reset
    assert core.squash('HEAD~1').returncode == 1
    assert utils.shell_stdout('git diff --cached') == ''
Ejemplo n.º 3
0
def test_squash(mocker):
    """Tests core.squash"""
    mocker.patch.object(
        formaldict.Schema,
        'prompt',
        autospec=True,
        side_effect=[
            {
                # The first commit
                'type': 'bug',
                'summary': 'summary',
                'description': 'description',
                'jira': 'WEB-9999',
            },
            {
                # The second commit
                'type': 'trivial',
                'summary': 'Fixing up something',
            },
            {
                # The third commit
                'type': 'trivial',
                'summary': 'Fixing up something else',
            },
            {
                # The commit when squashing all commits
                'type': 'bug',
                'summary': 'final summary',
                'description': 'final description',
                'jira': 'WEB-9999',
            },
        ],
    )

    for f_name in ['file_to_commit1', 'file_to_commit2', 'file_to_commit3']:
        with open(f_name, 'w+') as f:
            f.write('Hello World')

        utils.shell('git add .')
        assert core.commit().returncode == 0

    assert core.squash('HEAD~2').returncode == 0

    commit = utils.shell_stdout('git show --summary')
    assert ('    final summary\n'
            '    \n'
            '    final description\n'
            '    \n'
            '    Type: bug\n'
            '    Jira: WEB-9999') in commit
Ejemplo n.º 4
0
def test_squash_no_commits(mocker):
    """Tests core.squash with no commits"""
    with pytest.raises(exceptions.NoSquashableCommitsError):
        core.squash('HEAD').returncode
Ejemplo n.º 5
0
def test_squash_diverging_branches(mocker):
    """Tests core.squash against a base branch that has diverged"""
    mocker.patch.object(
        formaldict.Schema,
        'prompt',
        autospec=True,
        side_effect=[
            {
                # The first commit on the base branch
                'type': 'bug',
                'summary': 'summary',
                'description': 'first master commit',
                'jira': 'WEB-9999',
            },
            {
                # The second commit on base after making squash branch
                'type': 'trivial',
                'summary': 'wont be seen in history',
            },
            {
                # The first commit on the branch to squash
                'type': 'trivial',
                'summary': 'Fixing up something',
            },
            {
                # The second commit on the branch to squash
                'type': 'trivial',
                'summary': 'Fixing up something else',
            },
            {
                # The commit when squashing all commits
                'type': 'bug',
                'summary': 'final summary',
                'description': 'final description',
                'jira': 'WEB-9999',
            },
        ],
    )

    # Make a commit that all branches will shared
    core.commit(allow_empty=True)

    # Make a branch that we will squash
    utils.shell('git branch test-squash')

    # Now commit against the base branch (i.e. make it diverge)
    core.commit(allow_empty=True)

    # Change branches and do a few more commits that will be squashed
    utils.shell('git checkout test-squash')

    core.commit(allow_empty=True)
    core.commit(allow_empty=True)

    assert core.squash('master', allow_empty=True).returncode == 0

    commits = utils.shell_stdout('git --no-pager log')

    # These commits disappeared when squashing
    assert 'Fixing up something' not in commits
    # First commit against master should be in log
    assert 'first master commit' in commits
    # Squashed commit should be in log
    assert 'final description' in commits
    # The divergent commit should not appear in history
    assert 'wont be seen' not in commits