#!/usr/bin/ruby #-- #------------------------------------------------------------------------------- #++ # #= Introduction # #== Name: checkcase.rb # #== Author: Tony Smith <tony@perforce.com> # #== Description # Example trigger to ensure that new files being added are # consistent in their use of case w.r.t. existing directories. # # This implementation is reasonably efficient as it only uses # "p4 depots" and "p4 dirs" commands and restricts itself to # looking only at the paths that it needs to. # #== Requires # Ruby # P4Ruby # P4Triggers module # #== Example 'triggers' section: # # Triggers: # checkcase //... "ruby whatever/checkcase.rb %changelist%" # #== Note # For triggers I recommend you use a P4CONFIG file rather than hard coding # username/password in the script itself. This script assumes you've taken # that advice. # #-- #------------------------------------------------------------------------------- #++ $:.unshift( File.dirname( __FILE__ ) ) require "P4" require "P4Triggers" # # The trigger class itself. The main method in here is validate() which # is invoked from the super-class' parse_change() method. # class CaseTrigger < P4Trigger # Constructor. In this trigger we want to limit the number of errors # we might report to the user because on a large changelist that # could be a real pain. Simply pass the maximum number you want your # users to deal with at one time. def initialize( max_errors ) @max_errors = max_errors # Set API level to 58 (2005.2) super( 58 ) end # # The error message we give to the user when we reject their change. # @@USER_MESSAGE = "\n\n" + "Your submission has been rejected because the following files\n" + "are inconsistent in their use of case with respect to existing\n" + "directories:\n\n" # The printf() style format string used for each of the bad file # entries @@BADFILE_FORMAT= " Your file: '%s' existing dir: '%s'\n" # Enforce case checking. the basic algorithm is that we split the # depot path of each file up into its components and use "p4 depots", # "p4 dirs" (depending on what level we're at to locate # existing depots/dirs with the same names in different case. If # we find any we reject submission. Note that we are only interested in # new files being added/branched since existing files are assumed # to be OK. def validate() badlist = Hash.new change.each_file do |file| # Ignore files not open for add or branch action = file.revisions[ 0 ].action next unless ( action == "add" || action == "branch" ) if ( mismatch_exists( file.depot_file ) ) # @mismatch set by mismatch_exists() et. al. badlist[ file.depot_file ] = @mismatch end end # Now report any problems to the user report( badlist ) if ( ! badlist.empty? ) return badlist.empty? end # This method does the work to check whether or not the components of # a path already exist in different case. We do it by breaking the depot # path up into components where the first part is the depot name, the # last part is the filename and everything in between is a directory # name. We then start at the depot and then iteratively check the # directories one by one. def mismatch_exists( path ) path = path[ 2..-1 ] # Strip off leading // dirs = path.split( "/" ) depot = dirs.shift file = dirs.pop # Now look for mis-matching depots return true if mismatch_depot( depot ) # Now look for mis-matching directories return true if mismatch_dirs( depot, dirs ) return false end # Method to see whether a depot with a name matching the argument # already exists. def mismatch_depot( depot ) match = false lcdepot = depot.downcase p4.run_depots.each do |d| dname = d[ "name" ] lcdname = dname.downcase if ( lcdname == lcdepot && dname != depot ) match = true @mismatch = "//" + dname break end end match end # Method to descend the tree looking for mismatched directory # names. If we find a mismatch at any level we break off and just # return true. If there are no mismatches, returns false. def mismatch_dirs( depot, dirs ) match = false path = "//" + depot dirs.each do |dir| lcpath = path.downcase p4.run_dirs( path + "/*" ).each do |d| d = d[ "dir" ] # We're in tagged mode dname = d.sub( path + "/", "" ) if ( dir.downcase == dname.downcase ) # We found a match if ( dir != dname ) match = true @mismatch = d end break end end # If we found a mismatch, we can break out now break if ( match ) # This level is now OK, we need to descend to the # next level in the tree path << "/" << dir end match 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. def report( badfiles ) errors = 0 msg = @@USER_MESSAGE badfiles.each do |file,mismatch| msg += sprintf( @@BADFILE_FORMAT, file, mismatch ) errors += 1 break if ( errors >= @max_errors ) end message( msg ) end end #-- #------------------------------------------------------------------------------- # Start of main script execution #------------------------------------------------------------------------------- #++ # By this stage it's pretty simple. Even argument validation is handled by # the P4Trigger class so we don't even need to check that we were passed # a changelist number. ct = CaseTrigger.new( 10 ) exit( ct.parse_change( ARGV.shift ) )
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#6 | 6437 | Tony Smith |
Update P4Ruby Library scripts to support Perforce P4Ruby 2007.3 rather than my old public depot P4Ruby. |
||
#5 | 5881 | Tony Smith |
Optimizations for checkcase.rb: add caching for path lookups so we don't end up looking up the same directory more than once. Also include negative lookups - those where no matching path was found in the cache. This brings the overhead of check new, large branches down to a few commands. |
||
#4 | 5854 | Tony Smith |
Bug fix: checking the case of files was broken because of clumsy re-use of a variable name in the mismatch_depot method. Thanks to Markus Spies at IDS Scheer. |
||
#3 | 5808 | Tony Smith |
Update checkcase.rb to support servers with tagged output for 'p4 depots' (2005.2 or later), and fix API level at that release to prevent compatibility problems with future servers. |
||
#2 | 3637 | Tony Smith | Add RDoc documentation to the sample triggers. | ||
#1 | 3634 | Tony Smith |
Kick off a library of P4Ruby resources with some sample trigger implementations based on Jeff Bowles and Wes Peters' scripts. These are not strict ports of their scripts to P4Ruby, but are roughly in the same area. |