diff --git a/commitizen/cli.py b/commitizen/cli.py index 2064ef3bf1..6abf0a4f59 100644 --- a/commitizen/cli.py +++ b/commitizen/cli.py @@ -12,6 +12,7 @@ CommitizenException, ExitCode, ExpectedExit, + InvalidCommandArgumentError, NoCommandFoundError, ) @@ -374,7 +375,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 @@ -383,6 +384,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: @@ -398,7 +417,7 @@ def main(): ) sys.excepthook = no_raise_debug_excepthook - args.func(conf, vars(args))() + args.func(conf, arguments)() if __name__ == "__main__": diff --git a/commitizen/commands/commit.py b/commitizen/commands/commit.py index 13e61abe6d..0715221bbf 100644 --- a/commitizen/commands/commit.py +++ b/commitizen/commands/commit.py @@ -83,6 +83,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) diff --git a/commitizen/git.py b/commitizen/git.py index 2c2cb5b368..b52c361521 100644 --- a/commitizen/git.py +++ b/commitizen/git.py @@ -92,11 +92,11 @@ def tag(tag: str, annotated: bool = False, signed: bool = False) -> cmd.Command: return c -def commit(message: str, args: str = "") -> cmd.Command: +def commit(message: str, args: str = "", *, extra_args: str = "") -> cmd.Command: f = NamedTemporaryFile("wb", delete=False) f.write(message.encode("utf-8")) f.close() - c = cmd.run(f"git commit {args} -F {f.name}") + c = cmd.run(f"git commit {args} {extra_args} -F {f.name}") os.unlink(f.name) return c