4 from . import diffparse
7 from .interaction
import Interaction
8 from .models
import prefs
11 def wrap_comment(context
, text
):
12 indent
= prefs
.comment_char(context
) + ' '
17 initial_indent
=indent
,
18 subsequent_indent
=indent
,
24 def strip_comments(context
, text
):
25 comment_char
= prefs
.comment_char(context
)
27 line
for line
in text
.split('\n') if not line
.startswith(comment_char
)
31 def patch_edit_header(context
, *, reverse
, apply_to_worktree
):
34 "Edit the following patch, which will then be applied to the worktree to"
35 " revert the changes:"
40 "Edit the following patch, which will then be applied to the staging"
41 " area to unstage the changes:"
45 "Edit the following patch, which will then be applied to the staging"
46 " area to stage the changes:"
48 return wrap_comment(context
, header
)
51 def patch_edit_footer(context
):
55 "To avoid applying removal lines ('-'), change them to context lines (' ')."
57 N_("To avoid applying addition lines ('+'), delete them."),
58 N_("To abort applying this patch, remove all lines."),
59 N_("Lines starting with '%s' will be ignored.") % prefs
.comment_char(context
),
61 "It is not necessary to update the hunk header lines as they will be"
62 " regenerated automatically."
65 return ''.join(wrap_comment(context
, part
) for part
in parts
)
68 def edit_patch(patch
, encoding
, context
, *, reverse
, apply_to_worktree
):
69 patch_file_path
= utils
.tmp_filename('edit', '.patch')
73 context
, reverse
=reverse
, apply_to_worktree
=apply_to_worktree
75 patch
.as_text(file_headers
=False),
76 patch_edit_footer(context
),
78 core
.write(patch_file_path
, ''.join(content_parts
), encoding
=encoding
)
79 status
, _
, _
= core
.run_command(
80 [*utils
.shell_split(prefs
.editor(context
)), patch_file_path
]
83 patch_text
= strip_comments(
84 context
, core
.read(patch_file_path
, encoding
=encoding
)
88 N_("Editor returned %s exit code. Not applying patch.") % status
91 return diffparse
.Patch
.parse(patch
.filename
, patch_text
)
93 core
.unlink(patch_file_path
)