如何在Windows系统中运行git-p4?

22

看起来git-p4不是Windows的MSys-Git项目的一部分。 有没有人知道如何在Windows上运行它?


你说得对,Git for Windows(也称为MSysGit)不支持此功能。请参见https://github.com/msysgit/msysgit/issues/108中的功能请求。 - Colonel Panic
4个回答

18

更新于2019年7月,现在Gabriel Morin提到了 "Git P4 clone broken" 问题:

[alias]
    p4 = !python.exe 'c:\\program files\\Git\\mingw64\\libexec\\git-core\\git-p4'

然后在命令行中使用git p4就可以了。

在 Git 2.32 (2021年第二季度) 中,git p4 学会更高效地查找分支点。

参见 commit 6b79818commit c3ab088 (2021年5月5日),由 Joachim Kuebart (jkuebart) 提交。
(合并于 commit e289f68,2021年5月14日,由 Junio C Hamano -- gitster -- 审核)

git-p4:加速搜索分支的父级

签署者:Joachim Kuebart
协助者:Junio C Hamano
协助者:Luke Diamand

For every new branch that git-p4 imports, it needs to find the commit where it branched off its parent branch.
While p4 doesn't record this information explicitly, the first changelist on a branch is usually an identical copy of the parent branch.

The method searchParent() tries to find a commit in the history of the given "parent" branch whose tree exactly matches the initial changelist of the new branch, "target".
The code iterates through the parent commits and compares each of them to this initial changelist using diff-tree.

Since we already know the tree object name we are looking for, spawning diff-tree for each commit is wasteful.

Use the "--format" option of "rev-list" to find out the tree object name of each commit in the history, and find the tree whose name is exactly the same as the tree of the target commit to optimize this.

This results in a considerable speed-up, at least on Windows.
On one Windows machine with a fairly large repository of about 16000 commits in the parent branch, the current code takes over 7 minutes, while the new code only takes just over 10 seconds for the same changelist:

Before:

$ time git p4 sync
Importing from/into multiple branches
Depot paths: //depot
Importing revision 31274 (100.0%)
Updated branches: b1

real    7m41.458s
user    0m0.000s
sys     0m0.077s

After:

$ time git p4 sync
Importing from/into multiple branches
Depot paths: //depot
Importing revision 31274 (100.0%)
Updated branches: b1

real    0m10.235s
user    0m0.000s
sys     0m0.062s

2020年3月更新

Git 2.27(2020年第二季度)更新了"git p4"以与Python 3兼容。

查看commit 6bb40ed(2020年1月23日)由SZEDER Gábor (szeder)提交。
查看commit 7575f4f, commit ce425eb, commit 2e2aa8d, commit a6b1306, commit 4294d74, commit 50da1e7, commit 5a5577d, commit d38208a, commit 86dca24, commit 6cec21a, commit 1f8b46d, commit 0b4396f(2019年12月13日)由Yang Zhao (yangminz)提交。
查看commit 484d09c(2019年12月13日)由Ben Keene (seraphire)提交。
(由Junio C Hamano -- gitster --commit 9a0fa17中合并,于2020年3月25日)

ci:在linux-gcc和osx-gcc中使用python3,在其他地方使用python2

签名:SZEDER Gábor

Python2已经终止生命周期,我们一直在准备将Python脚本与Python3配合使用。

'git p4',作为主要的内部用户,刚刚收到了许多兼容性更新。

这仅在Git 2.30(2021年第1季度)中记录在commit 263dc0中。


2018年7月更新:

Git 2.19 (2018年第三季度)将开始为git p4引入Python 3支持。

查看commit db2d997, commit f2606b1, commit efdcc99, commit 4d88519, commit dba1c9d, commit fc35c9d (2018年6月19日) 由 Luke Diamand (luked99) 提交。
(由 Junio C Hamano -- gitster -- 合并于 commit 6dcd364,2018年7月18日)

准备代码,使得"git p4"更接近与Python 3兼容。

2016年1月更新:

自2010年以来,contrib/fast-import/git-p4.README提到自2012年4月起。

git-p4脚本已移动到git源目录的顶层。 请注意,顶级git-p4.py脚本现在是源代码。

Windows用户可以直接复制git-p4.py源脚本,可能需要通过同一文件夹中名为"git-p4.bat"的批处理文件调用。
它应该只包含一行:

@python "%~d0%~p0git-p4.py" %*

虽然这篇2013年的文章直接调用了git-p4.py


原始回答(2010年4月)

如 "为什么Perforce比Git更具可伸缩性" 中所述,请注意不要在 Git 中导入 任何 p4 存储库。Git 无法处理 P4 可以管理的同一类型的巨大唯一存储库。

由于 Git-P4 是用 Python 编写的,因此它应该可以在 Windows 上运行(例如在 这个问题 中)

您可以在此处获取 git-p4.py 并将其用作 git-p4.bat

@python "%~d0%~p0git-p4" %*

需要安装 Python 2.x。

不要忘记还有另一种方法


安装Python 2.x并添加[alias]部分让我有了一些进展,但是git p4的命令行似乎没有很好的文档记录。git p4 clone //depot/mainline/...@all c:\path\to\git\clone似乎已经起作用了... - Jake
我还在Git安装目录下复制了一个无扩展名的文件git-p4,并将其命名为git-p4.py,但这可能是不必要的。 - Jake
@Jake,你使用的Git版本是什么?最新版本需要Python 3才能更好地运行git p4。 - VonC
"git版本为2.36.1.windows.1",使用Python 2.7.18,在Windows 10 Home 21H1 19043.1706上运行。现在已经将所有的修订历史记录从Perforce导入GitHub,并且看起来很好。" - Jake

6

我找到的最简单的答案,并且目前似乎完美无缺,来自Timo Geusch的博客

  • 确保您已安装Python 2.X,例如在C:\Python27中(截至2014年3月,git-p4.py不支持Python 3.X,因此我没有冒险尝试)
  • 从任何地方获取git-p4.py的副本,例如根据Eric Lathrop的博客文章建议,直接从 git repo获取(我从已经设置好的Linux版本的git 2.1中获取了我的)
  • 在MSysGit安装中,导航到libexec\git-core,例如在我的Windows 7 64位上安装的Git for Windows 1.9.4-preview20140611中,它是C:\Program Files (x86)\Git\libexec\git-core
  • 将您的git-p4.py文件放置在那里
  • 在同一位置,编辑“git-p4”扩展名为空的文件,用单行替换虚拟的echo和exit行

    c:/python27/python "c:/program files (x86)/Git/libexec/git-core/git-p4.py" $1 $2 $3 $4 $5 $6 $7 $8 $9

就这样。现在在您的MSysGit / Git for Windows安装上,git-p4可以像在任何Linux系统上一样工作。

(请注意,Eric Lathrop的现有答案提供了大量关于使用git-p4及其所有限制的其他信息;但我认为该问题可以从一个简单的howto回答中受益,即轻松在Windows上使其工作,以免更多的漫游者像我上周一样感到沮丧)


1
请注意,如果Python在您的PATH中,您可以直接将git-p4.py放在虚拟的libexec/git-core/git-p4之上。 - Paul Du Bois
Python3自带一个脚本_2to3.py_(在目录Tools/Scripts中),可用于将为Python2编写的脚本转换为在Python3下运行的脚本,但我尚未测试它是否适用于_git-p4.py_。 转换命令为:python 2to3.py -w git-p4.py(假设_2to3.py_和_git-p4.py_都在当前目录中)。 - user1364368

5

感谢您发布答案!请注意,您应该在此网站上发布答案的基本部分,否则您的帖子可能会被删除请参阅FAQ中提到的“几乎只是链接”的答案。 如果您愿意,仍然可以包含链接,但仅作为“参考”。答案应该能够独立存在,不需要链接的支持。 - Taryn
请注意,您不需要旧版本的msysgit;请参考下面的Tao的答案。 - Paul Du Bois
链接无法加载。遗憾的是,这里没有发布详细信息。我已经投反对票并建议删除此答案。 - Jake

1

这可能就是 git 命令行所做的一切,但最好还是将其设置为使用文档化的 CLI。 - Jake

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接