Checkin samples
There are multiple ways that you can use triggers that apply before a contributor checks in any changes. Refer to the following sections for examples:
- Apply a code beautifier to your files
- Apply a modifying action to items in block
- Check that there are check in comments
Apply a code beautifier to .java files
This sample is a Ruby script that processes all java files through a code beautifier. This example uses Ruby, but you can use any tool.
Note: This example runs cm cat
individually for each affected file, runs jindent
on it, creates a shelf for the changed file, and then deletes it from the disk. To improve performance, refer to Apply a modifying action to items in block.
#!/usr/bin/env ruby
# temp file that will be used for jindent
tmpfile = "c:\\tmp\\triggers\\trigger-validate.java"
# Process each line of stdin
STDIN.readlines.each_with_index do |line, index|
# split into item, revspec and wkspec
splitted = line.split(';')
# pick item name from item spec
filename = splitted[0].split('#')[0]
# if it is a .java file, apply jindent
if (filename =~ /\.java$/) then
# revspec is after the first ;
revspec = splitted[1];
# extract revision content from repository to temp file
res = system("cm cat #{revspec} --file=\"#{tmpfile}\"")
# execute jindent on temp file (jindent should be on path)
if (res) then res = system("jindent \"#{tmpfile}\"") end
# if jindent failed, signal the trigger failed too
if (!res || $? != 0) then exit(1) end
# store the re-formatted file on Plastic repository
if (res) then system("cm shelveset #{revspec} --file=\"#{tmpfile}\"") end
# delete the temp file
if (res) then system("del \"#{tmpfile}\"") end
end #if
end #each
Sample trigger creation command (on Windows):
To create the trigger that applies the code beautifier before checkin, you can use the following command:
cm trigger create before-checkin "apply jindent" "ruby c:\triggers\jindent.rb"
Apply a modifying action to items in block
This is the same example as above, except that this example runs cm cat
and shelves the files in block, which greatly improves performance.
#!/usr/bin/ruby
tmpdir = 'c:\\tmp\\triggers\\'
$files = []
$cat_shelve_specs = []
# Apply command sending revision info
def commandOnSpecs(cmd)
IO.popen(cmd, "w") do |io|
$cat_shelve_specs.each do |spec|
puts 'catting ' + spec
io.puts spec
end
end
end
# Process stdin
STDIN.readlines.each do |line|
itemspec, revspec, wkspec = line.split(';')
filename, branchspec, revno = itemspec.split('#')
# this may have problems with long paths
filename.gsub!(/\//, '_') # replace / with _ in filenames
filename = tmpdir + filename # add tmpdir
$files << filename
$cat_shelve_specs << "#{revspec};#{filename}"
end
# cat files on temp directory
commandOnSpecs("cm cat -")
# Apply action on files
$files.each { |file| system("jindent \"#{file}\"") }
# shelve files
commandOnSpecs("cm shelveset -")
# remove temp files
$files.each { |file| File.delete file }
Sample trigger creation command (on Windows):
cm trigger create before-checkin "apply block jindent" "ruby c:\triggers\jindent.rb"
Ensure that there are check in comments
You can use a trigger to ensure that any contributor who checks in changes provides comments.
The following sample ruby script checks the PLASTIC_COMMENT
environment variable:
c = ENV['PLASTIC_COMMENT']
if (c == nil || c == '') then exit(1) end
If the PLASTIC_COMMENT
environment variable is empty and there is no comment, this script exits with a status code of 1 and the command fails. As a before-checkin
trigger, UVCS throws an exception that aborts the check in operation and notifies the user.
Sample trigger creation command:
cm trigger create before-checkin "comment required" "ruby c:\triggers\check-comments.rb"