触发自动删除 EOL 空格?

发布于 2024-12-10 18:54:34 字数 74 浏览 0 评论 0原文

可以编写一个 perforce 触发器来在提交时自动删除空格吗?最好用Python?那会是什么样子?或者您不能在提交文件时修改它们吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

も让我眼熟你 2024-12-17 18:54:34

据我所知,这是无法完成的,因为您无法将修改后的文件内容放回服务器。唯一允许您查看的两种触发器类型 p4 print 的文件内容是 change-contentchange-commit。对于后者,文件已经提交到服务器上,而对于前者,虽然您可以看到(未提交的)文件内容,但无法修改它并将其放回到服务器上。

唯一可能的触发器是拒绝提交带有 EOL 空格的文件,以便提交者可以自行修复文件。以下是检查文件中选项卡的类似内容的摘录,请阅读有关触发器的文档并查看 Perforce 网站的示例:

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文