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
def initialize( max_errors )
@max_errors = max_errors
super()
end
Method to return the expected filetype given a filename. Returns nil if no
specific filetype has been mandated.
# File checktype.rb, line 97
def filetype( depot_file )
ext = depot_file.sub( /.*\.(.*)/, '\1' )
ext = ext.downcase
TYPEMAP[ ext ]
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
def validate()
# First gather the list of bad files
badlist = Array.new
change.each_file do
|file|
# Ignore files not open for add
next unless ( file.revisions[ 0 ].action == "add" )
type = filetype( file.depot_file )
next unless type
basetype = file.revisions[ 0 ].type.sub( /\+.*/, "" )
if ( ! type.match( basetype ) )
badlist.push( file )
end
end
# Now report any problems to the user
report( badlist ) if ( ! badlist.empty? )
return badlist.empty?
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
def report( badfiles )
errors = 0
msg = @@USER_MESSAGE
badfiles.each do
|file|
type = filetype( file.depot_file )
msg += sprintf( @@BADFILE_FORMAT,
file.depot_file,
file.revisions[ 0 ].type,
type.msg
)
errors += 1
break if ( errors >= @max_errors )
end
message( msg )
end