我正在尝试使用SharpSvn来获取特定版本的文件内容。我有以下代码:
var svnClient = new SvnClient();
var revisionInfo = new SvnFileVersionsArgs
{
Start = 80088,
End = 80093
};
Collection<SvnFileVersionEventArgs> fileVersions;
svnClient.GetFileVersions(
new SvnUriTarget("https://DbDiff.svn.codeplex.com/svn/DbDiffCommon/DataAccess/SqlCommand11.xml"),
revisionInfo,
out fileVersions);
var stream = fileVersions[0].GetContentStream();
但是在最后一行我得到以下异常:
System.InvalidOperationException was unhandled
Message=This method can only be invoked from the eventhandler handling this eventargs instance
Source=SharpSvn
StackTrace:
at SharpSvn.SvnFileVersionEventArgs.GetContentStream(SvnFileVersionWriteArgs args) in g:\dist\src\sharpsvn\commands\fileversions.cpp:line 545
at SharpSvn.SvnFileVersionEventArgs.GetContentStream() in g:\dist\src\sharpsvn\commands\fileversions.cpp:line 534
at SharpSvnFileVersions.Program.Main(String[] args) in c:\temp\SharpSvnFileVersions\Program.cs:line 29
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
我应该如何在每次修订时获取文件的内容?
答案 0 :(得分:3)
看起来我可以使用SvnClient.Write()
来获取文件内容:
SvnClient svnClient = new SvnClient();
SvnUriTarget target = new SvnUriTarget("https://DbDiff.svn.codeplex.com/svn/DbDiffCommon/DataAccess/SqlCommand11.xml", 80088);
Stream stream = new MemoryStream();
svnClient.Write(target, stream);
答案 1 :(得分:0)
GetFileVersions仅对特定场景有用。 Subversion本身使用它来实现责任。我碰巧为不同的场景创建了这个测试程序:将一个文件的所有修订版迁移到一个新的存储库。将其发布在此处供其他人使用。
using System;
using System.IO;
using SharpSvn;
using SharpSvn.UI;
class SvnDumpFileToRepository
{
static void Main(string[] args)
{
Uri uri = null;
if (args.Length < 2
|| !Uri.TryCreate(args[0], UriKind.Absolute, out uri))
{
Console.Error.WriteLine("Usage: SvnDumpFileToRepository URL PATH");
}
using (SvnRepositoryClient repos = new SvnRepositoryClient())
{
repos.CreateRepository(args[1]);
}
Uri reposUri = SvnTools.LocalPathToUri(args[1], true);
using (SvnClient client = new SvnClient())
using (SvnClient client2 = new SvnClient())
{
SvnUI.Bind(client, new SvnUIBindArgs());
string fileName = SvnTools.GetFileName(uri);
bool create = true;
client.FileVersions(uri,
delegate(object sender, SvnFileVersionEventArgs e)
{
Console.Write("Copying {0} in r{1}", fileName, e.Revision);
using (MemoryStream ms = new MemoryStream())
{
e.WriteTo(ms); // Write full text to memory stream
ms.Position = 0;
// And now use 'svnmucc' to update repository
client2.RepositoryOperation(reposUri,
new SvnRepositoryOperationArgs { LogMessage = e.LogMessage },
delegate(SvnMultiCommandClient mucc)
{
if (create)
{
mucc.CreateFile(fileName, ms);
create = false;
}
else
mucc.UpdateFile(fileName, ms);
});
}
});
}
}
}