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

Autosquash Improvements #394

Closed
wants to merge 2 commits into from
Closed
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
29 changes: 19 additions & 10 deletions crates/committed/src/checks.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::report;
use committed::Style;

const AUTOSQUASH_PREFIXES: [&str; 3] = ["fixup! ", "squash! ", "amend! "];

pub(crate) fn check_message(
source: report::Source<'_>,
message: &str,
mut message: &str,
config: &crate::config::Config,
report: report::Report,
) -> Result<bool, anyhow::Error> {
Expand All @@ -17,8 +19,14 @@ pub(crate) fn check_message(
if config.no_wip() {
failed |= check_wip(source, message, report)?;
}
if config.no_fixup() {
failed |= check_fixup(source, message, report)?;
// If we are checking to autosquash, we update the failed flag. Otherwise,
// we update the message to skip the autosquash prefix for the remaining
// checks.
let (autosquash_result, idx) = check_autosquash(source, message, report)?;
if config.no_autosquash() {
failed |= autosquash_result;
} else if let Some(idx) = idx {
message = &message[idx..];
}
// Bail out due to above checks
if failed {
Expand Down Expand Up @@ -313,17 +321,18 @@ pub(crate) fn check_wip(
}
}

pub(crate) fn check_fixup(
pub(crate) fn check_autosquash(
source: report::Source<'_>,
message: &str,
report: report::Report,
) -> Result<bool, anyhow::Error> {
if message.starts_with("fixup! ") {
report(report::Message::error(source, report::Fixup {}));
Ok(true)
} else {
Ok(false)
) -> Result<(bool, Option<usize>), anyhow::Error> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we instead return the new message? We could use strip_prefix rather than starts_with.

for prefix in AUTOSQUASH_PREFIXES.iter() {
if message.starts_with(prefix) {
report(report::Message::error(source, report::Autosquash {}));
return Ok((true, Some(prefix.len())));
}
}
Ok((false, None))
}

pub(crate) fn check_merge_commit(
Expand Down
13 changes: 7 additions & 6 deletions crates/committed/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ pub(crate) struct Config {
pub(crate) subject_capitalized: Option<bool>,
pub(crate) subject_not_punctuated: Option<bool>,
pub(crate) imperative_subject: Option<bool>,
pub(crate) no_fixup: Option<bool>,
#[serde(alias = "no_fixup")]
pub(crate) no_autosquash: Option<bool>,
pub(crate) no_wip: Option<bool>,
pub(crate) hard_line_length: Option<usize>,
pub(crate) line_length: Option<usize>,
Expand All @@ -40,7 +41,7 @@ impl Config {
subject_capitalized: Some(empty.subject_capitalized()),
subject_not_punctuated: Some(empty.subject_not_punctuated()),
imperative_subject: Some(empty.imperative_subject()),
no_fixup: Some(empty.no_fixup()),
no_autosquash: Some(empty.no_autosquash()),
no_wip: Some(empty.no_wip()),
hard_line_length: Some(empty.hard_line_length()),
line_length: Some(empty.line_length()),
Expand All @@ -66,8 +67,8 @@ impl Config {
if let Some(source) = source.imperative_subject {
self.imperative_subject = Some(source);
}
if let Some(source) = source.no_fixup {
self.no_fixup = Some(source);
if let Some(source) = source.no_autosquash {
self.no_autosquash = Some(source);
}
if let Some(source) = source.no_wip {
self.no_wip = Some(source);
Expand Down Expand Up @@ -109,8 +110,8 @@ impl Config {
self.imperative_subject.unwrap_or(true)
}

pub(crate) fn no_fixup(&self) -> bool {
self.no_fixup.unwrap_or(true)
pub(crate) fn no_autosquash(&self) -> bool {
self.no_autosquash.unwrap_or(true)
}

pub(crate) fn no_wip(&self) -> bool {
Expand Down
14 changes: 7 additions & 7 deletions crates/committed/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ struct Options {
#[arg(long, overrides_with("no_wip"), hide(true))]
wip: bool,

#[arg(long, overrides_with("fixup"))]
no_fixup: bool,
#[arg(long, overrides_with("no_fixup"), hide(true))]
fixup: bool,
#[arg(long, overrides_with("autosquash"), alias("no_fixup"))]
no_autosquash: bool,
#[arg(long, overrides_with("no_autosquash"), alias("fixup"), hide(true))]
autosquash: bool,

#[arg(
long = "format",
Expand All @@ -70,7 +70,7 @@ impl Options {
config::Config {
merge_commit: self.merge_commit(),
no_wip: self.wip().map(|b| !b),
no_fixup: self.fixup().map(|b| !b),
no_autosquash: self.autosquash().map(|b| !b),
..Default::default()
}
}
Expand All @@ -83,8 +83,8 @@ impl Options {
resolve_bool_arg(self.wip, self.no_wip)
}

fn fixup(&self) -> Option<bool> {
resolve_bool_arg(self.fixup, self.no_fixup)
fn autosquash(&self) -> Option<bool> {
resolve_bool_arg(self.autosquash, self.no_autosquash)
}
}

Expand Down
7 changes: 4 additions & 3 deletions crates/committed/src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ pub(crate) enum Content<'s> {
NoPunctuation(NoPunctuation),
Imperative(Imperative<'s>),
Wip(Wip),
Fixup(Fixup),
#[serde(alias = "fixup")]
Autosquash(Autosquash),
Comment on lines +66 to +67
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a breaking change. Our json output would be switching from emitting type = "fixup" messages to type = "autosquash".

InvalidCommitFormat(InvalidCommitFormat),
DisallowedCommitType(DisallowedCommitType),
MergeCommitDisallowed(MergeCommitDisallowed),
Expand Down Expand Up @@ -128,8 +129,8 @@ pub(crate) struct Wip {}
#[derive(Clone, Debug, serde::Serialize)]
#[serde(rename_all = "snake_case")]
#[derive(derive_more::Display)]
#[display("Fixup commits must be squashed")]
pub(crate) struct Fixup {}
#[display("Autosquash commits must be squashed")]
pub(crate) struct Autosquash {}

#[derive(Debug, serde::Serialize)]
#[serde(rename_all = "snake_case")]
Expand Down