Skip to content

Commit

Permalink
Load a commit.template message if one is configured (#671)
Browse files Browse the repository at this point in the history
closes #546
  • Loading branch information
wandernauta authored Apr 26, 2021
1 parent 838258c commit fa60610
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions src/components/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ use crate::{
use anyhow::Result;
use asyncgit::{
cached,
sync::{self, CommitId, HookResult},
sync::{self, utils::get_config_string, CommitId, HookResult},
CWD,
};
use crossterm::event::Event;
use std::{
fs::File,
fs::{read_to_string, File},
io::{Read, Write},
path::PathBuf,
};
Expand All @@ -35,6 +35,7 @@ pub struct CommitComponent {
queue: Queue,
key_config: SharedKeyConfig,
git_branch_name: cached::BranchName,
commit_template: Option<String>,
}

impl DrawableComponent for CommitComponent {
Expand Down Expand Up @@ -129,6 +130,13 @@ impl Component for CommitComponent {

self.input
.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()?;

Ok(())
Expand All @@ -154,12 +162,22 @@ impl CommitComponent {
),
key_config,
git_branch_name: cached::BranchName::new(CWD),
commit_template: None,
}
}

///
pub fn update(&mut self) -> Result<()> {
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(())
}

Expand Down Expand Up @@ -291,13 +309,22 @@ impl CommitComponent {
}

fn can_commit(&self) -> bool {
!self.input.get_text().is_empty()
!self.is_empty() && self.is_changed()
}

fn can_amend(&self) -> bool {
self.amend.is_none()
&& 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<()> {
Expand Down

0 comments on commit fa60610

Please sign in to comment.