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