Skip to content

Commit fa60610

Browse files
authored
Load a commit.template message if one is configured (#671)
closes #546
1 parent 838258c commit fa60610

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

src/components/commit.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ use crate::{
1313
use anyhow::Result;
1414
use asyncgit::{
1515
cached,
16-
sync::{self, CommitId, HookResult},
16+
sync::{self, utils::get_config_string, CommitId, HookResult},
1717
CWD,
1818
};
1919
use crossterm::event::Event;
2020
use std::{
21-
fs::File,
21+
fs::{read_to_string, File},
2222
io::{Read, Write},
2323
path::PathBuf,
2424
};
@@ -35,6 +35,7 @@ pub struct CommitComponent {
3535
queue: Queue,
3636
key_config: SharedKeyConfig,
3737
git_branch_name: cached::BranchName,
38+
commit_template: Option<String>,
3839
}
3940

4041
impl DrawableComponent for CommitComponent {
@@ -129,6 +130,13 @@ impl Component for CommitComponent {
129130

130131
self.input
131132
.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+
132140
self.input.show()?;
133141

134142
Ok(())
@@ -154,12 +162,22 @@ impl CommitComponent {
154162
),
155163
key_config,
156164
git_branch_name: cached::BranchName::new(CWD),
165+
commit_template: None,
157166
}
158167
}
159168

160169
///
161170
pub fn update(&mut self) -> Result<()> {
162171
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+
163181
Ok(())
164182
}
165183

@@ -291,13 +309,22 @@ impl CommitComponent {
291309
}
292310

293311
fn can_commit(&self) -> bool {
294-
!self.input.get_text().is_empty()
312+
!self.is_empty() && self.is_changed()
295313
}
296314

297315
fn can_amend(&self) -> bool {
298316
self.amend.is_none()
299317
&& 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())
301328
}
302329

303330
fn amend(&mut self) -> Result<()> {

0 commit comments

Comments
 (0)