@@ -42,14 +42,14 @@ fn commit_type_at_index (index: u8) -> Option<CommitType> {
42
42
CommitType :: iter_variants ( ) . nth ( index as usize )
43
43
}
44
44
45
- fn select_emoji ( ) -> Option < & ' static str > {
45
+ fn select_emoji ( initial_selected : Option < CommitType > ) -> Option < & ' static str > {
46
46
let mut log_update = LogUpdate :: new ( stderr ( ) ) . unwrap ( ) ;
47
47
let mut raw_output = stderr ( ) . into_raw_mode ( ) . unwrap ( ) ;
48
48
49
49
let mut key_stream = stdin ( ) . keys ( ) ;
50
50
51
51
let mut aborted = false ;
52
- let mut selected = CommitType :: Breaking ;
52
+ let mut selected = initial_selected . unwrap_or ( CommitType :: Breaking ) ;
53
53
54
54
// Clear possibly printed "hint" from git
55
55
log_update. render ( "" ) . unwrap ( ) ;
@@ -73,14 +73,14 @@ fn select_emoji() -> Option<&'static str> {
73
73
if aborted { None } else { Some ( selected. emoji ( ) ) }
74
74
}
75
75
76
- fn collect_commit_message ( selected_emoji : & ' static str , launch_editor : & mut bool ) -> Option < String > {
76
+ fn collect_commit_message ( selected_emoji : & ' static str , initial_message : Option < String > , launch_editor : & mut bool ) -> Option < String > {
77
77
let mut log_update = LogUpdate :: new ( stderr ( ) ) . unwrap ( ) ;
78
78
let mut raw_output = stderr ( ) . into_raw_mode ( ) . unwrap ( ) ;
79
79
80
80
let mut key_stream = stdin ( ) . keys ( ) ;
81
81
82
82
let mut aborted = false ;
83
- let mut input = String :: new ( ) ;
83
+ let mut input = initial_message . unwrap_or ( String :: new ( ) ) ;
84
84
85
85
loop {
86
86
let rule_text = commit_rules:: check_message ( & input)
@@ -143,6 +143,30 @@ fn launch_git_with_self_as_editor() {
143
143
run_cmd ( Command :: new ( "git" ) . arg ( "commit" ) . env ( "GIT_EDITOR" , self_path) )
144
144
}
145
145
146
+ fn git_parse_existing_message ( file : & mut File ) -> Option < ( CommitType , String ) > {
147
+ let first_line = file. read_line ( ) . unwrap ( ) . unwrap ( ) ;
148
+
149
+ if first_line. is_empty ( ) {
150
+ return None ;
151
+ }
152
+
153
+
154
+ let first_str = first_line. chars ( ) . next ( ) . unwrap ( ) . to_string ( ) ;
155
+
156
+ let commit_type = CommitType :: iter_variants ( ) . find ( |commit_type| {
157
+ first_str == commit_type. emoji ( )
158
+ } ) ;
159
+
160
+ if commit_type == None { return None ; }
161
+
162
+ // Check that the rest of the commit message is empty (i.e. no body)
163
+ if !git_message_is_empty ( file) { return None ; }
164
+
165
+ let emoji = commit_type. unwrap ( ) . emoji ( ) . to_string ( ) ;
166
+ let message = first_line. replace ( & emoji, "" ) . trim ( ) . to_string ( ) ;
167
+ Some ( ( commit_type. unwrap ( ) , message) )
168
+ }
169
+
146
170
fn git_message_is_empty ( file : & mut File ) -> bool {
147
171
for line in BufReader :: new ( file) . lines ( ) {
148
172
let line = line. expect ( "Failed to read line from git message file" ) ;
@@ -156,20 +180,28 @@ fn git_message_is_empty(file: &mut File) -> bool {
156
180
157
181
fn collect_information_and_write_to_file ( out_path : PathBuf ) {
158
182
let mut file = File :: options ( ) . read ( true ) . write ( true ) . create ( true ) . open ( & out_path) . unwrap ( ) ;
183
+ let mut initial_message: Option < String > = None ;
184
+ let mut initial_commit_type: Option < CommitType > = None ;
159
185
160
186
if !git_message_is_empty ( & mut file) {
161
- launch_default_editor ( out_path) ;
162
- return ;
187
+ file. rewind ( ) . unwrap ( ) ;
188
+ if let Some ( ( commit_type, message) ) = git_parse_existing_message ( & mut file) {
189
+ initial_commit_type = Some ( commit_type) ;
190
+ initial_message = Some ( message) ;
191
+ } else {
192
+ launch_default_editor ( out_path) ;
193
+ return ;
194
+ }
163
195
}
164
196
165
- let maybe_emoji = select_emoji ( ) ;
197
+ let maybe_emoji = select_emoji ( initial_commit_type ) ;
166
198
if maybe_emoji == None {
167
199
abort ( ) ;
168
200
}
169
201
170
202
if let Some ( emoji) = maybe_emoji {
171
203
let mut launch_editor = false ;
172
- let maybe_message = collect_commit_message ( emoji, & mut launch_editor) ;
204
+ let maybe_message = collect_commit_message ( emoji, initial_message , & mut launch_editor) ;
173
205
if maybe_message == None {
174
206
abort ( ) ;
175
207
}
0 commit comments