private MatchResult match(DiffRefs diffRefs, DiffToolInfo difftoolInfo) { FullContextDiffProvider provider = new FullContextDiffProvider(_gitRepository); FullContextDiff context = provider.GetFullContextDiff(diffRefs.LeftSHA, diffRefs.RightSHA, difftoolInfo.Left?.FileName ?? null, difftoolInfo.Right?.FileName ?? null); Debug.Assert(context.Left.Count == context.Right.Count); bool isCurrentSideLeft = difftoolInfo.IsLeftSideCurrent; DiffToolInfo.Side currentSide = isCurrentSideLeft ? difftoolInfo.Left.Value : difftoolInfo.Right.Value; SparsedList <string> firstList = isCurrentSideLeft ? context.Left : context.Right; SparsedList <string> secondList = isCurrentSideLeft ? context.Right : context.Left; SparsedListIterator <string> itFirst = SparsedListUtils.FindNth(firstList.Begin(), currentSide.LineNumber - 1); SparsedListIterator <string> itSecond = SparsedListUtils.Advance(secondList.Begin(), itFirst.Position); return(new MatchResult { LeftLineNumber = isCurrentSideLeft ? currentSide.LineNumber : itSecond.LineNumber + 1, RightLineNumber = isCurrentSideLeft ? itSecond.LineNumber + 1 : currentSide.LineNumber }); }
private int?getOppositeLine(DiffRefs refs, bool isLeftSide, string leftFileName, string rightFileName, int lineNumber) { FullContextDiff context = _git.FullContextDiffProvider.GetFullContextDiff( refs.LeftSHA, refs.RightSHA, leftFileName, rightFileName); Debug.Assert(context.Left.Count == context.Right.Count); SparsedList <string> currentList = isLeftSide ? context.Left : context.Right; SparsedList <string> oppositeList = isLeftSide ? context.Right : context.Left; SparsedListIterator <string> itCurrentList = SparsedListUtils.FindNth(currentList.Begin(), lineNumber - 1); SparsedListIterator <string> itOppositeList = SparsedListUtils.Advance(oppositeList.Begin(), itCurrentList.Position); return(itOppositeList.GetLineNumber() == null ? new int?() : itOppositeList.GetLineNumber().Value + 1); }
/// <summary> /// Throws ContextMakingException. /// </summary> public FullContextDiff GetFullContextDiff(string leftSHA, string rightSHA, string leftFileName, string rightFileName) { FullContextDiff fullContextDiff = new FullContextDiff(new SparsedList <string>(), new SparsedList <string>()); GitDiffArguments arguments = new GitDiffArguments( GitDiffArguments.DiffMode.Context, new GitDiffArguments.CommonArguments(leftSHA, rightSHA, leftFileName, rightFileName, null), new GitDiffArguments.DiffContextArguments(Constants.FullContextSize)); IEnumerable <string> fullDiff; try { fullDiff = _git?.ShowDiff(arguments); } catch (GitNotAvailableDataException ex) { throw new FullContextDiffProviderException("Cannot obtain git diff", ex); } if (fullDiff == null) { throw new FullContextDiffProviderException("Cannot obtain git diff", null); } fullDiff = fullDiff.Where(x => !String.IsNullOrEmpty(x)); if (fullDiff.Count() == 0) { Trace.TraceWarning(String.Format( "[FullContextDiffProvider] Context size is zero. LeftSHA: {0}, Right SHA: {1}," + " Left file: {2}, Right file: {3}", leftSHA, rightSHA, leftFileName, rightFileName)); } bool skip = true; foreach (string line in fullDiff) { char sign = line[0]; if (skip) { // skip meta information about diff if (sign == '@') { // next lines should not be skipped because they contain a diff itself skip = false; } continue; } string lineOrig = line.Substring(1, line.Length - 1); switch (sign) { case '-': fullContextDiff.Left.Add(lineOrig); fullContextDiff.Right.Add(null); break; case '+': fullContextDiff.Left.Add(null); fullContextDiff.Right.Add(lineOrig); break; case ' ': fullContextDiff.Left.Add(lineOrig); fullContextDiff.Right.Add(lineOrig); break; } } return(fullContextDiff); }