# # 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 //.../*.jpg "perl whatever/Binary.pl %changelist% %serverport% " # exampleB //.../*.bmp "perl whatever/Binary.pl %changelist% %serverport% " # exampleB //.../*.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); }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 5093 | Hari Krishna Dara |
Populating perforce branch. I will be adding p4admin files to it. |
||
//guest/perforce_software/triggers/Binary.pl | |||||
#4 | 3588 | Sam Stafford | Pull in JAB's latest triggers changes. | ||
#3 | 376 | james | push modifications to Jeff Bowles' sample scripts live | ||
#2 | 104 | Laura Wingerd |
Publish new trigger examples (change 103, change 102, change 101, change 100, change 99, change 91) |
||
#1 | 94 | Laura Wingerd |
Re-org "triggers" directory -- it's now part of the "Perforce Utilities" project. |
||
//public/perforce/triggers/Binary.pl | |||||
#1 | 92 | Laura Wingerd | Publishing Jeff's trigger examples | ||
//guest/jeff_bowles/perforce-triggers/Binary.pl | |||||
#2 | 91 | Jeff Bowles | Making sure that all output goes to <stdout> not <stderr> | ||
#1 | 81 | Jeff Bowles | adding some trigger examples.... |