5

What is the proper method to do a selective import of a large Perforce repo?

The git-p4 docs mention that you can do a -//depot/main/ignore switch to filter directories. Would this be equivalent to running a git filter-branch to remove the same directories after a clone?

Additionally, it appears Perforce provides another feature called a "client" view. I have not used Perforce before, so I am a little unfamiliar with the usage model. My current understanding is that one would use p4 somehow to setup a proper client view before running git p4 clone. Does anyone have the complete details?

cmcginty
  • 113,384
  • 42
  • 163
  • 163

1 Answers1

7

A Perforce clientspec determines what parts of the Perforce repository are visible (and will be synced) to the Perforce client. The clientspec also provides a mapping from Perforce repository paths to local paths.

You can prune a Perforce client by selectively including parts of the Perforce repository. For example

//depot/main/path1/... //your-perforce-client/main/path1/...
//depot/main/path2/... //your-perforce-client/main/path2/...

will include only //depot/main/path1/ and //depot/main/path2/ and not //depot/main/path3/. As you've noted, you also can explicitly exclude paths. For example

//depot/main/path1/... //your-perforce-client/main/path1/...
-//depot/main/path1/foo/... //your-perforce-client/main/path1/foo/...

will include everything in //depot/main/path1/ except files under its foo subdirectory.

Depending on how your Perforce repository is structured and depending on what you want to include (or exclude), you potentially could tell git-p4 directly which parts of the Perforce tree you want to import:

git p4 clone --destination=/path/to/new/git/tree //depot/path1 //depot/path2

If you want to use exclusions or if you want to adjust how Perforce depot paths are mapped to local paths, you will need to add the --use-client-spec option. You can configure which Perforce client should be used by creating a .p4config file in your Git tree's parent directory containing:

P4CLIENT=YOUR_PERFORCE_CLIENT_NAME

and then setting an environment variable:

P4CONFIG=.p4config

Doing this will cause p4 to look for a .p4config file in the current directory (and then progressively search parent directories) for Perforce configuration data.

The files that will be imported will be the intersection of paths included by the Perforce clientspec and by the paths explicitly provided on the git p4 clone command-line.

(As you mentioned, git-p4 clone does allow excluding paths by prefixing them with -. However, I do not recommend doing this because that means that those paths will be excluded only on the initial import. If files in that path are touched in Perforce in the future, performing git p4 rebase/git p4 sync will pick up those changed files (unless you remember to explicitly exclude them on the command-line again). Initially importing using --use-client-spec, however, will set a flag in .git/config that allows it to be honored automatically when using git p4 rebase/git p4 sync in the future.)


One caveat is that performing a selective clone will add extra complication if someday you want to include other parts of the Perforce repository. See my answer to "Extending git-p4 clientspec after initial clone" if you need to do that.

jamesdlin
  • 81,374
  • 13
  • 159
  • 204
  • Can I ask when running this command "git p4 clone --destination=/path/to/new/git/tree //depot/path1 //depot/path2" it would take code from path1 and path2 and place them into one git directory ? here the directory is named git/tree/ ? – zheng yu Nov 23 '17 at 10:01
  • @zhengyu I doubt it would work. There would be no way to send git changes back to Perforce because it would be ambiguous whether a new file belonged to `//depot/path1` or to `//depot/path2`, and what would happen if the same filename existed in both? (You of course could just try it yourself to see what happens; there's no harm in doing so.) – jamesdlin Nov 23 '17 at 12:23
  • ```sh git p4 clone --destination=/path/ //depot/path1 //depot/path2 ``` so could you tell me after running this command, how the two paths (1 and 2) from depot will look like in git repo ? two folders in git or ? – zheng yu Nov 23 '17 at 12:58
  • @zhengyu Again, why don't you just try it yourself and see what happens? If you don't like the results, you can delete the git repository you just created. – jamesdlin Nov 23 '17 at 18:07
  • Thanks for your explanation of --use-client-spec and .p4config. I couldn't find a decent description neither in the p4 nor in git p4 docu. You just have a typo in the P4CLIENT variable. In your example, I believe it should be P4CLIENT=your-perforce-client. – Fergie Mar 20 '20 at 17:32
  • @Fergie Oops! Thanks for catching the mistake. – jamesdlin Mar 20 '20 at 17:44