Enforce Workspace Naming
A "form-out" Trigger Example
TASK
How do I enforce a naming convention on user workspaces that makes them readily identifiable?
SOLUTION
Use a "form-out" trigger to check the name of a workspace specification before it is edited or saved. This prevents any names that do not meet a certain criteria, or forces the name to a specific format. In the following example Perl script the trigger forces all client workspaces to start with the Perforce user name and an underscore ("username_workspacename"):
#!/usr/bin/perl -w
$user = $ARGV[0];
$formName = $ARGV[1];
$formFile = $ARGV[2];
$curr_client = $formName;
$client_name = $user . "_" . $curr_client;
# Let things continue if client already exists:
open (CLIENTS, "p4 clients |") or die "Couldn't get workspace list";
while (<CLIENTS>)
{
if (/^Client \Q$formName\E .*/) {exit 0;}
}
# If the current client name already begins with username, bail:
if ($curr_client =~ m/^$user/) {exit 0;}
# Create modified client spec based on servers form:
$modifiedForm = "";
open (FORM, "<$formFile")
or die ("Trigger could not open form file for read");
while (<FORM>)
{
if (/^Client:/) {$_ = "Client: $client_name";}
if (/(^.*\/\/)$curr_client(.*)/) {$_ = "$1" . "$client_name" .
"$2\n";}
$modifiedForm .= $_;
}
# Write modified form file over servers version:
open (MODFORM, ">$formFile")
or die ("Trigger could not open form file for write");
print MODFORM "$modifiedForm";
exit 0;
This is an example only, and should be modified and tested thoroughly before being used in a production server.
To use this script you need to add the following line to the triggers table:
clientNameCheck form-out client "/path_to/check_client_name.pl %user% %formname% %formfile%"Note:
This same logic can be modified for other form types (enforcing job numbering format, for example), though you might need to implement it as a "form-in" trigger to prevent the modification of form names before they are submitted to Perforce.
