Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Feat 451 add commit args #621

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
Lee-W merged 8 commits into commitizen-tools:master from SCrocky:feat-451-add-commit-args
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat(cli.py): Added support for extra git CLI args after -- separator...
... for `cz commit` command
Additional edits were made in class `commitizen.commands.Commit` and `commit` func from `commitizen.git`
Closes #451 
  • Loading branch information
Sebastien Crocquevieille authored and Scrocky committed Oct 2, 2023
commit adfdfb185c1596670701a1bd0763efd99b46dcf7
23 changes: 21 additions & 2 deletions commitizen/cli.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
CommitizenException,
ExitCode,
ExpectedExit,
InvalidCommandArgumentError,
NoCommandFoundError,
)

Expand Down Expand Up @@ -441,7 +442,7 @@ def main():

# This is for the command required constraint in 2.0
try:
args= parser.parse_args()
args, unknown_args = parser.parse_known_args()
except (TypeError, SystemExit) as e:
# https://github.com/commitizen-tools/commitizen/issues/429
# argparse raises TypeError when non exist command is provided on Python < 3.9
Expand All @@ -450,6 +451,24 @@ def main():
raise NoCommandFoundError()
raise e

arguments = vars(args)
if unknown_args:
# Raise error for extra-args without -- separation
if "--" not in unknown_args:
raise InvalidCommandArgumentError(
f"Invalid commitizen arguments were found: `{' '.join(unknown_args)}`. "
"Please use -- separator for extra git args"
)
# Raise error for extra-args before --
elif unknown_args[0] != "--":
pos = unknown_args.index("--")
raise InvalidCommandArgumentError(
f"Invalid commitizen arguments were found before -- separator: `{' '.join(unknown_args[:pos])}`. "
)
# TODO: treat case when extra-args and commitizen args are identical
extra_args = " ".join(unknown_args[1:])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might need to handle the case that the user use -- without actually providing an argument. e.g., cz commit --

Copy link
Contributor Author

@SCrocky SCrocky Jun 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, how should we deal with it?

Warning or error?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning should be enough in this case 👍

SCrocky reacted with thumbs up emoji
arguments["extra_cli_args"] = extra_args

if args.name:
conf.update({"name": args.name})
elif not args.name and not conf.path:
Expand All @@ -465,7 +484,7 @@ def main():
)
sys.excepthook = no_raise_debug_excepthook

args.func(conf, vars(args))()
args.func(conf, arguments)()


if __name__ == "__main__":
Expand Down
3 changes: 3 additions & 0 deletions commitizen/commands/commit.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ def __call__(self):

if signoff:
c = git.commit(m, "-s")

if self.arguments.get("extra_cli_args"):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this if-else block? Could we handle the case in git.commit() when extra_cli_args is not provided?

SCrocky reacted with thumbs up emoji
c = git.commit(m, extra_args=self.arguments.get("extra_cli_args"))
else:
c = git.commit(m)

Expand Down
4 changes: 2 additions & 2 deletions commitizen/git.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ def add(args: str = "") -> cmd.Command:


def commit(
message: str, args: str = "", committer_date: str | None = None
message: str, args: str = "", extra_args: str = "", committer_date: str | None = None
) -> cmd.Command:
f = NamedTemporaryFile("wb", delete=False)
f.write(message.encode("utf-8"))
f.close()

command = f"git commit {args} -F {f.name}"
command = cmd.run(f"git commit {args} {extra_args} -F {f.name}")

if committer_date and os.name == "nt": # pragma: no cover
# Using `cmd /v /c "{command}"` sets environment variables only for that command
Expand Down

AltStyle によって変換されたページ (->オリジナル) /