check_format_rel_name.rb #2

  • //
  • guest/
  • robert_cowham/
  • perforce/
  • utils/
  • triggers/
  • check_format_rel_name.rb
  • View
  • Commits
  • Open Download .zip Download (3 KB)
#!/usr/bin/ruby
#--
#-------------------------------------------------------------------------------
#++
#
#= Introduction
#
#== Name:   check_format_rel_name.rb
#
#== Author: Robert Cowham  <[email protected]>
#
#== Description 
# 
#     Example trigger to enforce release naming convention
#     e.g. //software/releases/release_NN_NN.br/...
#     to avoid no-leading zeroes etc.
#
#== Requires
#     Ruby
#     P4Ruby
#     P4Triggers module
#
#== Example 'triggers' section:
#
#     Restrict to only releases area of the repository
#
#    Parameters Pass in serverport/username/changelist
#     Triggers:
#       release_check  change-submit //software/releases/...  "c:\ruby\bin\ruby whatever/check_format_rel_name.rb -p %serverport% -u perforce %changelist%"
#
#--
#-------------------------------------------------------------------------------
#++
$:.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 RelNameTrigger < P4Trigger

    @@EXCEPTIONS = %w{
        //software/releases/some-exception_01_05.br
        //software/releases/release_01p_00.br
        //software/releases/release_01p_01.br
      }

    def initialize(options)
        @exceptions = @@EXCEPTIONS.collect{|e| Regexp.new(e)}
        super(options)
    end

    #
    # The error message we give to the user
    #
    @@USER_MESSAGE = 
        "\n\n" +
        "Your submission has been rejected because the release name was of the\n" +
        "wrong format, i.e. not: //software/releases/release_nn_nn.br\n\n"

    # Enforce name checking.
    def validate()
        re_valid_file = Regexp.new('//software/releases/release_\d\d_\d\d.br/')
        change.each_file do
            |file|
            # Ignore files not open for add
            next unless ( file.revisions[ 0 ].action == "add" ||
                          file.revisions[ 0 ].action == "branch" )
       
            if file.depot_file !~ re_valid_file
                if @exceptions.select{|re| re.match(file.depot_file)}.size == 0
                    message(@@USER_MESSAGE)
                    return false
                end
            end
        end
        return true
    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.
trig = RelNameTrigger.new(P4TriggerOptions.new(ARGV))
result = trig.parse_change(ARGV.shift) 
exit(result)
# Change User Description Committed
#2 5664 Robert Cowham Add new example and also update docs to link to it.
#1 5663 Robert Cowham New version with extra flags -f etc.