1

我正在尝试在 Java 程序中克隆 P4 存储库,同样使用 P4java。

如何一次克隆整个 P4 存储库?,而不是一次读取一个文件(如下所示)?

fileList = server.getDepotFiles(FileSpecBuilder.makeFileSpecList(new String[] {"//depot/dir/apps/..."}), false);
for (IFileSpec fileSpec : fileList){
    if (fileSpec != null){
            BufferedReader br = new BufferedReader(new InputStreamReader(fileSpec.getContents(true)));
    // create new file locally and write content
    }
}

任何帮助表示赞赏。

4

2 回答 2

1

您的示例程序正在执行的操作(获取每个文件的 head rev 并写入本地副本)正是该sync命令执行的操作。我建议您只使用它sync

于 2017-07-31T17:56:18.533 回答
0

我找到了这个解决方案,工作正常。(感谢博客)。基本上,它会创建一个新的临时客户端并使用 filespec list(depot 文件名) 和 p4 sync 命令克隆 repo。此处编辑代码:

{
    InputStream input = new FileInputStream(getPerforceDetails);
    Properties prop = new Properties();
    prop.load(input);

    url = prop.getProperty("url"); //ex: perforce.xxx.xxxxxxx.com:port#
    repo = prop.getProperty("repo"); //ex: //depot/xxx/xxxx/apps/...
    username = prop.getProperty("username");
    password = prop.getProperty("password"));

    final String url = IServerAddress.Protocol.P4JAVA.toString() + "://" + serverUri;

    server = ServerFactory.getServer(url, null);
    server.connect();
    server.setUserName(username);       
    server.login(password);

    IServerInfo info = server.getServerInfo();
    System.out.println( "Server Info \n" + info);
    fileList = server.getDepotFiles(FileSpecBuilder.makeFileSpecList(new String[] {prop.getProperty("repo")}), false);
    //comment above line if you have only few files to clones and refer blog mentioned.

    // Creating new temporary client
    IClient tempClient = new Client();          
    tempClient.setName("tempClient" + UUID.randomUUID().toString().replace("-", ""));
    tempClient.setRoot("c:/tempP4"); 
    tempClient.setServer(server);

    // Setting the client as the current one for the server
    server.setCurrentClient(tempClient);

    // Creating Client View entry
    ClientViewMapping tempMappingEntry = new ClientViewMapping();

    // Setting up the mapping properties
    tempMappingEntry.setLeft("//depot/gxxxx/hxxxxxx/ixxxx/...");
    tempMappingEntry.setRight("//" + tempClient.getName() + "/...");
    tempMappingEntry.setType(EntryType.INCLUDE);     
    // Creating Client view
    ClientView tempClientView = new ClientView();
    // Attaching client view entry to client view
    tempClientView.addEntry(tempMappingEntry);
    tempClient.setClientView(tempClientView);
    // Registering the new client on the server
    System.out.println(server.createClient(tempClient));
    fileList = server.getDepotFiles(FileSpecBuilder.makeFileSpecList(new String[] {prop.getProperty("repo")}), false);
    try{
    // Forming the FileSpec collection to be synced-up
          List<IFileSpec> fileSpecsSet =     FileSpecBuilder.makeFileSpecList(pathsUnderDepot);
          // Syncing up the client
          //tempClient.sync(FileSpecBuilder.getValidFileSpecs(fileSpecsSet), true, false, false, false);
          //uncomment above line, n comment below if you are cloning only few files                 
          tempClient.sync(fileList, true, false, false, false);
    }finally{
        // Removing the temporary client from the server
          System.out.println(server.deleteClient(tempClient.getName(), false));
   }
 }

感谢@Sam Stafford 给了我正确的方向。

于 2017-08-07T11:47:46.177 回答