C# Perforce Api-如何命令;获取修订版[变更列表编号]”;
我想使用Perforce.NETAPI(C#)实现Perforce命令“获取修订版[changelistnumber]”。我目前有“获取最新版本”的代码,但我需要修改它以获取特定的变更列表 要将数据与变更列表编号同步,我应该怎么做 来源C# Perforce Api-如何命令;获取修订版[变更列表编号]”;,c#,perforce,p4api.net,C#,Perforce,P4api.net,我想使用Perforce.NETAPI(C#)实现Perforce命令“获取修订版[changelistnumber]”。我目前有“获取最新版本”的代码,但我需要修改它以获取特定的变更列表 要将数据与变更列表编号同步,我应该怎么做 来源 // --------Connenct---------------- Perforce.P4.Server server = new Perforce.P4.Server( new Perforce.P4.ServerAddress("127.0.0.
// --------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();
/--Connenct----------------
Perforce.P4.Server服务器=新Perforce.P4.Server(
新的performe.P4.ServerAddress(“127.0.0.1:9999”);
Perforce.P4.Repository=新Perforce.P4.Repository(服务器);
Perforce.P4.Connection con=代表连接;
con.UserName=m_P4ID;
字符串密码=m_P4PASS;
Perforce.P4.Options opconnect=新Perforce.P4.Options();
opconnect.Add(“-p”,密码);
con.Connect(opconnect);
如果(con.Credential==null)
con.登录(密码);
//----------下载----------
字符串clientPath=@“C:\P4V\”;
字符串ws_client=clientPath;
Perforce.P4.Client Client=新Perforce.P4.Client();
client.Name=ws\u client;
客户端初始化(con);
con.CommandTimeout=新的时间跨度(0);
IList fileList=client.SyncFiles(new Perforce.P4.Options());
//----------断开------------
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);
Perforce.P4.DepotPath depot=新Perforce.P4.DepotPath(“//P4V//”);
Perforce.P4.LocalPath local=新Perforce.P4.LocalPath(ws\u客户端);
Perforce.P4.FileSpec fs=new Perforce.P4.FileSpec(depot,null,local,
性能P4版本规格头);
IList listFiles=新列表();
添加(fs);
IList foundFiles=rep.getDepotFile(列表文件,
新性能P4.选项(1234));//1234=变更列表编号
client.SyncFiles(foundFiles,null);
错误消息
用法:files/print[-o localFile-q]files…无效选项:-c
我不知道有什么争论的问题
或者与此参考源没有关联
编辑2
我试图解决这个问题。然而,这并没有解决问题
Perforce.P4.Changelist changelist = rep.GetChangelist(1234);
IList<Perforce.P4.FileMetaData> fileMeta = changelist.Files;
Perforce.P4.Changelist Changelist=rep.GetChangelist(1234);
IList fileMeta=changelist.Files;
在这种情况下,我只能获取变更列表中的文件。我希望在更改列表1234时同步客户端的所有文件。SyncFiles采用可选的FileSpec arg。可以使用该FileSpec arg指定文件路径和修订说明符。以下是相关文件: 您不需要运行GetDepotFiles()来获取FileSpec对象;您可以直接创建一个,如FileSpec对象文档所示。GetDepotFiles()出现的错误是因为它希望将更改号指定为作为GetDepotFiles()的第一个参数传递到的FileSpec对象的一部分
为了进一步扩展,GetDepotFiles()在与Perforce对话时调用'p4 files'命令<代码>新建performe.P4.Options(1234)生成一个选项“-c 1234”,该选项“P4文件”不接受。这就是为什么错误消息是“用法:files/print[-o localFile-q]files…无效选项:-c.”我也遇到了同样的问题。最后,根据马特的回答,我终于成功了。请参阅下面的简单示例
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);
}
使用(连接con=rep.Connection)
{
//使用viewmap设置客户端对象
客户端=新客户端();
client.Name=“p4apinet\u解决方案\u构建器\u示例\u应用程序\u客户端”;
client.OwnerName=“p4username”;
client.Root=“c:\\clientRootPath”;
client.Options=ClientOption.AllWrite;
client.LineEnd=LineEnd.Local;
client.SubmitOptions=新的ClientSubmitOptions(false,SubmitType.RevertUnchanged);
client.ViewMap=新建ViewMap();
client.ViewMap.Add(“///depotpath/to/your/file.txt”,“//“+client.Name+”/clientpath/to/your/file.txt”,MapType.Include);
//连接到p4并在p4服务器上创建客户端
选项=新选项();
选项[“密码”]=“密码”;
con.UserName=“p4username”;
con.Client=新客户端();
con.Connect(选项);
con.Client=rep.CreateClient(客户端);
//将客户端视图映射中定义的所有文件(在本例中为1)同步到变更列表级别12345
Options syncFlags=新选项(SyncFilesCmdFlags.Force,100);
VersionSpec changeListLevel=新的ChangelistIdVersion(12345);
列出filesToBeSynced=con.Client.ViewMap.Select(me=>newfilespec(me.Left,changeListLevel)).ToList();
IList results=con.Client.SyncFiles(filesToBeSynced,syncFlags);
}
以下代码应允许您将仓库同步到特定版本(变更列表编号)
stringuri=“…”;
字符串user=“…”;
字符串workspace=“…”;
字符串pass=“…”;
int id=12345;//实际变更列表编号
字符串depotPath=“//depot/foo/main/…”;
int maxItemsToSync=10000;
服务器服务器=新服务器(新服务器地址(uri));
Repository rep=新存储库(服务器);
服务器=新服务器(新服务器地址(uri));
rep=新存储库(服务器);
连接con=代表连接;
con.UserName=用户;
con.Client=新客户端();
con.Client.Name=工作区;
//连接
bool connected=con.Connect(空);
如果(已连接)
{
尝试
{
//尝试登录
Perforce.P4.Credential cred=con.Login(通过);
}
捕获(例外情况除外)
{
控制台写入线(例如消息);
con.Disconnect();
连接=错误;
}
如果(已连接)
{
//获取p4信息并显示成功连接
ServerMetaData info=rep.GetServerMetaData(null);
Console.WriteLine(“连接到”+info.Address.Uri);
控制台。写线(“”);
尝试
{
选项选项=新选项();
//取消行下方的注释以仅获取
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();
}
}
}