您的位置:首页 > 其它

利用开源SharpSvn 自动下载和比较文件

2013-10-30 14:28 483 查看
利用开源SharpSvn提供的SVN Client API可以很方便的操作SVN,由于项目每次发布前都需要和服务器的做对比,写了一个小工具,界面如下:



其中第一部分参数部分,设置,SVN的URL地址,本地文件地址(也可以使用网络共享地址如
\\192.168.0.1\xxxx).

第二部分参数,设置,SVN文件的相对文件地址,以后文件比较工具的路径,同时支持指定SVN的版本号。

工作原理:

SharpSvn-〉下载文件到WorkPath,文件名=文件名_SVN版本.扩展名 (如果指定了SVN版本号,则下载指定的版本,否则使用最后一个版本)

在Info: 后面显示,SVN版本号以及最后修改时间。

最后调用文件比较工具,把下载到的文件和本地文件进行比较,这里使用了Ucl工具。

核心代码如下:

//SVN Case

System.IO.DirectoryInfo path = new System.IO.DirectoryInfo(Path.GetDirectoryName(strFile));
System.IO.FileInfo[] files = path.GetFiles(Path.GetFileName(strFile), System.IO.SearchOption.TopDirectoryOnly);
if (files.Length > 0)
{
strFile = files[0].FullName;
txtPageFile.Text = strFile.Replace(txtPathFile.Text, string.Empty);
}

string strSVN = txtPathSVN.Text + "/" + txtPageFile.Text.Replace(@"\", @"/");

//Start Work
if (!Directory.Exists(strWorkDir))
{
Directory.CreateDirectory(strWorkDir);
}

lblFileInfo.Text = "File Info:";
lblInfo.Text = "Info:";

lblInfo.Text += string.Format("{0}", File.GetLastWriteTime(strFile));

using (SvnClient client = new SvnClient())
{
// Checkout the code to the specified directory
SvnExportArgs tmpArg = new SvnExportArgs();
tmpArg.Overwrite = true;
if (tmpVer > 0)
{
tmpArg.Revision = tmpVer;
}
else
{
SvnInfoEventArgs info;
client.GetInfo(new Uri(strSVN), out info);
tmpVer = info.LastChangeRevision;
//show SVN info:
lblInfo.Text += string.Format("V#:{0} {1} {2}", info.LastChangeRevision, info.LastChangeAuthor, info.LastChangeTime.ToString());
}

SvnUpdateResult tmpRet;
//down file
client.Export(new Uri(strSVN), strWorkDir, tmpArg, out tmpRet);

}
string strFileSVN = Path.Combine(strWorkDir, Path.GetFileName(txtPageFile.Text));

string strFileSVNEx = strFileSVN;

if (tmpVer > 0 && File.Exists(strFileSVN))
{
strFileSVNEx = Path.Combine(strWorkDir, tmpVer + "_" + Path.GetFileName(txtPageFile.Text));
if (File.Exists(strFileSVNEx))
{
File.Delete(strFileSVNEx);
}

File.Move(strFileSVN, strFileSVNEx);
}

string strFileEx = Path.Combine( strWorkDir , "F_" + Path.GetFileName(txtPageFile.Text));
if(File.Exists(strFileEx))
{
File.Delete(strFileEx);
}

File.Copy(strFile, strFileEx, true);

string strCmd = txtCmd.Text;
if (strCmd.Length > 0 && File.Exists(strCmd) && File.Exists(strFileEx) && File.Exists(strFileSVNEx) )
{
string strArgs = string.Format(@"""{0}"" ""{1}""", strFileSVNEx, strFileEx);

System.Diagnostics.Process.Start(strCmd, strArgs);
}


可以使用Sharp SVN完全可以自动化SVN的一些操作,以提高工作效率。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: