eeab4e5186961f9af591d0264d96f0e39d154886

Author
Marc Cornellà <marc.cornella@live.com>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

feat(updater): add changelog display by parsing the commit list

Diff

This diff is truncated to protect this page.

  1diff --git a/tools/changelog.sh b/tools/changelog.sh
  2new file mode 100755
  3index 0000000000000000000000000000000000000000..015b26c4b8fc67de24d6a0d06aa1e7f631233a50
  4--- /dev/null
  5+++ b/tools/changelog.sh
  6@@ -0,0 +1,418 @@
  7+#!/usr/bin/env zsh
  8+
  9+##############################
 10+# CHANGELOG SCRIPT CONSTANTS #
 11+##############################
 12+
 13+#* Holds the list of valid types recognized in a commit subject
 14+#* and the display string of such type
 15+local -A TYPES
 16+TYPES=(
 17+  [build]="Build system"
 18+  [chore]="Chore"
 19+  [ci]="CI"
 20+  [docs]="Documentation"
 21+  [feat]="Features"
 22+  [fix]="Bug fixes"
 23+  [perf]="Performance"
 24+  [refactor]="Refactor"
 25+  [style]="Style"
 26+  [test]="Testing"
 27+)
 28+
 29+#* Types that will be displayed in their own section,
 30+#* in the order specified here.
 31+local -a MAIN_TYPES
 32+MAIN_TYPES=(feat fix perf docs)
 33+
 34+#* Types that will be displayed under the category of other changes
 35+local -a OTHER_TYPES
 36+OTHER_TYPES=(refactor style other)
 37+
 38+#* Commit types that don't appear in $MAIN_TYPES nor $OTHER_TYPES
 39+#* will not be displayed and will simply be ignored.
 40+
 41+
 42+############################
 43+# COMMIT PARSING UTILITIES #
 44+############################
 45+
 46+function parse-commit {
 47+
 48+  # This function uses the following globals as output: commits (A),
 49+  # subjects (A), scopes (A) and breaking (A). All associative arrays (A)
 50+  # have $hash as the key.
 51+  # - commits holds the commit type
 52+  # - subjects holds the commit subject
 53+  # - scopes holds the scope of a commit
 54+  # - breaking holds the breaking change warning if a commit does
 55+  #   make a breaking change
 56+
 57+  function commit:type {
 58+    local type="$(sed -E 's/^([a-zA-Z_\-]+)(\(.+\))?!?: .+$/\1/' <<< "$1")"
 59+
 60+    # If $type doesn't appear in $TYPES array mark it as 'other'
 61+    if [[ -n "${(k)TYPES[(i)$type]}" ]]; then
 62+      echo $type
 63+    else
 64+      echo other
 65+    fi
 66+  }
 67+
 68+  function commit:scope {
 69+    local scope
 70+
 71+    # Try to find scope in "type(<scope>):" format
 72+    scope=$(sed -nE 's/^[a-zA-Z_\-]+\((.+)\)!?: .+$/\1/p' <<< "$1")
 73+    if [[ -n "$scope" ]]; then
 74+      echo "$scope"
 75+      return
 76+    fi
 77+
 78+    # If no scope found, try to find it in "<scope>:" format
 79+    # Make sure it's not a type before printing it
 80+    scope=$(sed -nE 's/^([a-zA-Z_\-]+): .+$/\1/p' <<< "$1")
 81+    if [[ -z "${(k)TYPES[(i)$scope]}" ]]; then
 82+      echo "$scope"
 83+    fi
 84+  }
 85+
 86+  function commit:subject {
 87+    # Only display the relevant part of the commit, i.e. if it has the format
 88+    # type[(scope)!]: subject, where the part between [] is optional, only
 89+    # displays subject. If it doesn't match the format, returns the whole string.
 90+    sed -E 's/^[a-zA-Z_\-]+(\(.+\))?!?: (.+)$/\2/' <<< "$1"
 91+  }
 92+
 93+  # Return subject if the body or subject match the breaking change format
 94+  function commit:is-breaking {
 95+    local subject="$1" body="$2"
 96+
 97+    if [[ "$body" =~ "BREAKING CHANGE: (.*)" || \
 98+      "$subject" =~ '^[^ :\)]+\)?!: (.*)$' ]]; then
 99+      echo "${match[1]}"
100+    else
101+      return 1
102+    fi
103+  }
104+
105+  # Return truncated hash of the reverted commit
106diff --git a/tools/upgrade.sh b/tools/upgrade.sh
107index 634d5c03d5ab27cab5b204d6966c79509d648bd9..5d593b6c646ae4e7228b80d32a7846e635247784 100755
108--- a/tools/upgrade.sh
109+++ b/tools/upgrade.sh
110@@ -64,6 +64,13 @@ if git pull --rebase --stat origin master; then
111     ret=80 # non-zero exit code to indicate no changes pulled
112   else
113     message="Hooray! Oh My Zsh has been updated!"
114+
115+    # Display changelog with less if available, otherwise just print it to the terminal
116+    if (( $+commands[less] )); then
117+      command less -R <("$ZSH/tools/changelog.sh" HEAD "$last_commit")
118+    else
119+      "$ZSH/tools/changelog.sh" HEAD "$last_commit"
120+    fi
121   fi
122 
123   printf '%s         %s__      %s           %s        %s       %s     %s__   %s\n' $RAINBOW $RESET