Ruby から Subversion を利用 on Windows (その2)

昨日の記事の通りにすることでライブラリを読み込めるようになったので、今日はサーバへの接続をやってみる。

以下のようにすることで、ログを取得することができる。

require 'svn/client'

repos_uri = "http://svn-server/repos/my-proj"
user = "root"
pass = "root-passwd"

ctx = Svn::Client::Context.new
ctx.add_simple_prompt_provider(0) do |cred, realm, username, may_save|
  cred.username = user
  cred.password = pass
end
ctx.log(repos_uri, 1, "HEAD", 3, true, nil) {|changed_path, rev, auther, date, message|
  puts [changed_path, rev, auther, date, message].join("\t")
}

上記ではリビジョン1 からHEADまでのログのうち、最新から最大3件を表示するサンプルだ。
changed_path の実態は Hash となっており、キーがコミットしたファイルのパス、バリューが変更前後のパスなどの情報を保持するクラスとなっている。

なお、上記でSSLを利用したサーバへ接続しようとするとエラーが発生する。

Svn::Error::RaDavRequestFailed: C:\Projects\subversion-1.5.4\subversion\libsvn_ra_neon\util.c:592 OPTIONS of 'https://svn-server/repos/my-proj': Server certificate verification failed: issuer is not trusted (https://svn-server/)

SSLの認証情報が足りないためエラーとなる。
今回は Windows なので以下のコードを追加することで上記のエラーは発生しなくなる。

ctx.add_windows_ssl_server_trust_provider

ただ上記だけではまだ足りないようで、以下のエラーが発生する。

Svn::Error::RaNotAuthorized: C:\Projects\subversion-1.5.4\subversion\libsvn_ra_neon\util.c:592 OPTIONS of 'https://svn-server/repos/my-proj': authorization failed (https://svn-server)

これはユーザ名・パスワードの情報が足りていないためエラーとなっている。
以下のコードを追加することでエラーを解消できる。

ctx.add_ssl_client_cert_prompt_provider(0) do |cred, realm, username, may_save|
  cred.username = user
  cred.password = pass
end

上記のコードを追加したコードは以下のようになる。

require 'svn/client'

repos_uri = "https://svn-server/repos/my-proj"
user = "root"
pass = "root-pass"

ctx = Svn::Client::Context.new

ctx.add_simple_prompt_provider(0) do |cred, realm, username, may_save|
  cred.username = user
  cred.password = pass
end

ctx.add_windows_ssl_server_trust_provider
ctx.add_ssl_client_cert_prompt_provider(0) do |cred, realm, username, may_save|
  cred.username = user
  cred.password = pass
end

ctx.log(repos_uri, 1, "HEAD", 3, true, nil) do |changed_path, rev, auther, date, message|
  puts [changed_path, rev, auther, date, message].join("\t")
end