平々毎々(アーカイブ)

はてなダイアリーのアーカイブです。

Re: C#でsubversion

Rubyでは20行くらいで書けていた内容が、C#では80行近くに膨れあがりました。(ノ∀`)

20行は難しいかもしれないが、40行ぐらいなら簡単。(追記:XElementと書くべきところをXNodeと書いてた。コンパイル通らない。修正。)

using System;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            //
            // svn.exe を実行して、結果を取得する。
            //
            Process p = new Process();
            p.StartInfo = new ProcessStartInfo("svn", "log --xml -v http://svnrepo/ -l 5")
            {
                CreateNoWindow = true, // コンソールを開かない
                UseShellExecute = false, // シェル機能を使用しない
                RedirectStandardOutput = true, // 標準出力をリダイレクト
                StandardOutputEncoding = Encoding.UTF8 // 結果はUTF-8で来る
            };
            p.Start(); // アプリの実行開始
            p.WaitForExit();

            XDocument doc = XDocument.Load(p.StandardOutput);
            foreach (XElement entry in doc.Elements("log").Elements("logentry"))
            {
                // revision 属性の取得
                Console.Write("■r{0} ", entry.Attribute("revision").Value);
                foreach (XElement msg in entry.Elements("msg"))
                {
                    Console.WriteLine(Regex.Replace(msg.Value, "\n(?!$)", "\r\n    "));
                }
            }
        }
    }
}

id:mellow-mikan さんはhttp://d.hatena.ne.jp/mellow-mikan/20100228/1267374278という記事も書いてるけど、GetXmlFromSvn()のとこはこんな感じで書くといいんじゃないかなと思った。