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 #451
  • Loading branch information
Sebastien Crocquevieille committed Oct 26, 2022
1 parent a9873d1 commit fc81dc8
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 @@ -12,6 +12,7 @@
CommitizenException,
ExitCode,
ExpectedExit,
InvalidCommandArgumentError,
NoCommandFoundError,
)

Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -398,7 +417,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 @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions commitizen/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit fc81dc8

Please sign in to comment.