| 1 |
#! /usr/bin/env bash |
| 2 |
# |
| 3 |
# Assembles a draft CHANGES entry out of revisions committed since the last |
| 4 |
# entry was added. The entry is prepended to the current CHANGES file, and the |
| 5 |
# user then gets a chance to further edit it in the editor before it gets |
| 6 |
# committed. |
| 7 |
# |
| 8 |
# The script also maintains and updates a VERSION file. |
| 9 |
# |
| 10 |
# If the script finds a file called .update-changes.cfg it sources it at the |
| 11 |
# beginning. That script can define a function "new_version_hook" that will be |
| 12 |
# called with the new version number. It may use any of the replace_version_* |
| 13 |
# functions defined below to update other files as necessary. |
| 14 |
# |
| 15 |
# If $1 is given, it's interpreted as a release version and a corresponding |
| 16 |
# tag is created. |
| 17 |
|
| 18 |
file_changes="CHANGES" # The CHANGES file. |
| 19 |
file_version="VERSION" # The VERSION file. |
| 20 |
file_config=".update-changes.cfg" # This will be sourced if available. |
| 21 |
new_version_hook="new_version_hook" # Function that will be called with new version number. |
| 22 |
release_tag="release" # We mark the current release with this tag. |
| 23 |
beta_tag="beta" # We mark the current beta with this tag. |
| 24 |
new_commit_msg="Updating CHANGES and VERSION." # Commit message when creating a new commit. |
| 25 |
show_authors=1 # Include author names with commit. |
| 26 |
|
| 27 |
# The command line used to generate a revision's version string. |
| 28 |
git_describe="git describe --always" # {rev} will be added. |
| 29 |
|
| 30 |
# The command line used to generate a revision's date. The revision will be |
| 31 |
# appended. Not used with Bro-style CHANGES file. |
| 32 |
git_rev_date="git show -s --pretty=tformat:%ci" |
| 33 |
|
| 34 |
# The command line used to generate the list of revisions between old and new |
| 35 |
# state. |
| 36 |
git_rev_list="git rev-list --topo-order HEAD" # ^{past-rev} will be added. |
| 37 |
|
| 38 |
# The command line used to show the one-line summary of a revision before |
| 39 |
# editing. |
| 40 |
git_rev_summary="git show -s '--pretty=tformat: %h | %aN | %s'" # {rev} will be added. |
| 41 |
|
| 42 |
# The command line used to get a revision's author. |
| 43 |
git_author="git show -s --pretty=format:%aN" # {rev} will be added. |
| 44 |
git_author_email="git show -s --pretty=format:%aE" # {rev} will be added. |
| 45 |
|
| 46 |
# The command line used to get a revision's message. |
| 47 |
git_msg="git show -s --pretty=format:%B" # {rev} will be added. |
| 48 |
|
| 49 |
function usage |
| 50 |
{ |
| 51 |
echo "usage: `basename $0` [options]" |
| 52 |
echo |
| 53 |
echo " -p <rev> Explicitly name the past revision to compare with." |
| 54 |
echo " -R <tag> Tag the current revision as a release. Use VERSION to use that." |
| 55 |
echo " -B <tag> Tag the current revision as a beta release. Use VERSION to use that." |
| 56 |
echo " -I Initialize a new, initially empty CHANGES file." |
| 57 |
echo " -c Check whether CHANGES is up to date." |
| 58 |
echo |
| 59 |
exit 1 |
| 60 |
} |
| 61 |
|
| 62 |
### Functions that can be used to replace version strings in other files. |
| 63 |
### To use them, create a file $file_config and define a function |
| 64 |
### "new_version_hook" in there that does whatever is necessary, like calling |
| 65 |
### any of these. |
| 66 |
|
| 67 |
# Function that looks for lines of the form 'VERSION="1.2.3"' in $1. It will |
| 68 |
# replace the version number with $2 and then git-adds the change. |
| 69 |
function replace_version_in_script |
| 70 |
{ |
| 71 |
file=$1 |
| 72 |
version=$2 |
| 73 |
|
| 74 |
cat $file | sed "s#^\\( *VERSION *= *\\)\"\\([0-9.-]\\{1,\\}\\)\"#\1\"$version\"#g" >$file.tmp |
| 75 |
cat $file.tmp >$file |
| 76 |
rm -f $file.tmp |
| 77 |
git add $file |
| 78 |
} |
| 79 |
|
| 80 |
# Function that looks for lines of the form '.. |version| replace:: 0.3' in $1. |
| 81 |
# It will replace the version number with $2 and then git-adds the change. |
| 82 |
function replace_version_in_rst |
| 83 |
{ |
| 84 |
file=$1 |
| 85 |
version=$2 |
| 86 |
|
| 87 |
cat $file | sed "s#^\\( *\.\. *|version| *replace:: *\\)\\([0-9a-zA-Z.-]\\{1,\\}\\)#\1$version#g" >$file.tmp |
| 88 |
cat $file.tmp >$file |
| 89 |
rm -f $file.tmp |
| 90 |
git add $file |
| 91 |
} |
| 92 |
|
| 93 |
# Function that looks for lines of the form 'version="1.2-3"' in $1. It will |
| 94 |
# replace the version number with $2 and then git-adds the change. |
| 95 |
function replace_version_in_setup_py |
| 96 |
{ |
| 97 |
file=$1 |
| 98 |
version=$2 |
| 99 |
|
| 100 |
cat $file | sed "s#\\([[:blank:]]*version[[:blank:]]*=[[:blank:]]*\\)\"\\([0-9.-]\\{1,\}\\)\"#\\1\"$version\"#g" >$file.tmp |
| 101 |
cat $file.tmp >$file |
| 102 |
rm -f $file.tmp |
| 103 |
git add $file |
| 104 |
} |
| 105 |
|
| 106 |
# Function that looks for lines of the form "#define .*VERSION "0.3"", with the |
| 107 |
# number being "version * 100". It will replace the version with $2 and then |
| 108 |
# git-adds the change. |
| 109 |
function replace_version_in_c_header |
| 110 |
{ |
| 111 |
file=$1 |
| 112 |
version=$2 |
| 113 |
|
| 114 |
cat $file | sed "s#\\([[:blank:]]*\\#define[[:blank:]]*[_A-Za-z0-9]*_VERSION[[:blank:]]*\\)\"[0-9.-]\\{1,\\}\"#\\1\"$version\"#g" >$file.tmp |
| 115 |
mv $file.tmp $file |
| 116 |
git add $file |
| 117 |
} |
| 118 |
|
| 119 |
### |
| 120 |
|
| 121 |
function version |
| 122 |
{ |
| 123 |
rev=$1 |
| 124 |
$git_describe $rev --match "v*" 2>/dev/null | sed 's/^v//g' | sed 's/-g.*//g' |
| 125 |
} |
| 126 |
|
| 127 |
function start_changes_entry |
| 128 |
{ |
| 129 |
version=$1 |
| 130 |
dst=$2 |
| 131 |
|
| 132 |
if [ "$bro_style" == "0" ]; then |
| 133 |
date=`$git_rev_date HEAD` |
| 134 |
printf '%s | %s\n' "$version" "$date" >>$dst |
| 135 |
else |
| 136 |
date=`date` |
| 137 |
printf '%s %s\n' "$version" "$date" >>$dst |
| 138 |
fi |
| 139 |
} |
| 140 |
|
| 141 |
function add_to_changes_entry |
| 142 |
{ |
| 143 |
rev=$1 |
| 144 |
dst=$2 |
| 145 |
msg=$3 |
| 146 |
|
| 147 |
author="" |
| 148 |
|
| 149 |
if [ "$msg" == "" ]; then |
| 150 |
author_email=`$git_author_email $rev` |
| 151 |
author=`$git_author $rev` |
| 152 |
|
| 153 |
if [[ "$author_email" == *@corelight.com ]]; then |
| 154 |
author="$author, Corelight" |
| 155 |
else |
| 156 |
author="$author" |
| 157 |
fi |
| 158 |
|
| 159 |
msg=`$git_msg $rev` |
| 160 |
fi |
| 161 |
|
| 162 |
if [ "$msg" == "" ]; then |
| 163 |
return 1 |
| 164 |
fi |
| 165 |
|
| 166 |
if echo $msg | grep -q "^$new_commit_msg\$"; then |
| 167 |
# Ignore our own automated commits. |
| 168 |
return 1; |
| 169 |
fi |
| 170 |
|
| 171 |
echo >>$dst |
| 172 |
|
| 173 |
if [ "$bro_style" == "0" ]; then |
| 174 |
bullet=" *" |
| 175 |
else |
| 176 |
bullet="-" |
| 177 |
fi |
| 178 |
|
| 179 |
( echo -n "$msg"; test "$author" != "" && test "$show_authors" == "1" && printf " (%s)" "$author" ) \ |
| 180 |
| awk -v bullet="$bullet" 'NR==1{printf "%s %s\n", bullet, $0; next }{printf " %s\n", $0}' \ |
| 181 |
| sed 's/[[:blank:]]*$//' >>$dst |
| 182 |
|
| 183 |
return 0; |
| 184 |
} |
| 185 |
|
| 186 |
function init_changes |
| 187 |
{ |
| 188 |
echo >>$file_changes |
| 189 |
start_changes_entry `version HEAD` $file_changes |
| 190 |
echo >>$file_changes |
| 191 |
echo " * Starting $file_changes." >>$file_changes |
| 192 |
} |
| 193 |
|
| 194 |
function get_last_rev |
| 195 |
{ |
| 196 |
version=`cat $file_changes | egrep '^[0-9a-zA-Z.-]+ *\|' | head -1 | awk '{print $1}'` |
| 197 |
|
| 198 |
if echo $version | grep -q -- '-'; then |
| 199 |
# v1.0.4-14 |
| 200 |
# Find the revision with that number. |
| 201 |
for rev in `git rev-list HEAD`; do |
| 202 |
v=`version $rev` |
| 203 |
|
| 204 |
if [ "$v" == "$version" ]; then |
| 205 |
echo $rev |
| 206 |
return |
| 207 |
fi |
| 208 |
done |
| 209 |
|
| 210 |
echo "Cannot determine revision for version $version." >/dev/stderr |
| 211 |
exit 1 |
| 212 |
|
| 213 |
else |
| 214 |
# A tag. |
| 215 |
echo "v$version" |
| 216 |
fi |
| 217 |
} |
| 218 |
|
| 219 |
function check_release_tag |
| 220 |
{ |
| 221 |
if [ "$release" != "" ]; then |
| 222 |
git tag -d $release_tag 2>/dev/null |
| 223 |
git tag -a $release_tag -m "Current stable release." |
| 224 |
sleep 2 # Make sure git describe picks the next one. |
| 225 |
git tag -d $release 2>/dev/null |
| 226 |
git tag -a $release -m "Version tag" |
| 227 |
echo "Tagged with new tag $release and moved tag 'release' to here." |
| 228 |
echo |
| 229 |
echo "IMPORTANT: Don't forget to 'git push --tags'." |
| 230 |
fi |
| 231 |
} |
| 232 |
|
| 233 |
function check_beta_tag |
| 234 |
{ |
| 235 |
if [ "$beta" != "" ]; then |
| 236 |
git tag -d $beta_tag 2>/dev/null |
| 237 |
git tag -a $beta_tag -m "Current stable beta." |
| 238 |
sleep 2 # Make sure git describe picks the next one. |
| 239 |
git tag -d $beta 2>/dev/null |
| 240 |
git tag -a $beta -m "Beta version tag" |
| 241 |
echo "Tagged with new tag $beta and moved tag 'beta' to here." |
| 242 |
echo |
| 243 |
echo "IMPORTANT: Don't forget to 'git push --tags'." |
| 244 |
fi |
| 245 |
} |
| 246 |
|
| 247 |
function check_submodules |
| 248 |
{ |
| 249 |
if git submodule status --recursive | grep ^+; then |
| 250 |
cat <<EOF |
| 251 |
|
| 252 |
The revision recorded for the module(s) above does not |
| 253 |
match the one currently checked out in the respective |
| 254 |
subdirs. |
| 255 |
|
| 256 |
Please either update or checkout the recorded revision(s). |
| 257 |
|
| 258 |
Aborting. |
| 259 |
EOF |
| 260 |
|
| 261 |
exit 1 |
| 262 |
fi |
| 263 |
} |
| 264 |
|
| 265 |
###### |
| 266 |
|
| 267 |
last_rev="" |
| 268 |
release="" |
| 269 |
beta="" |
| 270 |
init=0 |
| 271 |
check=0 |
| 272 |
quiet=0 |
| 273 |
|
| 274 |
while getopts "hp:R:B:Ic" opt; do |
| 275 |
case "$opt" in |
| 276 |
p) last_rev="$OPTARG";; |
| 277 |
R) release="$OPTARG";; |
| 278 |
B) beta="$OPTARG";; |
| 279 |
I) init=1;; |
| 280 |
c) check=1; quiet=1;; |
| 281 |
*) usage;; |
| 282 |
esac |
| 283 |
done |
| 284 |
|
| 285 |
if [ -e $file_config ]; then |
| 286 |
if [ "$quiet" != "1" ]; then |
| 287 |
echo Reading $file_config ... |
| 288 |
fi |
| 289 |
source ./$file_config |
| 290 |
fi |
| 291 |
|
| 292 |
|
| 293 |
if [ "$release" != "" -a "$beta" != "" ]; then |
| 294 |
echo "Cannot tag as both beta and release." |
| 295 |
exit 1 |
| 296 |
fi |
| 297 |
|
| 298 |
if [ "$release" == "VERSION" ]; then |
| 299 |
release="v`cat VERSION`" |
| 300 |
fi |
| 301 |
|
| 302 |
if [ "$beta" == "VERSION" ]; then |
| 303 |
beta="v`cat VERSION`" |
| 304 |
fi |
| 305 |
|
| 306 |
bro_style=0 # If 1, we use a slightly different format. |
| 307 |
|
| 308 |
if [ "$init" != "0" ]; then |
| 309 |
if [ -e $file_changes ]; then |
| 310 |
echo "$file_changes already exists, remove it first." |
| 311 |
exit 1 |
| 312 |
else |
| 313 |
echo "Initializing $file_changes ..." |
| 314 |
init_changes |
| 315 |
exit 0 |
| 316 |
fi |
| 317 |
else |
| 318 |
if [ ! -e $file_changes ]; then |
| 319 |
echo "$file_changes does not exist, initialize it with '-I'." |
| 320 |
exit 1 |
| 321 |
else |
| 322 |
# If we find this marker, it's Bro-style CHANGES file. |
| 323 |
grep -vq -- '-+-+-+-+-+-+-+-+-+-' $file_changes; |
| 324 |
bro_style=$? |
| 325 |
fi |
| 326 |
fi |
| 327 |
|
| 328 |
if [ "$release" != "" ]; then |
| 329 |
if ! echo $release | egrep -q '^v[0-9]+\.[0-9]+'; then |
| 330 |
echo "Release tag must be of the form vX.Y[.Z]" |
| 331 |
exit 1 |
| 332 |
fi |
| 333 |
|
| 334 |
check_submodules |
| 335 |
fi |
| 336 |
|
| 337 |
if [ "$beta" != "" ]; then |
| 338 |
if ! echo $beta | egrep -q '^v[0-9]+\.[0-9]+(\.[0-9]+)?-beta'; then |
| 339 |
echo "Release tag must be of the form vX.Y[.Z]-beta*" |
| 340 |
exit 1 |
| 341 |
fi |
| 342 |
|
| 343 |
check_submodules |
| 344 |
fi |
| 345 |
|
| 346 |
if [ "$last_rev" == "" ]; then |
| 347 |
last_rev=`get_last_rev` |
| 348 |
fi |
| 349 |
|
| 350 |
if [ "$last_rev" == "" ]; then |
| 351 |
echo 'Cannot determine previous revision to compare with, specify with "-p <rev>".' |
| 352 |
exit 1 |
| 353 |
fi |
| 354 |
|
| 355 |
auto_version=`version HEAD` |
| 356 |
|
| 357 |
if [ "$auto_version" == "" ]; then |
| 358 |
echo "Cannot determine version for HEAD did not return anything." |
| 359 |
exit 1 |
| 360 |
fi |
| 361 |
|
| 362 |
tmp=${file_changes}.$$.tmp |
| 363 |
trap "rm -f $tmp" EXIT |
| 364 |
rm -f $tmp |
| 365 |
|
| 366 |
found=0 |
| 367 |
|
| 368 |
echo >>$tmp |
| 369 |
|
| 370 |
new_version=$auto_version |
| 371 |
version=`version $rev` |
| 372 |
|
| 373 |
if [ "$version" == "" ]; then |
| 374 |
echo "Cannot determine version for $rev." |
| 375 |
exit 1 |
| 376 |
fi |
| 377 |
|
| 378 |
if [ "$release" != "" ]; then |
| 379 |
new_version=`echo $release | sed 's/v//g'` |
| 380 |
fi |
| 381 |
|
| 382 |
if [ "$beta" != "" ]; then |
| 383 |
new_version=`echo $beta | sed 's/v//g'` |
| 384 |
fi |
| 385 |
|
| 386 |
if [ "$quiet" != "1" ]; then |
| 387 |
echo "New version is $new_version." |
| 388 |
echo "Listing revisions commited since `version $last_rev` ($last_rev) ... " |
| 389 |
echo |
| 390 |
fi |
| 391 |
|
| 392 |
start_changes_entry $new_version $tmp |
| 393 |
|
| 394 |
for rev in `$git_rev_list ^$last_rev`; do |
| 395 |
|
| 396 |
version=`version $rev` |
| 397 |
|
| 398 |
if [ "$version" == "" ]; then |
| 399 |
version="<no-version>" |
| 400 |
fi |
| 401 |
|
| 402 |
# printf "%15s |" $version |
| 403 |
|
| 404 |
if add_to_changes_entry $rev $tmp; then |
| 405 |
found=1 |
| 406 |
|
| 407 |
if [ "$quiet" != "1" ]; then |
| 408 |
eval "$git_rev_summary $rev | grep -v '^$' | cat" |
| 409 |
fi |
| 410 |
fi |
| 411 |
|
| 412 |
done |
| 413 |
|
| 414 |
if [ "$found" == "0" ]; then |
| 415 |
if [ "$check" == "1" ]; then |
| 416 |
echo "CHANGES is up to date." |
| 417 |
exit 0 |
| 418 |
fi |
| 419 |
|
| 420 |
echo " None." |
| 421 |
echo |
| 422 |
|
| 423 |
if [ "$release" != "" -o "$beta" != "" ]; then |
| 424 |
add_to_changes_entry head $tmp "Release $new_version." |
| 425 |
else |
| 426 |
exit 0 |
| 427 |
fi |
| 428 |
fi |
| 429 |
|
| 430 |
if [ "$check" == "1" ]; then |
| 431 |
echo "CHANGES is NOT to date." |
| 432 |
exit 0 |
| 433 |
fi |
| 434 |
|
| 435 |
echo >>$tmp |
| 436 |
|
| 437 |
cat $file_changes >>$tmp |
| 438 |
|
| 439 |
# If we are ahead of origin, we can amend. If not, we need to create a new |
| 440 |
# commit even if the user wants otherwise. |
| 441 |
amend=0 |
| 442 |
if git remote | grep -q origin; then |
| 443 |
if git rev-list origin..HEAD | grep -q .; then |
| 444 |
amend=1 |
| 445 |
fi |
| 446 |
fi |
| 447 |
|
| 448 |
echo |
| 449 |
|
| 450 |
if [ "$amend" == "0" ]; then |
| 451 |
echo Update to $file_changes will become a new commit. |
| 452 |
else |
| 453 |
echo Update to $file_changes will be amended to last commit. |
| 454 |
fi |
| 455 |
|
| 456 |
|
| 457 |
echo |
| 458 |
echo Type Enter to edit new $file_changes, or CTRL-C to abort without any modifications. |
| 459 |
read |
| 460 |
|
| 461 |
# Run editor. |
| 462 |
if [ -z "$EDITOR" ]; then |
| 463 |
EDITOR=vi |
| 464 |
fi |
| 465 |
eval $EDITOR $tmp |
| 466 |
|
| 467 |
# Put changes in place. |
| 468 |
mv $tmp $file_changes |
| 469 |
echo "Updated $file_changes." |
| 470 |
|
| 471 |
if [ "$file_version" != "" ]; then |
| 472 |
echo $new_version >$file_version |
| 473 |
echo "Updated $version to $new_version." |
| 474 |
fi |
| 475 |
|
| 476 |
# Call hook function if it exists. |
| 477 |
if type $new_version_hook >/dev/null 2>&1; then |
| 478 |
$new_version_hook $new_version |
| 479 |
fi |
| 480 |
|
| 481 |
# Commit changes. |
| 482 |
git add $file_changes $file_version |
| 483 |
|
| 484 |
if [ "$amend" == "1" ]; then |
| 485 |
git commit --amend |
| 486 |
else |
| 487 |
git commit -m "$new_commit_msg" |
| 488 |
fi |
| 489 |
|
| 490 |
echo "Updates committed." |
| 491 |
|
| 492 |
check_release_tag |
| 493 |
check_beta_tag |