diff --git a/docs/commands.md b/docs/commands.md index 994c581..1669b15 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -266,9 +266,10 @@ usage: git elegant release-work [tag name] Annotates the latest commit of `master` branch with a given tag and publishes it. The tag's message will be prepopulated using commits subjects (from oldest -to newest) between the last available tag and HEAD. The release notes will be -either copied to clipboard (if `pbcopy` or `xclip` is available) or printed -to standard output using `git elegant show-release-notes`. +to newest) between the last available tag and HEAD. If there are no tags, then +all commits will be used. The release notes will be either copied to clipboard +(if `pbcopy` or `xclip` is available) or printed to standard output using +`git elegant show-release-notes`. The command uses branch and stash pipes to preserve the current Git state prior to execution and restore after. diff --git a/libexec/git-elegant-accept-work b/libexec/git-elegant-accept-work index 129e235..b205a5f 100644 --- a/libexec/git-elegant-accept-work +++ b/libexec/git-elegant-accept-work @@ -45,7 +45,7 @@ MESSAGE --accept-work-logic() { local WORK_BRANCH="__eg" - source ${BINS}/plugins/rebase + source ${BINS}/plugins/state if is-there-active-rebase; then local rb=$(rebasing-branch) if [[ ${WORK_BRANCH} == ${rb} ]]; then diff --git a/libexec/git-elegant-deliver-work b/libexec/git-elegant-deliver-work index 4b8eb61..34d69e7 100644 --- a/libexec/git-elegant-deliver-work +++ b/libexec/git-elegant-deliver-work @@ -51,7 +51,7 @@ MESSAGE } --deliver-work-logic() { - source ${BINS}/plugins/rebase + source ${BINS}/plugins/state if is-there-active-rebase; then git-verbose rebase --continue else diff --git a/libexec/git-elegant-polish-work b/libexec/git-elegant-polish-work index 2bfbf46..ad47aa6 100644 --- a/libexec/git-elegant-polish-work +++ b/libexec/git-elegant-polish-work @@ -42,7 +42,7 @@ default() { error-box "'master' branch history can't be rewritten. Please read more on ${__site}" exit 42 fi - source ${BINS}/plugins/rebase + source ${BINS}/plugins/state if is-there-active-rebase; then git-verbose rebase --continue else diff --git a/libexec/git-elegant-release-work b/libexec/git-elegant-release-work index 417e5fe..9e28976 100755 --- a/libexec/git-elegant-release-work +++ b/libexec/git-elegant-release-work @@ -16,9 +16,10 @@ command-description() { cat<> ${message} echo "" >> ${message} - git log ${last_tag}...@ --pretty=format:'- %s' --reverse >> ${message} + local diapason=${last_tag}...@ + if [[ -z ${last_tag} ]]; then + diapason=@ + last_tag=all-commits + fi + git log ${diapason} --pretty=format:'- %s' --reverse >> ${message} echo "" >> ${message} echo "" >> ${message} git-verbose tag --annotate --file ${message} --edit ${new_tag} remove-file ${message} git-verbose push --tags - git-verbose-op --copy-notes-if-possible elegant show-release-notes smart ${last_tag} ${new_tag} + git-verbose-op --copy-notes-if-possible elegant show-release-notes smart ${last_tag} @ } default() { diff --git a/libexec/git-elegant-show-release-notes b/libexec/git-elegant-show-release-notes index 67b1fc8..464e9e0 100755 --- a/libexec/git-elegant-show-release-notes +++ b/libexec/git-elegant-show-release-notes @@ -37,8 +37,7 @@ MESSAGE } --github-release-notes() { - local first="${1}" - local second="${2}" + local diapason="${1}" local changelog="github-release-notes" echo "

Release notes

