【问题标题】:Perforce Api - How to command "get revision [changelist number]"Perforce Api - 如何命令“获取修订 [更改列表编号]”
【发布时间】:2014-01-27 07:17:12
【问题描述】:

我想使用 Perforce .NET API (C#) 实现 Perforce 命令“Get Revision [Changelist Number]”。我目前有“获取最新版本”的代码,但我需要对其进行修改以获取特定的更改列表。

要将数据与变更列表编号同步,我应该怎么做?

来源

// --------Connenct----------------
Perforce.P4.Server server = new Perforce.P4.Server(
    new Perforce.P4.ServerAddress("127.0.0.1:9999"));
Perforce.P4.Repository rep = new Perforce.P4.Repository(server);
Perforce.P4.Connection con = rep.Connection;

con.UserName = m_P4ID;
string password = m_P4PASS;

Perforce.P4.Options opconnect = new Perforce.P4.Options();
opconnect.Add("-p", password);
con.Connect(opconnect);

if (con.Credential == null)
    con.Login(password);

//----------Download----------
string clientPath = @"C:\P4V\";
string ws_client = clientPath;
Perforce.P4.Client client = new Perforce.P4.Client();
client.Name = ws_client;
client.Initialize(con);

con.CommandTimeout = new TimeSpan(0);
IList<Perforce.P4.FileSpec> fileList = client.SyncFiles(new Perforce.P4.Options());

//----------Disconnect------------
con.Disconnect();
con.Dispose();

编辑:尝试 1

Perforce.P4.DepotPath depot = new Perforce.P4.DepotPath("//P4V//");
Perforce.P4.LocalPath local = new Perforce.P4.LocalPath(ws_client);
Perforce.P4.FileSpec fs = new Perforce.P4.FileSpec(depot, null, local,
    Perforce.P4.VersionSpec.Head);

IList<Perforce.P4.FileSpec> listFiles = new List<Perforce.P4.FileSpec>();
listFiles.Add(fs);
IList<Perforce.P4.FileSpec> foundFiles = rep.GetDepotFiles(listFiles,
    new Perforce.P4.Options(1234)); // 1234 = Changelist number

client.SyncFiles(foundFiles, null);

错误信息

用法:文件/打印 [-o localFile -q] 文件...无效选项:-c.

我不知道任何论点的问题。

或者不会有相关的参考来源?

编辑 2

我试图解决这个问题。但是,它还没有解决问题。

Perforce.P4.Changelist changelist = rep.GetChangelist(1234);
IList<Perforce.P4.FileMetaData> fileMeta = changelist.Files;

在这种情况下,我只能获取更改列表中的文件。我想在changelist 1234的那一刻同步客户端的所有文件。

【问题讨论】:

    标签: c# perforce p4api.net


    【解决方案1】:

    SyncFiles 采用可选的 FileSpec 参数。您可以使用该 FileSpec arg 指定文件路径和修订说明符。以下是相关文档:

    FileSpec object docs

    SyncFiles method docs

    您无需运行 GetDepotFiles() 即可获取 FileSpec 对象;您可以直接创建一个,如 FileSpec 对象文档中所示。 GetDepotFiles() 出现的错误是因为它希望将更改编号指定为 FileSpec 对象的一部分,作为 GetDepotFiles() 的第一个参数传入。

    为了进一步扩展,GetDepotFiles() 在与 Perforce 对话时调用“p4 文件”命令。 new Perforce.P4.Options(1234) 生成一个“-c 1234”选项,“p4 文件”不接受该选项。这就是为什么错误消息是'Usage: files/print [-o localFile -q] files...Invalid option: -c.'

    【讨论】:

    • 我看到了这篇文章,但没有解决问题。请您再看看这个问题好吗?
    • @GaneshSittampalam // 我还有问题。我添加了一个问题。我需要帮助
    【解决方案2】:

    我也遇到了同样的问题。最后根据马特的回答,我得到了它的工作。请看下面的简单示例。

            using (Connection con = rep.Connection)
            {
                //setting up client object with viewmap
                Client client = new Client();
                client.Name = "p4apinet_solution_builder_sample_application_client";
                client.OwnerName = "p4username";
                client.Root = "c:\\clientRootPath";
                client.Options = ClientOption.AllWrite;
                client.LineEnd = LineEnd.Local;
                client.SubmitOptions = new ClientSubmitOptions(false, SubmitType.RevertUnchanged);
                client.ViewMap = new ViewMap();
                client.ViewMap.Add("//depotpath/to/your/file.txt", "//" + client.Name + "/clientpath/to/your/file.txt", MapType.Include);
    
                //connecting to p4 and creating client on p4 server
                Options options = new Options();
                options["Password"] = "p4password";
                con.UserName = "p4username";
                con.Client = new Client();
                con.Connect(options);
                con.Client = rep.CreateClient(client);
    
                //syncing all files (in this case 1) defined in client's viewmap to the changelist level of 12345
                Options syncFlags = new Options(SyncFilesCmdFlags.Force, 100);
                VersionSpec changeListLevel = new ChangelistIdVersion(12345);
                List<FileSpec> filesToBeSynced = con.Client.ViewMap.Select<MapEntry, FileSpec>(me => new FileSpec(me.Left, changeListLevel)).ToList();
                IList<FileSpec> results = con.Client.SyncFiles(filesToBeSynced, syncFlags);
            }
    

    【讨论】:

      【解决方案3】:

      以下代码应允许您将软件仓库同步到特定修订版(更改列表编号)。

      string uri = "...";
      string user = "...";
      string workspace = "...";
      string pass = "..."; 
      int id = 12345; // the actual changelist number
      string depotPath = "//depot/foo/main/...";
      int maxItemsToSync = 10000;
      
      Server server = new Server(new ServerAddress(uri));
      Repository rep = new Repository(server);
      
      server = new Server(new ServerAddress(uri));
      rep = new Repository(server);
      Connection con = rep.Connection;
      con.UserName = user;
      con.Client = new Client();
      con.Client.Name = workspace;
      
      // connect
      bool connected = con.Connect(null);
      if (connected)
      {
          try
          {
              // attempt a login
              Perforce.P4.Credential cred = con.Login(pass);
          }
          catch (Exception ex)
          {
              Console.WriteLine(ex.Message);
              con.Disconnect();
              connected = false;
          }
      
          if (connected)
          {
              // get p4 info and show successful connection
              ServerMetaData info = rep.GetServerMetaData(null);
              Console.WriteLine("CONNECTED TO " + info.Address.Uri);
              Console.WriteLine("");
      
              try
              {
                  Options opts = new Options();
      
                  // uncomment below lines to only get a preview of the sync w/o updating the workspace
                  //SyncFilesCmdOptions syncOpts = new SyncFilesCmdOptions(SyncFilesCmdFlags.Preview, maxItemsToSync);
                  SyncFilesCmdOptions syncOpts = new SyncFilesCmdOptions(SyncFilesCmdFlags.None, maxItemsToSync);
      
                  VersionSpec version = new ChangelistIdVersion(id);
                  PathSpec path = new DepotPath(depotPath);
                  FileSpec depotFile = new FileSpec(path, version);
      
                  IList<FileSpec> syncedFiles = rep.Connection.Client.SyncFiles(syncOpts, depotFile);
                  //foreach (var file in syncedFiles)
                  //{
                  //    Console.WriteLine(file.ToString());
                  //}
                  Console.WriteLine($"{syncedFiles.Count} files got synced!");
              }
              catch (Exception ex)
              {
                  Console.WriteLine("");
                  Console.WriteLine(ex.Message);
                  Console.WriteLine("");
              }
              finally
              {
                  con.Disconnect();
              }
          }
      }
      

      如果您正在寻找其他方法来构建文件规范,例如仅将特定文件列表同步到“head”等,请访问此link 并搜索“Building a FileSpec”

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多