Пример #1
0
    def run(self):

        repo_check()

        # Interrupt for git log results in bad tty
        signal.signal(signal.SIGINT, signal.SIG_IGN)

        try:
            commit_logs(self.limit,
                        diff=self.diff,
                        show_revision=self.show,
                        extra_args=self.extra_args,
                        to_pager=True)
        except Exception as e:
            # Oddly, git log returns non-zero exit whenever user exits while it is still printing
            if self.debug:
                log.exception(e)
Пример #2
0
    def changes_since_last_publish(self):
        commit_msgs = extract_commit_msgs(commit_logs(limit=100, repo=repo_path()), True)
        changes = []
        published_version = None

        for msg in commit_msgs:
            if msg.startswith(PUBLISH_VERSION_PREFIX):
                published_version = msg.split(PUBLISH_VERSION_PREFIX)[-1]
                break
            if len(msg) < 7 or IGNORE_CHANGE_RE.match(msg):
                continue
            changes.append(msg)

        return published_version, changes
Пример #3
0
    def changes_since_last_publish(self):
        commit_msgs = extract_commit_msgs(commit_logs(limit=100, repo=repo_path()), True)
        changes = []
        published_version = None

        for msg in commit_msgs:
            if msg.startswith(PUBLISH_VERSION_PREFIX):
                published_version = msg.split(PUBLISH_VERSION_PREFIX)[-1]
                break
            if len(msg) < 7 or IGNORE_CHANGE_RE.match(msg):
                continue
            changes.append(msg)

        return published_version, changes
Пример #4
0
def test_commit(wst):
    with temp_dir():
        with pytest.raises(SystemExit):
            wst('commit')

    with temp_git_repo():
        with pytest.raises(SystemExit):
            wst('commit "no files to commit"')

        with open('new_file', 'w') as fp:
            fp.write('Hello World')
        assert 'new_file' in stat_repo(return_output=True)

        wst('commit "Add new file" --branch master')

        assert 'working tree clean' in stat_repo(return_output=True)
        assert 'Hello World' == open('new_file').read()

        with open('new_file', 'w') as fp:
            fp.write('New World')

        wst('commit "Update file"')

        assert ['update-file@master', 'master'] == all_branches()

        wst('commit --move release')

        assert ['update-file@master', 'master', 'release'] == all_branches()

        wst('commit --discard')

        assert ['master', 'release'] == all_branches()

        wst('checkout release')

        wst('commit --discard')

        assert ['release', 'master'] == all_branches()

        logs = commit_logs()
        assert 'new file' in logs
        assert 1 == len(list(filter(None, logs.split('commit'))))
