1

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 being submitted?

Charles
  • 50,943
  • 13
  • 104
  • 142
i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
  • I don't know much about perforce, but the [docs](http://www.perforce.com/perforce/r10.2/manuals/cmdref/triggers.html) show it is possible. You'd need to write a script that calls the python function `rstrip()`[[docs](http://docs.python.org/library/stdtypes.html#str.rstrip)] and then call that using a `change-content` trigger type. – brc Oct 18 '11 at 23:28
  • 1
    I'd be careful about doing something like this automatically... you wouldn't want to strip whitespace from where someone had intended it. (admittedly, a strange case, unless you're programming in whitespace, or over zealously stripping a binary file.) If you do go this route, warn them and show them the diff of the change you're making, so they can validate that it is good. – Thanatos Oct 19 '11 at 07:25

1 Answers1

0

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)
jhwist
  • 15,201
  • 3
  • 40
  • 47
  • This is what I had suspected. In that case I think what I'll do is write a p4 wrapper on the client that runs pre-presubmit tests locally, and this can be one of them. – i_am_jorf Oct 19 '11 at 15:09
  • @jhwist I know this is an old questions, but I'm confused by the above script. Won't it always return 1 and thus always fail the submit (meaning it is impossible to submit files to the p4 server). Also do you not need to import os, sys ? Also where does sChangeNr get defined? – Lorenz03Tx Apr 23 '13 at 22:03
  • @Lorenz03Tx I've left out a lot of boilerplate for brevity reasons. – jhwist Apr 26 '13 at 11:36
  • Sorry to be pedantic, but the script will always fail and thus never allow any check ins to the perforce server. That is more than just bolierplate. The indention of everything bellow the for loop is wrong, and as you know whitespace is significant in python and as such should be fixed. My suggested edit for the script was rejected (I'm guessing by reviewers who are un familiar with python) or I'd simply fix it rather than bother you over a 2 year old question. – Lorenz03Tx May 20 '13 at 15:18