Skip to content
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
* support 'n'/'p' key to move to the next/prev hunk in diff component [[@hamflx](https://github.com/hamflx)] ([#1523](https://github.com/extrawurst/gitui/issues/1523))
* simplify theme overrides [[@cruessler](https://github.com/cruessler)] ([#1367](https://github.com/extrawurst/gitui/issues/1367))
* support for sign-off of commits [#1757](https://github.com/extrawurst/gitui/issues/1757)

### Fixes
* fix commit dialog char count for multibyte characters ([#1726](https://github.com/extrawurst/gitui/issues/1726))
Expand Down
52 changes: 51 additions & 1 deletion src/components/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use crate::{
keys::{key_match, SharedKeyConfig},
options::SharedOptions,
queue::{InternalEvent, NeedsUpdate, Queue},
strings, try_or_popup,
strings::{self, commit_title_signoff},
try_or_popup,
ui::style::SharedTheme,
};
use anyhow::{bail, Ok, Result};
Expand Down Expand Up @@ -57,6 +58,7 @@ pub struct CommitComponent {
commit_msg_history_idx: usize,
options: SharedOptions,
verify: bool,
sign_off: bool,
}

const FIRST_LINE_LIMIT: usize = 50;
Expand Down Expand Up @@ -88,6 +90,7 @@ impl CommitComponent {
commit_msg_history_idx: 0,
options,
verify: true,
sign_off: false,
}
}

Expand Down Expand Up @@ -255,7 +258,13 @@ impl CommitComponent {
return Ok(CommitResult::Aborted);
}
}

let mut msg = message_prettify(msg, Some(b'#'))?;

if self.sign_off {
msg = self.add_sign_off(&msg)?;
}

if verify {
// run commit message check hook - can reject commit
if let HookResult::NotOk(e) =
Expand Down Expand Up @@ -341,6 +350,14 @@ impl CommitComponent {

Ok(())
}
fn toggle_signoff(&mut self) {
self.sign_off = !self.sign_off;
if self.sign_off {
self.input.set_title(commit_title_signoff());
} else {
self.input.set_title(strings::commit_title());
}
}
fn toggle_verify(&mut self) {
self.verify = !self.verify;
}
Expand Down Expand Up @@ -416,6 +433,28 @@ impl CommitComponent {

Ok(())
}

fn add_sign_off(&self, msg: &str) -> Result<String> {
const CONFIG_KEY_USER_NAME: &str = "user.name";
const CONFIG_KEY_USER_MAIL: &str = "user.email";

let user = get_config_string(
&self.repo.borrow(),
CONFIG_KEY_USER_NAME,
)?;

let mail = get_config_string(
&self.repo.borrow(),
CONFIG_KEY_USER_MAIL,
)?;

let mut msg = msg.to_owned();
if let (Some(user), Some(mail)) = (user, mail) {
msg.push_str(&format!("\nSigned-off-by {user} <{mail}>"));
}

Ok(msg)
}
}

impl DrawableComponent for CommitComponent {
Expand Down Expand Up @@ -464,6 +503,12 @@ impl Component for CommitComponent {
true,
));

out.push(CommandInfo::new(
strings::commands::commit_signoff(&self.key_config),
true,
true,
));

out.push(CommandInfo::new(
strings::commands::commit_open_editor(
&self.key_config,
Expand Down Expand Up @@ -531,6 +576,11 @@ impl Component for CommitComponent {
self.input.set_text(msg);
self.commit_msg_history_idx += 1;
}
} else if key_match(
e,
self.key_config.keys.toggle_signoff,
) {
self.toggle_signoff();
}
// stop key event propagation
return Ok(EventState::Consumed);
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 @@ -88,6 +88,7 @@ pub struct KeysList {
pub log_reset_comit: GituiKeyEvent,
pub log_reword_comit: GituiKeyEvent,
pub commit_amend: GituiKeyEvent,
pub toggle_signoff: GituiKeyEvent,
pub toggle_verify: GituiKeyEvent,
pub copy: GituiKeyEvent,
pub create_branch: GituiKeyEvent,
Expand Down Expand Up @@ -174,6 +175,7 @@ impl Default for KeysList {
log_reset_comit: GituiKeyEvent { code: KeyCode::Char('R'), modifiers: KeyModifiers::SHIFT },
log_reword_comit: GituiKeyEvent { code: KeyCode::Char('r'), modifiers: KeyModifiers::empty() },
commit_amend: GituiKeyEvent::new(KeyCode::Char('a'), KeyModifiers::CONTROL),
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()),
create_branch: GituiKeyEvent::new(KeyCode::Char('c'), KeyModifiers::empty()),
Expand Down
15 changes: 15 additions & 0 deletions src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ pub fn commit_title_revert() -> String {
pub fn commit_title_amend() -> String {
"Commit (Amend)".to_string()
}
pub fn commit_title_signoff() -> String {
"Commit (Sign-off)".to_string()
}
pub fn commit_msg(_key_config: &SharedKeyConfig) -> String {
"type commit message..".to_string()
}
Expand Down Expand Up @@ -980,6 +983,18 @@ pub mod commands {
CMD_GROUP_COMMIT_POPUP,
)
}
pub fn commit_signoff(
key_config: &SharedKeyConfig,
) -> CommandText {
CommandText::new(
format!(
"Sing-off [{}]",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matthiasbeyer this PR is already merged, could you open a PR fixing the typos?

key_config.get_hint(key_config.keys.toggle_signoff),
),
"sign-off commit (-s option)",
CMD_GROUP_COMMIT_POPUP,
)
}
pub fn edit_item(key_config: &SharedKeyConfig) -> CommandText {
CommandText::new(
format!(
Expand Down