diff --git a/Commands.md b/Commands.md index 64e6791..c511de1 100644 --- a/Commands.md +++ b/Commands.md @@ -458,6 +458,13 @@ Release commit with the given <tag> and other options: $ git release 0.1.0 ``` +If you are using [semver](https://semver.org) in your project, you could also use the command below: +(Run `git help release` for more information) + +```bash +$ git release --semver major/minor/patch +``` + Does the following: - Executes _.git/hooks/pre-release.sh_ (if present), passing it the given tag and remain arguments diff --git a/bin/git-release b/bin/git-release index 623d2da..d39e71f 100755 --- a/bin/git-release +++ b/bin/git-release @@ -25,10 +25,7 @@ exit_with_msg() { } if test $# -gt 0; then - version=$1 - shift #remove version from arguments list remote='' - hook_args="$version" # check for flags while test $# != 0 @@ -37,12 +34,51 @@ if test $# -gt 0; then -c) need_changelog=true;; -r) remote=$2; shift ;; -m) msg=$2; shift ;; + --semver) semver=$2; shift ;; --) shift; hook_args="$hook_args $*"; break;; + *) test -z "$version" && version=$1 ;; esac + shift + done + + if [ -n "$semver" ]; then + if [ -z "$(git tag)" ]; then + echo "there is no tag in the git repo" 1>&2 + exit 1 + fi + + latest_tag=$(git describe --tags "$(git rev-list --tags --max-count=1)") + + if [[ ! "$latest_tag" =~ \ + ([0-9]|[1-9][0-9]+)\.([0-9]|[1-9][0-9]+)\.([0-9]|[1-9][0-9]+)(.*) ]]; then + echo "the latest tag doesn't match semver format requirement" 1>&2 + exit 1 + fi + + case "$semver" in + major ) version="${BASH_REMATCH[1]}" ;; + minor ) version="${BASH_REMATCH[2]}" ;; + patch ) version="${BASH_REMATCH[3]}" ;; + * ) echo "invalid semver argument given: $semver" 1>&2 + exit 1 + ;; + esac + + (( ++version )) + + case "$semver" in + major ) version="$version.${BASH_REMATCH[2]}.${BASH_REMATCH[3]}${BASH_REMATCH[4]}" ;; + minor ) version="${BASH_REMATCH[1]}.$version.${BASH_REMATCH[3]}${BASH_REMATCH[4]}" ;; + patch ) version="${BASH_REMATCH[1]}.${BASH_REMATCH[2]}.$version${BASH_REMATCH[4]}" ;; + esac + fi + + hook_args="$version" + if [ -z "$msg" ]; then - msg="Release ${version}" + msg="Release ${version}" fi # shellcheck disable=SC2086 @@ -50,7 +86,7 @@ if test $# -gt 0; then || exit_with_msg "pre-release hook failed! Cancelling release." echo "... releasing $version" if [ "$need_changelog" = true ]; then - git-changelog -t "$version" + git-changelog -t "$version" fi git commit -a -m "$msg" # shellcheck disable=SC2086 diff --git a/man/git-release.1 b/man/git-release.1 index b0f4c12..44a1fc6 100644 --- a/man/git-release.1 +++ b/man/git-release.1 @@ -1,13 +1,13 @@ .\" generated with Ronn/v0.7.3 .\" http://github.com/rtomayko/ronn/tree/0.7.3 . -.TH "GIT\-RELEASE" "1" "May 2017" "" "" +.TH "GIT\-RELEASE" "1" "December 2017" "" "" . .SH "NAME" \fBgit\-release\fR \- Commit, tag and push changes to the repository . .SH "SYNOPSIS" -\fBgit\-release\fR [\-r ] [\-m ] [\-c] [[\-\-] ] +\fBgit\-release\fR [ | \-\-semver ] [\-r ] [\-m ] [\-c] [[\-\-] ] . .SH "DESCRIPTION" Commits changes with message "Release " or custom commit information, tags with the given and pushes the branch / tags\. @@ -19,6 +19,12 @@ Optionally it generates a changelog (see git\-changelog) and a remote can be def If \fB\.git/hook/pre\-release\fR or \fB\.git/hook/post\-release\fR exist, they will be triggered with \fBtagname\fR and extra hook arguments before/after the release\. . .SH "OPTIONS" +\-\-semver +. +.P +If the latest tag in your repo matches the semver format requirement, you could increase part of it as the new release tag with this option\. The name must be one of the \fBmajor\fR, \fBminor\fR, \fBpatch\fR\. For example, assumed the latest tag is \fB4\.4\.0\fR, with \fBgit release \-\-semver minor\fR you will make a new release with tag \fB4\.5\.0\fR\. +. +.P . .P diff --git a/man/git-release.html b/man/git-release.html index 7323f48..5998704 100644 --- a/man/git-release.html +++ b/man/git-release.html @@ -76,7 +76,7 @@

SYNOPSIS

-

git-release <tagname> [-r <remote>] [-m <commit info>] [-c] [[--] <hook arguments...>]

+

git-release [<tagname> | --semver <name>] [-r <remote>] [-m <commit info>] [-c] [[--] <hook arguments...>]

DESCRIPTION

@@ -88,6 +88,12 @@

OPTIONS

+

--semver <name>

+ +

If the latest tag in your repo matches the semver format requirement, you could increase part of it as the new release tag + with this option. The name must be one of the major, minor, patch. For example, assumed the latest tag is 4.4.0, with + git release --semver minor you will make a new release with tag 4.5.0.

+

<tagname>

The name of the newly created tag. Also used in tag comment.

@@ -135,8 +141,8 @@ populate changelog, and push to specific remote.

AUTHOR

-

Written by Tj Holowaychuk <tj@vision-media.ca> -Extended by David Hartmann <dh@tsl.io>

+

Written by Tj Holowaychuk <tj@vision-media.ca> +Extended by David Hartmann <dh@tsl.io>

REPORTING BUGS

@@ -149,7 +155,7 @@ Extended by David Hartmann <