Skip to content

Instantly share code, notes, and snippets.

@rafasoares
Last active August 24, 2020 23:46
Show Gist options
  • Save rafasoares/ea1f8c4833a7a3b164497adcb783495a to your computer and use it in GitHub Desktop.
Save rafasoares/ea1f8c4833a7a3b164497adcb783495a to your computer and use it in GitHub Desktop.
Generates a changelog from Git commits since previous version (macOS)

Git Changelog ZSH/Oh My Zsh plugin (macOS)

This function generates a nicely formatted changelog, great for GitHub releases, from the commit summaries since the specified revision/tag.

Usage:

$ gchlg v1.0.0
## CHANGELOG

* [PROJ-1234] Adds new feature (#12) (a8e0465a)
* [DEV-456] Fixes a nasty bug (#11) (63e650ce)
* Bump lodash from 4.17.15 to 4.17.19 (#10) (31b6491d)

[PROJ-1234]: https://myorg.atlassian.net/browse/PROJ-1234
[DEV-456]: https://myorg.atlassian.net/browse/DEV-456


** Copied changelog to the clipboard **

If you then paste the copied text in a release's description, it should produce an unordered list of changes, with links to the specific commits, PRs and JIRA issues, if any.

It's designed to work with macOS's pbcopy utility and to play nicely with GitHub's "Squash and Merge" strategy for PRs. It doesn't work well with merge commits. Rebase merges should work, but will likely generate a verbose changelog.

To make it work in other OSes, change pbcopy for the appropriate "copy to the clipboard" utility (eg. 'xclip' on Linux).

Note: I'm being explicit about ZSH/OMZ here because it's where I tested the code. It should work on other shells, as long as there's support for the =~ regex capture syntax.

Feel free to modify this for your preferred shell. (I'd appreciate if posted the changes here, so other users might benefit)

Installation

  1. Create a folder named git-changelog inside your Oh My Zsh custom plugins folder (usually ~/.oh-my-zsh/custom/plugins)
  2. Copy git-changelog.plugin.zsh to that folder
  3. Add git-changelog to your plugins list in ~/.zshrc.
function extract-issues() {
text=$1
link='https://myorg.atlassian.net/browse'
while read item; do
[[ "$item" =~ '[A-Z]+-[0-9]+' ]] && echo "[$MATCH]: $link/$MATCH"
done <<< "$text"
}
function git-changelog() {
ref=$1
log=$( git log "$ref"..HEAD --pretty=format:'* %s (%h)' )
issues=$( extract-issues "$log" )
read -rd '' output <<OUTPUT
## CHANGELOG
$log
$issues
OUTPUT
pbcopy <<< "$output"
echo "$output"
echo "\n\n** Copied changelog to the clipboard **"
}
alias gchlg='git-changelog'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment