最近想尝试用SVN来管理自己写的程序里面的数据。相当于内嵌到程序中。类似于vs中svn的插件。查找网络后发现有直接用SubVersion源码的,有用RapidSVN的。而我的程序都运行在windows下,所以第一想到的就是用TortoiseSVN。但是这个如果研究源码api啥的,就不知道又多长时间了。。。所以想了个取巧的办法。就是用TortoiseSVN的CLI。经过简单包装,发现可以运行。。。废话少说,看代码。。。
// svn_wrapper.h
#pragma once
/*
see as TortoiseSVN Help:
目录 -> TortoiseSVN -> Automating TortoiseSVN -> TortoiseSVN Commands
Command Description
: about Shows the about dialog.This is also shown if no command is given.
: checkout Opens the checkout dialog.
The /path specifies the target directory and
the /url specifies the URL to checkout from.If you specify
the key /blockpathadjustments, the automatic checkout path adjustments are blocked.
The /revision : XXX specifies the revision to check out.
: import Opens the import dialog.
The /path specifies the directory with the data to import.You can also specify
the /logmsg switch to pass a predefined log message to the import dialog.Or,
if you don't want to pass the log message on the command line, use /logmsgfile:path,
where path points to a file containing the log message.
......
for example:
TortoiseProc.exe /command:commit
/path:"c:\svn_wc\file1.txt*c:\svn_wc\file2.txt"
/logmsg:"test log message" /closeonend:0
TortoiseProc.exe /command:update /path:"c:\svn_wc\" /closeonend:0
TortoiseProc.exe /command:log /path:"c:\svn_wc\file1.txt"
/startrev:50 /endrev:60 /closeonend:0
*/
#include <iostream>
#include <string>
using namespace std;
class svn_wrapper
{
public:
svn_wrapper(string strBinPath);
~svn_wrapper();
void About();
void Log(string strPath);
void CheckOut(string strPath, string strUrl);
void Import(string strPath);
void Update(string strPath);
void Commit(string strPath);
void Add(string strPath);
void Revert(string strPath);
void Cleanup(string strPath);
void Resolve(string strPath);
void RepoCreate(string strPath);
void Switch(string strPath, string strUrl);
void Export(string strPath);
void DropExport(string strPath);
void DropVendor(string strPath);
void Merge(string strPath);
void MergeAll(string strPath);
void Copy(string strPath);
void Settings();
void Remove(string strPath);
void Rename(string strPath);
void Diff(string strUrl1, string strUrl2, string revision1, string revision2);
void ShowCompare(string strPath);
void Conflicteditor(string strPath);
void Relocate(string strPath);
void Help();
void RepoStatus();
void RepoBrowser(string strPath);
void Ignore(string strPath);
void Blame(string strPath);
void Cat(string strPath);
void CreatePatch(string strPath);
void RevisionGraph(string strPath);
void Lock(string strPath);
void UnLock(string strPath);
void Rebuildiconcache();
void Properties(string strPath);
private:
void Run(string strCommand, string strPath = "", string strUrl = "");
string m_strBinPath;
};
然后是实现文件:
// svn_wrapper.cpp
#include "svn_wrapper.h"
svn_wrapper::svn_wrapper(string strBinPath)
{
m_strBinPath = strBinPath;
}
svn_wrapper::~svn_wrapper()
{
}
void svn_wrapper::Run(string strCommand, string strPath, string strUrl)
{
system(("cd " + m_strBinPath).c_str());
// 在这里要判定TortoiseProc.exe是否存在
// 如果不存在则提示重新输入TortoiseSVN的bin目录
string strPrefix = "TortoiseProc.exe /command:" + strCommand;
string strArgPath = strPath.empty() ? "" : " /path:" + strPath;
string strArgUrl = strUrl.empty() ? "" : " /url:" + strUrl;
system((strPrefix + strArgPath + strArgUrl).c_str());
}
void svn_wrapper::About()
{
Run("about");
}
void svn_wrapper::Log(string strPath)
{
Run("log", strPath);
}
void svn_wrapper::CheckOut(string strPath, string strUrl)
{
Run("checkout", strPath, strUrl);
}
void svn_wrapper::Import(string strPath)
{
Run("import", strPath);
}
void svn_wrapper::Update(string strPath)
{
Run("update", strPath);
}
void svn_wrapper::Commit(string strPath)
{
Run("commit", strPath);
}
void svn_wrapper::Add(string strPath)
{
Run("add", strPath);
}
void svn_wrapper::Revert(string strPath)
{
Run("revert", strPath);
}
void svn_wrapper::Cleanup(string strPath)
{
Run("cleanup", strPath);
}
void svn_wrapper::Resolve(string strPath)
{
Run("resolve", strPath);
}
void svn_wrapper::RepoCreate(string strPath)
{
Run("repocreate", strPath);
}
void svn_wrapper::Switch(string strPath, string strUrl)
{
Run("switch", strPath, strUrl);
}
void svn_wrapper::Export(string strPath)
{
Run("export", strPath);
}
void svn_wrapper::DropExport(string strPath)
{
Run("dropexport", strPath);
}
void svn_wrapper::DropVendor(string strPath)
{
Run("dropvender", strPath);
}
void svn_wrapper::Merge(string strPath)
{
Run("merge", strPath);
}
void svn_wrapper::MergeAll(string strPath)
{
Run("mergeall", strPath);
}
void svn_wrapper::Copy(string strPath)
{
Run("copy", strPath);
}
void svn_wrapper::Settings()
{
Run("settings");
}
void svn_wrapper::Remove(string strPath)
{
Run("remove", strPath);
}
void svn_wrapper::Rename(string strPath)
{
Run("rename", strPath);
}
void svn_wrapper::Diff(string strUrl1, string strUrl2, string strRevision1, string strRevision2)
{
system(("cd " + m_strBinPath).c_str());
string strCommand = "TortoiseProc.exe /command:diff /url1:" + strUrl1 + " /revision1:"+ strRevision1
+ " /url2:" + strUrl2 + " /revision2 : "+ strRevision2;
system(strCommand.c_str());
}
void svn_wrapper::ShowCompare(string strPath)
{
Run("showcompare", strPath);
}
void svn_wrapper::Conflicteditor(string strPath)
{
Run("conflicteditor", strPath);
}
void svn_wrapper::Relocate(string strPath)
{
Run("relocate", strPath);
}
void svn_wrapper::Help()
{
Run("help");
}
void svn_wrapper::RepoStatus()
{
Run("repostatus");
}
void svn_wrapper::RepoBrowser(string strPath)
{
Run("repobrowser", strPath);
}
void svn_wrapper::Ignore(string strPath)
{
Run("ignore", strPath);
}
void svn_wrapper::Blame(string strPath)
{
Run("blame", strPath);
}
void svn_wrapper::Cat(string strPath)
{
Run("cat", strPath);
}
void svn_wrapper::CreatePatch(string strPath)
{
Run("createpatch", strPath);
}
void svn_wrapper::RevisionGraph(string strPath)
{
Run("revisiongraph", strPath);
}
void svn_wrapper::Lock(string strPath)
{
Run("lock", strPath);
}
void svn_wrapper::UnLock(string strPath)
{
Run("unlock", strPath);
}
void svn_wrapper::Rebuildiconcache()
{
Run("rebuildiconcache");
}
void svn_wrapper::Properties(string strPath)
{
Run("properties", strPath);
}
下面是测试程序:
#include "svn_wrapper.h"
int _tmain(int argc, _TCHAR* argv[])
{
svn_wrapper sw("E:/Program Files/TortoiseSVN/bin"); // 这里是TortoiseSVN的bin目录
sw.Help();
sw.About();
sw.Settings();
return 0;
}
好吧。上面的程序只测试了小部分,但我想表达的意思估计大家都明白了。我在这里算是抛砖引玉,大家关于svn二次开发有什么好的想法都可以说一说。大家交流一下。