From 68130c36f0eabf7dbde4418dadd674b21d4b7a51 Mon Sep 17 00:00:00 2001 From: Matthais Held Date: Tue, 17 Nov 2020 15:15:08 +0100 Subject: [PATCH 1/4] fix conventional commits incrementer Remove --oneline and format log without sha Improve regex to also cover breaking changes indicated by ! after the scope --- src/main/kotlin/io/wusa/GitService.kt | 2 +- .../ConventionalCommitsIncrementer.kt | 25 ++++--- .../ConventionalCommitsIncrementerTest.kt | 72 ++++++++++--------- 3 files changed, 55 insertions(+), 44 deletions(-) diff --git a/src/main/kotlin/io/wusa/GitService.kt b/src/main/kotlin/io/wusa/GitService.kt index 49a5784..2c441ab 100644 --- a/src/main/kotlin/io/wusa/GitService.kt +++ b/src/main/kotlin/io/wusa/GitService.kt @@ -76,7 +76,7 @@ class GitService { } return try { val lastTag = GitCommandRunner.execute(project.projectDir, cmdArgs) - GitCommandRunner.execute(project.projectDir, arrayOf("log", "--oneline", "$lastTag..@")).lines() + GitCommandRunner.execute(project.projectDir, arrayOf("git", "log", "--pretty=format:%s %(trailers:separator=%x2c)", "$lastTag..@")).lines() } catch (ex: GitException) { emptyList() } diff --git a/src/main/kotlin/io/wusa/incrementer/ConventionalCommitsIncrementer.kt b/src/main/kotlin/io/wusa/incrementer/ConventionalCommitsIncrementer.kt index 749cfa3..b8418f2 100644 --- a/src/main/kotlin/io/wusa/incrementer/ConventionalCommitsIncrementer.kt +++ b/src/main/kotlin/io/wusa/incrementer/ConventionalCommitsIncrementer.kt @@ -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) { diff --git a/src/test/kotlin/io/wusa/ConventionalCommitsIncrementerTest.kt b/src/test/kotlin/io/wusa/ConventionalCommitsIncrementerTest.kt index 44211e0..4bbd63b 100644 --- a/src/test/kotlin/io/wusa/ConventionalCommitsIncrementerTest.kt +++ b/src/test/kotlin/io/wusa/ConventionalCommitsIncrementerTest.kt @@ -38,15 +38,15 @@ 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)) } @@ -54,15 +54,15 @@ class ConventionalCommitsIncrementerTest { @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)) } @@ -70,26 +70,34 @@ class ConventionalCommitsIncrementerTest { @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)) } } From 482f2f0ed63c742b57252a58b42c9270b2d9d337 Mon Sep 17 00:00:00 2001 From: Matthias Held Date: Tue, 17 Nov 2020 17:12:16 +0100 Subject: [PATCH 2/4] fix calling GitCommandRunner and detached head --- src/main/kotlin/io/wusa/GitService.kt | 18 ++++-------- src/test/kotlin/io/wusa/GitServiceTest.kt | 36 ++++++++--------------- 2 files changed, 17 insertions(+), 37 deletions(-) diff --git a/src/main/kotlin/io/wusa/GitService.kt b/src/main/kotlin/io/wusa/GitService.kt index 2c441ab..731de61 100644 --- a/src/main/kotlin/io/wusa/GitService.kt +++ b/src/main/kotlin/io/wusa/GitService.kt @@ -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) { @@ -71,12 +70,12 @@ class GitService { fun getCommitsSinceLastTag(project: Project, tagPrefix : String = "", tagType : TagType = TagType.ANNOTATED): List { 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("git", "log", "--pretty=format:%s %(trailers:separator=%x2c)", "$lastTag..@")).lines() + GitCommandRunner.execute(project.projectDir, arrayOf("log", "--pretty=format:%s %(trailers:separator=%x2c)", "$lastTag..@")).lines() } catch (ex: GitException) { emptyList() } @@ -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] } } } diff --git a/src/test/kotlin/io/wusa/GitServiceTest.kt b/src/test/kotlin/io/wusa/GitServiceTest.kt index 46c60dc..4611ae1 100644 --- a/src/test/kotlin/io/wusa/GitServiceTest.kt +++ b/src/test/kotlin/io/wusa/GitServiceTest.kt @@ -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)) } From f6c057ac224d78582021939862b97bc302a42fbb Mon Sep 17 00:00:00 2001 From: Matthias Held Date: Tue, 17 Nov 2020 17:18:46 +0100 Subject: [PATCH 3/4] update changelog and readme, bump version --- CHANGELOG.md | 6 ++++++ README.md | 2 +- build.gradle.kts | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a914206..43cb74b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/README.md b/README.md index 51c1eeb..90506de 100644 --- a/README.md +++ b/README.md @@ -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") } ``` diff --git a/build.gradle.kts b/build.gradle.kts index 0726419..c019ccc 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,7 +7,7 @@ plugins { } group = "io.wusa" -version = "2.3.1" +version = "2.3.2" dependencies { implementation(kotlin("stdlib-jdk8")) From ae7a5257e283d55a75a3c88446d01931d7c04d38 Mon Sep 17 00:00:00 2001 From: Matthias Held Date: Tue, 17 Nov 2020 17:19:58 +0100 Subject: [PATCH 4/4] update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 90506de..1c2db38 100644 --- a/README.md +++ b/README.md @@ -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)