# # Example trigger to enforce a rule "files of suffix .x need to be added # as type Z". (For example, ".jpg" files must always be "binary" and ".sh" # files should always be "text".) # # This script will do the following: # 1. Return "success" (exit code 0) if the change has # no applicable files in the changelist; # 2. Otherwise, it checks that the files you've given are # being submitted with the other component in that pair, and # if not, reports an error. # # Unix usage: # perl /whatever/Binary.pl %changelist% %serverport% # NT usage: # c:/perl/bin/perl.exe c:/whatever/Binary.pl %changelist% %serverport% # (Note that the name of this script might need to be an "8.3" filename, # depending on the version of Perl you're running.) # Example 'triggers' section: # (Note: since this is applicable to only filenames with certain suffixes, you # might want to restrict the trigger to run when those files are submitted. # So, if this is looking at .cpp/.h/.txt/.html lines, you might want to have # it run only on those files.) # Triggers: # exampleB change-submit //.../*.jpg "perl whatever/Binary.pl %changelist% %serverport% " # exampleB change-submit //.../*.bmp "perl whatever/Binary.pl %changelist% %serverport% " # exampleB change-submit //.../*.sh "perl whatever/Binary.pl %changelist% %serverport% " # # Tested on Platforms: FreeBSD, NT (as program, not service). # # You might need to... # 1. You might need to run change the "$p4 = ......." line, below, to add a username # and password ('p4 -u hardcodedusername -P hardcodeduserpasswd -p $ServerPort') # if the default user it's connecting as isn't appropriate. # $ChangeNum = $ARGV[0]; $ServerPort = $ARGV[1]; $p4 = "p4 -p $ServerPort"; $nerrs = 0; $MaxErrs = 10; $OptimizeErrorOutput = "yes"; #-------- enter the suffixes for the pairs, here ----------------------------------- $filetype{"sh"} = ".*text"; # x.sh is text $errmsg{"sh"} = "filetype of .sh files should be text/xtext/ktext"; $filetype{"jpg"} = ".*binary"; # x.jpg is binary $errmsg{"jpg"} = "filetype of .jpg files should be binary"; #----------------------------------------------------------------------------------- Fatal("Changelist $ChangeNum (1st arg) needs to be numeric!\n") unless ($ChangeNum =~ /^\d+$/); Fatal("\%serverport\% (2nd arg) wasn't specified.\n") if ($ServerPort eq ""); # cannot use 'p4 describe -s changenum', so we get a list of open files # this way... @OpenedList = `$p4 opened -a -c $ChangeNum 2>&1`; # # Algorithm used: # First, don't bother with files that aren't being added/deleted. # (We only look at revision #1 of a file to notice that.) # # Pry out the suffix/type for each file being submitted, # and pass it to "CheckType". # It'll pass back "-1" for each bad file type. # # open(LOG, ">c:/temp/trigger.$ChangeNum.txt") || die "Cannot open log\n"; chomp(@OpenedList); # print LOG @OpenedList; foreach (@OpenedList) { my($tmp); # print LOG "Examining $_\n"; next unless /^(\/\/.*)#1 - .*\s$ChangeNum\s\((\S+)\)\s.*/; if (CheckType($1, $2, %filetype) < 0) { $nerrs++; } if ($nerrs >= $MaxErrs) { # close(LOG); Fatal("*** Only first $MaxErrs errors shown\n"); last; } } if ($nerrs > 0) { # close(LOG); Fatal("Errors found, submission refused\n"); # Note, this prints to STDOUT. See above. } # close(LOG); exit(0); sub CheckType { my($fname, $filetype, %filetypes) = @_; my($suffix) = $1 if ($fname =~ /^.*\.([^\.]+)$/); if ($1 ne "" && defined($filetypes{$suffix}) && $filetype !~ /^$filetypes{$suffix}/ ) { Warn("$fname: $errmsg{$suffix}.\n"); return (-1); } return 0; } ############################################################################### # Note that all messages (warning/fatal/info) go to STDOUT, not STDERR. Trigger # output to standard output is sent to the user; standard error isn't. ############################################################################### sub TellUser { my($str, $msgtype) = @_; print "$str"; print "\n" unless ($str =~ /\n$/); exit(1) if ($msgtype eq fatal); } sub Warn { TellUser(@_, warning); } sub Fatal { TellUser(@_, fatal); } sub Inform { TellUser(@_, inform); }