Skip to content

Commit ebd8427

Browse files
committed
feat: filter commit by scope to calculated next version
1 parent 53e6c62 commit ebd8427

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,18 @@ This plugin provides two main commands:
1414
Generates or updates a `CHANGELOG.md` file using Git history.
1515

1616
* Supports a `dryRun` option to preview the next changelog without writing to disk.
17+
* Supports a scope option to include only commits containing a specific scope.
18+
19+
```bash
20+
mvn io.github.zorin95670:semantic-version:changelog -DtagPrefix=plugin-a@ -Dscope=plugin-a
21+
# Example:
22+
# chore(plugin-a): Test -> Included
23+
# chore(plugin-b): Test -> Ignored
24+
```
25+
1726
* Supports a `tagPrefix` option to filter Git tags by prefix when generating the changelog (e.g., only include tags like `[email protected]`). Default value is `v`
1827

28+
1929
```bash
2030
mvn io.github.zorin95670:semantic-version:changelog -DtagPrefix=plugin-a@
2131
```
@@ -36,7 +46,7 @@ Performs the following operations:
3646
* Creates a Git tag for the new release with the specified prefix.
3747

3848
```bash
39-
mvn io.github.zorin95670:semantic-version:release -DtagPrefix=plugin-a@
49+
mvn io.github.zorin95670:semantic-version:release -DtagPrefix=plugin-a@ -Dscope=plugin-a
4050
```
4151

4252
When `tagPrefix` is specified:
@@ -45,6 +55,11 @@ When `tagPrefix` is specified:
4555
* The generated tag will also include the prefix (e.g., `[email protected]`).
4656
* Default value is `v`
4757

58+
When `scope` is specified:
59+
60+
* Only commits that include the scope `(plugin-a)` will be considered for version bumping and changelog generation.
61+
* This is useful for monorepos or multi-module projects where only a subset of commits should affect a specific release.
62+
4863
> ℹ️ After running the `release` goal, make sure to **push the commit and the tag** manually:
4964
5065
```bash

src/main/java/io/github/zorin95670/semver/GenerateChangelogMojo.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public class GenerateChangelogMojo extends AbstractMojo {
1616
@Parameter(property = "tagPrefix", defaultValue = "v")
1717
private String tagPrefix;
1818

19+
@Parameter(property = "scope")
20+
private String scope;
21+
1922
@Parameter(property = "dryRun", defaultValue = "false")
2023
private boolean dryRun;
2124

@@ -30,7 +33,7 @@ public void execute() throws MojoExecutionException {
3033

3134
changelogService.generateFromBeginning(
3235
gitService.getUrl(),
33-
gitService.getCommitsFrom(null),
36+
gitService.getCommitsFrom(null, scope),
3437
gitService.getAllTags(null, tagPrefix),
3538
dryRun,
3639
tagPrefix

src/main/java/io/github/zorin95670/semver/ReleaseMojo.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public class ReleaseMojo extends AbstractMojo {
1919
@Parameter(property = "tagPrefix", defaultValue = "v")
2020
private String tagPrefix;
2121

22+
@Parameter(property = "scope")
23+
private String scope;
24+
2225
@Parameter(defaultValue = "${project}", readonly = true)
2326
private MavenProject project;
2427

@@ -31,7 +34,7 @@ public void execute() throws MojoExecutionException {
3134
ChangelogService changelogService = new ChangelogService(basedir);
3235

3336
var lastTag = gitService.getLastTag(tagPrefix).orElse(null);
34-
var commits = gitService.getCommitsFrom(lastTag);
37+
var commits = gitService.getCommitsFrom(lastTag, scope);
3538
var opt = gitService.getNewTagName(commits, lastTag, tagPrefix);
3639

3740
if (opt.isEmpty()) {
@@ -47,7 +50,7 @@ public void execute() throws MojoExecutionException {
4750
gitService.tag(nextTag);
4851
changelogService.generateFromBeginning(
4952
gitService.getUrl(),
50-
gitService.getCommitsFrom(null),
53+
gitService.getCommitsFrom(null, scope),
5154
gitService.getAllTags(null, tagPrefix),
5255
false,
5356
tagPrefix

src/main/java/io/github/zorin95670/semver/service/GitService.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.IOException;
2121
import java.net.URISyntaxException;
2222
import java.util.*;
23+
import java.util.regex.Matcher;
2324
import java.util.regex.Pattern;
2425

2526
import static org.eclipse.jgit.lib.Constants.R_TAGS;
@@ -153,8 +154,9 @@ public Optional<RevTag> getLastTag(String tagPrefix) {
153154
}
154155
}
155156

156-
public List<RevCommit> getCommitsFrom(RevTag origin) {
157+
public List<RevCommit> getCommitsFrom(RevTag origin, String scope) {
157158
List<RevCommit> commits = new ArrayList<>();
159+
var scopePattern = Pattern.compile("^\\w+\\(" + Pattern.quote(scope) + "\\):");
158160

159161
try (RevWalk revWalk = new RevWalk(repository)) {
160162
RevCommit head = revWalk.parseCommit(repository.resolve("HEAD"));
@@ -172,7 +174,10 @@ public List<RevCommit> getCommitsFrom(RevTag origin) {
172174
if (stopCommit != null && commit.equals(stopCommit)) {
173175
break;
174176
}
175-
commits.add(commit);
177+
178+
if (scope.isEmpty() || scopePattern.matcher(commit.getShortMessage()).matches()) {
179+
commits.add(commit);
180+
}
176181
}
177182

178183
} catch (IOException e) {
@@ -222,7 +227,7 @@ public Optional<String> getNewTagName(List<RevCommit> commits, RevTag lastTag, S
222227
// Exemple header : feat!: message, fix(scope): message
223228
// Regex pour extraire type et "!" :
224229
// ^(\w+)(!)?(\(.+\))?: .+
225-
java.util.regex.Matcher matcher = java.util.regex.Pattern.compile("^(\\w+)(!)?(\\(.+\\))?:").matcher(header);
230+
Matcher matcher = Pattern.compile("^(\\w+)(!)?(\\(.+\\))?:").matcher(header);
226231

227232
if (matcher.find()) {
228233
String type = matcher.group(1);

0 commit comments

Comments
 (0)