-
-
Notifications
You must be signed in to change notification settings - Fork 281
WIP: Fix/issue 251 #279
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Fix/issue 251 #279
Changes from all commits
712a69c
38b15c3
aa19ed7
139020c
be85bcc
ec235d3
7985e9a
b80d9de
b3bf009
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from commitizen import cmd, factory, git, out | ||
from commitizen.config import BaseConfig | ||
from commitizen.exceptions import InvalidCommandArgumentError | ||
|
||
|
||
class Undo: | ||
"""Reset the latest git commit or git tag.""" | ||
|
||
def __init__(self, config: BaseConfig, arguments: dict): | ||
self.config: BaseConfig = config | ||
self.cz = factory.commiter_factory(self.config) | ||
self.arguments = arguments | ||
|
||
def _get_bump_command(self): | ||
created_tag = git.get_latest_tag() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this one named as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
commits = git.get_commits() | ||
|
||
if created_tag and commits: | ||
created_commit = commits[0] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as the question above |
||
else: | ||
raise InvalidCommandArgumentError("There is no tag or commit to undo") | ||
|
||
if created_tag.rev != created_commit.rev: | ||
raise InvalidCommandArgumentError( | ||
"The revision of the latest tag is not equal to the latest commit, use git undo --commit instead\n\n" | ||
f"Latest Tag: {created_tag.name}, {created_tag.rev}, {created_tag.date}\n" | ||
f"Latest Commit: {created_commit.title}, {created_commit.rev}" | ||
) | ||
|
||
command = f"git tag --delete {created_tag.name} && git reset HEAD~ && git reset --hard HEAD" | ||
|
||
Lee-W marked this conversation as resolved.
Show resolved
Hide resolved
|
||
out.info("Reverting version bump, running:") | ||
out.info(f"{command}") | ||
out.info( | ||
f"The tag can be removed from a remote by running `git push origin :{created_tag.name}`" | ||
) | ||
|
||
return command | ||
|
||
def __call__(self): | ||
bump: bool = self.arguments.get("bump") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
commit: bool = self.arguments.get("commit") | ||
|
||
if bump: | ||
command = self._get_bump_command() | ||
elif commit: | ||
command = "git reset HEAD~" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sometimes we'll encounter the situation that we don't have |
||
else: | ||
raise InvalidCommandArgumentError( | ||
( | ||
"One and only one argument is required for check command! " | ||
"See 'cz undo -h' for more information" | ||
) | ||
) | ||
|
||
c = cmd.run(command) | ||
if c.err: | ||
out.error(c.err) | ||
|
||
out.write(c.out) | ||
out.success("Undo successful!") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer the latter 👍 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import sys | ||
|
||
import pytest | ||
|
||
from commitizen import cli, git | ||
from tests.utils import create_file_and_commit | ||
|
||
|
||
@pytest.mark.usefixtures("tmp_commitizen_project") | ||
def test_undo_commit(config, mocker): | ||
create_file_and_commit("feat: new file") | ||
# We can not revert the first commit, thus we commit twice. | ||
create_file_and_commit("feat: extra file") | ||
|
||
testargs = ["cz", "undo", "--commit"] | ||
mocker.patch.object(sys, "argv", testargs) | ||
cli.main() | ||
|
||
commits = git.get_commits() | ||
|
||
assert len(commits) == 1 | ||
|
||
|
||
def _execute_command(mocker, testargs): | ||
mocker.patch.object(sys, "argv", testargs) | ||
cli.main() | ||
|
||
|
||
def _undo_bump(mocker, tag_num: int = 0): | ||
testargs = ["cz", "undo", "--bump"] | ||
_execute_command(mocker, testargs) | ||
|
||
tags = git.get_tags() | ||
assert len(tags) == tag_num | ||
|
||
|
||
@pytest.mark.usefixtures("tmp_commitizen_project") | ||
def test_undo_bump(config, mocker): | ||
# MINOR | ||
create_file_and_commit("feat: new file") | ||
_execute_command(mocker, ["cz", "bump", "--yes"]) | ||
_undo_bump(mocker) | ||
|
||
# PATCH | ||
create_file_and_commit("feat: new file") | ||
_execute_command(mocker, ["cz", "bump", "--yes"]) | ||
|
||
create_file_and_commit("fix: username exception") | ||
_execute_command(mocker, ["cz", "bump"]) | ||
_undo_bump(mocker, 1) | ||
|
||
# PRERELEASE | ||
create_file_and_commit("feat: location") | ||
_execute_command(mocker, ["cz", "bump", "--prerelease", "alpha"]) | ||
_undo_bump(mocker, 1) | ||
|
||
# PRERELEASE BUMP CREATES VERSION WITHOUT PRERELEASE | ||
create_file_and_commit("feat: location") | ||
_execute_command(mocker, ["cz", "bump", "--prerelease", "alpha"]) | ||
_execute_command(mocker, ["cz", "bump"]) | ||
_undo_bump(mocker, 2) | ||
|
||
# MAJOR | ||
create_file_and_commit( | ||
"feat: new user interface\n\nBREAKING CHANGE: age is no longer supported" | ||
) | ||
_execute_command(mocker, ["cz", "bump"]) | ||
_undo_bump(mocker, 2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems we use various terms for the same meaning (i.e., reset / revert / undo). I'll suggest unifying them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could use "undo". 👌