Skip to content

Commit fce8f35

Browse files
nikitabobkoruro
authored andcommitted
Add support for git config commit.verbose
See `--verbose` in `man git commit`. Fix #29 Co-authored-by: ruro <[email protected]>
1 parent f0dfa38 commit fce8f35

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

gitrevise/utils.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,18 @@ def cleanup_editor_content(
160160
data: bytes,
161161
commentchar: bytes,
162162
cleanup_mode: EditorCleanupMode,
163+
force_cut_after_scissors: bool = False,
163164
allow_preceding_whitespace: bool = False,
164165
) -> bytes:
165-
if cleanup_mode == EditorCleanupMode.VERBATIM:
166-
return data
167-
168166
lines_list = data.splitlines(keepends=True)
169167

170-
if cleanup_mode == EditorCleanupMode.SCISSORS:
168+
# Force cut after scissors even in verbatim mode
169+
if force_cut_after_scissors or cleanup_mode == EditorCleanupMode.SCISSORS:
171170
lines_list = cut_after_scissors(lines_list, commentchar)
172171

172+
if cleanup_mode == EditorCleanupMode.VERBATIM:
173+
return b"".join(lines_list)
174+
173175
if cleanup_mode == EditorCleanupMode.STRIP:
174176
lines_list = strip_comments(lines_list, commentchar, allow_preceding_whitespace)
175177

@@ -210,6 +212,7 @@ def run_specific_editor(
210212
text: bytes,
211213
cleanup_mode: EditorCleanupMode,
212214
comments: Optional[str] = None,
215+
commit_diff: Optional[bytes] = None,
213216
allow_empty: bool = False,
214217
allow_whitespace_before_comments: bool = False,
215218
) -> bytes:
@@ -228,12 +231,22 @@ def run_specific_editor(
228231
handle.write(b" " + comment.encode("utf-8"))
229232
handle.write(b"\n")
230233

234+
if commit_diff:
235+
handle.write(commentchar + b"\n")
236+
lines = [commentchar + b" " + line.encode() for line in
237+
EditorCleanupMode.SCISSORS.comment.splitlines(keepends=True)]
238+
for line in lines:
239+
handle.write(line)
240+
handle.write(commit_diff)
241+
231242
# Invoke the editor
232243
data = edit_file_with_editor(editor, path)
233244
data = cleanup_editor_content(
234245
data,
235246
commentchar,
236247
cleanup_mode,
248+
# If diff is appended then git always cuts after the scissors (even when commit.cleanup=verbatim)
249+
force_cut_after_scissors=commit_diff is not None,
237250
allow_preceding_whitespace=allow_whitespace_before_comments,
238251
)
239252

@@ -257,6 +270,7 @@ def run_editor(
257270
text: bytes,
258271
cleanup_mode: EditorCleanupMode = EditorCleanupMode.DEFAULT,
259272
comments: Optional[str] = None,
273+
commit_diff: Optional[bytes] = None,
260274
allow_empty: bool = False,
261275
) -> bytes:
262276
"""Run the editor configured for git to edit the given text"""
@@ -267,6 +281,7 @@ def run_editor(
267281
text=text,
268282
cleanup_mode=cleanup_mode,
269283
comments=comments,
284+
commit_diff=commit_diff,
270285
allow_empty=allow_empty,
271286
)
272287

@@ -310,15 +325,26 @@ def edit_commit_message(commit: Commit) -> Commit:
310325

311326
cleanup_mode = EditorCleanupMode.from_repository(repo)
312327
comments = cleanup_mode.comment
328+
commit_diff = None
313329

314330
# If the target commit is not a merge commit, produce a diff --stat to
315331
# include in the commit message comments.
316332
if len(commit.parents()) < 2:
317333
tree_a = commit.parent_tree().persist().hex()
318334
tree_b = commit.tree().persist().hex()
319335
comments += "\n" + repo.git("diff-tree", "--stat", tree_a, tree_b).decode()
320-
321-
message = run_editor(repo, "COMMIT_EDITMSG", commit.message, cleanup_mode, comments=comments)
336+
verbose = repo.bool_config("commit.verbose", False)
337+
if verbose:
338+
commit_diff = repo.git("diff", tree_a, tree_b)
339+
340+
message = run_editor(
341+
repo,
342+
"COMMIT_EDITMSG",
343+
commit.message,
344+
cleanup_mode,
345+
comments=comments,
346+
commit_diff=commit_diff,
347+
)
322348
return commit.update(message=message)
323349

324350

tests/test_cleanup_editor_content.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,20 @@ def test_scissors() -> None:
100100
)
101101

102102

103+
def test_force_cut_scissors_in_verbatim_mode() -> None:
104+
actual = cleanup_editor_content(
105+
(
106+
"foo\n"
107+
f"# {GIT_SCISSOR_LINE_WITHOUT_COMMENT_CHAR}"
108+
"bar\n"
109+
).encode(),
110+
b"#",
111+
EditorCleanupMode.VERBATIM,
112+
force_cut_after_scissors=True
113+
)
114+
assert actual == b"foo\n"
115+
116+
103117
def _do_test(data: bytes, expected_strip: bytes, expected_whitespace: Optional[bytes] = None,
104118
expected_scissors: Optional[bytes] = None):
105119
if expected_whitespace is None:

0 commit comments

Comments
 (0)