0267cb89eba6dc9f2744bfba73ada391a6582249

Author
Marc Cornellà <hello@mcornella.com>
Committer
Marc Cornellà <hello@mcornella.com>
Date

Message

perf(changelog): use regex-match instead of `sed` to parse commit subjects

Diff

 1diff --git a/tools/changelog.sh b/tools/changelog.sh
 2index 5c3159732a5e210b5e77e7b32bcef99cd81a8b2c..3641179e255e75db408d399ab7eda298697a5bef 100755
 3--- a/tools/changelog.sh
 4+++ b/tools/changelog.sh
 5@@ -52,10 +52,15 @@ function parse-commit {
 6   #   make a breaking change
 7 
 8   function commit:type {
 9-    local type="$(sed -E 's/^([a-zA-Z_\-]+)(\(.+\))?!?: .+$/\1/' <<< "$1")"
10+    local type
11+
12+    # Parse commit type from the subject
13+    if [[ "$1" =~ '^([a-zA-Z_\-]+)(\(.+\))?!?: .+$' ]]; then
14+      type="${match[1]}"
15+    fi
16 
17     # If $type doesn't appear in $TYPES array mark it as 'other'
18-    if [[ -n "${(k)TYPES[(i)$type]}" ]]; then
19+    if [[ -n "$type" && -n "${(k)TYPES[(i)$type]}" ]]; then
20       echo $type
21     else
22       echo other
23@@ -66,17 +71,18 @@ function parse-commit {
24     local scope
25 
26     # Try to find scope in "type(<scope>):" format
27-    scope=$(sed -nE 's/^[a-zA-Z_\-]+\((.+)\)!?: .+$/\1/p' <<< "$1")
28-    if [[ -n "$scope" ]]; then
29-      echo "$scope"
30+    if [[ "$1" =~ '^[a-zA-Z_\-]+\((.+)\)!?: .+$' ]]; then
31+      echo "${match[1]}"
32       return
33     fi
34 
35     # If no scope found, try to find it in "<scope>:" format
36-    # Make sure it's not a type before printing it
37-    scope=$(sed -nE 's/^([a-zA-Z_\-]+): .+$/\1/p' <<< "$1")
38-    if [[ -z "${(k)TYPES[(i)$scope]}" ]]; then
39-      echo "$scope"
40+    if [[ "$1" =~ '^([a-zA-Z_\-]+): .+$' ]]; then
41+      scope="${match[1]}"
42+      # Make sure it's not a type before printing it
43+      if [[ -z "${(k)TYPES[(i)$scope]}" ]]; then
44+        echo "$scope"
45+      fi
46     fi
47   }
48 
49@@ -84,7 +90,11 @@ function parse-commit {
50     # Only display the relevant part of the commit, i.e. if it has the format
51     # type[(scope)!]: subject, where the part between [] is optional, only
52     # displays subject. If it doesn't match the format, returns the whole string.
53-    sed -E 's/^[a-zA-Z_\-]+(\(.+\))?!?: (.+)$/\2/' <<< "$1"
54+    if [[ "$1" =~ '^[a-zA-Z_\-]+(\(.+\))?!?: (.+)$' ]]; then
55+      echo "${match[2]}"
56+    else
57+      echo "$1"
58+    fi
59   }
60 
61   # Return subject if the body or subject match the breaking change format