Documentation

Support

Unity Version Control

Checkin samples

Refer to examples of triggers that run before a check in operation.
Read time 2 minutesLast updated 21 days ago

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 .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.
#!/usr/bin/env ruby# temp file that will be used for jindenttmpfile = "c:\\tmp\\triggers\\trigger-validate.java"# Process each line of stdinSTDIN.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 #ifend #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/rubytmpdir = 'c:\\tmp\\triggers\\'$files = []$cat_shelve_specs = []# Apply command sending revision infodef commandOnSpecs(cmd) IO.popen(cmd, "w") do |io| $cat_shelve_specs.each do |spec| puts 'catting ' + spec io.puts spec end endend# Process stdinSTDIN.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 directorycommandOnSpecs("cm cat -")# Apply action on files$files.each { |file| system("jindent \"#{file}\"") }# shelve filescommandOnSpecs("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"