Skip to content

Commit

Permalink
Merge pull request #57 from ilovemilk/hotfix/56-conventional-commits-…
Browse files Browse the repository at this point in the history
…incrementer

fix conventional commits incrementer
  • Loading branch information
ilovemilk authored Nov 25, 2020
2 parents a3c3caa + ae7a525 commit ea5998c
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 83 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.3.2]
### Fixed
- Fix conventional commits incrementer for BREAKING CHANGES marked by a ! after the scope.
- Improve conventional commits incrementer regex parsing.
- Remove `git branch --show-current` to resolve detached HEAD problems and to improve Git < 2.22.0 compatibility.

## [2.3.1]
### Fixed
- Fix conventional commits incrementer for windows and annotated and lightweight tags.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# :ghost: __semver-git-plugin__
*Version your gradle projects with git tags and semantic versioning. Requires git 2.22 or higher*
*Version your gradle projects with git tags and semantic versioning.*

[![GitHub version](https://img.shields.io/github/tag/ilovemilk/semver-git-plugin.svg)](https://img.shields.io/github/tag/ilovemilk/semver-git-plugin.svg)
[![License](https://img.shields.io/github/license/ilovemilk/semver-git-plugin.svg)](https://img.shields.io/github/license/ilovemilk/semver-git-plugin.svg)
Expand All @@ -12,7 +12,7 @@ Gradle 2.1 and higher

```
plugins {
id("io.wusa.semver-git-plugin").version("2.3.0")
id("io.wusa.semver-git-plugin").version("2.3.2")
}
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "io.wusa"
version = "2.3.1"
version = "2.3.2"

dependencies {
implementation(kotlin("stdlib-jdk8"))
Expand Down
18 changes: 5 additions & 13 deletions src/main/kotlin/io/wusa/GitService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ class GitService {
@Throws(NoCurrentBranchFoundException::class)
fun currentBranch(project: Project): String {
return try {
val branches = getCurrentBranch(project)
filterCurrentBranch(branches)
getCurrentBranch(project)
} catch (ex: GitException) {
throw NoCurrentBranchFoundException(ex)
} catch (ex: KotlinNullPointerException) {
Expand Down Expand Up @@ -71,12 +70,12 @@ class GitService {

fun getCommitsSinceLastTag(project: Project, tagPrefix : String = "", tagType : TagType = TagType.ANNOTATED): List<String> {
var cmdArgs = arrayOf("describe", "--dirty", "--abbrev=0", "--match", "$tagPrefix*")
if (tagType == TagType.LIGHTWEIGHT){
if (tagType == TagType.LIGHTWEIGHT) {
cmdArgs = arrayOf("describe", "--tags", "--dirty", "--abbrev=0", "--match", "$tagPrefix*")
}
return try {
val lastTag = GitCommandRunner.execute(project.projectDir, cmdArgs)
GitCommandRunner.execute(project.projectDir, arrayOf("log", "--oneline", "$lastTag..@")).lines()
GitCommandRunner.execute(project.projectDir, arrayOf("log", "--pretty=format:%s %(trailers:separator=%x2c)", "$lastTag..@")).lines()
} catch (ex: GitException) {
emptyList()
}
Expand All @@ -101,16 +100,9 @@ class GitService {
}
}

private fun filterCurrentBranch(branches: String) =
"""(\*)? +(.*?) +(.*?)?""".toRegex().find(branches)!!.groupValues[2]

private fun getCurrentBranch(project: Project): String {
val branchName = GitCommandRunner.execute(project.projectDir, arrayOf("branch", "--show-current"))
return GitCommandRunner.execute(project.projectDir, arrayOf("branch", branchName, "--verbose", "--no-abbrev", "--contains"))
}

private fun getAllBranches(project: Project): String {
return GitCommandRunner.execute(project.projectDir, arrayOf("branch", "--all", "--verbose", "--no-abbrev", "--contains"))
val head = GitCommandRunner.execute(project.projectDir, arrayOf("log", "-n", "1", "--pretty=%d", "HEAD"))
return """\(HEAD -> (.*?)[,|)]""".toRegex().find(head)!!.groupValues[1]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,21 @@ class ConventionalCommitsIncrementer: IIncrementer {
val semverGitPluginExtension = project.extensions.getByType(SemverGitPluginExtension::class.java)

val listOfCommits = GitService.getCommitsSinceLastTag(project, semverGitPluginExtension.tagPrefix, semverGitPluginExtension.tagType)
var major = 0
var minor = 0
var patch = 0
var major = 0
var minor = 0
var patch = 0
val optionalScope = "(\\(.*?\\))?"
val feat = "^feat$optionalScope"
val fix = "^fix$optionalScope"
val breakingChange = "\\bBREAKING CHANGE\\b:"

listOfCommits.forEach {
if (it.contains("""^[0-9a-f]{7} BREAKING CHANGE""".toRegex())) {
major += 1
}
if (it.contains("""^[0-9a-f]{7} feat""".toRegex())) {
minor += 1
}
if (it.contains("""^[0-9a-f]{7} fix""".toRegex())) {
patch += 1
when {
it.contains("$feat!:".toRegex()) -> major += 1
it.contains("$fix!:".toRegex()) -> major += 1
it.contains(breakingChange.toRegex()) -> major += 1
it.contains("$feat:".toRegex()) -> minor += 1
it.contains("$fix:".toRegex()) -> patch += 1
}
}
if (patch > 0) {
Expand Down
72 changes: 40 additions & 32 deletions src/test/kotlin/io/wusa/ConventionalCommitsIncrementerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,58 +38,66 @@ class ConventionalCommitsIncrementerTest {
@Test
fun `patch should be increased by 1`() {
every { GitCommandRunner.execute(projectDir = any(), args = any()) } returns
"7356414 fix: update gradle plugin publish due to security bugs\n" +
"45f65f6 fix: update version an changelog\n" +
"67f03b1 feat: Merge pull request #18 from ilovemilk/feature/support-multi-module\n" +
"fba5872 fix: add default tagPrefix behaviour\n" +
"2d03c4b fix: Merge pull request #17 from jgindin/support-multi-module\n" +
"f96697f fix: Merge remote-tracking branch 'origin/feature/add-more-tests' into develop\n" +
"73fc8b4 fix: Add support for multi-module projects.\n" +
"74e3eb1 fix: add test for kebab-case with numbers\n" +
"63ca60f fix: add more tests"
"fix: update gradle plugin publish due to security bugs\n" +
"fix: update version an changelog\n" +
"feat: Merge pull request #18 from ilovemilk/feature/support-multi-module\n" +
"fix: add default tagPrefix behaviour\n" +
"fix: Merge pull request #17 from jgindin/support-multi-module\n" +
"fix: Merge remote-tracking branch 'origin/feature/add-more-tests' into develop\n" +
"fix: Add support for multi-module projects.\n" +
"fix: add test for kebab-case with numbers\n" +
"fix: add more tests"

Assertions.assertEquals(VersionIncrementer.getVersionIncrementerByName("CONVENTIONAL_COMMITS_INCREMENTER").increment(Version(0, 0, 0, "", "", null), project), Version(0, 1, 0, "", "", null))
}

@Test
fun `minor should be increased by 1`() {
every { GitCommandRunner.execute(projectDir = any(), args = any()) } returns
"7356414 fix: update gradle plugin publish due to security bugs\n" +
"45f65f6 fix: update version an changelog\n" +
"67f03b1 fix: Merge pull request #18 from ilovemilk/feature/support-multi-module\n" +
"fba5872 fix: add default tagPrefix behaviour\n" +
"2d03c4b fix: Merge pull request #17 from jgindin/support-multi-module\n" +
"f96697f fix: Merge remote-tracking branch 'origin/feature/add-more-tests' into develop\n" +
"73fc8b4 fix: Add support for multi-module projects.\n" +
"74e3eb1 fix: add test for kebab-case with numbers\n" +
"63ca60f fix: add more tests"
"fix: update gradle plugin publish due to security bugs\n" +
"fix: update version an changelog\n" +
"fix: Merge pull request #18 from ilovemilk/feature/support-multi-module\n" +
"fix: add default tagPrefix behaviour\n" +
"fix: Merge pull request #17 from jgindin/support-multi-module\n" +
"fix: Merge remote-tracking branch 'origin/feature/add-more-tests' into develop\n" +
"fix: Add support for multi-module projects.\n" +
"fix: add test for kebab-case with numbers\n" +
"fix: add more tests"

Assertions.assertEquals(VersionIncrementer.getVersionIncrementerByName("CONVENTIONAL_COMMITS_INCREMENTER").increment(Version(0, 0, 0, "", "", null), project), Version(0, 0, 1, "", "", null))
}

@Test
fun `major should be increased by 1`() {
every { GitCommandRunner.execute(projectDir = any(), args = any()) } returns
"7356414 fix: update gradle plugin publish due to security bugs\n" +
"45f65f6 BREAKING CHANGE: update version an changelog\n" +
"67f03b1 fix: Merge pull request #18 from ilovemilk/feature/support-multi-module\n" +
"fba5872 feat: add default tagPrefix behaviour\n" +
"2d03c4b fix: Merge pull request #17 from jgindin/support-multi-module\n" +
"f96697f feat: Merge remote-tracking branch 'origin/feature/add-more-tests' into develop\n" +
"73fc8b4 fix: Add support for multi-module projects.\n" +
"74e3eb1 feat: add test for kebab-case with numbers\n" +
"63ca60f fix: add more tests"
"fix: update gradle plugin publish due to security bugs\n" +
"BREAKING CHANGE: update version an changelog\n" +
"fix: Merge pull request #18 from ilovemilk/feature/support-multi-module\n" +
"feat: add default tagPrefix behaviour\n" +
"fix: Merge pull request #17 from jgindin/support-multi-module\n" +
"feat: Merge remote-tracking branch 'origin/feature/add-more-tests' into develop\n" +
"fix: Add support for multi-module projects.\n" +
"feat: add test for kebab-case with numbers\n" +
"fix: add more tests"

Assertions.assertEquals(VersionIncrementer.getVersionIncrementerByName("CONVENTIONAL_COMMITS_INCREMENTER").increment(Version(0, 0, 0, "", "", null), project), Version(1, 0, 0, "", "", null))
}

@Test
fun `issue-47 increment minor by one`() {
fun `issue-56 breaking change with ! after the feat scope`() {
every { GitCommandRunner.execute(projectDir = any(), args = any()) } returns
"685f852 (HEAD -> feature/JiraIssue-11111_add_semver_plugin) feat: added semver plugin incrementer parameter\n" +
"7c03cdd feat: added semver plugin incrementer parameter\n" +
"f45e853 feat: added semver plugin incrementer parameter"
"feat!: added semver plugin incrementer parameter\n" +
"feat: added semver plugin incrementer parameter"

Assertions.assertEquals(VersionIncrementer.getVersionIncrementerByName("CONVENTIONAL_COMMITS_INCREMENTER").increment(Version(0, 0, 0, "", "", null), project), Version(0, 1, 0, "", "", null))
Assertions.assertEquals(VersionIncrementer.getVersionIncrementerByName("CONVENTIONAL_COMMITS_INCREMENTER").increment(Version(0, 0, 0, "", "", null), project), Version(1, 0, 0, "", "", null))
}

@Test
fun `issue-56 breaking change with ! after the fix scope`() {
every { GitCommandRunner.execute(projectDir = any(), args = any()) } returns
"fix!: added semver plugin incrementer parameter\n" +
"feat: added semver plugin incrementer parameter"

Assertions.assertEquals(VersionIncrementer.getVersionIncrementerByName("CONVENTIONAL_COMMITS_INCREMENTER").increment(Version(0, 0, 0, "", "", null), project), Version(1, 0, 0, "", "", null))
}
}
36 changes: 12 additions & 24 deletions src/test/kotlin/io/wusa/GitServiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,95 +94,83 @@ class GitServiceTest {

@Test
fun `get current branch master`() {
every { GitCommandRunner.execute(projectDir = any(), args = any()) } returns "* master 5824168c73ba0618c1b6e384fbd7d61c5e8b8bc3"
every { GitCommandRunner.execute(projectDir = any(), args = any()) } returns "(HEAD -> master)"
Assertions.assertEquals("master", GitService.currentBranch(project))
}

@Test
fun `get current branch feature-test`() {
every { GitCommandRunner.execute(projectDir = any(), args = any()) } returns "* feature/test 5824168c73ba0618c1b6e384fbd7d61c5e8b8bc3"
every { GitCommandRunner.execute(projectDir = any(), args = any()) } returns "(HEAD -> feature/test)"
Assertions.assertEquals("feature/test", GitService.currentBranch(project))
}

@Test
fun `get current branch feature-test with origin`() {
every { GitCommandRunner.execute(projectDir = any(), args = any()) } returns
"* feature/test cd55642b18ef34d976eda337d2b7abd296b37c8f remove code quality\n" +
" remotes/origin/feature/test cd55642b18ef34d976eda337d2b7abd296b37c8f remove code quality"
"(HEAD -> feature/test, origin/feature/test)"
Assertions.assertEquals("feature/test", GitService.currentBranch(project))
}

@Test
fun `get current branch feature-reactiveTests with origin`() {
every { GitCommandRunner.execute(projectDir = any(), args = any()) } returns
"* feature/reactiveTests 831965a6c57434276c70c8e1134244dd6077b1fc fix tests with timeout\n" +
" hotfix/codePrefix 4ad7116a019553ff19d0e338bf7e602374d72c04 [behind 1] fixed publish code test for added prefix\n" +
" remotes/origin/develop 13fc04d392d51d9bc7b70c8d52b2d8cd6cc1199a Merge branch 'feature/reactive-tests' into 'develop'\n" +
" remotes/origin/hotfix/codePrefix a3b40aa8be599003c8656d5cc9a460ffd61fe1f9 escaping / in regex for branch detection\n"
"(HEAD -> feature/reactiveTests)"
Assertions.assertEquals("feature/reactiveTests", GitService.currentBranch(project))
}

@Test
fun `get current branch hotfix-codePrefix with origin`() {
every { GitCommandRunner.execute(projectDir = any(), args = any()) } returns
"* hotfix/codePrefix 4ad7116a019553ff19d0e338bf7e602374d72c04 [behind 1] fixed publish code test for added prefix\n" +
" remotes/origin/hotfix/codePrefix a3b40aa8be599003c8656d5cc9a460ffd61fe1f9 escaping / in regex for branch detection"
"(HEAD -> hotfix/codePrefix, origin/hotfix/codePrefix)"
Assertions.assertEquals("hotfix/codePrefix", GitService.currentBranch(project))
}

@Test
fun `get current branch feature-s-version-3 with origin`() {
every { GitCommandRunner.execute(projectDir = any(), args = any()) } returns
"* feature/s-version-3 9ea487bb167c89a6453fd2e72740a492c6782887 use kebab case\n" +
" remotes/origin/feature/s-version-3 9ea487bb167c89a6453fd2e72740a492c6782887 use kebab case"
"(HEAD -> feature/s-version-3, origin/feature/s-version-3)"
Assertions.assertEquals("feature/s-version-3", GitService.currentBranch(project))
}

@Test
fun `get current branch feature-abcd-10847-abcde-abc with origin`() {
every { GitCommandRunner.execute(projectDir = any(), args = any()) } returns
"* feature/abcd-10847-abcde-abc 9ea487bb167c89a6453fd2e72740a492c6782887 abcd-10847-abcde-abc\n" +
" remotes/origin/feature/abcd-10847-abcde-abc 9ea487bb167c89a6453fd2e72740a492c6782887 abcd-10847-abcde-abc"
"(HEAD -> feature/abcd-10847-abcde-abc, origin/feature/abcd-10847-abcde-abc)"
Assertions.assertEquals("feature/abcd-10847-abcde-abc", GitService.currentBranch(project))
}

@Test
fun `get current branch hotfix-5-3-1 with origin`() {
every { GitCommandRunner.execute(projectDir = any(), args = any()) } returns
"* hotfix/5.3.1 9ea487bb167c89a6453fd2e72740a492c6782887 abcd-10847-abcde-abc\n" +
" remotes/origin/hotfix/5.3.1 9ea487bb167c89a6453fd2e72740a492c6782887 abcd-10847-abcde-abc"
"(HEAD -> hotfix/5.3.1, origin/hotfix/5.3.1)"
Assertions.assertEquals("hotfix/5.3.1", GitService.currentBranch(project))
}

@Test
fun `issue-35 fix branch regex`() {
every { GitCommandRunner.execute(projectDir = any(), args = any()) } returns
"* feature/bellini/test-branch-version 9ea487bb167c89a6453fd2e72740a492c6782887 abcd-10847-abcde-abc\n" +
" remotes/origin/feature/bellini/test-branch-version 9ea487bb167c89a6453fd2e72740a492c6782887 abcd-10847-abcde-abc"
"(HEAD -> feature/bellini/test-branch-version, origin/feature/bellini/test-branch-version)"
Assertions.assertEquals("feature/bellini/test-branch-version", GitService.currentBranch(project))
}

@Test
fun `camelCase branch`() {
every { GitCommandRunner.execute(projectDir = any(), args = any()) } returns
"* feature/camelCase 9ea487bb167c89a6453fd2e72740a492c6782887 abcd-10847-abcde-abc\n" +
" remotes/origin/feature/camelCase 9ea487bb167c89a6453fd2e72740a492c6782887 abcd-10847-abcde-abc"
"(HEAD -> feature/camelCase, remotes/origin/feature/camelCase)"
Assertions.assertEquals("feature/camelCase", GitService.currentBranch(project))
}

@Test
fun `UPPER_CASE branch`() {
every { GitCommandRunner.execute(projectDir = any(), args = any()) } returns
"* feature/UPPER_CASE 9ea487bb167c89a6453fd2e72740a492c6782887 abcd-10847-abcde-abc\n" +
" remotes/origin/feature/UPPER_CASE 9ea487bb167c89a6453fd2e72740a492c6782887 abcd-10847-abcde-abc"
"(HEAD -> feature/UPPER_CASE, origin/feature/UPPER_CASE)"
Assertions.assertEquals("feature/UPPER_CASE", GitService.currentBranch(project))
}

@Test
fun `PascalCase branch`() {
every { GitCommandRunner.execute(projectDir = any(), args = any()) } returns
"* feature/PascalCase 9ea487bb167c89a6453fd2e72740a492c6782887 abcd-10847-abcde-abc\n" +
" remotes/origin/feature/PascalCase 9ea487bb167c89a6453fd2e72740a492c6782887 abcd-10847-abcde-abc"
"(HEAD -> feature/PascalCase, origin/feature/PascalCase)"
Assertions.assertEquals("feature/PascalCase", GitService.currentBranch(project))
}

Expand Down

0 comments on commit ea5998c

Please sign in to comment.