c#實現遠程操作svn


/// <summary>
        /// 本地svn服務器地址
        /// </summary>
        private static string localSVN = ConfigurationManager.AppSettings["localSVN"].ToString();
        /// <summary>
        /// 線上svn服務器地址
        /// </summary>
        private static string onlineSVN = ConfigurationManager.AppSettings["onlineSVN"].ToString();
        /// <summary>
        /// 本地地址
        /// </summary>
        private static string localPath = ConfigurationManager.AppSettings["localPath"].ToString();

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="fullpath">添加的文件所在路徑</param>
        /// <returns></returns>
        public static bool AddSvn(string fullpath) {
            using (SvnClient client = new SvnClient()) {
                string path = fullpath;
                SvnAddArgs args = new SvnAddArgs();
                args.Depth = SvnDepth.Empty;
                return client.Add(path, args);
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="fileName">文件夾名稱</param>
        /// <returns></returns>
        public static string UpdateSvn(string fileName, string user, string pwd, string type) {
            string result = string.Empty;
            using (SvnClient client = new SvnClient()) {
                GetPermission(client, user, pwd);
                SvnInfoEventArgs serverInfo;
                SvnInfoEventArgs clientInfo;
                SvnUriTarget repos = new SvnUriTarget("http://" + (type == "local" ? localSVN : onlineSVN) + fileName);
                SvnPathTarget local = new SvnPathTarget(localPath + "\\" + fileName);

                client.GetInfo(repos, out serverInfo);

                client.Update(localPath + "\\" + fileName);

                client.GetInfo(local, out clientInfo);
                if (serverInfo.Revision > 0 && clientInfo.Revision > 0) {
                    result = serverInfo.Revision.ToString() + "-" + clientInfo.Revision.ToString();
                }
                return result;
            }
        }

        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string CommitSvn(string fileName, string user, string pwd, string type) {
            string result = string.Empty;
            using (SvnClient client = new SvnClient()) {
                GetPermission(client, user, pwd);
                SvnCommitArgs commitargs = new SvnCommitArgs();
                commitargs.LogMessage = "OA提交";
                SvnInfoEventArgs serverInfo;
                SvnInfoEventArgs clientInfo;
                SvnUriTarget repos = new SvnUriTarget("http://" + (type == "local" ? localSVN : onlineSVN) + fileName);
                SvnPathTarget local = new SvnPathTarget(localPath + "\\" + fileName);

                var b = client.Commit(localPath + "\\" + fileName, commitargs);
                client.GetInfo(repos, out serverInfo);
                client.GetInfo(local, out clientInfo);
                return serverInfo.Revision.ToString();
            }
        }

        /// <summary>
        /// 回滾
        /// </summary>
        /// <param name="fileName">文件夾名稱</param>
        /// <param name="ver">指定版本號</param>
        /// <returns></returns>
        public static string RollBackSvn(string fileName, int ver, string user, string pwd) {
            string result = string.Empty;
            using (SvnClient client = new SvnClient()) {
                GetPermission(client, user, pwd);

                SvnInfoEventArgs clientInfo;
                SvnPathTarget local = new SvnPathTarget(localPath + "\\" + fileName);

                client.Update(localPath + "\\" + fileName, new SvnUpdateArgs() { Revision = new SvnRevision(ver) });
                client.GetInfo(local, out clientInfo);

                return clientInfo.Revision.ToString();
            }
        }

        /// <summary>
        /// 獲取權限
        /// </summary>
        /// <param name="client"></param>
        private static void GetPermission(SvnClient client, string username, string password) {
            client.LoadConfiguration(Path.Combine(Path.GetTempPath(), "Svn"), true);
            client.Authentication.UserNamePasswordHandlers +=
                new EventHandler<SvnUserNamePasswordEventArgs>(
                    delegate(object s, SvnUserNamePasswordEventArgs e) {
                        e.UserName = username;
                        e.Password = password;
                    });
            client.Authentication.SslServerTrustHandlers +=
                new EventHandler<SvnSslServerTrustEventArgs>(
                    delegate(object s, SvnSslServerTrustEventArgs e) {
                        e.Save = true;
                    });
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM