common.sh.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. # git-packaging-hooks - git hooks to semi-automate releases and distro packaging
  2. #
  3. # Copyright 2017 bill-auger <https://github.com/bill-auger/git-packaging-hooks/issues>
  4. #
  5. # git-packaging-hooks is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License version 3 as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # git-packaging-hooks is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License version 3
  16. # along with git-packaging-hooks. If not, see <http://www.gnu.org/licenses/>.
  17. # trace helpers
  18. function TraceStage() { local message=$* ; echo -e "HOOKS: $message" ; }
  19. function TraceStep() { local message=$* ; echo -e " -> $message" ; }
  20. function TraceError() { local message=$* ; echo -e "\033[0;31mERROR: $message\033[0m" ; }
  21. function Quit() { local message=$* ; TraceError $message ; exit 1 ; }
  22. # project-specific constants
  23. readonly PROJECT_DEFS_FILE=$(git config --local core.hooksPath)/project-defs.sh.inc
  24. source $PROJECT_DEFS_FILE
  25. [ ! -f $PROJECT_DEFS_FILE ] && Quit "missing $PROJECT_DEFS_FILE - aborting commit"
  26. [ -z "$GITHUB_LOGIN" -o -z "$OBS_LOGIN" -o -z "$UPSTREAM_NAME" -o \
  27. -z "$OBS_NAME" -o -z "$DEBIAN_NAME" -o -z "$REMOTE_NAME" -o \
  28. -z "$STAGING_BRANCH" -o -z "$PACKAGING_BRANCH" -o -z "$OBS_DIRNAME" -o \
  29. -z "$OSC_DIR" -o -z "$BUG_URL" -o -z "$N_MAKE_JOBS" ] && \
  30. Quit "$PROJECT_DEFS_FILE incomplete - aborting commit"
  31. [ $(echo $DEB_BUILD_TOOL | grep -E "^(debuild|sbuild|gbp)$") ] || \
  32. Quit "invalid \$DEB_BUILD_TOOL '$DEB_BUILD_TOOL' - aborting commit"
  33. function init()
  34. {
  35. readonly PROJECT_DIR=${PWD}
  36. readonly OBS_DIR=${PROJECT_DIR}/${OBS_DIRNAME}
  37. # files to modify
  38. readonly VERSION_FILE=configure.ac
  39. readonly SERVICE_FILE=$OBS_DIR/_service
  40. readonly SPEC_FILE=$OBS_DIR/$OBS_NAME.spec
  41. readonly DSC_FILE=$OBS_DIR/$OBS_NAME.dsc
  42. readonly PKGBUILD_FILE=$OBS_DIR/PKGBUILD
  43. # misc string constants
  44. readonly MINOR_VERSION_REGEX='^v[0-9]*\.[0-9]*$'
  45. readonly REVISION_REGEX='^v[0-9]*\.[0-9]*\.[0-9]*$'
  46. readonly GIT_USER=$(git config user.name )
  47. readonly GPG_KEY=$( git config user.signingkey)
  48. readonly CURL_ERROR_MSG="could not find \`curl\` executable"
  49. readonly BRANCHES_ERROR_MSG="\$STAGING_BRANCH and \$PACKAGING_BRANCH can not be the same - aborting commit"
  50. readonly GIT_USER_ERROR_MSG="git config user.name not set - aborting commit"
  51. readonly GPG_KEY_ERROR_MSG="git config user.signingkey not set - aborting commit"
  52. readonly GITHUB_TOKEN_ERROR_MSG="\$GITHUB_AUTH_TOKEN not set in environment"
  53. readonly OBS_DIR_ERROR_MSG="OBS_DIR does not exist '$OBS_DIR'"
  54. readonly OSC_DIR_ERROR_MSG="OSC_DIR does not exist '$OSC_DIR'"
  55. readonly VERSION_TAG_ERROR_MSG="could not locate a minor version tag of the form: vMAJOR.MINOR"
  56. readonly REV_TAG_ERROR_MSG="could not locate a revision tag of the form: vMAJOR.MINOR.REV"
  57. readonly TAGS_FILE=$(git config --local core.hooksPath)/RESTORE_TAGS
  58. readonly FAUX_DSC_MD5SUM='0123456789abcdef0123456789abcdef '
  59. readonly FAUX_DSC_SIZE='1234567'
  60. readonly GITHUB_API_URL=https://api.github.com/repos/${GITHUB_LOGIN}/${UPSTREAM_NAME}/releases
  61. local tarball_download_url=https://github.com/${GITHUB_LOGIN}/${UPSTREAM_NAME}/archive
  62. local assets_download_url=https://github.com/${GITHUB_LOGIN}/${UPSTREAM_NAME}/releases/download
  63. readonly RELEASEID_REGEX='s/^.*\"assets_url\": \"https:\/\/api.github.com\/repos\/'${GITHUB_LOGIN}'\/loopidity\/releases\/\(.*\)\/assets\",.*$/\1/'
  64. readonly UPLOADURL_REGEX='s/^.*\"upload_url\": \"\(.*\){?.*\",.*$/\1/'
  65. readonly CURL_RESP_ARGS='--silent'
  66. readonly CURL_FETCH_ARGS='--silent --location --remote-name'
  67. readonly CURL_STATUS_ARGS='--silent --output /dev/null --write-out %{http_code}'
  68. readonly DEBOOTSTRAP_REPO="http://httpredir.debian.org/debian main"
  69. readonly CHROOTBALL=${DEB_SBUILD_DIR}/${DEB_BUILD_DIST}-${DEB_BUILD_ARCH}.tar.gz
  70. readonly PIUPARTS_CHROOT=${DEB_PBUILDER_DIR}/base-${DEB_BUILD_DIST}-${DEB_BUILD_ARCH}.cow/
  71. # detect amend commit
  72. [ "$(ps --pid $PPID --format command= | grep '\-\-amend')" ] && readonly IS_AMEND_COMMIT=1 || \
  73. readonly IS_AMEND_COMMIT=0
  74. # differentiate pre/post commit stages
  75. [ "$*" == '.git/COMMIT_EDITMSG' ] && readonly IS_PRE_COMMIT_STAGE=1 || \
  76. readonly IS_PRE_COMMIT_STAGE=0
  77. # detect empty commit message
  78. readonly GIT_COMMIT_MSG_FILE=.git/COMMIT_EDITMSG
  79. [ "$(grep --invert-match "^\s*#" $GIT_COMMIT_MSG_FILE)" ] && IS_EMPTY_COMMIT_MSG=0 || \
  80. IS_EMPTY_COMMIT_MSG=1
  81. # detect commit to staging or packaging branch
  82. CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
  83. [ "$CURRENT_BRANCH" == "$STAGING_BRANCH" ] && readonly IS_STAGING_BRANCH=1 || \
  84. readonly IS_STAGING_BRANCH=0
  85. [ "$CURRENT_BRANCH" == "$PACKAGING_BRANCH" ] && readonly IS_PACKAGING_BRANCH=1 || \
  86. readonly IS_PACKAGING_BRANCH=0
  87. # get existing revision tags
  88. local ifs=$IFS ; IFS=' ' ;
  89. local branch_minor_tags=$(git tag --list --merged HEAD | grep $MINOR_VERSION_REGEX)
  90. local branch_rev_tags=$( git tag --list --merged HEAD | grep $REVISION_REGEX )
  91. local unmerged_rev_tags=$(git tag --list --no-merged master | grep $REVISION_REGEX )
  92. readonly MINOR_VERSION_TAG=$(echo $branch_minor_tags | sort --version-sort | tail --lines=1)
  93. readonly LAST_REVISION_TAG=$(echo $branch_rev_tags | sort --version-sort | tail --lines=1)
  94. readonly HIGHEST_REV_TAG=$( echo $unmerged_rev_tags | sort --version-sort | tail --lines=1)
  95. readonly REVISION_TAGS=$( echo $unmerged_rev_tags | sort --version-sort )
  96. IFS=$ifs
  97. # get refs
  98. readonly HEAD_REF="$( git rev-parse HEAD )"
  99. readonly MINOR_VERSION_REF="$(git rev-parse $MINOR_VERSION_TAG)"
  100. readonly LAST_REVISION_REF="$(git rev-parse $LAST_REVISION_TAG)"
  101. # compute or extract next revision string
  102. if (($IS_STAGING_BRANCH))
  103. then local n_revisions_ahead=$(git rev-list $MINOR_VERSION_TAG..HEAD --count)
  104. readonly SHOULD_INCREMENT_REVISION=$((! ($IS_AMEND_COMMIT + $IS_PRE_COMMIT_STAGE) ))
  105. (($SHOULD_INCREMENT_REVISION)) && readonly N_REVISIONS_AHEAD=$(($n_revisions_ahead + 1)) || \
  106. readonly N_REVISIONS_AHEAD=$n_revisions_ahead
  107. readonly VERSION_STRING=$MINOR_VERSION_TAG.$(printf '%03d' $N_REVISIONS_AHEAD)
  108. else readonly N_REVISIONS_AHEAD='n/a'
  109. readonly SHOULD_INCREMENT_REVISION='n/a'
  110. readonly VERSION_STRING=$LAST_REVISION_TAG
  111. fi
  112. readonly VERSION=${VERSION_STRING//v}
  113. # commit log messages
  114. readonly GIT_COMMIT_MSG="update packaging files to $VERSION_STRING"
  115. readonly OSC_COMMIT_MSG="$VERSION_STRING"
  116. # github "tag release" params
  117. readonly GITHUB_RELEASE_JSON="{ \"tag_name\": \"$VERSION_STRING\" ,
  118. \"target_commitish\": \"\" ,
  119. \"name\": \"$VERSION_STRING\" ,
  120. \"body\": \"\" ,
  121. \"draft\": false ,
  122. \"prerelease\": true }"
  123. # versioned filenames
  124. readonly TARBALL_FILENAME=$VERSION_STRING.tar.gz
  125. readonly TARBALL_FILE=../$TARBALL_FILENAME
  126. readonly OBS_TARBALL_PATH="\/$GITHUB_LOGIN\/$UPSTREAM_NAME\/archive\/$VERSION_STRING\.tar\.gz"
  127. readonly DEB_PREFIX=${DEBIAN_NAME}_${VERSION}
  128. readonly DEB_TARBALL_FILENAME=${DEB_PREFIX}.orig.tar.gz
  129. readonly DEB_DIFFBALL_FILENAME=${DEB_PREFIX}-1.diff.gz
  130. readonly TARBALL_INNER_DIR=${UPSTREAM_NAME}-${VERSION}
  131. readonly TARBALL_URL=${tarball_download_url}/${TARBALL_FILENAME}
  132. readonly TARBALL_SIG_URL=${assets_download_url}/${VERSION_STRING}/${TARBALL_FILENAME}.sig
  133. readonly PKGBUILD_URL=${assets_download_url}/${VERSION_STRING}/PKGBUILD
  134. readonly PKGBUILD_SIG_URL=${assets_download_url}/${VERSION_STRING}/PKGBUILD.sig
  135. # source $(git config --local core.hooksPath)/debug-constants.sh.inc # DEBUG
  136. # exit 1 # debug
  137. # sanity checks
  138. [ -z "$(which curl 2> /dev/null)" ] && Quit "$CURL_ERROR_MSG"
  139. [ "$STAGING_BRANCH" == "$PACKAGING_BRANCH" ] && Quit "$BRANCHES_ERROR_MSG"
  140. [ -z "$GIT_USER" ] && Quit "$GIT_USER_ERROR_MSG"
  141. [ -z "$GPG_KEY" ] && Quit "$GPG_KEY_ERROR_MSG"
  142. [ -z "$GITHUB_AUTH_TOKEN" ] && Quit "$GITHUB_TOKEN_ERROR_MSG"
  143. (($IS_PACKAGING_BRANCH)) && [ ! -d $OBS_DIR ] && Quit "$OBS_DIR_ERROR_MSG"
  144. (($IS_PACKAGING_BRANCH)) && [ ! -d $OSC_DIR ] && Quit "$OSC_DIR_ERROR_MSG"
  145. # validations
  146. if (($IS_STAGING_BRANCH))
  147. then [ -z "$MINOR_VERSION_TAG" ] && Quit $VERSION_TAG_ERROR_MSG
  148. elif (($IS_PACKAGING_BRANCH))
  149. then [ -z "$LAST_REVISION_TAG" ] && Quit $REV_TAG_ERROR_MSG
  150. else exit 0
  151. fi
  152. }
  153. init
  154. # files to export
  155. declare -ar DSC_FILES=( "$DSC_FILE" \
  156. "${DSC_FILE/.dsc/-Debian_8.0.dsc}" \
  157. "${DSC_FILE/.dsc/-xUbuntu_16.04.dsc}" )
  158. declare -ar TEMPLATE_FILES=( "$SERVICE_FILE" \
  159. "$SPEC_FILE" \
  160. ${DSC_FILES[@]} \
  161. "$PKGBUILD_FILE" )
  162. declare -ar DEBIAN_FILES=( "debian/source/format" \
  163. "debian/changelog" \
  164. "debian/compat" \
  165. "debian/control" \
  166. "debian/copyright" \
  167. "debian/rules" )
  168. declare -ar OBS_FILES=( ${TEMPLATE_FILES[@]} \
  169. "$OBS_DIR/debian.changelog" \
  170. "$OBS_DIR/debian.compat" \
  171. "$OBS_DIR/debian.control" \
  172. "$OBS_DIR/debian.copyright" \
  173. "$OBS_DIR/debian.rules" )
  174. declare -ar UPLOAD_FILES=( "$TARBALL_FILE.sig" \
  175. "$PKGBUILD_FILE" \
  176. "$PKGBUILD_FILE.sig" )
  177. declare -ar REMOTE_FILENAMES=( "$TARBALL_FILENAME" \
  178. "$TARBALL_FILENAME.sig" \
  179. "PKGBUILD" \
  180. "PKGBUILD.sig" )
  181. declare -ar CLEANUP_FILES=( "$TARBALL_FILE.sig" )