#include "StdAfx.h"
#include "svn.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
SVN::SVN(void)
{
parentpool = svn_pool_create(NULL);
svn_client_create_context(&m_pctx, parentpool);
Err = svn_config_ensure(NULL, parentpool);
pool = svn_pool_create (parentpool);
// set up the configuration
if (Err == 0)
Err = svn_config_get_config (&(m_pctx->config), NULL, parentpool);
if (Err == 0)
{
//set up the SVN_SSH param
stdstring tsvn_ssh = CRegStdString(_T("Software\\TortoiseSVN\\SSH"));
if (!tsvn_ssh.empty())
{
svn_config_t * cfg = (svn_config_t *)apr_hash_get (m_pctx->config, SVN_CONFIG_CATEGORY_CONFIG,
APR_HASH_KEY_STRING);
svn_config_set(cfg, SVN_CONFIG_SECTION_TUNNELS, "ssh", CUnicodeUtils::StdGetUTF8(tsvn_ssh).c_str());
}
}
// set up authentication
svn_auth_provider_object_t *provider;
/* The whole list of registered providers */
apr_array_header_t *providers = apr_array_make (pool, 12, sizeof (svn_auth_provider_object_t *));
/* The main disk-caching auth providers, for both
'username/password' creds and 'username' creds. */
svn_auth_get_windows_simple_provider (&provider, pool);
APR_ARRAY_PUSH (providers, svn_auth_provider_object_t *) = provider;
svn_auth_get_simple_provider (&provider, pool);
APR_ARRAY_PUSH (providers, svn_auth_provider_object_t *) = provider;
svn_auth_get_username_provider (&provider, pool);
APR_ARRAY_PUSH (providers, svn_auth_provider_object_t *) = provider;
/* The server-cert, client-cert, and client-cert-password providers. */
svn_auth_get_windows_ssl_server_trust_provider(&provider, pool);
APR_ARRAY_PUSH(providers, svn_auth_provider_object_t *) = provider;
svn_auth_get_ssl_server_trust_file_provider (&provider, pool);
APR_ARRAY_PUSH (providers, svn_auth_provider_object_t *) = provider;
svn_auth_get_ssl_client_cert_file_provider (&provider, pool);
APR_ARRAY_PUSH (providers, svn_auth_provider_object_t *) = provider;
svn_auth_get_ssl_client_cert_pw_file_provider (&provider, pool);
APR_ARRAY_PUSH (providers, svn_auth_provider_object_t *) = provider;
/* Two prompting providers, one for username/password, one for
just username. */
//svn_auth_get_simple_prompt_provider (&provider, (svn_auth_simple_prompt_func_t)simpleprompt, this, 3, /* retry limit */ pool);
//APR_ARRAY_PUSH (providers, svn_auth_provider_object_t *) = provider;
//svn_auth_get_username_prompt_provider (&provider, (svn_auth_username_prompt_func_t)userprompt, this, 3, /* retry limit */ pool);
//APR_ARRAY_PUSH (providers, svn_auth_provider_object_t *) = provider;
/* Three prompting providers for server-certs, client-certs,
and client-cert-passphrases. */
//svn_auth_get_ssl_server_trust_prompt_provider (&provider, sslserverprompt, this, pool);
//APR_ARRAY_PUSH (providers, svn_auth_provider_object_t *) = provider;
//svn_auth_get_ssl_client_cert_prompt_provider (&provider, sslclientprompt, this, 2, pool);
//APR_ARRAY_PUSH (providers, svn_auth_provider_object_t *) = provider;
//svn_auth_get_ssl_client_cert_pw_prompt_provider (&provider, sslpwprompt, this, 2, pool);
//APR_ARRAY_PUSH (providers, svn_auth_provider_object_t *) = provider;
/* Build an authentication baton to give to libsvn_client. */
svn_auth_open (&auth_baton, providers, pool);
m_pctx->auth_baton = auth_baton;
}
SVN::~SVN(void)
{
svn_pool_destroy (parentpool);
}
bool SVN::Cat(stdstring sUrl, stdstring sFile)
{
// we always use the HEAD revision to fetch a file
apr_file_t * file;
svn_stream_t * stream;
apr_status_t status;
SVNPool localpool(pool);
// if the file already exists, delete it before recreating it
::DeleteFile(sFile.c_str());
status = apr_file_open(&file, CUnicodeUtils::StdGetANSI(sFile).c_str(),
APR_WRITE | APR_CREATE | APR_TRUNCATE, APR_OS_DEFAULT, localpool);
if (status)
{
Err = svn_error_wrap_apr(status, NULL);
return false;
}
stream = svn_stream_from_aprfile(file, localpool);
svn_opt_revision_t pegrev, rev;
pegrev.kind = svn_opt_revision_head;
rev.kind = svn_opt_revision_head;
Err = svn_client_cat2(stream, CUnicodeUtils::StdGetUTF8(sUrl).c_str(),
&pegrev, &rev, m_pctx, localpool);
apr_file_close(file);
return (Err == NULL);
}
const SVNInfoData * SVN::GetFirstFileInfo(stdstring path, svn_revnum_t pegrev, svn_revnum_t revision, bool recurse /* = false */)
{
SVNPool localpool(pool);
m_arInfo.clear();
m_pos = 0;
svn_opt_revision_t peg, rev;
if (pegrev == -1)
peg.kind = svn_opt_revision_head;
else
{
peg.kind = svn_opt_revision_number;
peg.value.number = pegrev;
}
if (revision == -1)
rev.kind = svn_opt_revision_head;
else
{
rev.kind = svn_opt_revision_number;
rev.value.number = revision;
}
Err = svn_client_info(CUnicodeUtils::StdGetUTF8(path).c_str(), &peg, &rev, infoReceiver, this, recurse, m_pctx, localpool);
if (Err != NULL)
return NULL;
if (m_arInfo.size() == 0)
return NULL;
return &m_arInfo[0];
}
const SVNInfoData * SVN::GetNextFileInfo()
{
m_pos++;
if (m_arInfo.size()>m_pos)
return &m_arInfo[m_pos];
return NULL;
}
svn_error_t * SVN::infoReceiver(void* baton, const char * path, const svn_info_t* info, apr_pool_t * /*pool*/)
{
if ((path == NULL)||(info == NULL))
return NULL;
SVN * pThis = (SVN *)baton;
SVNInfoData data;
if (info->URL)
data.url = CUnicodeUtils::StdGetUnicode(info->URL);
data.rev = info->rev;
data.kind = info->kind;
if (info->repos_root_URL)
data.reposRoot = CUnicodeUtils::StdGetUnicode(info->repos_root_URL);
if (info->repos_UUID)
data.reposUUID = CUnicodeUtils::StdGetUnicode(info->repos_UUID);
data.lastchangedrev = info->last_changed_rev;
data.lastchangedtime = info->last_changed_date/1000000L;
if (info->last_changed_author)
data.author = CUnicodeUtils::StdGetUnicode(info->last_changed_author);
if (info->lock)
{
if (info->lock->path)
data.lock_path = CUnicodeUtils::StdGetUnicode(info->lock->path);
if (info->lock->token)
data.lock_token = CUnicodeUtils::StdGetUnicode(info->lock->token);
if (info->lock->owner)
data.lock_owner = CUnicodeUtils::StdGetUnicode(info->lock->owner);
if (info->lock->comment)
data.lock_comment = CUnicodeUtils::StdGetUnicode(info->lock->comment);
data.lock_davcomment = !!info->lock->is_dav_comment;
data.lock_createtime = info->lock->creation_date/1000000L;
data.lock_expirationtime = info->lock->expiration_date/1000000L;
}
data.hasWCInfo = !!info->has_wc_info;
if (info->has_wc_info)
{
data.schedule = info->schedule;
if (info->copyfrom_url)
data.copyfromurl = CUnicodeUtils::StdGetUnicode(info->copyfrom_url);
data.copyfromrev = info->copyfrom_rev;
data.texttime = info->text_time/1000000L;
data.proptime = info->prop_time/1000000L;
if (info->checksum)
data.checksum = CUnicodeUtils::StdGetUnicode(info->checksum);
if (info->conflict_new)
data.conflict_new = CUnicodeUtils::StdGetUnicode(info->conflict_new);
if (info->conflict_old)
data.conflict_old = CUnicodeUtils::StdGetUnicode(info->conflict_old);
if (info->conflict_wrk)
data.conflict_wrk = CUnicodeUtils::StdGetUnicode(info->conflict_wrk);
if (info->prejfile)
data.prejfile = CUnicodeUtils::StdGetUnicode(info->prejfile);
}
pThis->m_arInfo.push_back(data);
return NULL;
}