git-p4 migrate branches in different subdirectories

后端 未结 3 1633
名媛妹妹
名媛妹妹 2020-12-30 14:29

I want to migrate source code tree from perforce to git. The source code contains several dev branches scattered across perforce depot, not necessarily in the same directory

3条回答
  •  悲哀的现实
    2020-12-30 15:03

    Summary: It works, git-p4 is a great tool, very intelligent, comes with lot of configurable options. Multiple branches scattered wherever across depot tree migrated successfully. We need to run the import at highest level (topmost) perforce directory that covers all sub-directories or branches of interest. For efficient operation, suggested to use --changesfile option, to explicitly specify changelists to be imported. Also use git-p4.branchUser and git-p4.branchList to explicitly specify branchspecs.

    Details: Here I am showing the settings that worked for me. There may be a better way to achieve the goal.

    Perforce depot structure: (as mentioned in question)

    Perforce client: This is set at highest (topmost) p4 directory. This is very important, otherwise git-p4 may exclude changelists (restricted due to client view) as empty commits.

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

    Perforce branchspecs: I created a single branchspec that covers all my branches dependency (parent/child) information

    $ 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 config items: Next, I setup an empty git repository and following config items.

     mkdir workdir
     cd workdir
     git init
    

    (** perforce variables)

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

    (** force to use perforce client spec)

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

    ( ** restrict to explore branchspecs created only by me)

    git config git-p4.branchUser myp4user
    

    ( ** branch information, dependency relation, interestingly only last name (directory name in branch path) is required to mention, git-p4 automatically detects/pick what is required i.e. fully expanding the branch name )

    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
    

    Changelists file: Next, I collected all the changelists, for all branches those I am migrating.

    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
    

    Import: Finally I ran the import as below, I used "sync" and not clone.

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

    On smaller depots “ ../git-p4.py sync //depot@all --detect-branches --verbose “ shall also work, in that case no need to create changelists file (earlier step)

    Once import is finished, I am able to see git-p4 created all remote perforce branches inside single git repository.

     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
    

    Then I created local branches from remote p4 branches

      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
    

    Next I simply added a remote origin and pushed the code into git repo.
    Thanks for various pointers/help available in stackoverflow and online.

提交回复
热议问题