Difference between revisions of "Verifying Header Comments"

From Pterodactylus
Jump to: navigation, search
m (Fix formatting)
(Add script to update years in copyright lines)
Line 19: Line 19:
  
 
  # cy=$(date +%Y); grep -r "Copyright" src/ | grep "java:" | while read line; do f="$(echo "$line" | cut -d: -f1)"; v="$(echo "$line" | cut -d- -f3 | cut -d' ' -f4)"; y="${v:(-4)}"; if [ "$y" != "$cy" ]; then echo "$f: $y"; fi; done
 
  # cy=$(date +%Y); grep -r "Copyright" src/ | grep "java:" | while read line; do f="$(echo "$line" | cut -d: -f1)"; v="$(echo "$line" | cut -d- -f3 | cut -d' ' -f4)"; y="${v:(-4)}"; if [ "$y" != "$cy" ]; then echo "$f: $y"; fi; done
 +
 +
To update all files that need updating:
 +
 +
# cy=$(date +%Y); sed -i "" -E -e 's,^( \* .*Copyright © )([[:digit:]]{4})(–[[:digit:]]{4})?(.*)$,\1\2–'$cy'\4,' -- $(grep -r "Copyright" src | grep "java:" | while read line; do f="$(echo "$line" | cut -d: -f1)"; v="$(echo "$line" | cut -d- -f3 | cut -d' ' -f4)"; y="${v:(-4)}"; if [ "$y" != "$cy" ]; then echo "$f"; fi; done)
  
 
[[Category:Programming]]
 
[[Category:Programming]]
 
[[Category:Java]]
 
[[Category:Java]]
 
[[Category:Shell]]
 
[[Category:Shell]]

Revision as of 07:11, 27 November 2015

A normal Java source file (in my projects) consists of a header like the following one:

/*
 * FreenetSone - EditImagePage.java - Copyright © 2010–2012 David Roden
 *
…

It should contain the name of the project and the name of the file. Sometimes, due to refactoring or copy/paste, the filename in the header does not match the name of the file in question. The following bash script will locate those files:

# grep -r "Copyright" src/ | grep "java:" | while read line; do f="$(echo "$line" | cut -d: -f1)"; c="$(echo "$line" | cut -d- -f2 | cut -d' ' -f2)"; f2="$(basename "$f")"; [ "$c" != "$f2" ] && echo "$f: $c"; done

And sometimes you even changed the name of the project at some point in the past. Use the following oneliner to find those files that have a different project name than the one you substitute at the end of the command:

# grep -r "Copyright" src/ | grep "java:" | while read line; do f="$(echo "$line" | cut -d: -f1)"; project="$(echo "$line" | cut -d' ' -f3)"; [ $project != "Sone" ] && echo "$f: $project"; done

And then there’s the ever-occuring issue with the years in the copyright line. Use this to find files that don’t have the current year as last year in the copyright line.

# cy=$(date +%Y); grep -r "Copyright" src/ | grep "java:" | while read line; do f="$(echo "$line" | cut -d: -f1)"; v="$(echo "$line" | cut -d- -f3 | cut -d' ' -f4)"; y="${v:(-4)}"; if [ "$y" != "$cy" ]; then echo "$f: $y"; fi; done

To update all files that need updating:

# cy=$(date +%Y); sed -i "" -E -e 's,^( \* .*Copyright © )(digit:{4})(–digit:{4})?(.*)$,\1\2–'$cy'\4,' -- $(grep -r "Copyright" src | grep "java:" | while read line; do f="$(echo "$line" | cut -d: -f1)"; v="$(echo "$line" | cut -d- -f3 | cut -d' ' -f4)"; y="${v:(-4)}"; if [ "$y" != "$cy" ]; then echo "$f"; fi; done)