Skip to content

Commit adfdfb1

Browse files
Sebastien CrocquevieilleScrocky
Sebastien Crocquevieille
authored and
Scrocky
committed
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 commitizen-tools#451
1 parent b848b8f commit adfdfb1

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

commitizen/cli.py

+21-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
CommitizenException,
1616
ExitCode,
1717
ExpectedExit,
18+
InvalidCommandArgumentError,
1819
NoCommandFoundError,
1920
)
2021

@@ -441,7 +442,7 @@ def main():
441442

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

454+
arguments = vars(args)
455+
if unknown_args:
456+
# Raise error for extra-args without -- separation
457+
if "--" not in unknown_args:
458+
raise InvalidCommandArgumentError(
459+
f"Invalid commitizen arguments were found: `{' '.join(unknown_args)}`. "
460+
"Please use -- separator for extra git args"
461+
)
462+
# Raise error for extra-args before --
463+
elif unknown_args[0] != "--":
464+
pos = unknown_args.index("--")
465+
raise InvalidCommandArgumentError(
466+
f"Invalid commitizen arguments were found before -- separator: `{' '.join(unknown_args[:pos])}`. "
467+
)
468+
# TODO: treat case when extra-args and commitizen args are identical
469+
extra_args = " ".join(unknown_args[1:])
470+
arguments["extra_cli_args"] = extra_args
471+
453472
if args.name:
454473
conf.update({"name": args.name})
455474
elif not args.name and not conf.path:
@@ -465,7 +484,7 @@ def main():
465484
)
466485
sys.excepthook = no_raise_debug_excepthook
467486

468-
args.func(conf, vars(args))()
487+
args.func(conf, arguments)()
469488

470489

471490
if __name__ == "__main__":

commitizen/commands/commit.py

+3
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ def __call__(self):
9999

100100
if signoff:
101101
c = git.commit(m, "-s")
102+
103+
if self.arguments.get("extra_cli_args"):
104+
c = git.commit(m, extra_args=self.arguments.get("extra_cli_args"))
102105
else:
103106
c = git.commit(m)
104107

commitizen/git.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,13 @@ def add(args: str = "") -> cmd.Command:
9898

9999

100100
def commit(
101-
message: str, args: str = "", committer_date: str | None = None
101+
message: str, args: str = "", extra_args: str = "", committer_date: str | None = None
102102
) -> cmd.Command:
103103
f = NamedTemporaryFile("wb", delete=False)
104104
f.write(message.encode("utf-8"))
105105
f.close()
106106

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

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

0 commit comments

Comments
 (0)