Skip to content

feat(commands): add cz commit arg to automatically stage every tracked and un-staged file #821

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions commitizen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@
"action": "store_true",
"help": "sign off the commit",
},
{
"name": ["-a", "--all"],
"action": "store_true",
"help": "Tell the command to automatically stage files that have been modified and deleted, but new files you have not told Git about are not affected.",
},
],
},
{
Expand Down
4 changes: 4 additions & 0 deletions commitizen/commands/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ def __call__(self):
dry_run: bool = self.arguments.get("dry_run")
write_message_to_file = self.arguments.get("write_message_to_file")

is_all: bool = self.arguments.get("all")
if is_all:
c = git.add("-u")

if git.is_staging_clean() and not dry_run:
raise NothingToCommitError("No files added to staging!")

Expand Down
5 changes: 5 additions & 0 deletions commitizen/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ def tag(tag: str, annotated: bool = False, signed: bool = False) -> cmd.Command:
return c


def add(args: str = "") -> cmd.Command:
c = cmd.run(f"git add {args}")
return c


def commit(
message: str, args: str = "", committer_date: str | None = None
) -> cmd.Command:
Expand Down
21 changes: 21 additions & 0 deletions tests/commands/test_commit_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,24 @@ def test_commit_in_non_git_project(tmpdir, config):
with tmpdir.as_cwd():
with pytest.raises(NotAGitProjectError):
commands.Commit(config, {})


@pytest.mark.usefixtures("staging_is_clean")
def test_commit_command_with_all_option(config, mocker: MockFixture):
prompt_mock = mocker.patch("questionary.prompt")
prompt_mock.return_value = {
"prefix": "feat",
"subject": "user created",
"scope": "",
"is_breaking_change": False,
"body": "",
"footer": "",
}

commit_mock = mocker.patch("commitizen.git.commit")
commit_mock.return_value = cmd.Command("success", "", b"", b"", 0)
success_mock = mocker.patch("commitizen.out.success")
add_mock = mocker.patch("commitizen.git.add")
commands.Commit(config, {"all": True})()
add_mock.assert_called()
success_mock.assert_called_once()