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;
}
}
}
}
}