Last active
August 29, 2015 13:57
-
-
Save gdey/9499804 to your computer and use it in GitHub Desktop.
This is a quick shell script to generate an changelog entry.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This should be a perl/ruby/python/go program, | |
# but for now it's going to be in Bash. | |
DIRNAME=$( dirname $0 ) | |
## First some configuration | |
if [[ -z "$MASTER_BRANCH" ]]; then | |
MASTER_BRANCH=master | |
fi | |
if [[ -z "$CONTROL_FILE" ]]; then | |
CONTROL_FILE="$DIRNAME/control" | |
fi | |
if [[ -z "$CHANGELOG_FILE" ]]; then | |
CHANGELOG_FILE="$DIRNAME/changelog" | |
fi | |
#------------------------------------------------------------------------------- | |
# Script variables | |
#------------------------------------------------------------------------------- | |
touch $CHANGELOG_FILE | |
if [ -z "$PACKAGE_URGENCY" ]; then | |
PACKAGE_URGENCY='low' | |
fi | |
if [ -z "$PACKAGE_STABLE" ]; then | |
PACKAGE_STABLE='unstable' | |
fi | |
PACKAGE_NAME=$( | |
grep 'Package:' ${CONTROL_FILE} \ | |
| cut -d ':' -f 2 \ | |
| tr -d ' ' | |
) | |
VERSION_NUMBER=$( | |
grep ${PACKAGE_NAME} ${CHANGELOG_FILE} \ | |
| head -n 1 \ | |
| perl -pe '/\(([[:alnum:].-]+)\)/; $_=$1' | |
) | |
if [[ ! -z "${SNAPSHOT_NAME}" ]]; then | |
#echo "Snapshot Name provided: $SNAPSHOT_NAME" | |
IS_GOOD=$(perl -E "say(('${SNAPSHOT_NAME}' =~/[^[:alnum:].-]/g)?'NO':'YES')") | |
if [[ "NO" == "$IS_GOOD" ]]; then | |
cat <<ERR_MSG | |
SNAPSHOT_NAME (${SNAPSHOT_NAME}) can only be alpha-numeric '.' or '-'. | |
ERR_MSG | |
exit 1 | |
fi | |
fi | |
if [[ -z "${VERSION_NUMBER}" ]]; then | |
VERSION_NUMBER='0.0-0-1' | |
INITIAL_CHANGLOG=1 | |
fi | |
if [[ "$SHOW_VERSION" == "CURRENT" ]]; then | |
# We are just going to show the verison and then exit. | |
echo -n $VERSION_NUMBER | |
exit 0 | |
fi | |
if [[ -z "${PACKAGE_AUTHOR}" ]]; then | |
PACKAGE_AUTHOR='gautam dey <gautam@deyfamily.org>' | |
fi | |
MAJOR_INC='$1' | |
MINOR_INC='$2+1' | |
PATCH_INC='0' | |
DEBIAN_INC='1' | |
# If there is a major change, signified by a commit message with | |
# "[changelog][major]", we will bump up the correct value. | |
git log ${STARTING_SHA1}..HEAD -i --grep '\[changelog\]\[major\]' | |
if [[ $? == 0 ]] && [[ -z "$UPDATE_TYPE" ]]; then | |
UPDATE_TYPE="MAJOR" | |
fi | |
case $UPDATE_TYPE in | |
"MAJOR") | |
MAJOR_INC='$1+1' | |
MINOR_INC='0' | |
;; | |
"PATCH") | |
PATCH_INC='$3+1' | |
MINOR_INC='$2' | |
;; | |
"PACKAGE") | |
PATCH_INC='$3' | |
DEBIAN_INC='$5+1' | |
;; | |
"NONE") | |
MINOR_INC='$2' | |
PATCH_INC='$3' | |
DEBIAN_INC='$5' | |
;; | |
esac | |
DATE=$(date +'%a, %d %B %Y %T %z') | |
NEXT_VERSION=$(perl -e "'$VERSION_NUMBER' =~ /^\s*(\d+)\.([\d.]+)-(\d+)(-(\d+))?(.*)?\s*\$/;printf(\"%u.%03u-%u-%u%s\",${MAJOR_INC},${MINOR_INC},${PATCH_INC},${DEBIAN_INC},'${SNAPSHOT_NAME}')") | |
if [[ "$SHOW_VERSION" == "NEXT" ]]; then | |
# We are just going to show the verison and then exit. | |
echo -n $NEXT_VERSION | |
exit 0 | |
fi | |
if [[ "$VERSION_NUMBER" == "$NEXT_VERSION" ]]; then | |
# We are just going to output the old changelog. | |
cat ${CHANGELOG_FILE} | |
exit 0 | |
fi | |
CURRENT_SHA1=$(git log -n 1 HEAD | grep commit | cut -d ' ' -f 2) | |
if [[ -z "$STARTING_SHA1" ]]; then | |
STARTING_SHA1=$( | |
git log -n 1 ${MASTER_BRANCH} -- ${CHANGELOG_FILE} \ | |
| grep commit \ | |
| cut -d ' ' -f 2 | |
) | |
fi | |
LOG_BODY=$( | |
git log ${STARTING_SHA1}..HEAD -i --grep '\[changelog\]' \ | |
| grep -iE '\[changelog\]|Merge pull request' \ | |
| perl -pe 's/^\s*\[changelog\](.*)\s*$/ * $1\n/i' \ | |
| perl -pe 's/^\s+(Merge pull request .*)\s*$/ $1/' | |
) | |
if [[ -z "${MESSAGE}" ]]; then | |
LOG=$LOG_BODY | |
else | |
LOG=" $MESSAGE | |
$LOG_BODY" | |
fi | |
cat <<ENTRY > ${TMPDIR:-/tmp}/$$.new | |
${PACKAGE_NAME} (${NEXT_VERSION}) ${PACKAGE_STABLE}; urgency=${PACKAGE_URGENCY} | |
local package built: sha1 $CURRENT_SHA1 | |
since $STARTING_SHA1 | |
$LOG | |
-- ${PACKAGE_AUTHOR} ${DATE} | |
$CHANGELOG_OUT | |
ENTRY | |
#------------------------------------------------------------------------------- | |
cat ${TMPDIR:-/tmp}/$$.new $CHANGELOG_FILE | |
rm ${TMPDIR:-/tmp}/$$.new | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment