利用sharpsvn自动提交源码到SVN服务器

SVN是一个基于Apache License的版本控制软件,我们可以用它来管理我们的代码以及控制代码版本,在日常开发过程中,我们经常需要手工提交源码,以保证项目成员能的代码是最新的,而我又是一个比较懒的人,自己做软件方便客户,为哈就不能做个软件方便自己呢,所以我希望我的代码是自动提交的,说干就干,工具的原理就是利用SVN的API来实现自定义提交。
一、在项目中引用sharpsvn.dl(下载地址:http://pan.baidu.com/s/1hqfIKSk);
二、新建一个Form,Form中增加一个Timer,用于定时获取代码文件状态,New一个SharpSvn.SvnClient对象 client;
三、接口调用步骤:
1、用GetStatus方法获取文件状态:
client.GetStatus(path, out changeFiles);
2、用Add方法将需要使用SVN管理的代码文件路径添加到SVN:
client.Add(changeFile.Path);
3、用Commit方法做最后提交
client.Commit(workingcopy, args);

关键代码如下,是不是很简单呢!

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace AutoComitGUI
{
    public class clsCommons
    {
        public static string AppTitle = "Auto Commit";

        //添加文件到SVN
        public static bool AddFileToSVN(string strLogFile, string path, 
                                             Timer myTimer)
        {
            try
            {
                using (SharpSvn.SvnClient client = new SharpSvn.SvnClient())
                {
                    Collection<sharpsvn .svnstatuseventargs=""> changeFiles = new 
                                    Collection<sharpsvn .svnstatuseventargs="">();
                    client.GetStatus(path, out changeFiles);

                    foreach (SharpSvn.SvnStatusEventArgs changeFile in 
                                                         changeFiles)
                    {
                        if (changeFile.LocalContentStatus == 
                                           SharpSvn.SvnStatus.Missing)
                        {
                            client.Delete(changeFile.Path);
                            WriteLog(strLogFile, changeFile.Path + " Removed - 
                                     文件未找到或不存在)");

                            myTimer.Enabled = false;
                        }

                        if (changeFile.LocalContentStatus == 
                                         SharpSvn.SvnStatus.NotVersioned)
                        {
                            client.Add(changeFile.Path);
                            WriteLog(strLogFile, changeFile.Path + " Added - 
                                       新增文件!");

                            myTimer.Enabled = false;
                        }
                    }
                    return true;
                }
            }
            catch (Exception ex)
            {
                myTimer.Enabled = true;
                WriteLog(strLogFile, ex);
                return false;
            }
        }

        // 提交到服务器
        public static bool CommitToSVN(string strLogFile, string workingcopy, 
                                              string message, Timer myTimer)
        {
            using (SharpSvn.SvnClient client = new SharpSvn.SvnClient())
            {
                SharpSvn.SvnCommitArgs args = new SharpSvn.SvnCommitArgs();

                args.LogMessage = message;
                args.ThrowOnError = true;
                args.ThrowOnCancel = true;

                try
                {
                    myTimer.Enabled = true;
                    return client.Commit(workingcopy, args);
                }
                catch (Exception ex)
                {
                    myTimer.Enabled = true;
                    WriteLog(strLogFile, ex);
                    return false;
                }
            }
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值