public async Task <IActionResult> CompareRevision(int articleId, int revisionId) { var article = await _dbContext.Articles.FindAsync(articleId); if (article == null) { _logger.LogInformation($"COMPARE: Article ID {articleId} not found"); return(RedirectToAction(nameof(Index))); } var revision = await _dbContext.ArticleRevisions.FindAsync(revisionId); if (revision == null || revision.ArticleId != articleId) { _logger.LogInformation($"COMPARE: Revision ID {revisionId} / Article ID {articleId} not found"); return(RedirectToAction(nameof(Index))); } var model = new CompareRevisionViewModel(); model.Article = article.ToModel(); model.Revision = revision.ToModel(); model.DiffModel = _diffBuilder.BuildDiffModel(revision.Body, article.Body); return(View(model)); }
private void FileSystemWatcher_Renamed(object sender, RenamedEventArgs e) { Console.ForegroundColor = Yellow; //Console.WriteLine($"A new file has been renamed from {e.OldName} to {e.Name}"); if (e.Name == FILE_NAME_TO_WATCH) { string oldText = File.ReadAllText(GetOldVersionPath()); string newText = File.ReadAllText(GetNewVersionPath()); var model = diffBuilder.BuildDiffModel(oldText ?? string.Empty, newText ?? string.Empty); int charCount = 0; for (int i = 0; i < model.OldText.Lines.Count; i++) { var oldLine = model.OldText.Lines[i]; var newLine = model.NewText.Lines[i]; if (oldLine.Type != DiffPlex.DiffBuilder.Model.ChangeType.Imaginary) { charCount += oldLine.Text.Length + 1; } switch (oldLine.Type) { case DiffPlex.DiffBuilder.Model.ChangeType.Unchanged: break; case DiffPlex.DiffBuilder.Model.ChangeType.Deleted: Console.ForegroundColor = Red; Console.WriteLine($"{oldLine.Position}: Removida: {oldLine.Text}"); session.AddActionDelete(charCount, oldLine.Text); break; case DiffPlex.DiffBuilder.Model.ChangeType.Inserted: Console.ForegroundColor = Green; Console.WriteLine($"{oldLine.Position}: Inserida: {oldLine.Text}"); //session.AddActionTyping(oldLine.Text); break; case DiffPlex.DiffBuilder.Model.ChangeType.Imaginary: session.AddActionTyping(charCount, "\n"); session.AddActionTyping(charCount, newLine.Text); break; case DiffPlex.DiffBuilder.Model.ChangeType.Modified: Console.ForegroundColor = Yellow; Console.WriteLine($"{oldLine.Position}: Modificada: {oldLine.Text}"); break; default: break; } } GenerateSessionJson(); StoreNewVersion(); } }
private void BuildDiffView() { // Show the file paths at the beginning richTextBoxSide1.Text = $" | {m_FileSideOld}" + Environment.NewLine; richTextBoxSide2.Text = $" | {m_FileSideNew}" + Environment.NewLine; var diffModel = m_DiffBuilder.BuildDiffModel(m_FileOldContent, m_FileNewContent); foreach (DiffPiece line in diffModel.OldText.Lines) { InsertAndColor(richTextBoxSide1, line); } foreach (DiffPiece line in diffModel.NewText.Lines) { InsertAndColor(richTextBoxSide2, line); } richTextBoxSide1.Select(0, 0); richTextBoxSide1.ScrollToCaret(); richTextBoxSide2.Select(0, 0); richTextBoxSide2.ScrollToCaret(); }
private static void PrintToHtml(Product product1, ISideBySideDiffBuilder diffBuilder, Product product2) { foreach (var key in product1.SpecDic.Keys) { //var diffResult = differ.CreateCharacterDiffs(product1.SpecDic[key].Value, product2.SpecDic[key].Value,true); var result = diffBuilder.BuildDiffModel(product1.SpecDic[key].Value, product2.SpecDic[key].Value); foreach (var line in result.OldText.Lines) { if (line.Type == ChangeType.Unchanged) { Console.Write(line.Text); } else { foreach (var piece in line.SubPieces) { if (piece.Type == ChangeType.Unchanged) { Console.Write(piece.Text); } else { ConsoleColor color = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; Console.Write(piece.Text); Console.ForegroundColor = color; } } } } Console.Write(" - "); foreach (var line in result.NewText.Lines) { if (line.Type == ChangeType.Unchanged) { Console.Write(line.Text); } else { foreach (var piece in line.SubPieces) { if (piece.Type == ChangeType.Unchanged) { Console.Write(piece.Text); } else { ConsoleColor color = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; Console.Write(piece.Text); Console.ForegroundColor = color; } } } } Console.WriteLine(); //foreach (var block in result.) //{ // diffResult.DiffBlocks[0]. //} } }
public ActionResult GetFileDiff(int issueId) { string newFile = string.Empty; string oldFile = string.Empty; string fileName = Request["filename"] ?? string.Empty; string fullfilename = Request["fullfilename"] ?? string.Empty; string provider = Request["provider"] ?? string.Empty; string revisionid = Request["revisionid"] ?? string.Empty; string fileid = Request["fileid"] ?? string.Empty; string repositoryUrl = Request["repositoryurl"] ?? string.Empty; string workspace = Request["workspace"] ?? string.Empty; fileName = fileName.Trim(); fullfilename = fullfilename.Trim(); provider = provider.Trim(); revisionid = revisionid.Trim(); fileid = fileid.Trim(); repositoryUrl = repositoryUrl.Trim(); workspace = workspace.Trim(); //Authentication details string authenticateForm = string.Empty; bool IsUserAuthorized = false; bool isFileIdMissing = false; string data = string.Empty; string errorMessage = string.Empty; if (!repositoryUrl.IsEmpty()) { if (provider == SourceControlProvider.GitHub.ToString()) { GitHub github = new GitHub(); if (github.AuthenticateUser(CurrentUser, repositoryUrl, GeminiContext)) { try { if (fileid.IsEmpty()) { isFileIdMissing = true; // Need to do this, because when github sends back the committed data there is no fileid(which we need to get the file content) for the files. // This will go and get the fileids once for each commit where fileid's are empty fileid = github.updateFileIds(GeminiContext, repositoryUrl, revisionid, fileName, issueId); } newFile = github.GetFileContent(GeminiContext, issueId, repositoryUrl, revisionid, fileName, fileid); oldFile = github.GetFileContent(GeminiContext, issueId, repositoryUrl, revisionid, fileName, fileid, true); IsUserAuthorized = true; } catch (UnauthorizedAccessException ex) { authenticateForm = github.CreateAuthenticationForm(UserContext.Url, repositoryUrl, fileName); errorMessage = "Invalid login details"; } } else { authenticateForm = github.CreateAuthenticationForm(UserContext.Url, repositoryUrl, fileName); errorMessage = "Invalid login details"; } } else if (provider == SourceControlProvider.TFS2012.ToString()) { TFS2012 tfs2012 = new TFS2012(); if (tfs2012.AuthenticateUser(CurrentUser, repositoryUrl, GeminiContext)) { try { oldFile = tfs2012.GetFileContent(GeminiContext, issueId, fileName, fullfilename, workspace, revisionid, fileid, repositoryUrl, true); newFile = tfs2012.GetFileContent(GeminiContext, issueId, fileName, fullfilename, workspace, revisionid, fileid, repositoryUrl); IsUserAuthorized = true; } catch (UnauthorizedAccessException ex) { authenticateForm = tfs2012.CreateAuthenticationForm(UserContext.Url, repositoryUrl, fileName); errorMessage = "Invalid login details"; } catch (TeamFoundationServerUnauthorizedException ex) { authenticateForm = tfs2012.CreateAuthenticationForm(UserContext.Url, repositoryUrl, fileName); errorMessage = ex.Message; } } else { authenticateForm = tfs2012.CreateAuthenticationForm(UserContext.Url, repositoryUrl, fileName); } } else if (provider == SourceControlProvider.TFS2010.ToString()) { TFS2010 tfs2010 = new TFS2010(); if (tfs2010.AuthenticateUser(CurrentUser, repositoryUrl, GeminiContext)) { try { oldFile = tfs2010.GetFileContent(GeminiContext, issueId, fileName, fullfilename, workspace, revisionid, fileid, repositoryUrl, true); newFile = tfs2010.GetFileContent(GeminiContext, issueId, fileName, fullfilename, workspace, revisionid, fileid, repositoryUrl); IsUserAuthorized = true; } catch (UnauthorizedAccessException ex) { authenticateForm = tfs2010.CreateAuthenticationForm(UserContext.Url, repositoryUrl, fileName); errorMessage = "Invalid login details"; } catch (TeamFoundationServerUnauthorizedException ex) { authenticateForm = tfs2010.CreateAuthenticationForm(UserContext.Url, repositoryUrl, fileName); errorMessage = ex.Message; } } else { authenticateForm = tfs2010.CreateAuthenticationForm(UserContext.Url, repositoryUrl, fileName); } } else if (provider == SourceControlProvider.SVN.ToString()) { SVN svn = new SVN(); if (svn.AuthenticateUser(CurrentUser, repositoryUrl, GeminiContext)) { try { oldFile = svn.GetFileContent(GeminiContext, issueId, repositoryUrl, fileName, revisionid, true); newFile = svn.GetFileContent(GeminiContext, issueId, repositoryUrl, fileName, revisionid); IsUserAuthorized = true; } catch (UnauthorizedAccessException ex) { authenticateForm = svn.CreateAuthenticationForm(UserContext.Url, repositoryUrl, fileName); errorMessage = "Invalid login details"; } } else { authenticateForm = svn.CreateAuthenticationForm(UserContext.Url, repositoryUrl, fileName); errorMessage = "Invalid login details"; } } else if (provider == SourceControlProvider.Git.ToString()) { Git git = new Git(); if (git.AuthenticateUser(CurrentUser, repositoryUrl, GeminiContext)) { try { oldFile = git.GetFileContent(GeminiContext, issueId, repositoryUrl, fileName, revisionid, true); newFile = git.GetFileContent(GeminiContext, issueId, repositoryUrl, fileName, revisionid); IsUserAuthorized = true; } catch (UnauthorizedAccessException ex) { authenticateForm = git.CreateAuthenticationForm(UserContext.Url, repositoryUrl, fileName); errorMessage = "Invalid login details"; } } else { authenticateForm = git.CreateAuthenticationForm(UserContext.Url, repositoryUrl, fileName); errorMessage = "Invalid login details"; } } else if (provider == SourceControlProvider.Bitbucket.ToString()) { Bitbucket bitbucket = new Bitbucket(); if (bitbucket.AuthenticateUser(CurrentUser, repositoryUrl, GeminiContext)) { try { oldFile = bitbucket.GetFileContent(GeminiContext, issueId, repositoryUrl, fileName, revisionid, true); newFile = bitbucket.GetFileContent(GeminiContext, issueId, repositoryUrl, fileName, revisionid); IsUserAuthorized = true; } catch (UnauthorizedAccessException ex) { authenticateForm = bitbucket.CreateAuthenticationForm(UserContext.Url, repositoryUrl, fileName); errorMessage = "Invalid login details"; } } else { authenticateForm = bitbucket.CreateAuthenticationForm(UserContext.Url, repositoryUrl, fileName); errorMessage = "Invalid login details"; } } } else { errorMessage = "ERROR: Repository Url is missing"; } if (IsUserAuthorized) { // Handle BOM markers oldFile = oldFile.Replace("\x00EF\x00BB\x00BF", ""); newFile = newFile.Replace("\x00EF\x00BB\x00BF", ""); // Diff var tmpModel = diffBuilder.BuildDiffModel(oldFile ?? string.Empty, newFile ?? string.Empty); IssueWidgetData <List <Comment> > tmpData = GeminiContext.IssueWidgetStore.Get <List <Comment> >(issueId, Constants.AppId, Constants.ControlId); DiffplexComments model = new DiffplexComments(); if (tmpData != null) { SourceControlProvider enumProvider; try { enumProvider = (SourceControlProvider)Enum.Parse(typeof(SourceControlProvider), provider, true); } catch (ArgumentException ex) { enumProvider = 0; GeminiApp.LogException(new Exception(ex.Message) { Source = "Saucery" }, false); } var comments = tmpData.Value.FindAll(f => f.FileName == fileName && f.RepositoryUrl == repositoryUrl && f.Provider == enumProvider && f.ChangesetId == revisionid); if (comments != null) { model.data = tmpModel; model.comments = comments; } } else { model.data = tmpModel; model.comments = new List <Comment>(); } data = RenderPartialViewToString(this, AppManager.Instance.GetAppUrl("F473D13E-19B7-45F3-98ED-6ED77B6BAB0A", "views/diff.cshtml"), model); } else { data = authenticateForm; } return(JsonSuccess(new { authenticationSuccess = IsUserAuthorized, fileid = fileid, data = data, isFileIdMissing = isFileIdMissing, errorMessage = errorMessage })); }
private static void PrintToConsole(Product product1, ISideBySideDiffBuilder diffBuilder, Product product2) { foreach (var key in product1.SpecDic.Keys) { //var diffResult = differ.CreateCharacterDiffs(product1.SpecDic[key].Value, product2.SpecDic[key].Value,true); var result = diffBuilder.BuildDiffModel(product1.SpecDic[key].Value, product2.SpecDic[key].Value); foreach (var line in result.OldText.Lines) { if (line.Type == ChangeType.Unchanged) { Console.Write(line.Text); } else { foreach (var piece in line.SubPieces) { if (piece.Type == ChangeType.Unchanged) { Console.Write(piece.Text); } else { foreach (var character in piece.SubPieces) { if (character.Type == ChangeType.Unchanged) { Console.Write(character.Text); } else { ConsoleColor color = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; Console.Write(character.Text); Console.ForegroundColor = color; } } } } } } Console.Write(" - "); foreach (var line in result.NewText.Lines) { if (line.Type == ChangeType.Unchanged) { Console.Write(line.Text); } else { foreach (var piece in line.SubPieces) { if (piece.Type == ChangeType.Unchanged) { Console.Write(piece.Text); } else { foreach (var character in piece.SubPieces) { if (character.Type == ChangeType.Unchanged) { Console.Write(character.Text); } else { ConsoleColor color = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; Console.Write(character.Text); Console.ForegroundColor = color; } } } } } } // line diff comment if (result.OldText.Lines[0].Type != ChangeType.Unchanged) { string old= result.OldText.Lines[0].Text; int oldNum; Match match = numberReg.Match(old); if (int.TryParse(match.Groups[0].Value, out oldNum)) { int newNum; if (int.TryParse(numberReg.Match(result.NewText.Lines[0].Text).Groups[0].Value, out newNum)) { int diff = newNum - oldNum; string lastStr = old.Substring(match.Groups[0].Index + match.Groups[0].Length); Console.Write(" - {0} {1}{2}",diff>0?"<":">",Math.Abs(diff),lastStr); } } } Console.WriteLine(); //foreach (var block in result.) //{ // diffResult.DiffBlocks[0]. //} } }
public ActionResult Diff(string oldText, string newText) { var model = diffBuilder.BuildDiffModel(oldText ?? string.Empty, newText ?? string.Empty); return(View(model)); }
/// <summary> /// Method for comparing sources between local copy /// and remote version of single page from sitemap /// </summary> /// <param name="localCopy"></param> /// <param name="remoteCopy"></param> private SideBySideDiffModel CompareSources(string localCopy, string remoteCopy) { var diff = _sideBySideDiffBuilder.BuildDiffModel(localCopy, remoteCopy); return(diff); }