Difference between revisions of "Verifying Header Comments"

From Pterodactylus
Jump to: navigation, search
(Add page.)
 
(Adjust for Kotlin source files)
 
(5 intermediate revisions by the same user not shown)
Line 10: Line 10:
 
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:
 
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
+
  # grep -r "Copyright" src/ | grep -E "\.(java|kt):" | 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:
 
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
+
  # grep -r "Copyright" src/ | grep -E "\.(java|kt):" | 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 -E "\.(java|kt):" | 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 © )(<nowiki>[[:digit:]]</nowiki>{4})(–<nowiki>[[:digit:]]</nowiki>{4})?(.*)$,\1\2–'$cy'\4,' -- $(grep -r "Copyright" src | grep -E "\.(java|kt):" | 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:Java]]
 +
[[Category:Shell]]

Latest revision as of 00:18, 23 February 2019

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 -E "\.(java|kt):" | 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 -E "\.(java|kt):" | 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 -E "\.(java|kt):" | 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 -E "\.(java|kt):" | 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)