Skip to content

Commit 495d4d5

Browse files
committed
do shell expansion for commit.template
more error logging around commit-template loading
1 parent 403c5aa commit 495d4d5

File tree

4 files changed

+74
-50
lines changed

4 files changed

+74
-50
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2525
* fix commit dialog char count for multibyte characters ([#1726](https://github.com/extrawurst/gitui/issues/1726))
2626
* fix wrong hit highlighting in fuzzy find popup [[@UUGTech](https://github.com/UUGTech)] ([#1731](https://github.com/extrawurst/gitui/pull/1731))
2727
* fix symlink support for configuration files [[@TheBlackSheep3](https://github.com/TheBlackSheep3)] ([#1751](https://github.com/extrawurst/gitui/issues/1751))
28+
* expand `~` in `commit.template` ([#1745](https://github.com/extrawurst/gitui/pull/1745))
2829

2930
## [0.23.0] - 2022-06-19
3031

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ ron = "0.8"
4747
scopeguard = "1.2"
4848
scopetime = { path = "./scopetime", version = "0.1" }
4949
serde = "1.0"
50+
shellexpand = "3.1"
5051
simplelog = { version = "0.12", default-features = false }
5152
struct-patch = "0.2"
5253
syntect = { version = "5.0", default-features = false, features = ["parsing", "default-syntaxes", "default-themes", "html"] }

src/components/commit.rs

Lines changed: 71 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ use ratatui::{
3030
use std::{
3131
fs::{read_to_string, File},
3232
io::{Read, Write},
33+
path::PathBuf,
34+
str::FromStr,
3335
};
3436

3537
enum CommitResult {
@@ -364,61 +366,80 @@ impl CommitComponent {
364366

365367
let repo_state = sync::repo_state(&self.repo.borrow())?;
366368

367-
self.mode =
368-
if repo_state != RepoState::Clean && reword.is_some() {
369-
bail!("cannot reword while repo is not in a clean state");
370-
} else if let Some(reword_id) = reword {
371-
self.input.set_text(
372-
sync::get_commit_details(
369+
self.mode = if repo_state != RepoState::Clean
370+
&& reword.is_some()
371+
{
372+
bail!("cannot reword while repo is not in a clean state");
373+
} else if let Some(reword_id) = reword {
374+
self.input.set_text(
375+
sync::get_commit_details(
376+
&self.repo.borrow(),
377+
reword_id,
378+
)?
379+
.message
380+
.unwrap_or_default()
381+
.combine(),
382+
);
383+
self.input.set_title(strings::commit_reword_title());
384+
Mode::Reword(reword_id)
385+
} else {
386+
match repo_state {
387+
RepoState::Merge => {
388+
let ids =
389+
sync::mergehead_ids(&self.repo.borrow())?;
390+
self.input
391+
.set_title(strings::commit_title_merge());
392+
self.input.set_text(sync::merge_msg(
373393
&self.repo.borrow(),
374-
reword_id,
375-
)?
376-
.message
377-
.unwrap_or_default()
378-
.combine(),
379-
);
380-
self.input.set_title(strings::commit_reword_title());
381-
Mode::Reword(reword_id)
382-
} else {
383-
match repo_state {
384-
RepoState::Merge => {
385-
let ids =
386-
sync::mergehead_ids(&self.repo.borrow())?;
387-
self.input
388-
.set_title(strings::commit_title_merge());
389-
self.input.set_text(sync::merge_msg(
390-
&self.repo.borrow(),
391-
)?);
392-
Mode::Merge(ids)
393-
}
394-
RepoState::Revert => {
395-
self.input
396-
.set_title(strings::commit_title_revert());
397-
self.input.set_text(sync::merge_msg(
398-
&self.repo.borrow(),
399-
)?);
400-
Mode::Revert
401-
}
394+
)?);
395+
Mode::Merge(ids)
396+
}
397+
RepoState::Revert => {
398+
self.input
399+
.set_title(strings::commit_title_revert());
400+
self.input.set_text(sync::merge_msg(
401+
&self.repo.borrow(),
402+
)?);
403+
Mode::Revert
404+
}
402405

403-
_ => {
404-
self.commit_template = get_config_string(
405-
&self.repo.borrow(),
406-
"commit.template",
407-
)
408-
.ok()
409-
.flatten()
410-
.and_then(|path| read_to_string(path).ok());
411-
412-
if self.is_empty() {
413-
if let Some(s) = &self.commit_template {
414-
self.input.set_text(s.clone());
415-
}
406+
_ => {
407+
self.commit_template = get_config_string(
408+
&self.repo.borrow(),
409+
"commit.template",
410+
)
411+
.map_err(|e| {
412+
log::error!("load git-config failed: {}", e);
413+
e
414+
})
415+
.ok()
416+
.flatten()
417+
.and_then(|path| {
418+
shellexpand::full(path.as_str())
419+
.ok()
420+
.and_then(|path| {
421+
PathBuf::from_str(path.as_ref()).ok()
422+
})
423+
})
424+
.and_then(|path| {
425+
read_to_string(&path)
426+
.map_err(|e| {
427+
log::error!("read commit.template failed: {e} (path: '{:?}')",path);
428+
e
429+
})
430+
.ok()
431+
});
432+
433+
if self.is_empty() {
434+
if let Some(s) = &self.commit_template {
435+
self.input.set_text(s.clone());
416436
}
417-
self.input.set_title(strings::commit_title());
418-
Mode::Normal
419437
}
438+
self.input.set_title(strings::commit_title());
439+
Mode::Normal
420440
}
421-
};
441+
}
442+
};
422443

423444
self.commit_msg_history_idx = 0;
424445
self.input.show()?;

0 commit comments

Comments
 (0)