Skip to content

Commit 37a118e

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 4060cc2 commit 37a118e

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
@@ -14,6 +14,7 @@
1414
CommitizenException,
1515
ExitCode,
1616
ExpectedExit,
17+
InvalidCommandArgumentError,
1718
NoCommandFoundError,
1819
)
1920

@@ -410,7 +411,7 @@ def main():
410411

411412
# This is for the command required constraint in 2.0
412413
try:
413-
args = parser.parse_args()
414+
args, unknown_args = parser.parse_known_args()
414415
except (TypeError, SystemExit) as e:
415416
# https://github.com/commitizen-tools/commitizen/issues/429
416417
# argparse raises TypeError when non exist command is provided on Python < 3.9
@@ -419,6 +420,24 @@ def main():
419420
raise NoCommandFoundError()
420421
raise e
421422

423+
arguments = vars(args)
424+
if unknown_args:
425+
# Raise error for extra-args without -- separation
426+
if "--" not in unknown_args:
427+
raise InvalidCommandArgumentError(
428+
f"Invalid commitizen arguments were found: `{' '.join(unknown_args)}`. "
429+
"Please use -- separator for extra git args"
430+
)
431+
# Raise error for extra-args before --
432+
elif unknown_args[0] != "--":
433+
pos = unknown_args.index("--")
434+
raise InvalidCommandArgumentError(
435+
f"Invalid commitizen arguments were found before -- separator: `{' '.join(unknown_args[:pos])}`. "
436+
)
437+
# TODO: treat case when extra-args and commitizen args are identical
438+
extra_args = " ".join(unknown_args[1:])
439+
arguments["extra_cli_args"] = extra_args
440+
422441
if args.name:
423442
conf.update({"name": args.name})
424443
elif not args.name and not conf.path:
@@ -434,7 +453,7 @@ def main():
434453
)
435454
sys.excepthook = no_raise_debug_excepthook
436455

437-
args.func(conf, vars(args))()
456+
args.func(conf, arguments)()
438457

439458

440459
if __name__ == "__main__":

commitizen/commands/commit.py

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

9393
if signoff:
9494
c = git.commit(m, "-s")
95+
96+
if self.arguments.get("extra_cli_args"):
97+
c = git.commit(m, extra_args=self.arguments.get("extra_cli_args"))
9598
else:
9699
c = git.commit(m)
97100

commitizen/git.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ def tag(tag: str, annotated: bool = False, signed: bool = False) -> cmd.Command:
9292
return c
9393

9494

95-
def commit(message: str, args: str = "") -> cmd.Command:
95+
def commit(message: str, args: str = "", *, extra_args: str = "") -> cmd.Command:
9696
f = NamedTemporaryFile("wb", delete=False)
9797
f.write(message.encode("utf-8"))
9898
f.close()
99-
c = cmd.run(f"git commit {args} -F {f.name}")
99+
c = cmd.run(f"git commit {args} {extra_args} -F {f.name}")
100100
os.unlink(f.name)
101101
return c
102102

0 commit comments

Comments
 (0)