From 9b46bb63f949aae4ed0b5be5fa9f3b0a92d09266 Mon Sep 17 00:00:00 2001 From: bc-universe <9028220+bc-universe@users.noreply.github.com> Date: Sun, 13 Nov 2022 11:18:59 +0100 Subject: [PATCH] Display commit changes (#1420) When using an external editor to edit the commit message, the changes are now displayed --- CHANGELOG.md | 3 +++ src/app.rs | 12 +++++++----- src/components/commit.rs | 27 ++++++++++++++++++++++++++- src/tabs/status.rs | 6 +++++- 4 files changed, 41 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a6e93136..8282fe4e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +### Added +* changes in commit message inside external editor [[@bc-universe]](https://github.com/bc-universe) ([#1420](https://github.com/extrawurst/gitui/issues/1420)) + ### Fixes * commit msg history ordered the wrong way ([#1445](https://github.com/extrawurst/gitui/issues/1445)) * improve help documentation for amend cmd ([#1448](https://github.com/extrawurst/gitui/issues/1448)) diff --git a/src/app.rs b/src/app.rs index 8fc8a432..1d634fec 100644 --- a/src/app.rs +++ b/src/app.rs @@ -446,15 +446,17 @@ impl App { } else if let InputEvent::State(polling_state) = ev { self.external_editor_popup.hide(); if matches!(polling_state, InputState::Paused) { - let result = match self.file_to_open.take() { - Some(path) => { + let result = + if let Some(path) = self.file_to_open.take() { ExternalEditorComponent::open_file_in_editor( &self.repo.borrow(), Path::new(&path), ) - } - None => self.commit.show_editor(), - }; + } else { + let changes = + self.status_tab.get_files_changes()?; + self.commit.show_editor(changes) + }; if let Err(e) = result { let msg = diff --git a/src/components/commit.rs b/src/components/commit.rs index cadaab43..929d0239 100644 --- a/src/components/commit.rs +++ b/src/components/commit.rs @@ -17,6 +17,7 @@ use asyncgit::{ self, get_config_string, CommitId, HookResult, RepoPathRef, RepoState, }, + StatusItem, StatusItemType, }; use crossterm::event::Event; use easy_cast::Cast; @@ -139,7 +140,23 @@ impl CommitComponent { } } - pub fn show_editor(&mut self) -> Result<()> { + const fn item_status_char( + item_type: StatusItemType, + ) -> &'static str { + match item_type { + StatusItemType::Modified => "modified", + StatusItemType::New => "new file", + StatusItemType::Deleted => "deleted", + StatusItemType::Renamed => "renamed", + StatusItemType::Typechange => " ", + StatusItemType::Conflicted => "conflicted", + } + } + + pub fn show_editor( + &mut self, + changes: Vec, + ) -> Result<()> { let file_path = sync::repo_dir(&self.repo.borrow())? .join("COMMIT_EDITMSG"); @@ -153,6 +170,14 @@ impl CommitComponent { strings::commit_editor_msg(&self.key_config) .as_bytes(), )?; + + for change in changes { + let status_char = + Self::item_status_char(change.status); + let message = + format!("\n#\t{status_char}: {}", change.path); + file.write_all(message.as_bytes())?; + } } ExternalEditorComponent::open_file_in_editor( diff --git a/src/tabs/status.rs b/src/tabs/status.rs index e17ce384..765ae1c4 100644 --- a/src/tabs/status.rs +++ b/src/tabs/status.rs @@ -21,7 +21,7 @@ use asyncgit::{ }, sync::{BranchCompare, CommitId}, AsyncBranchesJob, AsyncDiff, AsyncGitNotification, AsyncStatus, - DiffParams, DiffType, PushType, StatusParams, + DiffParams, DiffType, PushType, StatusItem, StatusParams, }; use crossbeam_channel::Sender; use crossterm::event::Event; @@ -465,6 +465,10 @@ impl Status { Ok(()) } + pub fn get_files_changes(&mut self) -> Result> { + Ok(self.git_status_stage.last()?.items) + } + fn update_status(&mut self) -> Result<()> { let stage_status = self.git_status_stage.last()?; self.index.set_items(&stage_status.items)?;