Пример #5
0
def test_publish(wst, monkeypatch):
    silent_run_mock = Mock()
    config_mock = Mock()
    monkeypatch.setattr('workspace.commands.publish.LocalConfig', config_mock)
    monkeypatch.setattr('workspace.commands.publish.silent_run',
                        silent_run_mock)
    monkeypatch.setattr('workspace.commands.publish.run', silent_run_mock)

    config_mock().get.side_effect = ['repo', 'user', 'pass'] * 10

    with temp_git_repo() as cwd:
        wst('setup --product')

        # Patch release
        run('git commit --allow-empty -m change1')
        run('git commit --allow-empty -m change2')

        wst('publish')

        changes = open('docs/CHANGELOG.rst').read()
        assert changes == """\
Version 0.0.1
================================================================================

* change2
* change1
"""

        setup_py = open('setup.py').read().split('\n')
        assert setup_py[5] == "    version='0.0.2',"

        python = Path('~/.virtualenvs').expanduser() / Path(
            cwd).name / 'bin' / 'python'

        repo_path = '/private' + str(cwd)
        assert silent_run_mock.call_args_list == [
            call('rm -rf dist/*', cwd=repo_path, shell=True),
            call(f'{python} setup.py sdist bdist_wheel', cwd=repo_path),
            call('twine upload -r "pypi" -u "user" -p "pass" dist/*',
                 cwd=repo_path,
                 shell=True,
                 silent=2)
        ]

        # No changes
        with pytest.raises(SystemExit):
            wst('publish')

        # Minor release
        run('git commit --allow-empty -m feature1')

        wst('publish --minor')

        assert 'Bump minor version' in commit_logs()

        changes = open('docs/CHANGELOG.rst').read()
        print(changes)
        assert changes == """\
Version 0.1.0
================================================================================

* feature1

Version 0.0.1
================================================================================

* change2
* change1
"""
        setup_py = open('setup.py').read().split('\n')
        assert setup_py[5] == "    version='0.1.1',"

        # Major release
        run('git commit --allow-empty -m feature2')

        wst('publish --major')

        assert 'Bump major version' in commit_logs()

        changes = open('docs/CHANGELOG.rst').read()
        print(changes)
        assert changes == """\
Version 1.0.0
================================================================================

* feature2

Version 0.1.0
================================================================================

* feature1

Version 0.0.1
================================================================================

* change2
* change1
"""
        setup_py = open('setup.py').read().split('\n')
        assert setup_py[5] == "    version='1.0.1',"

        # Already published patch version will bump before publish
        run('git commit --allow-empty -m "Publish version 1.0.1"', shell=True)
        run('git commit --allow-empty -m bugfix1')

        wst('publish')

        changes = open('docs/CHANGELOG.rst').read()
        assert changes == """\
Version 1.0.2
================================================================================

* bugfix1

Version 1.0.0
--------------------------------------------------------------------------------

* feature2

Version 0.1.0
================================================================================

* feature1

Version 0.0.1
================================================================================

* change2
* change1
"""

        setup_py = open('setup.py').read().split('\n')
        assert setup_py[5] == "    version='1.0.3',"
Пример #6
0
    def run(self):
        if self.discard or self.move:
            if not self.branch:
                if self.discard:
                    self.branch = current_branch()
                else:
                    self.branch = self.move[0]

            is_child_branch = base_branch = parent_branch(self.branch)

            if self.discard:
                changes = commit_logs(self.discard) if not is_child_branch else diff_branch(self.branch,
                                                                                            left_branch=base_branch)
            else:
                changes = commit_logs(1)
            changes = [_f for _f in changes.split('commit ') if _f]

            if self.discard and len(changes) <= self.discard and is_child_branch:
                checkout_branch(base_branch)
                remove_branch(self.branch, raises=True, force=True)

            else:
                match = re.match('([a-f0-9]+)(?: \(.*\))\n', changes[0])

                if match:
                    last_commit = match.group(1)

                    if self.move:
                        cur_branch = current_branch()
                        create_branch(self.branch)
                        checkout_branch(cur_branch)
                        click.echo('Moved {} to {}'.format(last_commit[:7], self.branch))
                        hard_reset(last_commit + '~1')

                    else:
                        checkout_branch(self.branch)
                        hard_reset(last_commit + '~' + str(self.discard))

                else:
                    log.error('Odd. No commit hash found in: %s', changes[0])

        else:
            if not self.skip_style_check and (self.test or self.push) and self.commander.command('test').supports_style_check():
                click.echo('Checking style')
                self.commander.run('test', env_or_file=['style'], silent=2)

            test_output = None

            if not (self.msg or self.amend):
                self.msg = prompt_with_editor('Please provide a commit message. Empty message will cancel the commit.')
                if not self.msg:
                    sys.exit()

            if not self.amend and self.test:
                click.echo('Running tests')
                test_output = self.commander.run('test', return_output=False, test_dependents=self.test > 1)

            branches = all_branches()
            cur_branch = branches and branches[0]

            if (not (self.push or self.amend) and config.commit.commit_branch_indicator not in cur_branch and
                    not self.branch and self.msg and config.commit.auto_branch_from_commit_words):
                self.branch = self._branch_for_msg(self.msg,
                                                   words=config.commit.auto_branch_from_commit_words,
                                                   branches=branches)
                if cur_branch:
                    self.branch = '{}@{}'.format(self.branch, cur_branch)

            if self.branch:
                if branches:
                    if self.branch in branches:
                        if self.branch != cur_branch:
                            checkout_branch(self.branch)

                    else:
                        create_branch(self.branch, cur_branch)

                else:  # Empty repo without a commit has no branches
                    create_branch(self.branch)

            add_files(files=self.files)
            local_commit(self.msg, self.amend)

            if self.amend and self.test:
                if self.commander.command('test').supports_style_check():
                    click.echo('Running style check')
                    self.commander.run('test', env_or_file=['style'], silent=2)

                click.echo('Running tests')
                test_output = self.commander.run('test', return_output=False, test_dependents=self.test > 1)

            if self.push:
                self.commander.run('push', branch=self.branch, force=self.amend, skip_style_check=True, all_remotes=int(self.push) > 1)

            return test_output
