328N/A#!/bin/bash -f
278N/A
278N/A#
278N/A# Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
278N/A# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
278N/A#
278N/A# This code is free software; you can redistribute it and/or modify it
278N/A# under the terms of the GNU General Public License version 2 only, as
278N/A# published by the Free Software Foundation.
278N/A#
278N/A# This code is distributed in the hope that it will be useful, but WITHOUT
278N/A# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
278N/A# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
278N/A# version 2 for more details (a copy is included in the LICENSE file that
278N/A# accompanied this code).
278N/A#
278N/A# You should have received a copy of the GNU General Public License version
278N/A# 2 along with this work; if not, write to the Free Software Foundation,
278N/A# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
278N/A#
278N/A# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
278N/A# or visit www.oracle.com if you need additional information or have any
278N/A# questions.
278N/A#
278N/A
278N/A# Script to update the Copyright YEAR range in Mercurial sources.
278N/A# (Originally from xdono, Thanks!)
278N/A
278N/Aif [ "`uname -s`" = "SunOS" ] ; then
278N/A awk=nawk
278N/Aelse
278N/A awk=awk
278N/Afi
278N/A
278N/A# Stop on any error
278N/Aset -e
278N/A
278N/A# Temp area
278N/Atmp=/tmp/`basename $0`.${USER}.$$
278N/Arm -f -r ${tmp}
278N/Amkdir -p ${tmp}
278N/Atotal=0
278N/A
278N/A# This year or supplied year
278N/Aif [ "$1" != "" ] ; then
278N/A year="$1"
278N/Aelse
278N/A year=`date +%Y`
278N/Afi
278N/A
278N/A# Return true if it makes sense to edit this file
278N/AsaneFileToCheck()
278N/A{
278N/A if [ "$1" != "" -a -f $1 ] ; then
278N/A isText=`file "$1" | egrep -i '(text|source)' | cat`
278N/A hasCopyright=`grep 'Copyright' "$1" | cat`
278N/A lastLineCount=`tail -1 "$1" | wc -l`
278N/A if [ "${isText}" != "" \
278N/A -a "${hasCopyright}" != "" \
278N/A -a ${lastLineCount} -eq 1 ] ; then
278N/A echo "true"
278N/A else
278N/A echo "false"
278N/A fi
278N/A else
278N/A echo "false"
278N/A fi
278N/A}
278N/A
278N/A# Update the copyright year on a file
278N/AupdateFile() # file
278N/A{
278N/A changed="false"
278N/A if [ `saneFileToCheck "$1"` = "true" ] ; then
278N/A copyright="Copyright (c)"
278N/A company="Oracle"
278N/A rm -f $1.OLD
278N/A mv $1 $1.OLD
278N/A cat $1.OLD | \
278N/A sed -e "s@\(${copyright} [12][0-9][0-9][0-9],\) [12][0-9][0-9][0-9], ${company}@\1 ${year}, ${company}@" | \
278N/A sed -e "s@\(${copyright} [12][0-9][0-9][0-9],\) ${company}@\1 ${year}, ${company}@" | \
278N/A sed -e "s@${copyright} ${year}, ${year}, ${company}@${copyright} ${year}, ${company}@" \
278N/A > $1
278N/A if ! diff -b -w $1.OLD $1 > /dev/null ; then \
278N/A changed="true"
278N/A rm -f $1.OLD
278N/A else
278N/A rm -f $1
278N/A mv $1.OLD $1
278N/A fi
278N/A fi
278N/A echo "${changed}"
278N/A}
278N/A
278N/A# Update the copyright year on all files changed by this changeset
278N/AupdateChangesetFiles() # changeset
278N/A{
278N/A count=0
278N/A files=${tmp}/files.$1
278N/A rm -f ${files}
278N/A hg log --rev $1 -v --template '{files}\n' | expand \
278N/A | ${awk} -F' ' '{for(i=1;i<=NF;i++)print $i}' \
278N/A > ${files}
278N/A if [ -f "${files}" -a -s "${files}" ] ; then
278N/A copyright="Copyright (c)"
278N/A company="Oracle"
278N/A fcount=`cat ${files}| wc -l`
278N/A for i in `cat ${files}` ; do
278N/A if [ `updateFile "${i}"` = "true" ] ; then
278N/A count=`expr ${count} '+' 1`
278N/A fi
278N/A done
278N/A if [ ${count} -gt 0 ] ; then
278N/A printf " UPDATED year on %d of %d files.\n" ${count} ${fcount}
278N/A total=`expr ${total} '+' ${count}`
278N/A else
278N/A printf " None of the %d files were changed.\n" ${fcount}
278N/A fi
278N/A else
278N/A printf " ERROR: No files changed in the changeset? Must be a mistake.\n"
278N/A set -x
278N/A ls -al ${files}
278N/A hg log --rev $1 -v --template '{files}\n'
278N/A hg log --rev $1 -v --template '{files}\n' | expand \
278N/A | ${awk} -F' ' '{for(i=1;i<=NF;i++)print $i}'
278N/A set +x
278N/A exit 1
278N/A fi
278N/A rm -f ${files}
278N/A}
278N/A
278N/A# Check if repository is clean
278N/Aprevious=`hg status|wc -l`
278N/Aif [ ${previous} -ne 0 ] ; then
278N/A echo "WARNING: This repository contains previously edited working set files."
278N/A echo " hg status | wc -l = `hg status | wc -l`"
278N/Afi
278N/A
278N/A# Get all changesets this year
278N/Aall_changesets=${tmp}/all_changesets
278N/Arm -f ${all_changesets}
278N/Ahg log --no-merges -v -d "${year}-01-01 to ${year}-12-31" --template '{node}\n' > ${all_changesets}
278N/A
278N/A# Check changeset to see if it is Copyright only changes, filter changesets
278N/Aif [ -s ${all_changesets} ] ; then
278N/A echo "Changesets made in ${year}: `cat ${all_changesets} | wc -l`"
278N/A index=0
278N/A cat ${all_changesets} | while read changeset ; do
278N/A index=`expr ${index} '+' 1`
278N/A desc=${tmp}/desc.${changeset}
278N/A rm -f ${desc}
278N/A echo "------------------------------------------------"
278N/A hg log --rev ${changeset} --template '{desc}\n' > ${desc}
278N/A printf "%d: %s\n%s\n" ${index} "${changeset}" "`cat ${desc}|head -1`"
328N/A if [ "${year}" = "2010" ] ; then
328N/A if cat ${desc} | fgrep -i "Added tag" > /dev/null ; then
328N/A printf " EXCLUDED tag changeset.\n"
328N/A elif cat ${desc} | fgrep -i rebrand > /dev/null ; then
328N/A printf " EXCLUDED rebrand changeset.\n"
328N/A elif cat ${desc} | fgrep -i copyright > /dev/null ; then
328N/A printf " EXCLUDED copyright changeset.\n"
328N/A else
328N/A updateChangesetFiles ${changeset}
328N/A fi
278N/A else
328N/A if cat ${desc} | fgrep -i "Added tag" > /dev/null ; then
328N/A printf " EXCLUDED tag changeset.\n"
328N/A elif cat ${desc} | fgrep -i "copyright year" > /dev/null ; then
328N/A printf " EXCLUDED copyright year changeset.\n"
328N/A else
328N/A updateChangesetFiles ${changeset}
328N/A fi
278N/A fi
278N/A rm -f ${desc}
278N/A done
278N/Afi
278N/A
278N/Aif [ ${total} -gt 0 ] ; then
278N/A echo "---------------------------------------------"
278N/A echo "Updated the copyright year on a total of ${total} files."
278N/A if [ ${previous} -eq 0 ] ; then
278N/A echo "This count should match the count of modified files in the repository: hg status -m"
278N/A else
278N/A echo "WARNING: This repository contained previously edited working set files."
278N/A fi
278N/A echo " hg status -m | wc -l = `hg status -m | wc -l`"
278N/Aelse
278N/A echo "---------------------------------------------"
278N/A echo "No files were changed"
278N/A if [ ${previous} -ne 0 ] ; then
278N/A echo "WARNING: This repository contained previously edited working set files."
278N/A fi
278N/A echo " hg status -m | wc -l = `hg status -m | wc -l`"
278N/Afi
278N/A
278N/A# Cleanup
278N/Arm -f -r ${tmp}
278N/Aexit 0
278N/A