From 9d948c53171ba04cf170285c4dd014d6b2511a59 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 | 121 +++++++++++++++++++++++---------------- 4 files changed, 74 insertions(+), 50 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f94682f6fb..f181d1da72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,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 4a42ff9307..37133b063b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -756,6 +756,7 @@ dependencies = [ "scopeguard", "scopetime", "serde", + "shellexpand", "simplelog", "struct-patch", "syntect", diff --git a/Cargo.toml b/Cargo.toml index 4a7299a7fa..2578b5821e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -46,6 +46,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 586dbb2569..ba32394a74 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 - } + )?); + 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.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.input.set_title(strings::commit_title()); + Mode::Normal } - }; + } + }; self.commit_msg_history_idx = 0; self.input.show()?;