update_copyright_year.sh revision 328:7e13dbf7e8af
1#!/bin/bash -f
2
3#
4# Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
5# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6#
7# This code is free software; you can redistribute it and/or modify it
8# under the terms of the GNU General Public License version 2 only, as
9# published by the Free Software Foundation.
10#
11# This code is distributed in the hope that it will be useful, but WITHOUT
12# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14# version 2 for more details (a copy is included in the LICENSE file that
15# accompanied this code).
16#
17# You should have received a copy of the GNU General Public License version
18# 2 along with this work; if not, write to the Free Software Foundation,
19# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20#
21# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22# or visit www.oracle.com if you need additional information or have any
23# questions.
24#
25
26# Script to update the Copyright YEAR range in Mercurial sources.
27#  (Originally from xdono, Thanks!)
28
29if [ "`uname -s`" = "SunOS" ] ; then
30  awk=nawk
31else
32  awk=awk
33fi
34
35# Stop on any error
36set -e
37
38# Temp area
39tmp=/tmp/`basename $0`.${USER}.$$
40rm -f -r ${tmp}
41mkdir -p ${tmp}
42total=0
43
44# This year or supplied year
45if [ "$1" != "" ] ; then
46  year="$1"
47else
48  year=`date +%Y`
49fi
50
51# Return true if it makes sense to edit this file
52saneFileToCheck()
53{
54  if [ "$1" != "" -a -f $1 ] ; then
55    isText=`file "$1" | egrep -i '(text|source)' | cat`
56    hasCopyright=`grep 'Copyright' "$1" | cat`
57    lastLineCount=`tail -1 "$1" | wc -l`
58    if [ "${isText}" != ""  \
59         -a "${hasCopyright}" != "" \
60	 -a ${lastLineCount} -eq 1 ] ; then
61      echo "true"
62    else
63      echo "false"
64    fi
65  else
66    echo "false"
67  fi
68}
69
70# Update the copyright year on a file
71updateFile() # file
72{
73  changed="false"
74  if [ `saneFileToCheck "$1"` = "true" ] ; then
75    copyright="Copyright (c)"
76    company="Oracle"
77    rm -f $1.OLD
78    mv $1 $1.OLD
79    cat $1.OLD | \
80      sed -e "s@\(${copyright} [12][0-9][0-9][0-9],\) [12][0-9][0-9][0-9], ${company}@\1 ${year}, ${company}@" | \
81      sed -e "s@\(${copyright} [12][0-9][0-9][0-9],\) ${company}@\1 ${year}, ${company}@" | \
82      sed -e "s@${copyright} ${year}, ${year}, ${company}@${copyright} ${year}, ${company}@"  \
83      > $1
84    if ! diff -b -w $1.OLD $1 > /dev/null ; then \
85      changed="true"
86      rm -f $1.OLD
87    else
88      rm -f $1
89      mv $1.OLD $1
90    fi
91  fi
92  echo "${changed}"
93}
94
95# Update the copyright year on all files changed by this changeset
96updateChangesetFiles() # changeset
97{
98  count=0
99  files=${tmp}/files.$1
100  rm -f ${files}
101  hg log --rev $1 -v --template '{files}\n' | expand \
102    | ${awk} -F' ' '{for(i=1;i<=NF;i++)print $i}' \
103    > ${files}
104  if [ -f "${files}" -a -s "${files}" ] ; then
105    copyright="Copyright (c)"
106    company="Oracle"
107    fcount=`cat ${files}| wc -l`
108    for i in `cat ${files}` ; do
109      if [ `updateFile "${i}"` = "true" ] ; then
110        count=`expr ${count} '+' 1`
111      fi
112    done
113    if [ ${count} -gt 0 ] ; then
114      printf "  UPDATED year on %d of %d files.\n" ${count} ${fcount}
115      total=`expr ${total} '+' ${count}`
116    else
117      printf "  None of the %d files were changed.\n" ${fcount}
118    fi
119  else
120    printf "  ERROR: No files changed in the changeset? Must be a mistake.\n"
121    set -x
122    ls -al ${files}
123    hg log --rev $1 -v --template '{files}\n'
124    hg log --rev $1 -v --template '{files}\n' | expand \
125      | ${awk} -F' ' '{for(i=1;i<=NF;i++)print $i}'
126    set +x
127    exit 1
128  fi
129  rm -f ${files}
130}
131
132# Check if repository is clean
133previous=`hg status|wc -l`
134if [ ${previous} -ne 0 ] ; then
135  echo "WARNING: This repository contains previously edited working set files."
136  echo "  hg status | wc -l = `hg status | wc -l`"
137fi
138
139# Get all changesets this year
140all_changesets=${tmp}/all_changesets
141rm -f ${all_changesets}
142hg log --no-merges -v -d "${year}-01-01 to ${year}-12-31" --template '{node}\n' > ${all_changesets}
143
144# Check changeset to see if it is Copyright only changes, filter changesets
145if [ -s ${all_changesets} ] ; then
146  echo "Changesets made in ${year}: `cat ${all_changesets} | wc -l`"
147  index=0
148  cat ${all_changesets} | while read changeset ; do
149    index=`expr ${index} '+' 1`
150    desc=${tmp}/desc.${changeset}
151    rm -f ${desc}
152    echo "------------------------------------------------"
153    hg log --rev ${changeset} --template '{desc}\n' > ${desc}
154    printf "%d: %s\n%s\n" ${index} "${changeset}" "`cat ${desc}|head -1`"
155    if [ "${year}" = "2010" ] ; then
156      if cat ${desc} | fgrep -i "Added tag" > /dev/null ; then
157        printf "  EXCLUDED tag changeset.\n"
158      elif cat ${desc} | fgrep -i rebrand > /dev/null ; then
159        printf "  EXCLUDED rebrand changeset.\n"
160      elif cat ${desc} | fgrep -i copyright > /dev/null ; then
161        printf "  EXCLUDED copyright changeset.\n"
162      else
163        updateChangesetFiles ${changeset}
164      fi
165    else
166      if cat ${desc} | fgrep -i "Added tag" > /dev/null ; then
167        printf "  EXCLUDED tag changeset.\n"
168      elif cat ${desc} | fgrep -i "copyright year" > /dev/null ; then
169        printf "  EXCLUDED copyright year changeset.\n"
170      else
171        updateChangesetFiles ${changeset}
172      fi
173    fi
174    rm -f ${desc}
175  done
176fi
177
178if [ ${total} -gt 0 ] ; then
179   echo "---------------------------------------------"
180   echo "Updated the copyright year on a total of ${total} files."
181   if [ ${previous} -eq 0 ] ; then
182     echo "This count should match the count of modified files in the repository: hg status -m"
183   else
184     echo "WARNING: This repository contained previously edited working set files."
185   fi
186   echo "  hg status -m | wc -l = `hg status -m | wc -l`"
187else
188   echo "---------------------------------------------"
189   echo "No files were changed"
190   if [ ${previous} -ne 0 ] ; then
191     echo "WARNING: This repository contained previously edited working set files."
192   fi
193   echo "  hg status -m | wc -l = `hg status -m | wc -l`"
194fi
195
196# Cleanup
197rm -f -r ${tmp}
198exit 0
199
200