Skip to content

Commit ca5eeee

Browse files
committed
refactor(utils): make get_backup_file_path to return a path for semantic correctness
1 parent 939ee43 commit ca5eeee

File tree

5 files changed

+28
-30
lines changed

5 files changed

+28
-30
lines changed

commitizen/commands/commit.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ def __init__(self, config: BaseConfig, arguments: CommitArgs) -> None:
5151
self.encoding = config.settings["encoding"]
5252
self.cz = factory.committer_factory(self.config)
5353
self.arguments = arguments
54-
self.temp_file: str = get_backup_file_path()
54+
self.backup_file_path = get_backup_file_path()
5555

5656
def _read_backup_message(self) -> str | None:
5757
# Check the commit backup file exists
58-
if not os.path.isfile(self.temp_file):
58+
if not self.backup_file_path.is_file():
5959
return None
6060

6161
# Read commit message from backup
62-
with open(self.temp_file, encoding=self.encoding) as f:
62+
with open(self.backup_file_path, encoding=self.encoding) as f:
6363
return f.read().strip()
6464

6565
def _prompt_commit_questions(self) -> str:
@@ -108,10 +108,10 @@ def manual_edit(self, message: str) -> str:
108108

109109
def _get_message(self) -> str:
110110
if self.arguments.get("retry"):
111-
m = self._read_backup_message()
112-
if m is None:
111+
commit_message = self._read_backup_message()
112+
if commit_message is None:
113113
raise NoCommitBackupError()
114-
return m
114+
return commit_message
115115

116116
if self.config.settings.get("retry_after_failure") and not self.arguments.get(
117117
"no_retry"
@@ -139,29 +139,29 @@ def __call__(self) -> None:
139139
if write_message_to_file is not None and write_message_to_file.is_dir():
140140
raise NotAllowed(f"{write_message_to_file} is a directory")
141141

142-
m = self._get_message()
142+
commit_message = self._get_message()
143143
if self.arguments.get("edit"):
144-
m = self.manual_edit(m)
144+
commit_message = self.manual_edit(commit_message)
145145

146-
out.info(f"\n{m}\n")
146+
out.info(f"\n{commit_message}\n")
147147

148148
if write_message_to_file:
149149
with smart_open(write_message_to_file, "w", encoding=self.encoding) as file:
150-
file.write(m)
150+
file.write(commit_message)
151151

152152
if dry_run:
153153
raise DryRunExit()
154154

155155
if self.config.settings["always_signoff"] or signoff:
156156
extra_args = f"{extra_args} -s".strip()
157157

158-
c = git.commit(m, args=extra_args)
158+
c = git.commit(commit_message, args=extra_args)
159159
if c.return_code != 0:
160160
out.error(c.err)
161161

162162
# Create commit backup
163-
with smart_open(self.temp_file, "w", encoding=self.encoding) as f:
164-
f.write(m)
163+
with smart_open(self.backup_file_path, "w", encoding=self.encoding) as f:
164+
f.write(commit_message)
165165

166166
raise CommitError()
167167

@@ -170,7 +170,7 @@ def __call__(self) -> None:
170170
return
171171

172172
with contextlib.suppress(FileNotFoundError):
173-
os.remove(self.temp_file)
173+
self.backup_file_path.unlink()
174174
out.write(c.err)
175175
out.write(c.out)
176176
out.success("Commit successful!")

commitizen/cz/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import re
33
import tempfile
4+
from pathlib import Path
45

56
from commitizen import git
67
from commitizen.cz import exceptions
@@ -22,10 +23,9 @@ def strip_local_version(version: str) -> str:
2223
return _RE_LOCAL_VERSION.sub("", version)
2324

2425

25-
def get_backup_file_path() -> str:
26+
def get_backup_file_path() -> Path:
2627
project_root = git.find_git_project_root()
2728
project = project_root.as_posix().replace("/", "%") if project_root else ""
2829

2930
user = os.environ.get("USER", "")
30-
31-
return os.path.join(tempfile.gettempdir(), f"cz.commit%{user}%{project}.backup")
31+
return Path(tempfile.gettempdir(), f"cz.commit%{user}%{project}.backup")

hooks/post-commit.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env python
2-
from pathlib import Path
32

43
try:
54
from commitizen.cz.utils import get_backup_file_path
@@ -9,11 +8,11 @@
98

109

1110
def post_commit() -> None:
12-
backup_file = Path(get_backup_file_path())
11+
backup_file_path = get_backup_file_path()
1312

1413
# remove backup file if it exists
15-
if backup_file.is_file():
16-
backup_file.unlink()
14+
if backup_file_path.is_file():
15+
backup_file_path.unlink()
1716

1817

1918
if __name__ == "__main__":

hooks/prepare-commit-msg.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import shutil
33
import subprocess
44
import sys
5-
from pathlib import Path
65
from subprocess import CalledProcessError
76

87
try:
@@ -25,12 +24,12 @@ def prepare_commit_msg(commit_msg_file: str) -> int:
2524
capture_output=True,
2625
).returncode
2726
if exit_code != 0:
28-
backup_file = Path(get_backup_file_path())
29-
if backup_file.is_file():
27+
backup_file_path = get_backup_file_path()
28+
if backup_file_path.is_file():
3029
# confirm if commit message from backup file should be reused
3130
answer = input("retry with previous message? [y/N]: ")
3231
if answer.lower() == "y":
33-
shutil.copyfile(backup_file, commit_msg_file)
32+
shutil.copyfile(backup_file_path, commit_msg_file)
3433
return 0
3534

