git-p4在不同的子目录中迁移分支

时间:2016-06-03 06:30:05

标签: git branch perforce git-p4

我想将源代码树从perforce迁移到git。源代码包含分散在perforce depot中的几个dev分支,不一定在同一目录中。例如,结构是这样的 -

//depot/dev/project/master 
//depot/dev/project/branch1 
//depot/dev/project/branch2
//depot/dev/sub-project/branch3 
//depot/dev/sub-project/branch4 
//depot/patch-project/branch5 
//depot/patch-project/special/developern/branch6 

我通过了git-p4文档https://git-scm.com/docs/git-p4分支检测部分以及类似的文章http://forums.perforce.com/index.php?/topic/1395-git-p4-and-multiple-branches/

我能够迁移具有历史记录的分支,例如

之下的直接分支
 //depot/dev/project/branch1 and 
 //depot/dev/project/branch2 

我无法实现的是如何一次将所有六个分支迁移到一起。

我在指定分支规范后尝试在// depot @ all级别上运行迁移,但由于perforce服务器很大,它会失败,它会提供maxresults异常或会话超时。有人可以指导如何处理这种情况吗?

我看到的另一个选项是单独迁移分支(一个分支到一个git repo),然后将所有分支合并到一个新的git仓库中。我不确定这样做会产生什么影响/不利影响。

Thanks and Regards,
Amar Kumbhar.

3 个答案:

答案 0 :(得分:9)

摘要:它有效,git-p4是一个很棒的工具,非常智能,有很多可配置的选项。多个分支分散在跨库存树成功迁移的任何位置。我们需要在最高级别(最顶层)的perforce目录中运行导入,该目录涵盖所有子目录或感兴趣的分支。为了有效运行,建议使用 - changesfile 选项,明确指定要导入的更改列表。还可以使用 git-p4.branchUser git-p4.branchList 来明确指定branchspecs。

详细信息:我在这里展示了适合我的设置。可能有更好的方法来实现目标。

Perforce depot结构:(如上所述)

Perforce客户端:这是在最高(最顶层)的p4目录中设置的。这非常重要,否则git-p4可能会将更改列表(由于客户端视图而受限制)排除为空提交。

   //depot/... //myp4client/...

Perforce branchspecs:我创建了一个包含所有分支依赖(父/子)信息的branchspec

$ p4 branch -o test1 | grep "//"

    //depot/dev/project/master/... //depot/dev/project/branch1/...
    //depot/dev/project/master/... //depot/dev/project/branch2/...
    //depot/dev/project/branch1/... //depot/dev/sub-project/branch3/...
    //depot/dev/project/branch1/... //depot/dev/sub-project/branch4/...
    //depot/dev/project/master/... //depot/patch-project/branch5/...
    //depot/patch-project/branch5/... //depot/patch-project/special/developern/branch6

git-p4配置项:接下来,我设置了一个空的git存储库并跟随配置项。

 mkdir workdir
 cd workdir
 git init

(** perforce变量)

git config git-p4.user myp4user
git config git-p4.passwowrd myp4password
git config git-p4.port myp4port
git config git-p4.client myp4client

(**强制使用perforce客户端规范)

git config git-p4.useClientSpec true
git config git-p4.client myp4client

(**限制探索仅由我创建的branchspec)

git config git-p4.branchUser myp4user

(**分支信息,依赖关系,有趣的只有姓氏(分支路径中的目录名)需要提及,git-p4自动检测/选择所需内容,即完全扩展分支名称)

git config git-p4.branchList master:branch1
git config --add git-p4.branchList master:branch2
git config --add git-p4.branchList branch1:branch3
git config --add git-p4.branchList branch1:branch4
git config --add git-p4.branchList master:branch5
git config --add git-p4.branchList branch5:branch6

更改列表文件接下来,我为所有正在迁移的分支机构收集了所有更改列表。

p4 changes //depot/dev/project/master/...  | cut -d' ' -f2 >> master.txt
p4 changes //depot/dev/project/branch1/...  | cut -d' ' -f2 >> master.txt
p4 changes //depot/dev/project/branch2/...  | cut -d' ' -f2 >> master.txt
p4 changes //depot/dev/sub-project/branch3/...  | cut -d' ' -f2 >> master.txt
p4 changes //depot/dev/sub-project/branch4/...  | cut -d' ' -f2 >> master.txt
p4 changes //depot/patch-project/branch5/...  | cut -d' ' -f2 >> master.txt
p4 changes //depot/patch-project/special/developern/branch6/...  | cut -d' ' -f2 >> master.txt

sort -n master.txt | uniq > master_sorted.txt

导入:最后我运行了如下导入,我使用了“sync”而不是克隆。

cd workdir 
../git-p4.py sync //depot/... --detect-branches --verbose --changesfile /home/myp4user/master_sorted.txt

在较小的软件仓库中“../git-p4.py sync // depot @ all --detect-branches --verbose”也可以工作,在这种情况下无需创建更改列表文件(前面的步骤)

导入完成后,我可以看到git-p4在单个git存储库中创建了所有远程perforce分支。

 git branch -a
  remotes/p4/depot/dev/project/master
  remotes/p4/depot/dev/project/branch1
  remotes/p4/depot/dev/dev/project/branch2
  remotes/p4/depot/dev/dev/sub-project/branch3
  remotes/p4/depot/dev/dev/sub-project/branch4
  remotes/p4/depot/patch-project/branch5
  remotes/p4/depot/patch-project/special/developern/branch6

然后我从远程p4分支创建了本地分支

  git checkout -b master  remotes/p4/depot/dev/project/master
  git checkout -b branch1  remotes/p4/depot/dev/project/branch1
  git checkout -b branch2   remotes/p4/depot/dev/dev/project/branch2
  git checkout -b branch3   remotes/p4/depot/dev/dev/sub-project/branch3
  git checkout -b branch4   remotes/p4/depot/dev/dev/sub-project/branch4
  git checkout -b branch5   remotes/p4/depot/patch-project/branch5
  git checkout -b branch6   remotes/p4/depot/patch-project/special/developern/branch6

接下来我只是添加了一个远程源并将代码推送到git repo 感谢stackoverflow和在线提供的各种指针/帮助。

答案 1 :(得分:0)

最新版本的git-p4不应该报告maxresults例外,因为它一次最多可以检索500个更改。您可以尝试使用--changes-block-size参数修改此值,这可能有助于您克服报告的问题。

这里是对这个论点的描述,可以看出here

--changes-block-size <n>::
    The internal block size to use when converting a revision
    specifier such as '@all' into a list of specific change
    numbers. Instead of using a single call to 'p4 changes' to
    find the full list of changes for the conversion, there are a
    sequence of calls to 'p4 changes -m', each of which requests
    one block of changes of the given size. The default block size
    is 500, which should usually be suitable.

答案 2 :(得分:0)

我之前遇到过类似的问题,在我的情况下,我必须找到一个你所描述的解决方法,我将每个分支克隆到它自己的Git仓库中。

即使这样,个别分支机构的对象也太多,P4管理员也不愿意提高所请求对象的限制,所以我不得不限制我克隆的历史数量。

对于非活动分支,我只会克隆最新版本并丢弃其所有历史记录。

对于更活跃的分支机构,我将保留4-6周的历史。

这意味着您必须手动确定要保留的历史记录数量的CL编号:

来自git-p4 docs(部分:DEPOT PATH SYNTAX):

$ git p4 clone //depot/my/project@1,6 # Import only changes 1 through 6.