Пример #7
0
def test_publish(wst, monkeypatch):
    silent_run_mock = Mock()
    config_mock = Mock()
    monkeypatch.setattr('workspace.commands.publish.LocalConfig', config_mock)
    monkeypatch.setattr('workspace.commands.publish.silent_run', silent_run_mock)
    monkeypatch.setattr('workspace.commands.publish.run', silent_run_mock)

    config_mock().get.side_effect = ['repo', 'user', 'pass'] * 10

    with temp_git_repo() as cwd:
        wst('setup --product')

        # Patch release
        run('git commit --allow-empty -m change1')
        run('git commit --allow-empty -m change2')

        wst('publish')

        changes = open('docs/CHANGELOG.rst').read()
        assert changes == """\
Version 0.0.1
================================================================================

* change2
* change1
"""

        setup_py = open('setup.py').read().split('\n')
        assert setup_py[5] == "    version='0.0.2',"

        python = Path('~/.virtualenvs').expanduser() / Path(cwd).name / 'bin' / 'python'

        assert silent_run_mock.call_args_list == [
            call('rm -rf dist/*', cwd=str(cwd), shell=True),
            call(f'{python} setup.py sdist bdist_wheel', cwd=str(cwd)),
            call('twine upload -r "pypi" -u "user" -p "pass" dist/*', cwd=str(cwd), shell=True, silent=2)]

        # No changes
        with pytest.raises(SystemExit):
            wst('publish')

        # Minor release
        run('git commit --allow-empty -m feature1')

        wst('publish --minor')

        assert 'Bump minor version' in commit_logs()

        changes = open('docs/CHANGELOG.rst').read()
        print(changes)
        assert changes == """\
Version 0.1.0
================================================================================

* feature1

Version 0.0.1
================================================================================

* change2
* change1
"""
        setup_py = open('setup.py').read().split('\n')
        assert setup_py[5] == "    version='0.1.1',"

        # Major release
        run('git commit --allow-empty -m feature2')

        wst('publish --major')

        assert 'Bump major version' in commit_logs()

        changes = open('docs/CHANGELOG.rst').read()
        print(changes)
        assert changes == """\
Version 1.0.0
================================================================================

* feature2

Version 0.1.0
================================================================================

* feature1

Version 0.0.1
================================================================================

* change2
* change1
"""
        setup_py = open('setup.py').read().split('\n')
        assert setup_py[5] == "    version='1.0.1',"

        # Already published patch version will bump before publish
        run('git commit --allow-empty -m "Publish version 1.0.1"', shell=True)
        run('git commit --allow-empty -m bugfix1')

        wst('publish')

        changes = open('docs/CHANGELOG.rst').read()
        assert changes == """\
Version 1.0.2
================================================================================

* bugfix1

Version 1.0.0
--------------------------------------------------------------------------------

* feature2

Version 0.1.0
================================================================================

* feature1

Version 0.0.1
================================================================================

* change2
* change1
"""

        setup_py = open('setup.py').read().split('\n')
        assert setup_py[5] == "    version='1.0.3',"