2 # Adds Whamcloud copyright notices to files modified by Whamcloud.
3 # Does not add copyright notices to files that are missing them
5 # Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
6 # Copyright (c) 2012, Whamcloud, Inc.
10 TMPFILE=$(mktemp $TMP/updatecopy.XXXXXX)
12 DIRS=${DIRS:-"build ldiskfs libcfs lnet lustre snmp lustre-iokit"}
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
22 git ls-files $DIRS | grep -v ${0##*/} | while read FILE; do
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
28 # Skip files not modified by $AUTHOR
29 [ -s "$TMPFILE" ] || continue
31 OLDCOPY="$(egrep "$ORACOPY1|$ORACOPY2|$ADDCOPY" $FILE|tail -n1|tr / .)"
32 if [ -z "$OLDCOPY" ]; then
34 *.[ch]) echo "$FILE: ** NO COPYRIGHT **" && continue ;;
37 elif [ -z "$(echo "$OLDCOPY" | grep "$ADDCOPY")" ]; then
40 # Skip files that already have a copyright for this year
41 echo "$OLDCOPY" | grep -q "$NOW" && continue
45 NEWYEAR=$(head -1 $TMPFILE)
46 OLDYEAR=$(tail -1 $TMPFILE)
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)
52 # If copyright is unchanged (excluding whitespace), we're done
53 [ "$OLDCOPY" == "$NEWCOPY" ] && continue
55 [ ! -w $FILE ] && echo "Unable to write to $FILE" && exit 1
57 echo "$FILE: Copyright $YEAR"
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
66 # Replace the old copyright line with a new copyright
67 sed -e "s#.*$ADDCOPY#$NEWCOPY#" $FILE > $FILE.tmp
69 [ -s $FILE.tmp ] && cp $FILE.tmp $FILE