Skip to content

Commit

Permalink
feat: add keybinding to copy commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
wugeer committed Oct 19, 2024
1 parent 1866bf5 commit 7f1350e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
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

0 comments on commit 7f1350e

Please sign in to comment.