2017-07-31 54 views
1

我試圖在Java程序中使用P4java來克隆P4存儲庫。如何使用p4java克隆p4 repo?

如何一次克隆整個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 
    } 
} 

任何幫助表示讚賞。

+1

它爲什麼必須在Java中?爲什麼不直接運行'p4 clone'?或者,如果您只想獲取一組文件的頭部修訂版本,請創建一個工作區並同步它? –

回答

0

我發現this解決方案,工作正常。 (感謝博客)。 基本上,它創建一個新的臨時客戶端並使用filespec列表(庫文件名)和p4 sync命令克隆回購。 編輯代碼在這裏:

{ 
    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斯塔福德給我正確的方向。

1

您的示例程序正在做什麼(獲取每個文件的頭部轉速並編寫本地副本)正是sync命令所做的。我建議您只使用sync

相關問題