如何使用 python 在 perforce 中创建分支并避免编辑器通过参数传递映射

How to create a branch in perforce with python and avoid the editor passing the mapping by argument

我想用 python 在 perforce 中创建一个新分支,我使用了:self._p4.run("branch", name) 但是,每次我使用它时,它都会自动打开一个编辑器来插入映射。

我的问题是,是否可以在不打开编辑器的情况下创建新分支,在完美的世界中,我可以将映射作为字符串列表传递。

谢谢!

如果你想自己控制映射,避免编辑器,你只需要使用p4 branch -i并自己传递已经填写的表格。

因此,例如:

  1. p4 branch -o name > branchData.txt将分支表格生成到临时文件
  2. 根据需要修改 branchData.txt(例如,将映射更改为所需的集合)
  3. p4 branch -i < branchData.txt 将您的分支数据加载到 Perforce 服务器并在服务器中创建新的分支规范。

当然,如果您的程序已经知道如何设置分支规范数据,则您实际上不需要执行步骤 (1)。只需将所需的分支规范数据放入文件中,然后将 运行 p4 branch -i 与标准输入重定向到该文件。或者,甚至,您可以直接将表单数据从您的程序输入 p4 branch -i,因为它只是从标准输入中读取。

在 P4Python 中,您可以通过 fetch_specsave_spec 方法编辑规范:

https://www.perforce.com/perforce/r14.2/manuals/p4script/python.p4.html#python.p4.fetch_spectype https://www.perforce.com/perforce/r14.2/manuals/p4script/python.p4.html#python.p4.save_spectype

等同于 p4 <spec> -op4 <spec> -i,但它们将规范与字典相互转换以使其更易于操作。创建分支规范类似于:

branch = p4.fetch_branch(name)
branch["View"] = ["//depot/main/... //depot/"+name+"/..."]
p4.save_branch(branch)