|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +read -r -d '' USAGE << EOUSAGE |
| 4 | +Mark a release for project, ensure commits in master are in debian changelog. |
| 5 | +If there are changes to release, update version and debian changelog, |
| 6 | +also can tag and push the release. |
| 7 | +If all commits are in changelog, won't change anything. |
| 8 | +The last line of output indicates if a release is marked or not. |
| 9 | +usage: $0 [options] |
| 10 | +options: |
| 11 | + -a automated mode (commit, tag, push) |
| 12 | + -c commit the changes for marking the release |
| 13 | + -h show this help and exit |
| 14 | + -p git push HEAD and tags |
| 15 | + -t tag the commit |
| 16 | + -v verbose output |
| 17 | +environment: |
| 18 | + AUTHOR commit author (name <email>) format |
| 19 | + DEBFULLNAME changelog writer name |
| 20 | + DEBEMAIL changelog writer email |
| 21 | + MESSAGE commit message |
| 22 | +EOUSAGE |
| 23 | + |
| 24 | +# ==== opts ==== |
| 25 | +COMMIT=false |
| 26 | +PUSH=false |
| 27 | +TAG=false |
| 28 | +VERBOSE=false |
| 29 | + |
| 30 | +while getopts 'achptv' flag; do |
| 31 | +case "$flag" in |
| 32 | + a) |
| 33 | + COMMIT=true |
| 34 | + TAG=true |
| 35 | + PUSH=true |
| 36 | + ;; |
| 37 | + c) |
| 38 | + COMMIT=true |
| 39 | + ;; |
| 40 | + h) |
| 41 | + echo "$USAGE" |
| 42 | + exit |
| 43 | + ;; |
| 44 | + p) |
| 45 | + PUSH=true |
| 46 | + ;; |
| 47 | + t) |
| 48 | + TAG=true |
| 49 | + ;; |
| 50 | + v) |
| 51 | + VERBOSE=true |
| 52 | + ;; |
| 53 | +esac |
| 54 | +done |
| 55 | + |
| 56 | + |
| 57 | +set -o errexit |
| 58 | + |
| 59 | +ROOT_DIR=$(realpath $(dirname $0)) |
| 60 | +CHANGELOG="$ROOT_DIR/debian/changelog" |
| 61 | +VERSION=$(date "+%Y%m%d.%H%M%S") |
| 62 | +LOG_USER="${SUDO_USER:-$USER}" |
| 63 | + |
| 64 | +# === options from env ==== |
| 65 | + |
| 66 | +# committer info |
| 67 | +AUTHOR="${AUTHOR:-}" |
| 68 | +MESSAGE="${MESSAGE}" |
| 69 | +if [[ -z "$MESSAGE" ]]; then |
| 70 | + MESSAGE="[automated] Marking Release $VERSION" |
| 71 | +fi |
| 72 | + |
| 73 | +# changelog writer info |
| 74 | +export DEBFULLNAME="${DEBFULLNAME:-Hypernode team}" |
| 75 | +export DEBEMAIL="${DEBMAIL:-hypernode@byte.nl}" |
| 76 | + |
| 77 | + |
| 78 | +log() { |
| 79 | + logger --tag 'hypernode-docs-next-mark-release' "[$$ @$LOG_USER] $1" |
| 80 | + if $VERBOSE; then |
| 81 | + echo "$1" |
| 82 | + fi |
| 83 | +} |
| 84 | + |
| 85 | +# deb_changelog_updated: returns 0 if there are changes to be released or 1 otherwise |
| 86 | +deb_changelog_updated() { |
| 87 | + log "checking if debian changelog needs update" |
| 88 | + # use gbp dch itself to see if there are any changes detected. |
| 89 | + # this way the logic of detecting changes is more consistent with |
| 90 | + # the rest of the system. |
| 91 | + gbp dch --debian-tag="%(version)s" --debian-branch=master |
| 92 | + # find changelog diff to see if there new commits in changelog. |
| 93 | + # commits are usually added like this to the changelog: |
| 94 | + # [ author ] |
| 95 | + # * commit message |
| 96 | + # first look for commit messages, if nothing found, then see if there is an empty author section, |
| 97 | + # as a fallback. |
| 98 | + local changed=$(git diff --text --ignore-all-space --unified=0 --no-color $CHANGELOG | grep --line-regexp --perl-regexp '\+\s{2,}\* .+') |
| 99 | + if [[ -z "$changed" ]]; then |
| 100 | + changed=$(git diff --text --ignore-all-space --unified=0 --no-color $CHANGELOG | grep --line-regexp --perl-regexp '\+\\s{2,}\[.+\].*') |
| 101 | + fi |
| 102 | + log "reverting possible changes in debian changelog during update detection" |
| 103 | + git checkout -- $CHANGELOG |
| 104 | + if [[ -n "$changed" ]]; then |
| 105 | + return 0 |
| 106 | + fi |
| 107 | + return 1 |
| 108 | +} |
| 109 | + |
| 110 | +mark_release() { |
| 111 | + log "marking release $VERSION ..." |
| 112 | + |
| 113 | + log "generating debian changelog" |
| 114 | + gbp dch --debian-tag="%(version)s" --new-version=$VERSION --debian-branch=master |
| 115 | + git add $CHANGELOG |
| 116 | +} |
| 117 | + |
| 118 | +commit() { |
| 119 | + log "comitting the release changes" |
| 120 | + if [[ -z "$AUTHOR" ]]; then |
| 121 | + git commit --no-edit --message="$MESSAGE" |
| 122 | + else |
| 123 | + git commit --no-edit --message="$MESSAGE" --author "$AUTHOR" |
| 124 | + fi |
| 125 | +} |
| 126 | + |
| 127 | + |
| 128 | +if ! deb_changelog_updated; then |
| 129 | + log "detected no change, skipping marking release!" |
| 130 | + echo 'no change' |
| 131 | + exit |
| 132 | +fi |
| 133 | + |
| 134 | +mark_release |
| 135 | + |
| 136 | +if $COMMIT; then |
| 137 | + commit |
| 138 | + if $TAG; then |
| 139 | + log "creating git tag '$VERSION' ..." |
| 140 | + git tag $VERSION |
| 141 | + fi |
| 142 | + |
| 143 | + if $PUSH; then |
| 144 | + log "pushing changes to origin ..." |
| 145 | + git push origin HEAD |
| 146 | + if $TAG; then |
| 147 | + git push origin $VERSION |
| 148 | + fi |
| 149 | + fi |
| 150 | +else |
| 151 | + log "not committing, skipping tagging and pushing!" |
| 152 | +fi |
| 153 | + |
| 154 | +echo "marked release $VERSION" |
0 commit comments