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

利用开源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的一些操作,以提高工作效率。

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值