Skip to content Skip to sidebar Skip to footer

Trigger To Automatically Remove EOL Whitespace?

Can one write a perforce trigger to automatically remove whitespace at submission time? Preferably in python? What would that look like? Or can you not modify files as they're b

Solution 1:

To my knowledge this cannot be done, since you cannot put the modified file-content back to the server. The only two trigger types that allow you to see the file-content with p4 print are change-content and change-commit. For the latter, the files are already submitted on the server and for the former, while you can see the (unsubmitted) file content, there is no way to modify it and put it back on the server.

The only trigger that is possible is to reject files with EOL whitespace to be submitted, so that the submitters can fix the files on their own. Here is an excerpt of a similar one that checks for tabs in files, please read the docu on triggers and look at the Perforce site for examples:

def fail(sComment):
  print sComment
  sys.exit(1)
  return

sCmd = "p4 -G files //sw/...@=%s" % sChangeNr

stream = os.popen(sCmd, 'rb')
dictResult = []
try:
  while 1:
   dictResult.append(marshal.load(stream))
except EOFError:
  pass

stream.close()


failures = []
# check all files for tabs
for element in dictResult:
  depotFile =  element['depotFile']

sCmd = "p4 print -q %s@=%s" % (depotFile,sChangeNr)
content = os.popen(sCmd, 'rb').read()
if content.find('\t') != -1:
  failures.append(depotFile)

if len(failures) != 0:
  error = "Files contain tabulators (instead of spaces):\n"
  for i in failures:
    error = error + str(i) + "\n"
  fail(error)

Post a Comment for "Trigger To Automatically Remove EOL Whitespace?"