Whamcloud - gitweb
LU-1201 checksum: add libcfs crypto hash
[fs/lustre-release.git] / build / updatecw.sh
1 #!/bin/bash
2 # Adds Whamcloud copyright notices to files modified by Whamcloud.
3 # Does not add copyright notices to files that are missing them
4 #
5 # Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
6 # Copyright (c) 2012, Whamcloud, Inc.
7 #
8
9 TMP=${TMP:-/tmp}
10 TMPFILE=$(mktemp $TMP/updatecopy.XXXXXX)
11 NOW=$(date +%Y)
12 DIRS=${DIRS:-"build ldiskfs libcfs lnet lustre snmp lustre-iokit"}
13
14 ORACOPY1="Copyright.*Oracle.*"
15 ORACOPY2="Use is subject to license terms."
16 ADDCOPY=${ADDCOPY:-"Copyright.*Whamcloud, Inc."}
17 AUTHOR=${AUTHOR:-".*@whamcloud.com"}
18 START=${START:-"2010-06-01"}
19 ECHOE=${ECHOE:-"echo -e"}
20 [ "$($ECHOE foo)" = "-e foo" ] && ECHOE=echo
21
22 git ls-files $DIRS | grep -v ${0##*/} | while read FILE; do
23         NEEDCOPY=false
24         # Pick only files that have changed since START
25         git log --follow --since=$START --author=$AUTHOR --pretty=format:%ci \
26                 $FILE | cut -d- -f1 > $TMPFILE
27
28         # Skip files not modified by $AUTHOR
29         [ -s "$TMPFILE" ] || continue
30
31         OLDCOPY="$(egrep "$ORACOPY1|$ORACOPY2|$ADDCOPY" $FILE|tail -n1|tr / .)"
32         if [ -z "$OLDCOPY" ]; then
33                 case $FILE in
34                         *.[ch]) echo "$FILE: ** NO COPYRIGHT **" && continue ;;
35                         *) continue ;;
36                 esac
37         elif [ -z "$(echo "$OLDCOPY" | grep "$ADDCOPY")" ]; then
38                 NEEDCOPY=true
39         else
40                 # Skip files that already have a copyright for this year
41                 echo "$OLDCOPY" | grep -q "$NOW" && continue
42         fi
43
44         # Get commit dates
45         NEWYEAR=$(head -1 $TMPFILE)
46         OLDYEAR=$(tail -1 $TMPFILE)
47
48         [ $NEWYEAR == $OLDYEAR ] && YEAR="$NEWYEAR" || YEAR="$OLDYEAR, $NEWYEAR"
49         COMMENT=$(echo "$OLDCOPY" | sed -e "s/^\( *[^A-Z]*\) [A-Z].*/\1/")
50         NEWCOPY=$(sed -e "s/^/$COMMENT /" -e "s/\.\*/ (c) $YEAR, /" <<<$ADDCOPY)
51
52         # If copyright is unchanged (excluding whitespace), we're done
53         [ "$OLDCOPY" == "$NEWCOPY" ] && continue
54
55         [ ! -w $FILE ] && echo "Unable to write to $FILE" && exit 1
56
57         echo "$FILE: Copyright $YEAR"
58         if $NEEDCOPY; then
59                 # Add a new copyright line after the existing copyright.
60                 # Using a temporary file is ugly, but I couldn't get
61                 # newlines into the substitution pattern for some reason,
62                 # and this is not a performance-critical process.
63                 $ECHOE "${COMMENT}\n${NEWCOPY}" > $TMPFILE
64                 sed -e "/$OLDCOPY/r $TMPFILE" $FILE > $FILE.tmp
65         else
66                 # Replace the old copyright line with a new copyright
67                 sed -e "s#.*$ADDCOPY#$NEWCOPY#" $FILE > $FILE.tmp
68         fi
69         [ -s $FILE.tmp ] && cp $FILE.tmp $FILE
70         rm $FILE.tmp
71 done
72 rm $TMPFILE