summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoris Roovers <joris.roovers@gmail.com>2023-05-02 09:58:38 +0200
committerGitHub <noreply@github.com>2023-05-02 09:58:38 +0200
commit2a77afd845832c1a00a65e210f9339344dd6f114 (patch)
treef057a13caabefff1041ada495ff815e9036a6e9d
parent2a48cdadad2e0085ac3e66cfc2b498e0dbdc9519 (diff)
Fix msg-filename kept open after edition (#491)
--msg-filename type is now a Path and not a managed click.File. This makes it easier to control the opening and closing of the file. Context manager will make sure that the file we use will be refreshed after edition. Co-authored-by: Maxime Roussin-BĂ©langer <maxime.roussinbelanger@gmail.com>
-rw-r--r--gitlint-core/gitlint/cli.py17
1 files changed, 8 insertions, 9 deletions
diff --git a/gitlint-core/gitlint/cli.py b/gitlint-core/gitlint/cli.py
index d3de5ac..2c18695 100644
--- a/gitlint-core/gitlint/cli.py
+++ b/gitlint-core/gitlint/cli.py
@@ -5,6 +5,7 @@ import platform
import stat
import sys
from dataclasses import dataclass
+from pathlib import Path
from typing import Optional
import click
@@ -185,7 +186,8 @@ def build_git_context(lint_config, msg_filename, commit_hash, refspec):
# 1. Any data specified via --msg-filename
if msg_filename:
LOG.debug("Using --msg-filename.")
- return from_commit_msg(str(msg_filename.read()))
+ with msg_filename.open(encoding=gitlint.utils.FILE_ENCODING) as msg_file:
+ return from_commit_msg(str(msg_file.read()))
# 2. Any data sent to stdin (unless stdin is being ignored)
if not lint_config.ignore_stdin:
@@ -245,7 +247,7 @@ class ContextObj:
config_builder: LintConfigBuilder
commit_hash: str
refspec: str
- msg_filename: str
+ msg_filename: Optional[Path] = None
gitcontext: Optional[GitContext] = None
@@ -270,7 +272,7 @@ class ContextObj:
@click.option("--ignore", envvar="GITLINT_IGNORE", default="", help="Ignore rules (comma-separated by id or name).")
@click.option("--contrib", envvar="GITLINT_CONTRIB", default="",
help="Contrib rules to enable (comma-separated by id or name).")
-@click.option("--msg-filename", type=click.File(encoding=gitlint.utils.FILE_ENCODING),
+@click.option("--msg-filename", type=click.Path(exists=True, dir_okay=False, path_type=Path),
help="Path to a file containing a commit-msg.")
@click.option("--ignore-stdin", envvar="GITLINT_IGNORE_STDIN", is_flag=True,
help="Ignore any stdin data. Useful for running in CI server.")
@@ -458,13 +460,10 @@ def run_hook(ctx):
exit_code = GITLINT_SUCCESS
elif value == "e":
LOG.debug("run-hook: editing commit message")
- msg_filename = ctx.obj.msg_filename
- if msg_filename:
- msg_filename.seek(0)
+ if ctx.obj.msg_filename:
editor = os.environ.get("EDITOR", DEFAULT_COMMIT_MSG_EDITOR)
- msg_filename_path = os.path.realpath(msg_filename.name)
- LOG.debug("run-hook: %s %s", editor, msg_filename_path)
- shell(editor + " " + msg_filename_path)
+ LOG.debug("run-hook: %s %s", editor, ctx.obj.msg_filename)
+ shell(f"{editor} {ctx.obj.msg_filename}")
else:
click.echo("Editing only possible when --msg-filename is specified.")
ctx.exit(exit_code)