Push-content triggers
Use the push-content
trigger type to create triggers that
fire after changelist creation and file transfer, but prior to committing
the push to the database. Push-content triggers can access file contents
by using the p4 diff2
, p4 files
,
p4 fstat
, and p4 print
commands
with the @=change
revision specifier, where
change
is the number of the pending changelist as
passed to the trigger script in the %changelist%
variable.
Use push-content triggers to validate file contents as part of changelist submission and to abort changelist submission if the validation fails.
Even when a push-submit
or push-content
trigger script succeeds, the push can fail because of subsequent trigger
failures, or for other reasons. Use push-submit
and
push-content
triggers only for validation, and use
push-commit
triggers for operations that are contingent on
the successful completion of the push.
Example
The following push-content trigger is a Bourne shell script that ensures that every file in every changelist contains a copyright notice for the current year. The script assumes the existence of a client workspace called
copychecker
that includes all of //depot/src
.
This workspace does not have to be synced.
#!/bin/sh # Set target string, files to search, location of p4 executable... TARGET="Copyright 'date +%Y' Example Company" DEPOT_PATH="//depot/src/..." CHANGE=$1 P4CMD="/usr/local/bin/p4 -p 1666 -c copychecker" XIT=0 echo "" # For each file, strip off #version and other non-filename info # Use sed to swap spaces w/"%" to obtain single arguments for "for" for FILE in `$P4CMD files $DEPOT_PATH@=$CHANGE | \ sed -e 's/\(.*\)\#[0-9]* - .*$/\1/' -e 's/ /%/g'` do # Undo the replacement to obtain filename... FILE="'echo $FILE | sed -e 's/%/ /g''" # ...and use @= specifier to access file contents: # p4 print -q //depot/src/file.c@=12345 if $P4CMD print -q "$FILE@=$CHANGE" | grep "$TARGET" > /dev/null then echo "" else echo "Submit fails: '$TARGET' not found in $FILE" XIT=1 fi done exit $XIT
To use the trigger, add the following line to your triggers table:
sample2 push-content //depot/src/... "copydate.sh %change%"
The trigger fires when any changelist with at least one file in
//depot/src
is pushed. The corresponding
DEPOT_PATH
defined in the script ensures that of
all the files in the triggering changelist, only those files actually
under //depot/src
are checked.
The p4 change
and p4 describe
commands do not display associated fixes when run from the push-submit
or push-content triggers, even if the changes being pushed have
associated fixes that are added as part of the push.