0

我将通过 perforce API 同步 perforce 文件。我期望每个文件操作的输出。类似于我们看到的 p4 cmd 的输出:

  • //depot/file.txt#1 -更新X:\file.txt
  • //depot/file.txt#2 -删除为X:\file.txt

这是我用于同步文件的 perforce api 代码:

var repository = new P4.Repository(new P4.Server(new P4.ServerAddress("server:111111")));
repository.Connection.UserName = "me";
repository.Connection.Connect(new P4.Options());
repository.Connection.Login("password");
repository.Connection.Client = repository.GetClient("client");
var syncFlags = new P4.Options(P4.SyncFilesCmdFlags.Force, 100);
var clientPath = new P4.ClientPath(@"X:\File.txt");

IList<P4.FileSpec> results = repository.Connection.Client.SyncFiles(syncFlags, new[] { new P4.FileSpec(clientPath, new P4.Revision(1)) });
P4.VersionSpec downloadedVersion = results.First().Version; // This is #1 as expected

results = repository.Connection.Client.SyncFiles(syncFlags, new[] { new P4.FileSpec(clientPath, new P4.Revision(2)) });
downloadedVersion = results.First().Version; // This is #2 as expected

results = repository.Connection.Client.SyncFiles(syncFlags, new[] { new P4.FileSpec(clientPath, new P4.Revision(0)) });
downloadedVersion = results.First().Version; // File is really deleted and I hoped for #0, but it's #2!

如何获取文件被删除的信息?我尝试为此使用 SyncFiles 输出,但已删除文件的信息不正确。还有其他方法吗?

4

2 回答 2

2

这是我找到的解决方案:

repository.Connection.TaggedOutputReceived += Connection_TaggedOutputReceived;


static void Connection_TaggedOutputReceived(uint cmdId, int ObjId, P4.TaggedObject t)
{
    string action, oldAction, haveRevStr, depotFile;

    t.TryGetValue("action", out action);
    t.TryGetValue("oldAction", out oldAction);
    t.TryGetValue("haveRev", out haveRevStr);
    t.TryGetValue("depotFile", out depotFile);

    if (haveRevStr == null || haveRevStr == "none")
        haveRevStr = string.Empty;
    else
        haveRevStr = "#" + haveRevStr;

    if (string.IsNullOrEmpty(oldAction))
        oldAction = string.Empty;
    else
        oldAction = oldAction + " ";

    if (depotFile != null && action != null)
        Console.Out.WriteLine("{0}{1} - {2}{3}", depotFile, haveRevStr, oldAction, action);
}

... repository.Connection 还包含其他有趣的委托挂钩:.CommandEcho、.Errorreceived、.InfoResultReceived、.TextResultsReceived

于 2015-02-25T14:56:10.957 回答
1

Client.SyncFiles 函数为您提供了哪些文件受到影响的整洁列表,但不是同步命令的其余输出。如果您只想确定文件是否被删除,则在本地计算机上统计 clientFile 应该可以解决问题。

如果你想要完整的输出,使用 P4Server 接口是更好的选择:

https://www.perforce.com/manuals/p4api.net/p4api.net_reference/html/M_Perforce_P4_P4Server_RunCommand.htm

如果您在“tagged”设置为 true 的情况下调用 RunCommand(),它应该会为您提供从“p4 -Ztag (command)”获得的所有数据,结果可通过 GetTaggedOutput() 访问。在没有“标记”选项的情况下运行它会给你作为最终用户看到的格式化消息,可以通过 GetInfoMessages() 访问。

于 2015-02-11T18:42:27.187 回答