13

I've been fiddling around this since yesterday.
I just can't seem to match all the possible cases.

I'm trying to come up with a regular expression which matches a Conventional Commit, but which also offers some error recovery functionality.

Current regexp:

(?<type>build)(?<scope>\(.*\)?(?=:))?(?<breaking>!)?(?<subject>:.*)?

Inputs:

build(one)
build(two)!
build(three)!:test
build(example:module)!: test
build: test
build(<> : dda!sd): test
build(:
build

Outputs:

enter image description here

What doesn't work:

  • first two cases
  • the ! isn't captured as breaking in the third and following cases

The sample is at Regex101, https://regex101.com/r/XYC04q/1
And I have other (16) tests here, https://regex101.com/r/sSrvyA/11

Even if you have no time to try and modify it, any comment is appreciated.

LppEdd
  • 20,274
  • 11
  • 84
  • 139

7 Answers7

24

Doesn't necessarily solve the desire to capture )!: as breaking group but this does seem to follow conventional commits specs 1-5 + 13.

^(?<type>build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test|¯\\_\(ツ\)_\/¯)(?<scope>\(\w+\)?((?=:\s)|(?=!:\s)))?(?<breaking>!)?(?<subject>:\s.*)?|^(?<merge>Merge \w+)

https://regex101.com/r/XYC04q/11

Gitlab Push Commit Regex

However, if you are using a platform like GitLab and want to set push rule to commit messages as of v13(?) they are using re2 standards and Golang parser.

Here it is for that Gitlab.

Note that GitLab enables the global flag (?m) which gave me some difficulty. Discussion on gitlab

| Restrict by commit message | Starter 7.10 | Only commit messages that match this regular expression are allowed to be pushed. Leave empty to allow any commit message. Uses multiline mode, which can be disabled using (?-m). |

source doc

## simplified and modified for gitlab's 
^((build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test|¯\\_\(ツ\)_/¯)(\(\w+\))?(!)?(: (.*\s*)*))|(Merge (.*\s*)*)|(Initial commit$)

## RE2 compliment but doesn't work on GitLab at this time
^(?P<type>build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test|¯\\_\(ツ\)_/¯)(?P<scope>\(\w+\))?(?P<breaking>!)?(?P<subject>:\s.*)?|^(?P<merge>Merge \w+)

https://regex101.com/r/XYC04q/28

GitLab using Terraform Push Rule Regex

Note if you are trying to use Terraform for Gitlab with Regex, note that Terraform parses the string prior to Gitlab requiring some bonus escapes.

resource "gitlab_project_push_rules" "github_flow" {
  project = gitlab_project.project.id

  # Conventional Commits https://www.conventionalcommits.org/en/v1.0.0/
  commit_message_regex = "^((build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test|¯\\\\_\\(ツ\\)_/¯)(\\(\\w+\\))?(!)?(: (.*\\s*)*))|(Merge (.*\\s*)*)|(Initial commit$)"
  branch_name_regex    = "(^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test|¯\\\\_\\(ツ\\)_\\/¯)\\/[a-z0-9\\-]{1,55}$)|master"

  prevent_secrets = true
}
codeangler
  • 779
  • 8
  • 16
7

You have some optional parts for which you could indeed a non capturing group to match either from an opening ( till a closing ) or match only an opening (

(?<type>build)(?<scope>(?:\([^()\r\n]*\)|\()?(?<breaking>!)?)(?<subject>:.*)?
  • (?<type>build) Group type, match build
  • (?<scope> Group scope
    • (?: Non capturing group
      • \([^()\r\n]*\) Match either from opening ( till closing )
      • | or
      • \( Match a single (
    • )? Close non capturing group and make it optional
    • (?<breaking>!)? Optional group breaking
  • ) Close group scope
  • (?<subject>:.*)? Optional group subject

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • 1
    Just seen the update. Thank you very much for the detailed dissection of the expression. I've learned something new. I had began trying on a whiteboard, with no success as of now. – LppEdd Nov 17 '19 at 12:52
  • 1
    Hi! I've settled with this one https://regex101.com/r/ONAful/1, to cover all the tokens available. If you think it's worth mentioning it on your answer, you may do it. Thanks again. – LppEdd Nov 17 '19 at 16:12
  • I just made this one for JIRA tickets: https://regex101.com/r/6VvOv0/1 – SalahAdDin Jul 02 '22 at 19:19
4

This one will validate a conventional commit but it does not break it into groups:

^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\(.*\))?: .*$

Original source: https://www.regextester.com/109925

Daniel Gomez Rico
  • 15,026
  • 20
  • 92
  • 162
2

To build off of codeangler's answer, here is a robust regex I created that additionally satisfies 6. and 7. of the Conventional Commits 1.0.0 Specification, regarding body and footer paragraphs and their line breaks.

\A(((Initial commit)|(Merge [^\r\n]+)|((build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\(\w+\))?!?: [^\r\n]+((\r|\n|\r\n)((\r|\n|\r\n)[^\r\n]+)+)*))(\r|\n|\r\n)?)\z

Notes:

  • line break characters are allowed within body paragraphs to allow for breaking long lines
  • a trailing line break is allowed and can be disallowed by removing the final (\r|\n|\r\n)?
  • the 'Merge borg' part can be removed if using a fast-forward git workflow with no merge commits
  • the 'not a line break' parts [^\r\n] can be replaced with . if the engine does not / is not set to match line breaks with .
  • other parts of the specification such as the elaborate footer content requirements are not addressed

Try it out:

https://regex101.com/r/llDgcv/1

Ders
  • 1,068
  • 13
  • 16
2

Recently I took a stab at this too and based off of a few answers here, some trial and error, and other assistance, I came up with the following that mostly covers the requirements as laid out in https://www.conventionalcommits.org/en/v1.0.0/#specification. The only situation my regex does not cover is should your commit message have multiple footers, capturing any footers after the first.

Regex:

Initial commit|Merge [^\r\n]+|(?:(?<type>build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test|BREAKING CHANGE)(?<scope>\(\w+\))?(?<breaking_change>!?): (?<summary>[\w -]+))(?<=\v\v){0,2}(?<body>[\w\s-]+)(?<footer>(?<=\v\v)(?<footer_token>[\w-]+): (?<footer_value>[\w -]+)|$)

Try it out here: https://regex101.com/r/VG2n9I/1

1

I use this:

^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\((.*?)\))?: (.*?)$

https://www.regextester.com/109925

Tzach Bonfil
  • 151
  • 2
  • 8
1

I wrote this based on the answer of cremedekhan, and it covers multiple footer entries for the mapping from footer_token to footer_value.

(?<initial_commit>^Initial commit\.?)|(?<merge>^Merge [^\r\n]+)|(?<type>^build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test|¯\\_\(ツ\)_\/¯)(?:\((?<scope>[\w-]+)\))?(?<breaking>!)?: (?<summary>[\w ,'.`:-]+)(?<=\v\v){0,2}(?<body>[\w\s ,'.`\[\]-]+)(?<footer>(?<=\v\v)(?:(?<footer_token>[\w\s-]+): (?<footer_value>[\w -`]+))+|$)

Try it out here: https://regex101.com/r/JB5nb8/1

Zoe
  • 27,060
  • 21
  • 118
  • 148
Ahmed Kamal
  • 126
  • 1
  • 2
  • 10