From 495d4d5da76b64a378c2927efdc8b6494d9a0f3e Mon Sep 17 00:00:00 2001 From: extrawurst Date: Wed, 12 Jul 2023 13:45:07 +0200 Subject: [PATCH] do shell expansion for `commit.template` more error logging around commit-template loading --- CHANGELOG.md | 1 + Cargo.lock | 1 + Cargo.toml | 1 + src/components/commit.rs | 127 +++++++++++++++++++++++---------------- 4 files changed, 77 insertions(+), 53 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index baf743c5..94fa0903 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * fix commit dialog char count for multibyte characters ([#1726](https://github.com/extrawurst/gitui/issues/1726)) * fix wrong hit highlighting in fuzzy find popup [[@UUGTech](https://github.com/UUGTech)] ([#1731](https://github.com/extrawurst/gitui/pull/1731)) * fix symlink support for configuration files [[@TheBlackSheep3](https://github.com/TheBlackSheep3)] ([#1751](https://github.com/extrawurst/gitui/issues/1751)) +* expand `~` in `commit.template` ([#1745](https://github.com/extrawurst/gitui/pull/1745)) ## [0.23.0] - 2022-06-19 diff --git a/Cargo.lock b/Cargo.lock index 2a3baf5c..5a5f9469 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -755,6 +755,7 @@ dependencies = [ "scopeguard", "scopetime", "serde", + "shellexpand", "simplelog", "struct-patch", "syntect", diff --git a/Cargo.toml b/Cargo.toml index 7fbfac34..59fd82eb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,6 +47,7 @@ ron = "0.8" scopeguard = "1.2" scopetime = { path = "./scopetime", version = "0.1" } serde = "1.0" +shellexpand = "3.1" simplelog = { version = "0.12", default-features = false } struct-patch = "0.2" syntect = { version = "5.0", default-features = false, features = ["parsing", "default-syntaxes", "default-themes", "html"] } diff --git a/src/components/commit.rs b/src/components/commit.rs index 586dbb25..ba32394a 100644 --- a/src/components/commit.rs +++ b/src/components/commit.rs @@ -30,6 +30,8 @@ use ratatui::{ use std::{ fs::{read_to_string, File}, io::{Read, Write}, + path::PathBuf, + str::FromStr, }; enum CommitResult { @@ -364,61 +366,80 @@ impl CommitComponent { let repo_state = sync::repo_state(&self.repo.borrow())?; - self.mode = - if repo_state != RepoState::Clean && reword.is_some() { - bail!("cannot reword while repo is not in a clean state"); - } else if let Some(reword_id) = reword { - self.input.set_text( - sync::get_commit_details( + self.mode = if repo_state != RepoState::Clean + && reword.is_some() + { + bail!("cannot reword while repo is not in a clean state"); + } else if let Some(reword_id) = reword { + self.input.set_text( + sync::get_commit_details( + &self.repo.borrow(), + reword_id, + )? + .message + .unwrap_or_default() + .combine(), + ); + self.input.set_title(strings::commit_reword_title()); + Mode::Reword(reword_id) + } else { + match repo_state { + RepoState::Merge => { + let ids = + sync::mergehead_ids(&self.repo.borrow())?; + self.input + .set_title(strings::commit_title_merge()); + self.input.set_text(sync::merge_msg( &self.repo.borrow(), - reword_id, - )? - .message - .unwrap_or_default() - .combine(), - ); - self.input.set_title(strings::commit_reword_title()); - Mode::Reword(reword_id) - } else { - match repo_state { - RepoState::Merge => { - let ids = - sync::mergehead_ids(&self.repo.borrow())?; - self.input - .set_title(strings::commit_title_merge()); - self.input.set_text(sync::merge_msg( - &self.repo.borrow(), - )?); - Mode::Merge(ids) - } - RepoState::Revert => { - self.input - .set_title(strings::commit_title_revert()); - self.input.set_text(sync::merge_msg( - &self.repo.borrow(), - )?); - Mode::Revert - } - - _ => { - self.commit_template = get_config_string( - &self.repo.borrow(), - "commit.template", - ) - .ok() - .flatten() - .and_then(|path| read_to_string(path).ok()); - - if self.is_empty() { - if let Some(s) = &self.commit_template { - self.input.set_text(s.clone()); - } - } - self.input.set_title(strings::commit_title()); - Mode::Normal - } + )?); + Mode::Merge(ids) } - }; + RepoState::Revert => { + self.input + .set_title(strings::commit_title_revert()); + self.input.set_text(sync::merge_msg( + &self.repo.borrow(), + )?); + Mode::Revert + } + + _ => { + self.commit_template = get_config_string( + &self.repo.borrow(), + "commit.template", + ) + .map_err(|e| { + log::error!("load git-config failed: {}", e); + e + }) + .ok() + .flatten() + .and_then(|path| { + shellexpand::full(path.as_str()) + .ok() + .and_then(|path| { + PathBuf::from_str(path.as_ref()).ok() + }) + }) + .and_then(|path| { + read_to_string(&path) + .map_err(|e| { + log::error!("read commit.template failed: {e} (path: '{:?}')",path); + e + }) + .ok() + }); + + if self.is_empty() { + if let Some(s) = &self.commit_template { + self.input.set_text(s.clone()); + } + } + self.input.set_title(strings::commit_title()); + Mode::Normal + } + } + }; self.commit_msg_history_idx = 0; self.input.show()?;