Skip to content

Commit

Permalink
Add support for git config commit.verbose
Browse files Browse the repository at this point in the history
See `--verbose` in `man git commit`.

Fix mystor#29

Co-authored-by: ruro <[email protected]>
  • Loading branch information
nikitabobko and RuRo committed Aug 26, 2022
1 parent 5816b5f commit 6f0fb29
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
38 changes: 32 additions & 6 deletions gitrevise/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,18 @@ def cleanup_editor_content(
data: bytes,
commentchar: bytes,
cleanup_mode: EditorCleanupMode,
force_cut_after_scissors: bool = False,
allow_preceding_whitespace: bool = False,
) -> bytes:
if cleanup_mode == EditorCleanupMode.VERBATIM:
return data

lines_list = data.splitlines(keepends=True)

if cleanup_mode == EditorCleanupMode.SCISSORS:
# Force cut after scissors even in verbatim mode
if force_cut_after_scissors or cleanup_mode == EditorCleanupMode.SCISSORS:
lines_list = cut_after_scissors(lines_list, commentchar)

if cleanup_mode == EditorCleanupMode.VERBATIM:
return b"".join(lines_list)

if cleanup_mode == EditorCleanupMode.STRIP:
lines_list = strip_comments(lines_list, commentchar, allow_preceding_whitespace)

Expand Down Expand Up @@ -210,6 +212,7 @@ def run_specific_editor(
text: bytes,
cleanup_mode: EditorCleanupMode,
comments: Optional[str] = None,
commit_diff: Optional[bytes] = None,
allow_empty: bool = False,
allow_whitespace_before_comments: bool = False,
) -> bytes:
Expand All @@ -228,12 +231,22 @@ def run_specific_editor(
handle.write(b" " + comment.encode("utf-8"))
handle.write(b"\n")

if commit_diff:
handle.write(commentchar + b"\n")
lines = [commentchar + b" " + line.encode() for line in
EditorCleanupMode.SCISSORS.comment.splitlines(keepends=True)]
for line in lines:
handle.write(line)
handle.write(commit_diff)

# Invoke the editor
data = edit_file_with_editor(editor, path)
data = cleanup_editor_content(
data,
commentchar,
cleanup_mode,
# If diff is appended then git always cuts after the scissors (even when commit.cleanup=verbatim)
force_cut_after_scissors=commit_diff is not None,
allow_preceding_whitespace=allow_whitespace_before_comments,
)

Expand All @@ -257,6 +270,7 @@ def run_editor(
text: bytes,
cleanup_mode: EditorCleanupMode = EditorCleanupMode.DEFAULT,
comments: Optional[str] = None,
commit_diff: Optional[bytes] = None,
allow_empty: bool = False,
) -> bytes:
"""Run the editor configured for git to edit the given text"""
Expand All @@ -267,6 +281,7 @@ def run_editor(
text=text,
cleanup_mode=cleanup_mode,
comments=comments,
commit_diff=commit_diff,
allow_empty=allow_empty,
)

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

cleanup_mode = EditorCleanupMode.from_repository(repo)
comments = cleanup_mode.comment
commit_diff = None

# If the target commit is not a merge commit, produce a diff --stat to
# include in the commit message comments.
if len(commit.parents()) < 2:
tree_a = commit.parent_tree().persist().hex()
tree_b = commit.tree().persist().hex()
comments += "\n" + repo.git("diff-tree", "--stat", tree_a, tree_b).decode()

message = run_editor(repo, "COMMIT_EDITMSG", commit.message, cleanup_mode, comments=comments)
verbose = repo.bool_config("commit.verbose", False)
if verbose:
commit_diff = repo.git("diff", tree_a, tree_b)

message = run_editor(
repo,
"COMMIT_EDITMSG",
commit.message,
cleanup_mode,
comments=comments,
commit_diff=commit_diff,
)
return commit.update(message=message)


Expand Down
14 changes: 14 additions & 0 deletions tests/test_cleanup_editor_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,20 @@ def test_scissors() -> None:
)


def test_force_cut_scissors_in_verbatim_mode() -> None:
actual = cleanup_editor_content(
(
"foo\n"
f"# {GIT_SCISSOR_LINE_WITHOUT_COMMENT_CHAR}"
"bar\n"
).encode(),
b"#",
EditorCleanupMode.VERBATIM,
force_cut_after_scissors=True
)
assert actual == b"foo\n"


def _do_test(data: bytes, expected_strip: bytes, expected_whitespace: Optional[bytes] = None,
expected_scissors: Optional[bytes] = None):
if expected_whitespace is None:
Expand Down

0 comments on commit 6f0fb29

Please sign in to comment.