如何使用SharpSVN修改文件xml并提交修改后的文件

时间:2015-05-05 06:22:06

标签: c# svn sharpsvn

我是SharpSVN的新手。

我们目前正在忙于品牌重塑,这需要用新颜色等更新我们的所有报告。有太多的报告要手动完成,所以我试图找到一种方法来查找和替换颜色/字体等走。 我们的报告被序列化并存储在一个易于替换的数据库中,但我们也希望在源代码控制中使用.rdl报告中的更改,即Subversion。

我的问题如下: 我知道您可以使用SharpSVN将文件写入流中,我已经完成了,现在我想将更新的xml作为最新版本推送回Subversion。 这是可能吗?如果是这样,我将如何做到这一点?我用Google搜索了很多,但未能找到任何明确的答案。

到目前为止我的代码(请记住这是一次性的事情,所以我不太关心干净的代码等):

    private void ReplaceFiles()
    {
        SvnCommitArgs args = new SvnCommitArgs(); 
        SvnCommitResult result;
        args.LogMessage = "Rebranding - Replace fonts, colours, and font sizes";

        using (SvnClient client = new SvnClient())
        {
            client.Authentication.DefaultCredentials = new NetworkCredential("mkassa", "Welcome1");
            client.CheckOut(SvnUriTarget.FromString(txtSubversionDirectory.Text), txtCheckoutDirectory.Text);
            client.Update(txtCheckoutDirectory.Text);
            SvnUpdateResult upResult;
            client.Update(txtCheckoutDirectory.Text, out upResult);
            ProcessDirectory(txtCheckoutDirectory.Text, args, client);
        }
        MessageBox.Show("Done");
    }

    // Process all files in the directory passed in, recurse on any directories  
    // that are found, and process the files they contain. 
    public void ProcessDirectory(string targetDirectory, SvnCommitArgs args, SvnClient client)
    {
        var ext = new List<string> { ".rdl" };

        // Process the list of files found in the directory. 
        IEnumerable<string> fileEntries = Directory.EnumerateFiles(targetDirectory, "*.*", SearchOption.AllDirectories)
                                        .Where(s => ext.Any(e=> s.EndsWith(e)));
        foreach (string fileName in fileEntries)
            ProcessFile(fileName, args, client);
    }


    private void ProcessFile(string fileName, SvnCommitArgs args, SvnClient client)
    {
        using (MemoryStream stream = new MemoryStream())
        {
            SvnCommitResult result;
            if (client.Write(SvnTarget.FromString(fileName), stream))
            {
                stream.Position = 0;
                using (var reader = new StreamReader(stream))
                {
                    string contents = reader.ReadToEnd();

                    DoReplacement(contents);

                    client.Commit(txtCheckoutDirectory.Text, args, out result);

                    //if (result != null)
                    //    MessageBox.Show(result.PostCommitError);
                }
            }
        }
    }

感谢能够对此提供一些见解的任何人!

2 个答案:

答案 0 :(得分:2)

您不希望对文件执行合并,因为您只会使用它来将更改从一个位置合并到另一个位置

如果您不能只检查整个树并替换+ commit,那么您可以使用以下内容:

string tmpDir = "C:\tmp\mytmp";
using(SvnClient svn = new SvnClient())
{
     List<Uri> toProcess = new List<Uri>();
     svn.List(new Uri("http://my-repos/trunk"), new SvnListArgs() { Depth=Infinity }),
     delegate(object sender, SvnListEventArgs e)
     {
         if (e.Path.EndsWith(".rdl", StringComparison.OrdinalIgnoreCase))
             toProcess.Add(e.Uri);
     });

     foreach(Uri i in toProcess)
     {
         Console.WriteLine("Processing {0}", i);
         Directory.Delete(tmpDir, true);

         // Create a sparse checkout with just one file (see svnbook.org)
         string name = SvnTools.GetFileName(i);
         string fileName = Path.Join(tmpDir, name)
         svn.CheckOut(new Uri(toProcess, "./"), new SvnCheckOutArgs { Depth=Empty });
         svn.Update(fileName);

         ProcessFile(fileName); // Read file and save in same location

         // Note that the following commit is a no-op if the file wasn't
         // changed, so you don't have to check for that yourself
         svn.Commit(fileName, new SvnCommitArgs { LogMessage="Processed" });
    }
}

更新主干后,我建议将更改合并到维护分支中......如有必要,请在此之后再修复它们。否则,进一步的合并将比必要的更难执行。

答案 1 :(得分:0)

我设法完成了这项工作。发布答案以供将来参考。

基本上我所要做的就是使用修改后的XML创建一个新的.rdl文件,并在提交之前将签出的文件替换为新文件。

                    string contents = reader.ReadToEnd();

                    contents = DoReplacement(contents);

                    // Create an XML document
                    XmlDocument doc = new XmlDocument();
                    string xmlData = contents;
                    doc.Load(new StringReader(xmlData));
                    //Save XML document to file
                    doc.Save(fileName);

                    client.Commit(txtCheckoutDirectory.Text, args, out result);

希望这可以帮助任何需要这样做的人。