update_copyright_year.sh revision 278:024a6755895b
13229Spst#!/bin/sh -f
23229Spst
33229Spst#
43229Spst# Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
53229Spst# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
63229Spst#
73229Spst# This code is free software; you can redistribute it and/or modify it
83229Spst# under the terms of the GNU General Public License version 2 only, as
93229Spst# published by the Free Software Foundation.
103229Spst#
113229Spst# This code is distributed in the hope that it will be useful, but WITHOUT
123229Spst# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
133229Spst# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
143229Spst# version 2 for more details (a copy is included in the LICENSE file that
153229Spst# accompanied this code).
163229Spst#
173229Spst# You should have received a copy of the GNU General Public License version
183229Spst# 2 along with this work; if not, write to the Free Software Foundation,
193229Spst# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
203229Spst#
2118471Swosch# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
223229Spst# or visit www.oracle.com if you need additional information or have any
233229Spst# questions.
243229Spst#
253229Spst
263229Spst# Script to update the Copyright YEAR range in Mercurial sources.
273229Spst#  (Originally from xdono, Thanks!)
283229Spst
293229Spstif [ "`uname -s`" = "SunOS" ] ; then
303229Spst  awk=nawk
313229Spstelse
323229Spst  awk=awk
333229Spstfi
343229Spst
353229Spst# Stop on any error
363229Spstset -e
373229Spst
383229Spst# Temp area
393229Spsttmp=/tmp/`basename $0`.${USER}.$$
40110395Scharnierrm -f -r ${tmp}
41110395Scharniermkdir -p ${tmp}
423229Spsttotal=0
433229Spst
443229Spst# This year or supplied year
453229Spstif [ "$1" != "" ] ; then
463229Spst  year="$1"
473229Spstelse
483229Spst  year=`date +%Y`
493229Spstfi
5013575Spst
513229Spst# Return true if it makes sense to edit this file
523229SpstsaneFileToCheck()
533229Spst{
543229Spst  if [ "$1" != "" -a -f $1 ] ; then
553229Spst    isText=`file "$1" | egrep -i '(text|source)' | cat`
563229Spst    hasCopyright=`grep 'Copyright' "$1" | cat`
573229Spst    lastLineCount=`tail -1 "$1" | wc -l`
583229Spst    if [ "${isText}" != ""  \
5913575Spst         -a "${hasCopyright}" != "" \
603229Spst	 -a ${lastLineCount} -eq 1 ] ; then
613229Spst      echo "true"
623229Spst    else
633229Spst      echo "false"
643229Spst    fi
653229Spst  else
663229Spst    echo "false"
6769793Sobrien  fi
683229Spst}
693229Spst
70153706Strhodes# Update the copyright year on a file
713229SpstupdateFile() # file
723229Spst{
733229Spst  changed="false"
743229Spst  if [ `saneFileToCheck "$1"` = "true" ] ; then
753229Spst    copyright="Copyright (c)"
763229Spst    company="Oracle"
773229Spst    rm -f $1.OLD
783229Spst    mv $1 $1.OLD
793229Spst    cat $1.OLD | \
803229Spst      sed -e "s@\(${copyright} [12][0-9][0-9][0-9],\) [12][0-9][0-9][0-9], ${company}@\1 ${year}, ${company}@" | \
813229Spst      sed -e "s@\(${copyright} [12][0-9][0-9][0-9],\) ${company}@\1 ${year}, ${company}@" | \
823229Spst      sed -e "s@${copyright} ${year}, ${year}, ${company}@${copyright} ${year}, ${company}@"  \
833229Spst      > $1
843229Spst    if ! diff -b -w $1.OLD $1 > /dev/null ; then \
853229Spst      changed="true"
863229Spst      rm -f $1.OLD
873229Spst    else
883229Spst      rm -f $1
893229Spst      mv $1.OLD $1
903229Spst    fi
913229Spst  fi
923229Spst  echo "${changed}"
933229Spst}
943229Spst
953229Spst# Update the copyright year on all files changed by this changeset
963229SpstupdateChangesetFiles() # changeset
973229Spst{
983229Spst  count=0
993229Spst  files=${tmp}/files.$1
1003229Spst  rm -f ${files}
1013229Spst  hg log --rev $1 -v --template '{files}\n' | expand \
1023229Spst    | ${awk} -F' ' '{for(i=1;i<=NF;i++)print $i}' \
1033229Spst    > ${files}
1043229Spst  if [ -f "${files}" -a -s "${files}" ] ; then
1053229Spst    copyright="Copyright (c)"
1063229Spst    company="Oracle"
1073229Spst    fcount=`cat ${files}| wc -l`
10897417Salfred    for i in `cat ${files}` ; do
1093229Spst      if [ `updateFile "${i}"` = "true" ] ; then
11097417Salfred        count=`expr ${count} '+' 1`
11197417Salfred      fi
1123229Spst    done
11397417Salfred    if [ ${count} -gt 0 ] ; then
1143229Spst      printf "  UPDATED year on %d of %d files.\n" ${count} ${fcount}
11597417Salfred      total=`expr ${total} '+' ${count}`
11697417Salfred    else
11797417Salfred      printf "  None of the %d files were changed.\n" ${fcount}
11897417Salfred    fi
11997417Salfred  else
1203229Spst    printf "  ERROR: No files changed in the changeset? Must be a mistake.\n"
1213229Spst    set -x
1223229Spst    ls -al ${files}
1233229Spst    hg log --rev $1 -v --template '{files}\n'
1243229Spst    hg log --rev $1 -v --template '{files}\n' | expand \
1253229Spst      | ${awk} -F' ' '{for(i=1;i<=NF;i++)print $i}'
1263229Spst    set +x
1273229Spst    exit 1
1283229Spst  fi
1293229Spst  rm -f ${files}
1303229Spst}
1313229Spst
1323229Spst# Check if repository is clean
1333229Spstprevious=`hg status|wc -l`
1343229Spstif [ ${previous} -ne 0 ] ; then
1353229Spst  echo "WARNING: This repository contains previously edited working set files."
1363229Spst  echo "  hg status | wc -l = `hg status | wc -l`"
1373229Spstfi
1383229Spst
1393229Spst# Get all changesets this year
1403229Spstall_changesets=${tmp}/all_changesets
1413229Spstrm -f ${all_changesets}
1423229Spsthg log --no-merges -v -d "${year}-01-01 to ${year}-12-31" --template '{node}\n' > ${all_changesets}
1433229Spst
1443229Spst# Check changeset to see if it is Copyright only changes, filter changesets
1453229Spstif [ -s ${all_changesets} ] ; then
1463229Spst  echo "Changesets made in ${year}: `cat ${all_changesets} | wc -l`"
1473229Spst  index=0
1483229Spst  cat ${all_changesets} | while read changeset ; do
1493229Spst    index=`expr ${index} '+' 1`
1503229Spst    desc=${tmp}/desc.${changeset}
1513229Spst    rm -f ${desc}
1523229Spst    echo "------------------------------------------------"
1533229Spst    hg log --rev ${changeset} --template '{desc}\n' > ${desc}
1543229Spst    printf "%d: %s\n%s\n" ${index} "${changeset}" "`cat ${desc}|head -1`"
1553229Spst    if cat ${desc} | fgrep -i "Added tag" > /dev/null ; then
1563229Spst      printf "  EXCLUDED tag changeset.\n"
1573229Spst    elif cat ${desc} | fgrep -i rebrand > /dev/null ; then
15845422Sbrian      printf "  EXCLUDED rebrand changeset.\n"
15945422Sbrian    elif cat ${desc} | fgrep -i copyright > /dev/null ; then
16013575Spst      printf "  EXCLUDED copyright changeset.\n"
1613229Spst    else
1623229Spst      updateChangesetFiles ${changeset}
1633229Spst    fi
1643229Spst    rm -f ${desc}
1653229Spst  done
1663229Spstfi
1673229Spst
1683229Spstif [ ${total} -gt 0 ] ; then
1693229Spst   echo "---------------------------------------------"
1703229Spst   echo "Updated the copyright year on a total of ${total} files."
1713229Spst   if [ ${previous} -eq 0 ] ; then
1723229Spst     echo "This count should match the count of modified files in the repository: hg status -m"
1733229Spst   else
1743229Spst     echo "WARNING: This repository contained previously edited working set files."
1753229Spst   fi
1763229Spst   echo "  hg status -m | wc -l = `hg status -m | wc -l`"
1773229Spstelse
1783229Spst   echo "---------------------------------------------"
17946078Simp   echo "No files were changed"
1803229Spst   if [ ${previous} -ne 0 ] ; then
1813229Spst     echo "WARNING: This repository contained previously edited working set files."
1823229Spst   fi
1833229Spst   echo "  hg status -m | wc -l = `hg status -m | wc -l`"
1843229Spstfi
1853229Spst
1863229Spst# Cleanup
1873229Spstrm -f -r ${tmp}
1883229Spstexit 0
189141918Sstefanf
190141918Sstefanf