Skip to content

Commit

Permalink
feat(cli.py): Added support for extra git CLI args after -- separator…
Browse files Browse the repository at this point in the history
… for `cz commit` command

Additional edits were made in class `commitizen.commands.Commit` and `commit` func from `commitizen.git`

Closes commitizen-tools#451
  • Loading branch information
Sebastien Crocquevieille authored and Scrocky committed Oct 2, 2023
1 parent b848b8f commit adfdfb1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
23 changes: 21 additions & 2 deletions commitizen/cli.py
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:])
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
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"):
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
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

0 comments on commit adfdfb1

Please sign in to comment.