" > ${changelog} local url=$(git remote get-url origin) @@ -47,7 +46,7 @@ MESSAGE [[ ${url} =~ "github.com:" ]] && repository=${url##*github.com:} repository=${repository%%.git} - for hash in $(git log ${first}...${second} --format=%H --reverse); do + for hash in $(git log ${diapason} --format=%H --reverse); do local issues=$(git show -s --pretty=%B ${hash} | grep "#") [[ ! -z ${issues} ]] && issues=" [${issues}]" local subject=$(git show -s --pretty=%s ${hash}) @@ -58,11 +57,10 @@ MESSAGE } --simple-release-notes() { - local first="${1}" - local second="${2}" + local diapason="${1}" local changelog="simple-release-notes" echo "Release notes" > ${changelog} - for hash in $(git log ${first}...${second} --format=%H --reverse); do + for hash in $(git log ${diapason} --format=%H --reverse); do echo "- $(git show -s --pretty=%s ${hash})" >> ${changelog} done cat ${changelog} @@ -70,28 +68,23 @@ MESSAGE } default() { - local layout="${1}" - local first="${2}" - local second="${3}" - if [[ -z "${layout}" ]]; then - layout=simple + source ${BINS}/plugins/state + local layout=${1:-simple} + local first=${2:-$(last-tag)} + local second=${3:-@} + local diapason=${first}...${second} + if [[ ${first} == all-commits ]]; then + diapason=${second} fi - if [[ -z "${first}" ]]; then - first=$(git for-each-ref --sort "-version:refname" --format "%(refname:short)" refs/tags --count 1) - fi - if [[ -z "${second}" ]]; then - second=HEAD - fi - case ${layout} in simple) - --simple-release-notes ${first} ${second} + --simple-release-notes ${diapason} ;; smart) if [[ "$(git remote get-url origin)" =~ ((https://)|(git@))github.com ]]; then - --github-release-notes ${first} ${second} + --github-release-notes ${diapason} else - --simple-release-notes ${first} ${second} + --simple-release-notes ${diapason} fi ;; *) error-text "A layout can be 'simple' or 'smart'! '${layout}' layout is not supported." diff --git a/libexec/plugins/rebase b/libexec/plugins/state similarity index 73% rename from libexec/plugins/rebase rename to libexec/plugins/state index 886d4e8..1c218f3 100644 --- a/libexec/plugins/rebase +++ b/libexec/plugins/state @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# The plugin that wraps some useful operations around `git rebase`. +# The plugin that wraps the useful operations that describes a state of the repository. is-there-active-rebase(){ # Says if there is a rebase in progress. @@ -23,3 +23,8 @@ rebasing-branch() { fi done } + +last-tag() { + # Seeks for the last created tag. + git for-each-ref --sort "-version:refname" --format "%(refname:short)" refs/tags --count 1 +} diff --git a/tests/git-elegant-release-work.bats b/tests/git-elegant-release-work.bats index 5be32db..d01b8cb 100644 --- a/tests/git-elegant-release-work.bats +++ b/tests/git-elegant-release-work.bats @@ -23,7 +23,6 @@ setup() { fake-pass "git push --tags" fake-pass "git tag --annotate --file tag-message --edit ${new_tag}" fake-pass "git remote get-url origin" "https://fake-repo.git" - read-answer ${new_tag} } teardown() { @@ -39,6 +38,7 @@ teardown() { } @test "'release-work': release work when a new tag is provided via question" { + read-answer ${new_tag} check git-elegant release-work [[ "${status}" -eq 0 ]] [[ "${lines[@]}" =~ "Release notes" ]] @@ -46,7 +46,7 @@ teardown() { @test "'release-work': working branch is restored when the command runs in non-master branch" { repo "git checkout -b new" - check git-elegant release-work + check git-elegant release-work ${new_tag} [[ ${status} -eq 0 ]] [[ ${lines[@]} =~ "git checkout new" ]] } @@ -60,3 +60,11 @@ teardown() { [[ ${status} -eq 0 ]] [[ ${lines[@]} =~ "git checkout new" ]] } + +@test "'release-work': creates an annotated tag if there are no other tags" { + repo "git tag | xargs git tag -d" + check git-elegant release-work ${new_tag} + [[ "${status}" -eq 0 ]] + [[ "${lines[@]}" =~ "Release notes" ]] + [[ "${lines[@]}" =~ "- Add file" ]] +}