Skip to content

Commit a46fe2b

Browse files
authored
feat!: PrepareRelease now git adds all modified files. (#267)
Closes #215 Co-authored-by: Dylan Anthony <[email protected]>
1 parent a865726 commit a46fe2b

30 files changed

+158
-122
lines changed

Diff for: Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: docs/src/config/step/PrepareRelease.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# PrepareRelease step
22

3-
This will look through all commits since the version tag and parse any [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) it finds. It will then bump the package version (depending on the [Semantic Versioning] rule determined from the commits) and add a new changelog entry using the [Keep A Changelog](https://keepachangelog.com/en/1.0.0/) format.
3+
This will look through all commits since the version tag and parse any [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) it finds. It will then bump the package version (depending on the [Semantic Versioning] rule determined from the commits) and add a new changelog entry using the [Keep A Changelog](https://keepachangelog.com/en/1.0.0/) format. Any files altered (`versioned_files` and `changelog`) will be staged for commit with `git add` **but not committed**.
44

55
The version bumping follows the same rules and logic as the [BumpVersion] step, with the rule selected for you automatically. Which files are edited (both for versioning and changelog) is determined by the [packages] section.
66

Diff for: docs/src/config/step/Release.md

+3-33
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,7 @@ name = "release"
3434
[[workflows.steps]]
3535
type = "PrepareRelease"
3636

37-
# Updates Cargo.lock from Cargo.toml so the versions match.
38-
[[workflow.steps]]
39-
type = "Command"
40-
command = "cargo update -w"
41-
42-
# Add the freshly modified changes
43-
[[workflows.steps]]
44-
type = "Command"
45-
command = "git add Cargo.toml Cargo.lock CHANGELOG.md"
46-
47-
# Commit the changes (make sure this is run on your default branch!)
37+
# Commit the changes that PrepareRelease added
4838
[[workflows.steps]]
4939
type = "Command"
5040
command = "git commit -m \"chore: Bump to version\""
@@ -82,17 +72,7 @@ name = "release"
8272
[[workflows.steps]]
8373
type = "PrepareRelease"
8474

85-
# Updates Cargo.lock from Cargo.toml so the versions match.
86-
[[workflow.steps]]
87-
type = "Command"
88-
command = "cargo update -w"
89-
90-
# Add the freshly modified changes
91-
[[workflows.steps]]
92-
type = "Command"
93-
command = "git add Cargo.toml Cargo.lock CHANGELOG.md"
94-
95-
# Commit the changes (make sure this is your default branch!)
75+
# Commit the changes that PrepareRelease made
9676
[[workflows.steps]]
9777
type = "Command"
9878
command = "git commit -m \"chore: Bump to version\""
@@ -128,17 +108,7 @@ name = "release"
128108
[[workflows.steps]]
129109
type = "PrepareRelease"
130110

131-
# Updates Cargo.lock from Cargo.toml so the versions match.
132-
[[workflow.steps]]
133-
type = "Command"
134-
command = "cargo update -w"
135-
136-
# Add the freshly modified changes
137-
[[workflows.steps]]
138-
type = "Command"
139-
command = "git add Cargo.lock knope/Cargo.toml knope/CHANGELOG.md knope-utils/Cargo.toml knope-utils/CHANGELOG.md"
140-
141-
# Commit the changes (make sure this is run on your default branch!)
111+
# Commit the changes that PrepareRelease made
142112
[[workflows.steps]]
143113
type = "Command"
144114
command = "git commit -m \"chore: Prepare releases\""

Diff for: src/config.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -140,17 +140,17 @@ pub(crate) fn generate() -> Result<()> {
140140
github = Some(GitHub { owner, repo });
141141
vec![
142142
Step::Command {
143-
command: String::from("git add . && git commit -m \"chore: prepare release $version\" && git push"),
143+
command: String::from(
144+
"git commit -m \"chore: prepare release $version\" && git push",
145+
),
144146
variables: Some(variables),
145147
},
146148
Step::Release,
147149
]
148150
}
149151
_ => vec![
150152
Step::Command {
151-
command: String::from(
152-
"git add . && git commit -m \"chore: prepare release $version\"",
153-
),
153+
command: String::from("git commit -m \"chore: prepare release $version\""),
154154
variables: Some(variables),
155155
},
156156
Step::Release,

Diff for: src/git.rs

+63-52
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::path::PathBuf;
12
use std::str::FromStr;
23

34
use git2::build::CheckoutBuilder;
@@ -124,6 +125,46 @@ fn select_issue_from_branch_name(ref_name: &str) -> Result<Issue, StepError> {
124125
Ok(Issue { key, summary })
125126
}
126127

128+
#[cfg(test)]
129+
mod test_select_issue_from_branch_name {
130+
use super::*;
131+
132+
#[test]
133+
fn jira_style() {
134+
let issue = select_issue_from_branch_name("ABC-123-some-summary")
135+
.expect("Failed to parse branch name");
136+
137+
assert_eq!(
138+
issue,
139+
Issue {
140+
key: "ABC-123".to_string(),
141+
summary: "some-summary".to_string(),
142+
}
143+
);
144+
}
145+
146+
#[test]
147+
fn github_style() {
148+
let issue =
149+
select_issue_from_branch_name("123-some-summary").expect("Failed to parse branch name");
150+
151+
assert_eq!(
152+
issue,
153+
Issue {
154+
key: "123".to_string(),
155+
summary: "some-summary".to_string(),
156+
}
157+
);
158+
}
159+
160+
#[test]
161+
fn no_number() {
162+
let result = select_issue_from_branch_name("some-summary");
163+
164+
assert!(result.is_err());
165+
}
166+
}
167+
127168
fn create_branch<'repo>(
128169
repo: &'repo Repository,
129170
name: &str,
@@ -190,6 +231,21 @@ pub(crate) fn branch_name_from_issue(issue: &Issue) -> String {
190231
format!("{}-{}", issue.key, issue.summary.to_ascii_lowercase()).replace(' ', "-")
191232
}
192233

234+
#[cfg(test)]
235+
mod test_branch_name_from_issue {
236+
use super::*;
237+
238+
#[test]
239+
fn branch_name_from_issue() {
240+
let issue = Issue {
241+
key: "FLOW-5".to_string(),
242+
summary: "A test issue".to_string(),
243+
};
244+
let branch_name = super::branch_name_from_issue(&issue);
245+
assert_eq!(&branch_name, "FLOW-5-a-test-issue");
246+
}
247+
}
248+
193249
pub(crate) fn get_commit_messages_after_last_stable_version(
194250
package_name: &Option<String>,
195251
) -> Result<Vec<String>, StepError> {
@@ -243,57 +299,12 @@ pub(crate) fn get_commit_messages_after_last_stable_version(
243299
Ok(messages)
244300
}
245301

246-
#[cfg(test)]
247-
mod tests {
248-
use super::*;
249-
250-
#[test]
251-
fn branch_name_from_issue() {
252-
let issue = Issue {
253-
key: "FLOW-5".to_string(),
254-
summary: "A test issue".to_string(),
255-
};
256-
let branch_name = super::branch_name_from_issue(&issue);
257-
assert_eq!(&branch_name, "FLOW-5-a-test-issue");
258-
}
259-
}
260-
261-
#[cfg(test)]
262-
mod test_select_issue_from_branch_name {
263-
use super::*;
264-
265-
#[test]
266-
fn jira_style() {
267-
let issue = select_issue_from_branch_name("ABC-123-some-summary")
268-
.expect("Failed to parse branch name");
269-
270-
assert_eq!(
271-
issue,
272-
Issue {
273-
key: "ABC-123".to_string(),
274-
summary: "some-summary".to_string()
275-
}
276-
);
277-
}
278-
279-
#[test]
280-
fn github_style() {
281-
let issue =
282-
select_issue_from_branch_name("123-some-summary").expect("Failed to parse branch name");
283-
284-
assert_eq!(
285-
issue,
286-
Issue {
287-
key: "123".to_string(),
288-
summary: "some-summary".to_string()
289-
}
290-
);
291-
}
292-
293-
#[test]
294-
fn no_number() {
295-
let result = select_issue_from_branch_name("some-summary");
296-
297-
assert!(result.is_err());
302+
/// Add some files to Git to be committed later.
303+
pub(crate) fn add_files(file_names: &[&PathBuf]) -> Result<(), StepError> {
304+
let repo = Repository::open(".").map_err(|_| StepError::NotAGitRepo)?;
305+
let mut index = repo.index()?;
306+
for file_name in file_names {
307+
index.add_path(file_name)?;
298308
}
309+
index.write().map_err(StepError::from)
299310
}

Diff for: src/releases/conventional_commits.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
use std::io::Write;
2+
13
use git_conventional::{Commit, Type};
24
use log::debug;
3-
use std::io::Write;
45

5-
use crate::git::get_commit_messages_after_last_stable_version;
6+
use crate::git::{add_files, get_commit_messages_after_last_stable_version};
67
use crate::releases::semver::PackageVersion;
78
use crate::releases::Package;
89
use crate::step::StepError;
@@ -422,6 +423,7 @@ fn prepare_release_for_package(
422423
if let Some(changelog) = changelog {
423424
let contents = add_version_to_changelog(&changelog.content, &new_changes);
424425
std::fs::write(&changelog.path, contents)?;
426+
add_files(&[&changelog.path])?;
425427
}
426428
Ok(Some(release))
427429
}

Diff for: src/releases/package.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub(crate) struct VersionedFile {
4141
/// The type of file format that `content` is.
4242
pub(crate) format: PackageFormat,
4343
/// The path to the file that was parsed.
44-
path: PathBuf,
44+
pub(crate) path: PathBuf,
4545
/// The raw content of the package manager file so it doesn't have to be read again.
4646
content: String,
4747
}

Diff for: src/releases/semver.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use semver::{Prerelease, Version};
22
use serde::{Deserialize, Serialize};
33

4+
use crate::git::add_files;
45
use crate::releases::git::get_current_versions_from_tag;
56
use crate::releases::package::Package;
67
use crate::releases::CurrentVersions;
@@ -169,9 +170,12 @@ fn set_version(
169170
version,
170171
} = package_version;
171172
let latest = version.latest();
173+
let mut paths = Vec::with_capacity(package.versioned_files.len());
172174
for versioned_file in &mut package.versioned_files {
173175
versioned_file.set_version(latest)?;
176+
paths.push(&versioned_file.path);
174177
}
178+
add_files(&paths)?;
175179
Ok(PackageVersion { version, package })
176180
}
177181

Diff for: tests/generate/github/knope.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ type = "PrepareRelease"
66

77
[[workflows.steps]]
88
type = "Command"
9-
command = "git add . && git commit -m \"chore: prepare release $version\" && git push"
9+
command = "git commit -m \"chore: prepare release $version\" && git push"
1010

1111
[workflows.steps.variables]
1212
"$version" = "Version"

Diff for: tests/generate/no_remote/knope.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ type = "PrepareRelease"
66

77
[[workflows.steps]]
88
type = "Command"
9-
command = "git add . && git commit -m \"chore: prepare release $version\""
9+
command = "git commit -m \"chore: prepare release $version\""
1010

1111
[workflows.steps.variables]
1212
"$version" = "Version"

Diff for: tests/generate/package_changelog/changelog_knope.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type = "PrepareRelease"
1010

1111
[[workflows.steps]]
1212
type = "Command"
13-
command = "git add . && git commit -m \"chore: prepare release $version\""
13+
command = "git commit -m \"chore: prepare release $version\""
1414

1515
[workflows.steps.variables]
1616
"$version" = "Version"

Diff for: tests/generate/package_changelog/no_changelog_knope.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type = "PrepareRelease"
99

1010
[[workflows.steps]]
1111
type = "Command"
12-
command = "git add . && git commit -m \"chore: prepare release $version\""
12+
command = "git commit -m \"chore: prepare release $version\""
1313

1414
[workflows.steps.variables]
1515
"$version" = "Version"

Diff for: tests/generate/packages/Cargo.toml_knope.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type = "PrepareRelease"
99

1010
[[workflows.steps]]
1111
type = "Command"
12-
command = "git add . && git commit -m \"chore: prepare release $version\""
12+
command = "git commit -m \"chore: prepare release $version\""
1313

1414
[workflows.steps.variables]
1515
"$version" = "Version"

Diff for: tests/generate/packages/multiple_knope.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type = "PrepareRelease"
99

1010
[[workflows.steps]]
1111
type = "Command"
12-
command = "git add . && git commit -m \"chore: prepare release $version\""
12+
command = "git commit -m \"chore: prepare release $version\""
1313

1414
[workflows.steps.variables]
1515
"$version" = "Version"

Diff for: tests/generate/packages/package.json_knope.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type = "PrepareRelease"
99

1010
[[workflows.steps]]
1111
type = "Command"
12-
command = "git add . && git commit -m \"chore: prepare release $version\""
12+
command = "git commit -m \"chore: prepare release $version\""
1313

1414
[workflows.steps.variables]
1515
"$version" = "Version"

Diff for: tests/generate/packages/pyproject.toml_knope.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type = "PrepareRelease"
99

1010
[[workflows.steps]]
1111
type = "Command"
12-
command = "git add . && git commit -m \"chore: prepare release $version\""
12+
command = "git commit -m \"chore: prepare release $version\""
1313

1414
[workflows.steps.variables]
1515
"$version" = "Version"

Diff for: tests/git_release/dry_run_output.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ Would add the following to CHANGELOG.md:
66

77
- New feature
88

9-
Would run git add Cargo.toml && git commit -m "chore: Bump to 1.1.0"
9+
Would run git commit -m "chore: Bump to 1.1.0"
1010
Would create Git tag v1.1.0

Diff for: tests/git_release/knope.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type = "PrepareRelease"
1010

1111
[[workflows.steps]]
1212
type = "Command"
13-
command = "git add Cargo.toml && git commit -m \"chore: Bump to version\""
13+
command = "git commit -m \"chore: Bump to version\""
1414
variables = { "version" = "Version" }
1515

1616
[[workflows.steps]]

Diff for: tests/git_release/multiple_packages/dry_run_output.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ Would add the following to SECOND_CHANGELOG.md:
1414

1515
- New breaking feature
1616

17-
Would run git add Cargo.toml pyproject.toml package.json FIRST_CHANGELOG.md SECOND_CHANGELOG.md && git commit -m "chore: Prepare release"
17+
Would run git commit -m "chore: Prepare release"
1818
Would create Git tag first/v2.0.0
1919
Would create Git tag second/v0.5.0

Diff for: tests/git_release/multiple_packages/knope.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type = "PrepareRelease"
1414

1515
[[workflows.steps]]
1616
type = "Command"
17-
command = "git add Cargo.toml pyproject.toml package.json FIRST_CHANGELOG.md SECOND_CHANGELOG.md && git commit -m \"chore: Prepare release\""
17+
command = "git commit -m \"chore: Prepare release\""
1818

1919
[[workflows.steps]]
2020
type = "Release"

Diff for: tests/git_release/output.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
[..] chore: Bump to 1.1.0
2-
1 file changed, 2 insertions(+)
2+
2 files changed, 13 insertions(+)
3+
create mode 100644 CHANGELOG.md
34
create mode 100644 Cargo.toml

0 commit comments

Comments
 (0)