Skip to content

Add no-verify commit command #1375

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

Merged
merged 3 commits into from
Jan 13, 2023
Merged
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
55 changes: 43 additions & 12 deletions src/components/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub struct CommitComponent {
theme: SharedTheme,
commit_msg_history_idx: usize,
options: SharedOptions,
verify: bool,
}

const FIRST_LINE_LIMIT: usize = 50;
Expand Down Expand Up @@ -85,6 +86,7 @@ impl CommitComponent {
repo,
commit_msg_history_idx: 0,
options,
verify: true,
}
}

Expand Down Expand Up @@ -236,6 +238,11 @@ impl CommitComponent {
&mut self,
msg: String,
) -> Result<CommitResult> {
if !self.verify {
self.do_commit(&msg)?;
self.verify = true;
return Ok(CommitResult::ComitDone);
}
if let HookResult::NotOk(e) =
sync::hooks_pre_commit(&self.repo.borrow())?
{
Expand All @@ -258,18 +265,7 @@ impl CommitComponent {
return Ok(CommitResult::Aborted);
}

match &self.mode {
Mode::Normal => sync::commit(&self.repo.borrow(), &msg)?,
Mode::Amend(amend) => {
sync::amend(&self.repo.borrow(), *amend, &msg)?
}
Mode::Merge(ids) => {
sync::merge_commit(&self.repo.borrow(), &msg, ids)?
}
Mode::Revert => {
sync::commit_revert(&self.repo.borrow(), &msg)?
}
};
self.do_commit(&msg)?;

if let HookResult::NotOk(e) =
sync::hooks_post_commit(&self.repo.borrow())?
Expand All @@ -284,6 +280,22 @@ impl CommitComponent {
Ok(CommitResult::ComitDone)
}

fn do_commit(&self, msg: &str) -> Result<()> {
match &self.mode {
Mode::Normal => sync::commit(&self.repo.borrow(), msg)?,
Mode::Amend(amend) => {
sync::amend(&self.repo.borrow(), *amend, msg)?
}
Mode::Merge(ids) => {
sync::merge_commit(&self.repo.borrow(), msg, ids)?
}
Mode::Revert => {
sync::commit_revert(&self.repo.borrow(), msg)?
}
};
Ok(())
}

fn can_commit(&self) -> bool {
!self.is_empty() && self.is_changed()
}
Expand Down Expand Up @@ -320,6 +332,9 @@ impl CommitComponent {

Ok(())
}
fn toggle_verify(&mut self) {
self.verify = !self.verify
}
}

impl DrawableComponent for CommitComponent {
Expand Down Expand Up @@ -353,6 +368,15 @@ impl Component for CommitComponent {
true,
));

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

out.push(CommandInfo::new(
strings::commands::commit_amend(&self.key_config),
self.can_amend(),
Expand Down Expand Up @@ -394,6 +418,12 @@ impl Component for CommitComponent {
"commit error:",
self.commit()
);
} else if key_match(
e,
self.key_config.keys.toggle_verify,
) && self.can_commit()
{
self.toggle_verify();
} else if key_match(
e,
self.key_config.keys.commit_amend,
Expand Down Expand Up @@ -462,6 +492,7 @@ impl Component for CommitComponent {
.set_text(sync::merge_msg(&self.repo.borrow())?);
Mode::Revert
}

_ => {
self.commit_template = get_config_string(
&self.repo.borrow(),
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_tag_commit: GituiKeyEvent,
pub log_mark_commit: GituiKeyEvent,
pub commit_amend: GituiKeyEvent,
pub toggle_verify: GituiKeyEvent,
pub copy: GituiKeyEvent,
pub create_branch: GituiKeyEvent,
pub rename_branch: GituiKeyEvent,
Expand Down Expand Up @@ -170,6 +171,7 @@ impl Default for KeysList {
log_tag_commit: GituiKeyEvent::new(KeyCode::Char('t'), KeyModifiers::empty()),
log_mark_commit: GituiKeyEvent::new(KeyCode::Char(' '), KeyModifiers::empty()),
commit_amend: GituiKeyEvent::new(KeyCode::Char('a'), 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()),
rename_branch: GituiKeyEvent::new(KeyCode::Char('r'), KeyModifiers::empty()),
Expand Down
2 changes: 2 additions & 0 deletions src/keys/key_list_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub struct KeysListFile {
pub log_tag_commit: Option<GituiKeyEvent>,
pub log_mark_commit: Option<GituiKeyEvent>,
pub commit_amend: Option<GituiKeyEvent>,
pub toggle_verify: Option<GituiKeyEvent>,
pub copy: Option<GituiKeyEvent>,
pub create_branch: Option<GituiKeyEvent>,
pub rename_branch: Option<GituiKeyEvent>,
Expand Down Expand Up @@ -150,6 +151,7 @@ impl KeysListFile {
log_tag_commit: self.log_tag_commit.unwrap_or(default.log_tag_commit),
log_mark_commit: self.log_mark_commit.unwrap_or(default.log_mark_commit),
commit_amend: self.commit_amend.unwrap_or(default.commit_amend),
toggle_verify: self.toggle_verify.unwrap_or(default.toggle_verify),
copy: self.copy.unwrap_or(default.copy),
create_branch: self.create_branch.unwrap_or(default.create_branch),
rename_branch: self.rename_branch.unwrap_or(default.rename_branch),
Expand Down
16 changes: 16 additions & 0 deletions src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,22 @@ pub mod commands {
)
.hide_help()
}
pub fn toggle_verify(
key_config: &SharedKeyConfig,
current_verify: bool,
) -> CommandText {
let verb = if current_verify { "disable" } else { "enable" };
CommandText::new(
format!(
"{} hooks [{}]",
verb,
key_config.get_hint(key_config.keys.toggle_verify),
),
"toggle running on commit hooks (available in commit popup)",
CMD_GROUP_COMMIT_POPUP,
)
}

pub fn commit_amend(key_config: &SharedKeyConfig) -> CommandText {
CommandText::new(
format!(
Expand Down