File tree Expand file tree Collapse file tree 3 files changed +37
-0
lines changed Expand file tree Collapse file tree 3 files changed +37
-0
lines changed Original file line number Diff line number Diff line change @@ -162,6 +162,12 @@ def __call__(
162
162
"default" : 0 ,
163
163
"help" : "length limit of the commit message; 0 for no limit" ,
164
164
},
165
+ {
166
+ "name" : ["-e" , "--edit" ],
167
+ "action" : "store_true" ,
168
+ "default" : False ,
169
+ "help" : "force edit the commit message in the editor" ,
170
+ },
165
171
],
166
172
},
167
173
{
Original file line number Diff line number Diff line change 2
2
3
3
import contextlib
4
4
import os
5
+ import shutil
6
+ import subprocess
7
+ import tempfile
5
8
6
9
import questionary
7
10
@@ -72,9 +75,27 @@ def prompt_commit_questions(self) -> str:
72
75
73
76
return message
74
77
78
+ def force_edit (self , message : str ) -> str :
79
+ editor = git .get_core_editor ()
80
+ if editor is None :
81
+ raise RuntimeError ("No 'editor' value given and no default available." )
82
+ exec_path = shutil .which (editor )
83
+ if exec_path is None :
84
+ raise RuntimeError (f"Editor '{ editor } ' not found." )
85
+ with tempfile .NamedTemporaryFile (mode = "w" , delete = False ) as file :
86
+ file .write (message )
87
+ file_path = file .name
88
+ argv = [exec_path , file_path ]
89
+ subprocess .call (argv )
90
+ with open (file_path ) as temp_file :
91
+ message = temp_file .read ().strip ()
92
+ os .remove (file_path )
93
+ return message
94
+
75
95
def __call__ (self ):
76
96
dry_run : bool = self .arguments .get ("dry_run" )
77
97
write_message_to_file : bool = self .arguments .get ("write_message_to_file" )
98
+ force_edit : bool = self .arguments .get ("edit" )
78
99
79
100
is_all : bool = self .arguments .get ("all" )
80
101
if is_all :
@@ -101,6 +122,9 @@ def __call__(self):
101
122
else :
102
123
m = self .prompt_commit_questions ()
103
124
125
+ if force_edit :
126
+ m = self .force_edit (m )
127
+
104
128
out .info (f"\n { m } \n " )
105
129
106
130
if write_message_to_file :
Original file line number Diff line number Diff line change @@ -271,6 +271,13 @@ def get_eol_style() -> EOLTypes:
271
271
return map ["native" ]
272
272
273
273
274
+ def get_core_editor () -> str | None :
275
+ c = cmd .run ("git var GIT_EDITOR" )
276
+ if c .out :
277
+ return c .out .strip ()
278
+ return None
279
+
280
+
274
281
def smart_open (* args , ** kargs ):
275
282
"""Open a file with the EOL style determined from Git."""
276
283
return open (* args , newline = get_eol_style ().get_eol_for_open (), ** kargs )
You can’t perform that action at this time.
0 commit comments