Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add keybinding to copy commit message #2376

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* use default shell instead of bash on Unix-like OS [[@yerke](https://github.com/yerke)] ([#2343](https://github.com/extrawurst/gitui/pull/2343))

### Fixes
* add keybinding to copy commit message ([#2370](https://github.com/extrawurst/gitui/issues/2370))
* respect env vars like `GIT_CONFIG_GLOBAL` ([#2298](https://github.com/extrawurst/gitui/issues/2298))
* Set `CREATE_NO_WINDOW` flag when executing Git hooks on Windows ([#2371](https://github.com/extrawurst/gitui/pull/2371))

Expand Down
27 changes: 27 additions & 0 deletions src/components/commitlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,33 @@ impl CommitList {
Ok(())
}

///
pub fn selected_commit_message(&self) -> Option<String> {
let commit = self.selected_entry().map(|e| e.id);
let data = commit.and_then(|id| {
sync::get_commit_details(&self.repo.borrow(), id).ok()
});
data.as_ref().and_then(|data| data.message.as_ref()).map(
|message| {
message.body.as_ref().map_or_else(
|| message.subject.clone(),
|body| format!("{}\n{}", message.subject, body),
)
},
)
}

///
pub fn copy_commit_msg(&self) -> Result<()> {
if let Some(yank) = self.selected_commit_message() {
crate::clipboard::copy_string(&yank)?;
self.queue.push(InternalEvent::ShowInfoMsg(
strings::copy_success(&yank),
));
};
Ok(())
}

///
pub fn checkout(&self) {
if let Some(commit_hash) =
Expand Down
2 changes: 2 additions & 0 deletions src/keys/key_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ pub struct KeysList {
pub toggle_signoff: GituiKeyEvent,
pub toggle_verify: GituiKeyEvent,
pub copy: GituiKeyEvent,
pub copy_commit_msg: GituiKeyEvent,
pub create_branch: GituiKeyEvent,
pub rename_branch: GituiKeyEvent,
pub select_branch: GituiKeyEvent,
Expand Down Expand Up @@ -190,6 +191,7 @@ impl Default for KeysList {
toggle_signoff: GituiKeyEvent::new(KeyCode::Char('s'), KeyModifiers::CONTROL),
toggle_verify: GituiKeyEvent::new(KeyCode::Char('f'), KeyModifiers::CONTROL),
copy: GituiKeyEvent::new(KeyCode::Char('y'), KeyModifiers::empty()),
copy_commit_msg: GituiKeyEvent::new(KeyCode::Char('m'), KeyModifiers::empty()),
create_branch: GituiKeyEvent::new(KeyCode::Char('c'), KeyModifiers::empty()),
rename_branch: GituiKeyEvent::new(KeyCode::Char('r'), KeyModifiers::empty()),
select_branch: GituiKeyEvent::new(KeyCode::Char('b'), KeyModifiers::empty()),
Expand Down
14 changes: 14 additions & 0 deletions src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,20 @@ pub mod commands {
CMD_GROUP_LOG,
)
}

pub fn copy_commit_msg(
key_config: &SharedKeyConfig,
) -> CommandText {
CommandText::new(
format!(
"Copy Msg [{}]",
key_config.get_hint(key_config.keys.copy_commit_msg),
),
"copy selected commit msg to clipboard",
CMD_GROUP_LOG,
)
}

pub fn copy_path(key_config: &SharedKeyConfig) -> CommandText {
CommandText::new(
format!(
Expand Down
20 changes: 20 additions & 0 deletions src/tabs/revlog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,16 @@ impl Component for Revlog {
self.list.copy_commit_hash()
);
return Ok(EventState::Consumed);
} else if key_match(
k,
self.key_config.keys.copy_commit_msg,
) {
try_or_popup!(
self,
strings::POPUP_FAIL_COPY,
self.list.copy_commit_msg()
);
return Ok(EventState::Consumed);
} else if key_match(k, self.key_config.keys.push) {
self.queue.push(InternalEvent::PushTags);
return Ok(EventState::Consumed);
Expand Down Expand Up @@ -611,6 +621,8 @@ impl Component for Revlog {
Ok(EventState::NotConsumed)
}

// TODO: cleanup
#[allow(clippy::too_many_lines)]
fn commands(
&self,
out: &mut Vec<CommandInfo>,
Expand Down Expand Up @@ -677,6 +689,12 @@ impl Component for Revlog {
self.visible || force_all,
));

out.push(CommandInfo::new(
strings::commands::copy_commit_msg(&self.key_config),
self.selected_commit().is_some(),
self.visible || force_all,
));

out.push(CommandInfo::new(
strings::commands::log_tag_commit(&self.key_config),
self.selected_commit().is_some(),
Expand Down Expand Up @@ -718,11 +736,13 @@ impl Component for Revlog {
self.selected_commit().is_some(),
(self.visible && !self.is_search_pending()) || force_all,
));

out.push(CommandInfo::new(
strings::commands::log_reword_commit(&self.key_config),
self.selected_commit().is_some(),
(self.visible && !self.is_search_pending()) || force_all,
));

out.push(CommandInfo::new(
strings::commands::log_find_commit(&self.key_config),
self.can_start_search(),
Expand Down