// 点击“提交到SVN”按钮 private void btnCommit_Click(object sender, EventArgs e) { FileState fileState = Utils.GetFileState(AppValues.LocalExcelFilePath); if (fileState == FileState.Inexist) { MessageBox.Show("本地表已不存在,请勿在使用本工具时对母表进行操作", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (fileState == FileState.IsOpen) { MessageBox.Show("本地表正被其他软件使用,请关闭后再重试", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // 判断本地表是否相较SVN最新版本发生变动,如果本地就是SVN中最新版本且未进行改动则无需提交 SvnException svnException = null; SvnStatusEventArgs localFileState = OperateSvnHelper.GetLocalFileState(AppValues.LocalExcelFilePath, out svnException); if (svnException == null) { SvnInfoEventArgs svnFileInfo = OperateSvnHelper.GetSvnFileInfo(AppValues.SvnExcelFilePath, out svnException); if (svnException == null) { // 本地表的版本号 long localFileRevision = localFileState.LastChangeRevision; // SVN中的母表的版本号 long svnFileRevision = svnFileInfo.LastChangeRevision; if (localFileState.LocalContentStatus == SvnStatus.Normal) { if (localFileRevision == svnFileRevision) { MessageBox.Show("本地母表与SVN最新版本完全一致,无需进行提交操作", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } else if (MessageBox.Show("本地母表不是SVN最新版本,但与同版本SVN表完全一致\n\n确定要在此情况下进行提交操作吗?", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No) { return; } } // 本地表与SVN最新版本不同,则要下载一份SVN中最新版本与本地表进行对比,在用户手工选择需要提交哪些改动后将合并后的新表进行提交操作 string svnCopySavePath = Utils.CombinePath(AppValues.PROGRAM_FOLDER_PATH, string.Format("SVN最新母表副本 {0:yyyy年MM月dd日 HH时mm分ss秒} 对应SVN版本号{1}.xlsx", DateTime.Now, svnFileInfo.LastChangeRevision)); Exception exportException; bool result = OperateSvnHelper.ExportSvnFileToLocal(AppValues.SvnExcelFilePath, svnCopySavePath, svnFileInfo.LastChangeRevision, out exportException); if (exportException != null) { MessageBox.Show(string.Concat("下载SVN中最新母表存到本地失败,错误原因为:", svnException.Message), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else if (result == false) { MessageBox.Show("下载SVN中最新母表存到本地失败", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // 解析本地母表 string errorString = null; CommitExcelInfo localExcelInfo = CommitExcelFileHelper.AnalyzeCommitExcelFile(AppValues.LocalExcelFilePath, AppValues.CommentLineStartChar, localFileRevision, out errorString); if (errorString != null) { MessageBox.Show(string.Concat("本地母表存在以下错误,请修正后重试\n\n", errorString), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // 解析下载到本地的SVN最新母表的副本 CommitExcelInfo svnExcelInfo = CommitExcelFileHelper.AnalyzeCommitExcelFile(svnCopySavePath, AppValues.CommentLineStartChar, svnFileRevision, out errorString); if (errorString != null) { MessageBox.Show(string.Concat("SVN母表存在以下错误,请修正后重试\n\n", errorString), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // 对比本地母表与SVN中的母表 CommitCompareResult compareResult = CommitExcelFileHelper.CompareCommitExcelFile(localExcelInfo, svnExcelInfo); if (compareResult.IsHasDiff() == false) { MessageBox.Show("经对比发现本地母表与SVN中内容完全相同,无需进行提交操作\n请注意本功能仅会对比本地母表与SVN中母表的Key及主语言译文变动,不对各语种进行比较", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } else { // 弹出对比结果界面,让用户选择需要合并到SVN中母表的变动 ResolveConflictWhenCommitForm resolveForm = new ResolveConflictWhenCommitForm(compareResult, localExcelInfo, svnExcelInfo); resolveForm.ShowDialog(this); } } else { MessageBox.Show(string.Concat("无法获取SVN中母表信息,错误原因为:", svnException.Message), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } else { MessageBox.Show(string.Concat("无法获取本地表状态,错误原因为:", svnException.Message), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } }
// 点击“获取本地表状态”按钮 private void btnGetLocalExcelFileState_Click(object sender, EventArgs e) { FileState fileState = Utils.GetFileState(AppValues.LocalExcelFilePath); if (fileState == FileState.Inexist) { MessageBox.Show("本地表已不存在,请勿在使用本工具时对母表进行操作", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } SvnException svnException = null; SvnStatusEventArgs localFileState = OperateSvnHelper.GetLocalFileState(AppValues.LocalExcelFilePath, out svnException); if (svnException == null) { SvnInfoEventArgs svnFileInfo = OperateSvnHelper.GetSvnFileInfo(AppValues.SvnExcelFilePath, out svnException); if (svnException == null) { // 本地表的版本号 long localFileRevision = localFileState.LastChangeRevision; // 本地表文件相较SVN中的状态 SvnStatus svnStatus = localFileState.LocalContentStatus; // SVN中的母表的版本号 long svnFileRevision = svnFileInfo.LastChangeRevision; // 最后修改时间 DateTime svnFileChangeTime = svnFileInfo.LastChangeTime; // 最后修改者 string svnFileChangeAuthor = svnFileInfo.LastChangeAuthor; StringBuilder infoStringBuilder = new StringBuilder(); infoStringBuilder.Append("本地路径:").AppendLine(AppValues.LocalExcelFilePath); infoStringBuilder.Append("SVN路径:").AppendLine(AppValues.SvnExcelFilePath); infoStringBuilder.Append("本地版本号:").AppendLine(localFileRevision.ToString()); infoStringBuilder.Append("SVN版本号:").AppendLine(svnFileRevision.ToString()); infoStringBuilder.Append("SVN版本最后修改时间:").AppendLine(svnFileChangeTime.ToLocalTime().ToString()); infoStringBuilder.Append("SVN版本最后修改者:").AppendLine(svnFileChangeAuthor); infoStringBuilder.Append("本地文件是否被打开:").AppendLine(fileState == FileState.IsOpen ? "是" : "否"); if (svnFileInfo.Lock != null) { infoStringBuilder.AppendLine("SVN中此文件是否被锁定:是"); infoStringBuilder.Append("锁定者:").AppendLine(svnFileInfo.Lock.Owner); infoStringBuilder.Append("锁定时间:").AppendLine(svnFileInfo.Lock.CreationTime.ToLocalTime().ToString()); infoStringBuilder.Append("锁定原因:").AppendLine(svnFileInfo.Lock.Comment); } infoStringBuilder.Append("本地文件相较SVN中的状态:").Append(svnStatus.ToString()).Append("(").Append(OperateSvnHelper.GetSvnStatusDescription(svnStatus)).AppendLine(")"); infoStringBuilder.Append("本地文件是否是SVN中最新版本且本地内容未作修改:").AppendLine(localFileRevision == svnFileRevision && svnStatus == SvnStatus.Normal ? "是" : "否"); MessageBox.Show(infoStringBuilder.ToString(), "本地表状态信息", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show(string.Concat("无法获取SVN中母表信息,错误原因为:", svnException.Message), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } else { MessageBox.Show(string.Concat("无法获取本地表状态,错误原因为:", svnException.Message), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } }
// 点击“Revert并Update本地表”按钮 private void btnRevertAndUpdateLocalExcelFile_Click(object sender, EventArgs e) { FileState fileState = Utils.GetFileState(AppValues.LocalExcelFilePath); if (fileState == FileState.Inexist) { MessageBox.Show("本地表已不存在,请勿在使用本工具时对母表进行操作", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (fileState == FileState.IsOpen) { MessageBox.Show("本地表正被其他软件使用,请关闭后再重试", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // 判断本地文件是否相较SVN中发生变动,若有变动提示进行备份后执行Revert SvnException svnException = null; SvnStatusEventArgs localFileState = OperateSvnHelper.GetLocalFileState(AppValues.LocalExcelFilePath, out svnException); if (svnException == null) { if (localFileState.LocalContentStatus == SvnStatus.Modified) { DialogResult dialogResult = MessageBox.Show("检测到本地文件相较于线上已发生变动,是否要进行备份?\n\n点击“是”选择存放本地表备份路径后进行备份\n点击“否”不进行备份,直接Revert后Update", "选择是否对本地表进行备份", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dialogResult == DialogResult.Yes) { SaveFileDialog dialog = new SaveFileDialog(); dialog.ValidateNames = true; dialog.Title = "请选择本地表备份路径"; dialog.Filter = "Excel files (*.xlsx)|*.xlsx"; dialog.FileName = string.Format("Revert前本地母表备份 {0:yyyy年MM月dd日 HH时mm分ss秒} 对应SVN版本号{1}.xlsx", DateTime.Now, localFileState.LastChangeRevision); if (dialog.ShowDialog() == DialogResult.OK) { string backupPath = dialog.FileName; // 检查要覆盖备份的Excel文件是否已存在且正被其他程序使用 if (Utils.GetFileState(backupPath) == FileState.IsOpen) { MessageBox.Show("要覆盖的Excel文件正被其他程序打开,请关闭后重试\n\nRevert并Update功能被迫中止", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } try { File.Copy(AppValues.LocalExcelFilePath, backupPath, true); MessageBox.Show("备份本地母表成功,点击“确定”后开始执行Revert并Update操作", "恭喜", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception exception) { string errorString = string.Format("备份本地母表({0})至指定路径({1})失败:{2}\n\nRevert并Update功能被迫中止", AppValues.LocalExcelFilePath, backupPath, exception.Message); MessageBox.Show(errorString, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } else { MessageBox.Show("未选择备份路径,无法进行本地母表备份\n\nRevert并Update功能被迫中止", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } bool result = OperateSvnHelper.Revert(AppValues.LocalExcelFilePath, out svnException); if (svnException == null) { if (result == false) { MessageBox.Show("Revert失败", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } else { MessageBox.Show(string.Concat("Revert失败,错误原因为:", svnException.Message), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } else if (localFileState.LocalContentStatus != SvnStatus.Normal) { MessageBox.Show(string.Format("本地表状态为{0},本工具仅支持Normal或Modified状态", localFileState.LocalContentStatus), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // 判断是否需要进行Update SvnInfoEventArgs svnFileInfo = OperateSvnHelper.GetSvnFileInfo(AppValues.SvnExcelFilePath, out svnException); if (svnException == null) { // 本地表的版本号 long localFileRevision = localFileState.LastChangeRevision; // SVN中的母表的版本号 long svnFileRevision = svnFileInfo.LastChangeRevision; if (localFileRevision == svnFileRevision) { if (localFileState.LocalContentStatus == SvnStatus.Modified) { MessageBox.Show("成功进行Revert操作,本地表已是SVN最新版本无需Update\n\nRevert并Update功能完成", "恭喜", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } else { MessageBox.Show("本地表已是SVN最新版本且与SVN中内容一致,无需进行此操作", "恭喜", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } } else { bool result = OperateSvnHelper.Update(AppValues.LocalExcelFilePath, out svnException); if (svnException == null) { if (result == false) { MessageBox.Show("Update失败", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else { MessageBox.Show("执行Revert并Update功能成功", "恭喜", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } } else { if (svnException is SvnAuthorizationException || svnException is SvnOperationCanceledException) { MessageBox.Show("没有权限进行Update操作,请输入合法的SVN账户信息后重试", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else { MessageBox.Show(string.Concat("Update失败,错误原因为:", svnException.Message), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } } } else { MessageBox.Show(string.Concat("无法获取SVN中母表信息,错误原因为:", svnException.Message), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } else { MessageBox.Show(string.Concat("无法获取本地表状态,错误原因为:", svnException.Message), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } }
// 点击“检查本地表”按钮 private void btnCheckLocalExcelFilePath_Click(object sender, EventArgs e) { string localExcelFilePath = txtLocalExcelFilePath.Text.Trim(); if (string.IsNullOrEmpty(localExcelFilePath)) { MessageBox.Show("请先输入或选择本机Working Copy中Excel母表所在路径", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } FileState fileState = Utils.GetFileState(localExcelFilePath); if (fileState == FileState.Inexist) { MessageBox.Show("输入的本机Working Copy中Excel母表所在路径不存在,建议点击\"选择\"按钮进行文件选择", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (!AppValues.EXCEL_FILE_EXTENSION.Equals(Path.GetExtension(localExcelFilePath), StringComparison.CurrentCultureIgnoreCase)) { MessageBox.Show(string.Format("本工具仅支持读取扩展名为{0}的Excel文件", AppValues.EXCEL_FILE_EXTENSION), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // 检查指定的本地表是否处于SVN管理下 SvnException svnException = null; string fileFullPath = Path.GetFullPath(localExcelFilePath); SvnInfoEventArgs localFileInfo = OperateSvnHelper.GetLocalFileInfo(fileFullPath, out svnException); if (svnException == null) { // 判断该文件相较SVN中的状态 SvnStatusEventArgs localFileState = OperateSvnHelper.GetLocalFileState(fileFullPath, out svnException); if (svnException == null) { if (localFileState.LocalContentStatus == SvnStatus.Normal || localFileState.LocalContentStatus == SvnStatus.Modified) { _ChangeStateWhenSetLocalExcelPath(true); txtCommentLineStartChar.Enabled = false; // 读取设置的注释行开头字符 _SetCommentLineStartChar(); AppValues.LocalExcelFilePath = fileFullPath; AppValues.SvnExcelFilePath = localFileInfo.Uri.ToString(); } else { MessageBox.Show(string.Format("本地表状态为{0},本工具仅支持Normal或Modified状态", localFileState.LocalContentStatus), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } else { MessageBox.Show(string.Concat("无法获取本地表状态,错误原因为:", svnException.Message), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } else { if (svnException is SvnInvalidNodeKindException) { MessageBox.Show("输入的本机Working Copy中Excel母表所在路径不在SVN管理下", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else { MessageBox.Show(string.Concat("输入的本机Working Copy中Excel母表所在路径无效,错误原因为:", svnException.Message), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } }