尝试正确连接 git-p4 分支

trying to hook up git-p4 branches properly

我正在使用 git-p4 将多个分支从 Perforce 导入 git,具有完整的历史记录,并且运行良好, 除了 各个分支最终都“无尾”。我确切地知道在哪个修订版(作为 Perforce 更改列表编号和现在作为 newly-created git 修订版)它们中的每一个都是从主干中分支出来的,我非常想拥有这个明确反映在我的新 git 存储库中,但我似乎无法弄清楚如何实现它。

具体来说,在导入后,如果我在我的任何分支上执行 git log,它总是有一个没有 parent 的“最后”(最早)修订。我想对其进行调整,以便修订版具有 parent,这是 master.

的特定修订版

大概我不能直接将一个分支拼接到 master 上(即,简单地设置它的 parent link),因为 git 哈希反映了完整的历史,所以我分支上的所有哈希值都可能需要调整,我对此没有意见。我可能需要某种变基操作,但同样,我似乎无法弄清楚该怎么做。到目前为止,我尝试过的所有尝试都试图“重放”更改,就好像要创建所有内容略有不同的版本,并且它们导致了很多我没有时间也不想要的合并冲突解决。每次修订的内容都非常好(由git-p4创建);我想做的就是重新调整他们 link 在一起的方式。

具体来说,我想改变这个:

进入这个:

我查看了 git-p4--detect-branches 选项,但它看起来也不像我想要的那样。如果有办法让 git p4 sync 为分支中的“最后”修订设置 parent,而不是将其孤立,那将是完美的,但我没有找到办法.

抛开 git-p4 的所有细节,我认为我想要的是(实际上)一种强制执行特定修订的方法——特别是这些孤立的(parentless)修订之一,在“我的一个分支的尾巴”——有一个 parent link 这是我选择的 SHA 散列,然后重新计算该修订版的散列(及其所有 children 的散列)反映出来。我想我可以戴上水管工的帽子并编写自己的脚本来以某种方式执行此操作,但我希望有一种受支持的方法。

您可以使用 filter-branch 来完成此操作。

  1. 分别克隆分支
  2. 使用多个遥控器合并存储库
  3. 使用 filter-branch 将父级添加到较新的分支

来自: https://labs.consol.de/development/git/2017/09/08/reunite-separate-git-repositories.html

最近使用 https://github.com/newren/git-filter-repo 可能更好 - 但我还没有尝试过。

https://github.com/newren/git-filter-repo/blob/main/Documentation/converting-from-filter-branch.md#re-grafting-history 似乎它更清楚地涵盖了这个案例。

根据您的描述,我认为您缺少 branchList 配置选项。 请阅读 git-p4 文档的 Branch Detection 部分以获取更多信息。

顺便说一下,分支算法依赖于 P4 中的初始复制操作在没有文件更改的情况下完成,以便在新分支开始的源分支中找到准确的提交。否则,新分支将在源分支的顶部创建,您可能会得到不一致的代码。

git-p4 sync 应该能够使用 Git 2.37 (Q3 2022):

正确恢复分支的起源

commit 944db25 (21 Mar 2022) by Kirill Frolov (kfrolov)
(由 Junio C Hamano -- gitster -- in commit 586f237 合并,2022 年 5 月 20 日)

git-p4: fix issue with multiple perforce remotes

Signed-off-by: Kirill Frolov

Single perforce branch might be sync'ed multiple times with different revision numbers, so it will be seen to Git as complete different commits.
This can be done by the following command:

git p4 sync --branch=NAME //perforce/path...

It is assumed, that this command applied multiple times and perforce repository changes between command invocations.

In such situation, git p4 will see multiple perforce branches with same name and different revision numbers.
The problem is that to make a shelve, git-p4 script will try to find "origin" branch, if not specified in command line explicitly.
And previously script selected any branch with same name and don't mention particular revision number.
Later this may cause failure of the command "git diff-tree -r $rev^ $rev""(man), so shelve can't be created (due to wrong origin branch/commit).

This commit fixes the heuristic by which git p4 selects origin branch:

  • first it tries to select branch with same perforce path and perforce revision,
  • and if it fails, then selects branch with only same perforce path (ignoring perforce revision number).