Load a commit.template message if one is configured (#671)

closes #546
This commit is contained in:
Wander Nauta 2021-04-26 14:37:48 +02:00 committed by GitHub
parent 838258c933
commit fa606104db
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -13,12 +13,12 @@ use crate::{
use anyhow::Result; use anyhow::Result;
use asyncgit::{ use asyncgit::{
cached, cached,
sync::{self, CommitId, HookResult}, sync::{self, utils::get_config_string, CommitId, HookResult},
CWD, CWD,
}; };
use crossterm::event::Event; use crossterm::event::Event;
use std::{ use std::{
fs::File, fs::{read_to_string, File},
io::{Read, Write}, io::{Read, Write},
path::PathBuf, path::PathBuf,
}; };
@ -35,6 +35,7 @@ pub struct CommitComponent {
queue: Queue, queue: Queue,
key_config: SharedKeyConfig, key_config: SharedKeyConfig,
git_branch_name: cached::BranchName, git_branch_name: cached::BranchName,
commit_template: Option<String>,
} }
impl DrawableComponent for CommitComponent { impl DrawableComponent for CommitComponent {
@ -129,6 +130,13 @@ impl Component for CommitComponent {
self.input self.input
.set_title(strings::commit_title(&self.key_config)); .set_title(strings::commit_title(&self.key_config));
if self.is_empty() {
if let Some(s) = &self.commit_template {
self.input.set_text(s.clone());
}
}
self.input.show()?; self.input.show()?;
Ok(()) Ok(())
@ -154,12 +162,22 @@ impl CommitComponent {
), ),
key_config, key_config,
git_branch_name: cached::BranchName::new(CWD), git_branch_name: cached::BranchName::new(CWD),
commit_template: None,
} }
} }
/// ///
pub fn update(&mut self) -> Result<()> { pub fn update(&mut self) -> Result<()> {
self.git_branch_name.lookup().map(Some).unwrap_or(None); self.git_branch_name.lookup().map(Some).unwrap_or(None);
self.commit_template.get_or_insert_with(|| {
get_config_string(CWD, "commit.template")
.ok()
.unwrap_or(None)
.and_then(|path| read_to_string(path).ok())
.unwrap_or_else(String::new)
});
Ok(()) Ok(())
} }
@ -291,13 +309,22 @@ impl CommitComponent {
} }
fn can_commit(&self) -> bool { fn can_commit(&self) -> bool {
!self.input.get_text().is_empty() !self.is_empty() && self.is_changed()
} }
fn can_amend(&self) -> bool { fn can_amend(&self) -> bool {
self.amend.is_none() self.amend.is_none()
&& sync::get_head(CWD).is_ok() && sync::get_head(CWD).is_ok()
&& self.input.get_text().is_empty() && (self.is_empty() || !self.is_changed())
}
fn is_empty(&self) -> bool {
self.input.get_text().is_empty()
}
fn is_changed(&self) -> bool {
Some(self.input.get_text().trim())
!= self.commit_template.as_ref().map(|s| s.trim())
} }
fn amend(&mut self) -> Result<()> { fn amend(&mut self) -> Result<()> {