@@ -13,12 +13,12 @@ use crate::{
13
13
use anyhow:: Result ;
14
14
use asyncgit:: {
15
15
cached,
16
- sync:: { self , CommitId , HookResult } ,
16
+ sync:: { self , utils :: get_config_string , CommitId , HookResult } ,
17
17
CWD ,
18
18
} ;
19
19
use crossterm:: event:: Event ;
20
20
use std:: {
21
- fs:: File ,
21
+ fs:: { read_to_string , File } ,
22
22
io:: { Read , Write } ,
23
23
path:: PathBuf ,
24
24
} ;
@@ -35,6 +35,7 @@ pub struct CommitComponent {
35
35
queue : Queue ,
36
36
key_config : SharedKeyConfig ,
37
37
git_branch_name : cached:: BranchName ,
38
+ commit_template : Option < String > ,
38
39
}
39
40
40
41
impl DrawableComponent for CommitComponent {
@@ -129,6 +130,13 @@ impl Component for CommitComponent {
129
130
130
131
self . input
131
132
. set_title ( strings:: commit_title ( & self . key_config ) ) ;
133
+
134
+ if self . is_empty ( ) {
135
+ if let Some ( s) = & self . commit_template {
136
+ self . input . set_text ( s. clone ( ) ) ;
137
+ }
138
+ }
139
+
132
140
self . input . show ( ) ?;
133
141
134
142
Ok ( ( ) )
@@ -154,12 +162,22 @@ impl CommitComponent {
154
162
) ,
155
163
key_config,
156
164
git_branch_name : cached:: BranchName :: new ( CWD ) ,
165
+ commit_template : None ,
157
166
}
158
167
}
159
168
160
169
///
161
170
pub fn update ( & mut self ) -> Result < ( ) > {
162
171
self . git_branch_name . lookup ( ) . map ( Some ) . unwrap_or ( None ) ;
172
+
173
+ self . commit_template . get_or_insert_with ( || {
174
+ get_config_string ( CWD , "commit.template" )
175
+ . ok ( )
176
+ . unwrap_or ( None )
177
+ . and_then ( |path| read_to_string ( path) . ok ( ) )
178
+ . unwrap_or_else ( String :: new)
179
+ } ) ;
180
+
163
181
Ok ( ( ) )
164
182
}
165
183
@@ -291,13 +309,22 @@ impl CommitComponent {
291
309
}
292
310
293
311
fn can_commit ( & self ) -> bool {
294
- !self . input . get_text ( ) . is_empty ( )
312
+ !self . is_empty ( ) && self . is_changed ( )
295
313
}
296
314
297
315
fn can_amend ( & self ) -> bool {
298
316
self . amend . is_none ( )
299
317
&& sync:: get_head ( CWD ) . is_ok ( )
300
- && self . input . get_text ( ) . is_empty ( )
318
+ && ( self . is_empty ( ) || !self . is_changed ( ) )
319
+ }
320
+
321
+ fn is_empty ( & self ) -> bool {
322
+ self . input . get_text ( ) . is_empty ( )
323
+ }
324
+
325
+ fn is_changed ( & self ) -> bool {
326
+ Some ( self . input . get_text ( ) . trim ( ) )
327
+ != self . commit_template . as_ref ( ) . map ( |s| s. trim ( ) )
301
328
}
302
329
303
330
fn amend ( & mut self ) -> Result < ( ) > {
0 commit comments