In: |
checktype.rb
|
Parent: | P4Trigger |
The trigger class itself. The main method in here is validate() which is invoked from the super-class’ parse_change() method.
# File checktype.rb, line 78 78: def initialize( max_errors ) 79: @max_errors = max_errors 80: super() 81: end
Method to return the expected filetype given a filename. Returns nil if no specific filetype has been mandated.
# File checktype.rb, line 97 97: def filetype( depot_file ) 98: ext = depot_file.sub( /.*\.(.*)/, '\1' ) 99: ext = ext.downcase 100: TYPEMAP[ ext ] 101: end
Method to report the error to the user. Just formats the error message and sends it. We only report the first @max_errors bad files. On a large changelist they’ll be grateful for that.
# File checktype.rb, line 132 132: def report( badfiles ) 133: errors = 0 134: msg = @@USER_MESSAGE 135: badfiles.each do 136: |file| 137: type = filetype( file.depot_file ) 138: msg += sprintf( @@BADFILE_FORMAT, 139: file.depot_file, 140: file.revisions[ 0 ].type, 141: type.msg 142: ) 143: errors += 1 144: break if ( errors >= @max_errors ) 145: 146: end 147: message( msg ) 148: end
Enforce type checking. Basic algorithm is that we iterate over the typemap and match each extension against the depot file. If they match, we check the type.
# File checktype.rb, line 106 106: def validate() 107: 108: # First gather the list of bad files 109: badlist = Array.new 110: change.each_file do 111: |file| 112: # Ignore files not open for add 113: next unless ( file.revisions[ 0 ].action == "add" ) 114: 115: type = filetype( file.depot_file ) 116: next unless type 117: 118: basetype = file.revisions[ 0 ].type.sub( /\+.*/, "" ) 119: if ( ! type.match( basetype ) ) 120: badlist.push( file ) 121: end 122: end 123: 124: # Now report any problems to the user 125: report( badlist ) if ( ! badlist.empty? ) 126: return badlist.empty? 127: end