2

我在多个分支下的 P4 服务器上有文件,例如

//depot/branch1/file.txt
//depot/branch2/file.txt
//depot/branch3/file.txt

假设 file.txt 是同一个文件但不同的分支

当我使用命令行时

p4 -c testWorkspace where somepath\file.txt

我得到以下结果

-//depot/branch1/file.txt {client path depot path}
-//depot/branch2/file.txt {client path depot path}
//depot/branch3/file.txt {client path depot path}

从那我可以看出客户端testWorkspace中的file.txt应该通过branch3访问(所以从这个仓库路径我会得到FileSpec、元数据、编辑等

但是当我尝试通过 P4api.net 做同样的事情并使用

Client.GetClientFileMappings("somepath\file.txt")

或者

P4Command cmd3 = new P4Command(con, "where", true, "somepath\file.txt");
P4CommandResult result3 = cmd3.Run();

我得到了类似的结果,但没有前导减号(破折号 - )符号

//depot/branch1/file.txt {client path depot path}
//depot/branch2/file.txt {client path depot path}
//depot/branch3/file.txt {client path depot path}

而且我不知道我在这里做错了什么。

我需要的是获取给定工作空间的当前文件属于哪个分支的信息,或者更好地获取其正确的 FileSpec,以便我可以使用 MoveFile、Add 等。但我只获得所有分支的路径,并且可以识别它属于当前工作区的哪个分支

4

2 回答 2

2

查看 GetClientFileMappings 的接口:

https://www.perforce.com/manuals/v15.1/p4api.net/p4api.net_reference/html/M_Perforce_P4_Client_GetClientFileMappings.htm

看起来它实际上并没有返回映射;它返回一个 FileSpec 对象列表,没有关于映射类型的信息(例如-+&)。在 C++ API 中,这由MapType枚举表示:

https://swarm.workshop.perforce.com/projects/perforce_software-p4/files/2018-2/support/mapapi.h#6

在 .NET API 中有一个类似的枚举:

https://www.perforce.com/manuals/v15.1/p4api.net/p4api.net_reference/html/T_Perforce_P4_MapType.htm

这是MapEntry类型的一部分:

https://www.perforce.com/manuals/v15.1/p4api.net/p4api.net_reference/html/M_Perforce_P4_MapEntry__ctor.htm

如果你能找到任何返回MapEntrys 列表的东西,那将是你想要的东西,但我找不到任何东西。 GetClientFileMappings似乎是事情,特别是因为名称中有“映射”,但是......

于 2019-12-19T16:22:00.653 回答
0

所以我与 P4 团队成员讨论了这个问题,他们确认 GetClientFileMappings 确实不返回有关排除的信息。

他们给了我一个“解决方法”

P4Command cmd3 = new P4Command(con, "where", true, "somepath\file.txt");
P4CommandResult result3 = cmd3.Run();
if (result3.TaggedOutput!=null)
    {
        List<string> depotFiles = new List<string>();
        foreach(TaggedObject taggedObject in results3.TaggedOutput)
        {
            if (taggedObject.ContainsKey("unmap"))
            {
                continue;
            }
            else
            {
                string path = "";
                taggedObject.TryGetValue("depotFile", out path);
                depotFiles.Add(path);
            }
        }
    }

这对我有用。在最初的问题中,我提到这不会返回前导“-”,这是真的。但是 taggedObject 包含足以确定信息的关键“取消映射”。

我第一次没有注意到这一点,因为我以错误的方式传递了参数。“file1.txt file2.txt”作为简单字符串,而不是字符串数组。

我还想出了另一种更丑陋的“解决方法”(使用 p4 命令行、process.Start() 并解析字符串结果)

string commandText = $"/C p4 -u {UserName} -c {Client.Name} where {string.Join(" ", filePaths)}";

var process = new Process()
{
    StartInfo = new ProcessStartInfo()
    {
        UseShellExecute = false,
        CreateNoWindow = true,
        WindowStyle = ProcessWindowStyle.Hidden,
        FileName = "cmd.exe",
        Arguments = commandText,
        RedirectStandardError = true,
        RedirectStandardOutput = true

    }
};

process.Start();

string processTextResult = process.StandardOutput.ReadToEnd();

var exitCode = process.ExitCode;
var errorMsg = process.StandardError.ReadToEnd();


process.Dispose();
if (exitCode == 0)
{
    var resultLines = processTextResult.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

    List<FileSpec> fileSpecResults = new List<FileSpec>();

    foreach (var resultLine in resultLines)
    {
        var splitedLine = resultLine.Split(' ');

        if (!splitedLine.First().StartsWith("-"))
        {
            fileSpecResults.Add(new FileSpec(new DepotPath(splitedLine[0]), new ClientPath(splitedLine[1]), new LocalPath(splitedLine[2]), null));
        }
    }

    return fileSpecResults;
}
else
{
    Logger.TraceError("P4 error - get file spec :" + errorMsg);
    return new List<FileSpec>();
}
于 2020-01-07T06:53:59.720 回答