Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
config: minor
knope: minor
---

# Add `ignore_conventional_merge_commits` setting to `[changes]` config section

You can now ignore merge commits for conventional commit purposes.
6 changes: 5 additions & 1 deletion crates/knope-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ pub use self::{
pub use crate::release_notes::ReleaseNotes;

/// Configuration for how changes are tracked and processed
#[derive(Debug, Default, Deserialize, Serialize)]
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct Changes {
/// If set to true, conventional commits are ignored across all workflows
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub ignore_conventional_commits: bool,
/// If set to true, merge commits do not count as conventional commits,
/// even if they would normally be parsed as such.
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub ignore_conventional_merge_commits: bool,
}

#[derive(Debug, Default, Deserialize)]
Expand Down
20 changes: 10 additions & 10 deletions crates/knope/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ use crate::{
mod package;
mod toml;

pub(crate) use knope_config::Changes;
pub(crate) use toml::{GitHub, Gitea, Jira};

use crate::fs::WriteType;

/// A valid config, loaded from a supported file (or detected via default)
#[derive(Debug)]
pub(crate) struct Config {
pub(crate) changes: Changes,
pub(crate) release_notes: ReleaseNotes,
pub(crate) packages: Vec<Package>,
/// The list of defined workflows that are selectable
Expand All @@ -38,8 +40,6 @@ pub(crate) struct Config {
pub(crate) github: Option<GitHub>,
/// Optional configuration to communicate with a Gitea instance
pub(crate) gitea: Option<Gitea>,
/// If set to true, conventional commits are ignored across all workflows
pub(crate) ignore_conventional_commits: bool,
}

impl Config {
Expand Down Expand Up @@ -77,7 +77,7 @@ impl Config {
if let Step::PrepareRelease(prepare_release) = step {
if prepare_release.ignore_conventional_commits {
// Move to top-level config
self.ignore_conventional_commits = true;
self.changes.ignore_conventional_commits = true;
prepare_release.ignore_conventional_commits = false;
upgraded = true;
}
Expand Down Expand Up @@ -118,9 +118,12 @@ impl Config {
)
};

let changes = if self.ignore_conventional_commits {
let changes = if self.changes.ignore_conventional_commits
|| self.changes.ignore_conventional_merge_commits
{
Some(knope_config::Changes {
ignore_conventional_commits: true,
ignore_conventional_commits: self.changes.ignore_conventional_commits,
ignore_conventional_merge_commits: self.changes.ignore_conventional_merge_commits,
})
} else {
None
Expand Down Expand Up @@ -199,10 +202,7 @@ impl TryFrom<(ConfigLoader, String)> for Config {
jira: config.jira.map(Spanned::into_inner),
github: config.github.map(Spanned::into_inner),
gitea: config.gitea.map(Spanned::into_inner),
ignore_conventional_commits: config
.shared
.changes
.is_some_and(|c| c.ignore_conventional_commits),
changes: config.shared.changes.unwrap_or_default(),
})
}
}
Expand Down Expand Up @@ -332,7 +332,7 @@ pub(crate) fn generate() -> Result<Config, package::Error> {
github,
gitea,
packages,
ignore_conventional_commits: false,
changes: Changes::default(),
})
}

Expand Down
8 changes: 7 additions & 1 deletion crates/knope/src/integrations/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,10 @@ pub(crate) fn add_files(file_names: &[RelativePathBuf]) -> Result<(), Error> {
/// means that there could be paths which jump _behind_ the target tag... and we want to exclude
/// those as well. There's probably a way to optimize performance with some cool graph magic
/// eventually, but this is good enough for now.
pub(crate) fn get_commit_messages_after_tag(tag: Option<&str>) -> Result<Vec<Commit>, Error> {
pub(crate) fn get_commit_messages_after_tag(
tag: Option<&str>,
ignore_merge_commits: bool,
) -> Result<Vec<Commit>, Error> {
let repo = Repository::open(".")?;

let tag_ref = if let Some(tag) = tag {
Expand Down Expand Up @@ -406,6 +409,9 @@ pub(crate) fn get_commit_messages_after_tag(tag: Option<&str>) -> Result<Vec<Com
for oid in revwalk.filter_map(Result::ok) {
if !commits_to_exclude.contains(&oid) {
if let Ok(commit) = repo.find_commit(oid) {
if ignore_merge_commits && commit.parent_count() > 1 {
continue;
}
if let Some(message) = commit.message().map(String::from) {
commits.push(Commit {
message,
Expand Down
4 changes: 2 additions & 2 deletions crates/knope/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ fn create_state(
jira,
github,
gitea,
ignore_conventional_commits,
changes,
} = config;
let git_tags = if packages.is_empty() {
// Don't mess with Git if there aren't any packages defined
Expand Down Expand Up @@ -275,7 +275,7 @@ fn create_state(
packages,
versioned_files,
git_tags,
ignore_conventional_commits,
Some(changes),
);
Ok((state, workflows))
}
Expand Down
7 changes: 3 additions & 4 deletions crates/knope/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ pub(crate) struct State {
pub(crate) all_versioned_files: Vec<VersionedFile>,
pub(crate) pending_actions: Vec<Action>,
pub(crate) all_git_tags: Vec<String>,
/// If set to true, conventional commits are ignored across all workflows
pub(crate) ignore_conventional_commits: bool,
pub(crate) changes_config: config::Changes,
}

impl State {
Expand All @@ -36,7 +35,7 @@ impl State {
packages: Vec<releases::Package>,
all_versioned_files: Vec<VersionedFile>,
all_git_tags: Vec<String>,
ignore_conventional_commits: bool,
changes_config: Option<config::Changes>,
) -> Self {
State {
jira_config,
Expand All @@ -49,7 +48,7 @@ impl State {
all_versioned_files,
all_git_tags,
pending_actions: Vec::new(),
ignore_conventional_commits,
changes_config: changes_config.unwrap_or_default(),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/knope/src/step/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ mod test_run_command {
Vec::new(),
Vec::new(),
Vec::new(),
false,
None,
)),
command.to_string(),
false,
Expand All @@ -83,7 +83,7 @@ mod test_run_command {
Vec::new(),
Vec::new(),
Vec::new(),
false,
None,
)),
String::from("exit 1"),
false,
Expand Down
3 changes: 2 additions & 1 deletion crates/knope/src/step/releases/conventional_commits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::integrations::git::{self, get_commit_messages_after_tag};
pub(crate) fn get_conventional_commits_after_last_stable_version(
package_name: &package::Name,
all_tags: &[String],
ignore_merge_commits: bool,
) -> Result<Vec<Commit>, git::Error> {
debug!(
"Getting conventional commits since last release of package {}",
Expand All @@ -17,5 +18,5 @@ pub(crate) fn get_conventional_commits_after_last_stable_version(
.stable()
.map(|target_version| ReleaseTag::new(&target_version.into(), package_name));

get_commit_messages_after_tag(tag.as_ref().map(ReleaseTag::as_str))
get_commit_messages_after_tag(tag.as_ref().map(ReleaseTag::as_str), ignore_merge_commits)
}
2 changes: 1 addition & 1 deletion crates/knope/src/step/releases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub(crate) async fn prepare_release(
prepare_release,
&state.all_git_tags,
&changeset,
state.ignore_conventional_commits,
&state.changes_config,
)?;

if !changes.is_empty()
Expand Down
6 changes: 4 additions & 2 deletions crates/knope/src/step/releases/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl Package {
prepare_release: &PrepareRelease,
all_tags: &[String],
changeset: &[changesets::Release],
global_ignore_conventional_commits: bool,
changes_config: &config::Changes,
) -> Result<Vec<Change>, Error> {
let ignore_conventional_commits = prepare_release.ignore_conventional_commits;

Expand All @@ -114,14 +114,16 @@ impl Package {
}

// Use step-level setting if present, otherwise use global setting
let should_ignore = ignore_conventional_commits || global_ignore_conventional_commits;
let should_ignore =
ignore_conventional_commits || changes_config.ignore_conventional_commits;

let commit_messages = if should_ignore {
Vec::new()
} else {
conventional_commits::get_conventional_commits_after_last_stable_version(
&self.versioning.name,
all_tags,
changes_config.ignore_conventional_merge_commits,
)?
};

Expand Down
6 changes: 3 additions & 3 deletions crates/knope/src/variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ mod test_replace_variables {
use relative_path::RelativePathBuf;

use super::*;
use crate::step::issues::Issue;
use crate::{config, step::issues::Issue};

fn state() -> State {
let changelog = Changelog::new(RelativePathBuf::default(), String::new());
Expand Down Expand Up @@ -166,7 +166,7 @@ mod test_replace_variables {
vec![package],
all_versioned_files,
Vec::new(),
false,
None,
)
}

Expand Down Expand Up @@ -209,7 +209,7 @@ mod test_replace_variables {
all_git_tags: Vec::new(),
all_versioned_files: Vec::new(),
pending_actions: Vec::new(),
ignore_conventional_commits: false,
changes_config: config::Changes::default(),
};

let result =
Expand Down
11 changes: 10 additions & 1 deletion crates/knope/tests/helpers/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,22 @@ pub fn switch_branch(path: &Path, name: &str) {
debug!("{}", String::from_utf8_lossy(&output.stdout));
}

/// Merge a branch into the current branch
/// Merge a branch into the current branch, with the default message
pub fn merge_branch(path: &Path, name: &str) {
merge_branch_with_message(path, name, None);
}

/// Merge a branch into the current branch with the given message, or the default if None
pub fn merge_branch_with_message(path: &Path, name: &str, message: Option<&str>) {
let output = Command::new("git")
.arg("merge")
.arg("--no-ff")
.arg("--no-edit")
.arg(name)
.args(match message {
None => vec![],
Some(m) => vec!["-m", m],
})
.current_dir(path)
.output()
.unwrap();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod merge_commits;
mod pick_correct_commits;
mod pick_correct_commits_merges_ignored;
mod pick_correct_tag;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Would add the following to Cargo.toml: version = 2.0.1
Would add the following to CHANGELOG.md:
## 2.0.1 ([DATE])

### Fixes

- Another bug

Would add files to git:
Cargo.toml
CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Changelog

## 2.0.0

### Breaking Changes

#### A breaking feature

## 1.0.1

### Bug Fixes

#### A bug
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[package]
name = "default"
version = "2.0.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[changes]
ignore_conventional_merge_commits = true

[package]
versioned_files = ["Cargo.toml"]
changelog = "CHANGELOG.md"

[[workflows]]
name = "prepare-release"

[[workflows.steps]]
type = "PrepareRelease"
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/// Specifically designed to catch <https://github.com/knope-dev/knope/issues/1927/>
use crate::helpers::{
TestCase, commit, create_branch, merge_branch, merge_branch_with_message, switch_branch, tag,
};

#[test]
fn pick_correct_commits_from_branching_history_merges_ignored() {
let test = TestCase::new(file!());
let temp_dir = test.arrange();
let temp_path = temp_dir.path();

commit(temp_path, "Initial commit", "Knope <knope@knope.tech>");
tag(temp_path, "v1.0.0");
create_branch(temp_path, "patch");
commit(temp_path, "fix: A bug", "Knope <knope@knope.tech>");
switch_branch(temp_path, "main");
merge_branch(temp_path, "patch");
tag(temp_path, "v1.0.1");
create_branch(temp_path, "breaking");
commit(
temp_path,
"feat!: A breaking feature",
"Knope <knope@knope.tech>",
);
switch_branch(temp_path, "main");
merge_branch(temp_path, "breaking");
tag(temp_path, "v2.0.0");
switch_branch(temp_path, "breaking");
merge_branch(temp_path, "main");
commit(temp_path, "fix: Another bug", "Knope <knope@knope.tech>");
switch_branch(temp_path, "main");
merge_branch_with_message(temp_path, "breaking", Some("fix: Another bug"));

test.assert(test.act(temp_dir, "prepare-release"));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Changelog

## 2.0.1 ([DATE])

### Fixes

- Another bug

## 2.0.0

### Breaking Changes

#### A breaking feature

## 1.0.1

### Bug Fixes

#### A bug
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[package]
name = "default"
version = "2.0.1"
Loading