3635
# use commitizen to generate the commit message
@@ -50,7 +49,7 @@ def prepare_commit_msg(commit_msg_file: str) -> int:
5049
return error.returncode
5150

5251
# write message to backup file
53-
shutil.copyfile(commit_msg_file, backup_file)
52+
shutil.copyfile(commit_msg_file, backup_file_path)
5453
return 0
5554

5655

tests/commands/test_commit_command.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def test_commit_backup_on_failure(config, mocker: MockFixture):
7373

7474
with pytest.raises(CommitError):
7575
commit_cmd = commands.Commit(config, {})
76-
temp_file = commit_cmd.temp_file
76+
temp_file = commit_cmd.backup_file_path
7777
commit_cmd()
7878

7979
prompt_mock.assert_called_once()
@@ -101,7 +101,7 @@ def test_commit_retry_works(config, mocker: MockFixture):
101101
success_mock = mocker.patch("commitizen.out.success")
102102

103103
commit_cmd = commands.Commit(config, {"retry": True})
104-
temp_file = commit_cmd.temp_file
104+
temp_file = commit_cmd.backup_file_path
105105
commit_cmd()
106106

107107
commit_mock.assert_called_with("backup commit", args="")
@@ -144,7 +144,7 @@ def test_commit_retry_after_failure_works(config, mocker: MockFixture):
144144

145145
config.settings["retry_after_failure"] = True
146146
commit_cmd = commands.Commit(config, {})
147-
temp_file = commit_cmd.temp_file
147+
temp_file = commit_cmd.backup_file_path
148148
commit_cmd()
149149

150150
commit_mock.assert_called_with("backup commit", args="")
@@ -171,7 +171,7 @@ def test_commit_retry_after_failure_with_no_retry_works(config, mocker: MockFixt
171171

172172
config.settings["retry_after_failure"] = True
173173
commit_cmd = commands.Commit(config, {"no_retry": True})
174-
temp_file = commit_cmd.temp_file
174+
temp_file = commit_cmd.backup_file_path
175175
commit_cmd()
176176

177177
commit_mock.assert_called_with("feat: user created\n\ncloses #21", args="")

0 commit comments

Comments
 (0)