make/scripts/update_copyright_year.sh

changeset 0
75a576e87639
child 1133
50aaf272884f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/make/scripts/update_copyright_year.sh	Wed Apr 27 01:39:08 2016 +0800
     1.3 @@ -0,0 +1,199 @@
     1.4 +#!/bin/bash -f
     1.5 +
     1.6 +#
     1.7 +# Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
     1.8 +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.9 +#
    1.10 +# This code is free software; you can redistribute it and/or modify it
    1.11 +# under the terms of the GNU General Public License version 2 only, as
    1.12 +# published by the Free Software Foundation.
    1.13 +#
    1.14 +# This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 +# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 +# version 2 for more details (a copy is included in the LICENSE file that
    1.18 +# accompanied this code).
    1.19 +#
    1.20 +# You should have received a copy of the GNU General Public License version
    1.21 +# 2 along with this work; if not, write to the Free Software Foundation,
    1.22 +# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 +#
    1.24 +# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 +# or visit www.oracle.com if you need additional information or have any
    1.26 +# questions.
    1.27 +#
    1.28 +
    1.29 +# Script to update the Copyright YEAR range in Mercurial sources.
    1.30 +#  (Originally from xdono, Thanks!)
    1.31 +
    1.32 +if [ "`uname -s`" = "SunOS" ] ; then
    1.33 +  awk=nawk
    1.34 +else
    1.35 +  awk=awk
    1.36 +fi
    1.37 +
    1.38 +# Stop on any error
    1.39 +set -e
    1.40 +
    1.41 +# Temp area
    1.42 +tmp=/tmp/`basename $0`.${USER}.$$
    1.43 +rm -f -r ${tmp}
    1.44 +mkdir -p ${tmp}
    1.45 +total=0
    1.46 +
    1.47 +# This year or supplied year
    1.48 +if [ "$1" != "" ] ; then
    1.49 +  year="$1"
    1.50 +else
    1.51 +  year=`date +%Y`
    1.52 +fi
    1.53 +
    1.54 +# Return true if it makes sense to edit this file
    1.55 +saneFileToCheck()
    1.56 +{
    1.57 +  if [ "$1" != "" -a -f $1 ] ; then
    1.58 +    isText=`file "$1" | egrep -i '(text|source)' | cat`
    1.59 +    hasCopyright=`grep 'Copyright' "$1" | cat`
    1.60 +    lastLineCount=`tail -1 "$1" | wc -l`
    1.61 +    if [ "${isText}" != ""  \
    1.62 +         -a "${hasCopyright}" != "" \
    1.63 +	 -a ${lastLineCount} -eq 1 ] ; then
    1.64 +      echo "true"
    1.65 +    else
    1.66 +      echo "false"
    1.67 +    fi
    1.68 +  else
    1.69 +    echo "false"
    1.70 +  fi
    1.71 +}
    1.72 +
    1.73 +# Update the copyright year on a file
    1.74 +updateFile() # file
    1.75 +{
    1.76 +  changed="false"
    1.77 +  if [ `saneFileToCheck "$1"` = "true" ] ; then
    1.78 +    copyright="Copyright (c)"
    1.79 +    company="Oracle"
    1.80 +    rm -f $1.OLD
    1.81 +    mv $1 $1.OLD
    1.82 +    cat $1.OLD | \
    1.83 +      sed -e "s@\(${copyright} [12][0-9][0-9][0-9],\) [12][0-9][0-9][0-9], ${company}@\1 ${year}, ${company}@" | \
    1.84 +      sed -e "s@\(${copyright} [12][0-9][0-9][0-9],\) ${company}@\1 ${year}, ${company}@" | \
    1.85 +      sed -e "s@${copyright} ${year}, ${year}, ${company}@${copyright} ${year}, ${company}@"  \
    1.86 +      > $1
    1.87 +    if ! diff -b -w $1.OLD $1 > /dev/null ; then \
    1.88 +      changed="true"
    1.89 +      rm -f $1.OLD
    1.90 +    else
    1.91 +      rm -f $1
    1.92 +      mv $1.OLD $1
    1.93 +    fi
    1.94 +  fi
    1.95 +  echo "${changed}"
    1.96 +}
    1.97 +
    1.98 +# Update the copyright year on all files changed by this changeset
    1.99 +updateChangesetFiles() # changeset
   1.100 +{
   1.101 +  count=0
   1.102 +  files=${tmp}/files.$1
   1.103 +  rm -f ${files}
   1.104 +  hg log --rev $1 -v --template '{files}\n' | expand \
   1.105 +    | ${awk} -F' ' '{for(i=1;i<=NF;i++)print $i}' \
   1.106 +    > ${files}
   1.107 +  if [ -f "${files}" -a -s "${files}" ] ; then
   1.108 +    copyright="Copyright (c)"
   1.109 +    company="Oracle"
   1.110 +    fcount=`cat ${files}| wc -l`
   1.111 +    for i in `cat ${files}` ; do
   1.112 +      if [ `updateFile "${i}"` = "true" ] ; then
   1.113 +        count=`expr ${count} '+' 1`
   1.114 +      fi
   1.115 +    done
   1.116 +    if [ ${count} -gt 0 ] ; then
   1.117 +      printf "  UPDATED year on %d of %d files.\n" ${count} ${fcount}
   1.118 +      total=`expr ${total} '+' ${count}`
   1.119 +    else
   1.120 +      printf "  None of the %d files were changed.\n" ${fcount}
   1.121 +    fi
   1.122 +  else
   1.123 +    printf "  ERROR: No files changed in the changeset? Must be a mistake.\n"
   1.124 +    set -x
   1.125 +    ls -al ${files}
   1.126 +    hg log --rev $1 -v --template '{files}\n'
   1.127 +    hg log --rev $1 -v --template '{files}\n' | expand \
   1.128 +      | ${awk} -F' ' '{for(i=1;i<=NF;i++)print $i}'
   1.129 +    set +x
   1.130 +    exit 1
   1.131 +  fi
   1.132 +  rm -f ${files}
   1.133 +}
   1.134 +
   1.135 +# Check if repository is clean
   1.136 +previous=`hg status|wc -l`
   1.137 +if [ ${previous} -ne 0 ] ; then
   1.138 +  echo "WARNING: This repository contains previously edited working set files."
   1.139 +  echo "  hg status | wc -l = `hg status | wc -l`"
   1.140 +fi
   1.141 +
   1.142 +# Get all changesets this year
   1.143 +all_changesets=${tmp}/all_changesets
   1.144 +rm -f ${all_changesets}
   1.145 +hg log --no-merges -v -d "${year}-01-01 to ${year}-12-31" --template '{node}\n' > ${all_changesets}
   1.146 +
   1.147 +# Check changeset to see if it is Copyright only changes, filter changesets
   1.148 +if [ -s ${all_changesets} ] ; then
   1.149 +  echo "Changesets made in ${year}: `cat ${all_changesets} | wc -l`"
   1.150 +  index=0
   1.151 +  cat ${all_changesets} | while read changeset ; do
   1.152 +    index=`expr ${index} '+' 1`
   1.153 +    desc=${tmp}/desc.${changeset}
   1.154 +    rm -f ${desc}
   1.155 +    echo "------------------------------------------------"
   1.156 +    hg log --rev ${changeset} --template '{desc}\n' > ${desc}
   1.157 +    printf "%d: %s\n%s\n" ${index} "${changeset}" "`cat ${desc}|head -1`"
   1.158 +    if [ "${year}" = "2010" ] ; then
   1.159 +      if cat ${desc} | fgrep -i "Added tag" > /dev/null ; then
   1.160 +        printf "  EXCLUDED tag changeset.\n"
   1.161 +      elif cat ${desc} | fgrep -i rebrand > /dev/null ; then
   1.162 +        printf "  EXCLUDED rebrand changeset.\n"
   1.163 +      elif cat ${desc} | fgrep -i copyright > /dev/null ; then
   1.164 +        printf "  EXCLUDED copyright changeset.\n"
   1.165 +      else
   1.166 +        updateChangesetFiles ${changeset}
   1.167 +      fi
   1.168 +    else
   1.169 +      if cat ${desc} | fgrep -i "Added tag" > /dev/null ; then
   1.170 +        printf "  EXCLUDED tag changeset.\n"
   1.171 +      elif cat ${desc} | fgrep -i "copyright year" > /dev/null ; then
   1.172 +        printf "  EXCLUDED copyright year changeset.\n"
   1.173 +      else
   1.174 +        updateChangesetFiles ${changeset}
   1.175 +      fi
   1.176 +    fi
   1.177 +    rm -f ${desc}
   1.178 +  done
   1.179 +fi
   1.180 +
   1.181 +if [ ${total} -gt 0 ] ; then
   1.182 +   echo "---------------------------------------------"
   1.183 +   echo "Updated the copyright year on a total of ${total} files."
   1.184 +   if [ ${previous} -eq 0 ] ; then
   1.185 +     echo "This count should match the count of modified files in the repository: hg status -m"
   1.186 +   else
   1.187 +     echo "WARNING: This repository contained previously edited working set files."
   1.188 +   fi
   1.189 +   echo "  hg status -m | wc -l = `hg status -m | wc -l`"
   1.190 +else
   1.191 +   echo "---------------------------------------------"
   1.192 +   echo "No files were changed"
   1.193 +   if [ ${previous} -ne 0 ] ; then
   1.194 +     echo "WARNING: This repository contained previously edited working set files."
   1.195 +   fi
   1.196 +   echo "  hg status -m | wc -l = `hg status -m | wc -l`"
   1.197 +fi
   1.198 +
   1.199 +# Cleanup
   1.200 +rm -f -r ${tmp}
   1.201 +exit 0
   1.202 +

mercurial