Skip to content

Commit fc81dc8

Browse files
author
Sebastien Crocquevieille
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 #451
1 parent a9873d1 commit fc81dc8

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
@@ -12,6 +12,7 @@
1212
CommitizenException,
1313
ExitCode,
1414
ExpectedExit,
15+
InvalidCommandArgumentError,
1516
NoCommandFoundError,
1617
)
1718

@@ -374,7 +375,7 @@ def main():
374375

375376
# This is for the command required constraint in 2.0
376377
try:
377-
args = parser.parse_args()
378+
args, unknown_args = parser.parse_known_args()
378379
except (TypeError, SystemExit) as e:
379380
# https://github.com/commitizen-tools/commitizen/issues/429
380381
# argparse raises TypeError when non exist command is provided on Python < 3.9
@@ -383,6 +384,24 @@ def main():
383384
raise NoCommandFoundError()
384385
raise e
385386

387+
arguments = vars(args)
388+
if unknown_args:
389+
# Raise error for extra-args without -- separation
390+
if "--" not in unknown_args:
391+
raise InvalidCommandArgumentError(
392+
f"Invalid commitizen arguments were found: `{' '.join(unknown_args)}`. "
393+
"Please use -- separator for extra git args"
394+
)
395+
# Raise error for extra-args before --
396+
elif unknown_args[0] != "--":
397+
pos = unknown_args.index("--")
398+
raise InvalidCommandArgumentError(
399+
f"Invalid commitizen arguments were found before -- separator: `{' '.join(unknown_args[:pos])}`. "
400+
)
401+
# TODO: treat case when extra-args and commitizen args are identical
402+
extra_args = " ".join(unknown_args[1:])
403+
arguments["extra_cli_args"] = extra_args
404+
386405
if args.name:
387406
conf.update({"name": args.name})
388407
elif not args.name and not conf.path:
@@ -398,7 +417,7 @@ def main():
398417
)
399418
sys.excepthook = no_raise_debug_excepthook
400419

401-
args.func(conf, vars(args))()
420+
args.func(conf, arguments)()
402421

403422

404423
if __name__ == "__main__":

commitizen/commands/commit.py

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

8484
if signoff:
8585
c = git.commit(m, "-s")
86+
87+
if self.arguments.get("extra_cli_args"):
88+
c = git.commit(m, extra_args=self.arguments.get("extra_cli_args"))
8689
else:
8790
c = git.commit(m)
8891

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)