/// <summary> /// Throws ArgumentException. /// Throws GitOperationException in case of problems with git. /// </summary> public DiffContext GetContext(DiffPosition position, ContextDepth depth) { if (!Context.Helpers.IsValidPosition(position)) { throw new ArgumentException( String.Format("Bad \"position\": {0}", position.ToString())); } if (!Context.Helpers.IsValidContextDepth(depth)) { throw new ArgumentException( String.Format("Bad \"depth\": {0}", depth.ToString())); } bool isRightSideContext = position.RightLine != null; int linenumber = isRightSideContext ? int.Parse(position.RightLine) : int.Parse(position.LeftLine); string filename = isRightSideContext ? position.RightPath : position.LeftPath; string sha = isRightSideContext ? position.Refs.RightSHA : position.Refs.LeftSHA; List <string> contents = _gitRepository.ShowFileByRevision(filename, sha); if (linenumber > contents.Count) { throw new ArgumentException( String.Format("Line number {0} is greater than total line number count, invalid \"position\": {1}", linenumber.ToString(), position.ToString())); } return(createDiffContext(linenumber, contents, isRightSideContext, depth)); }
/// <summary> /// Throws ArgumentException, ContextMakingException. /// </summary> public DiffContext GetContext(DiffPosition position, ContextDepth depth) { if (!Context.Helpers.IsValidPosition(position)) { throw new ArgumentException( String.Format("Bad \"position\": {0}", position.ToString())); } if (!Context.Helpers.IsValidContextDepth(depth)) { throw new ArgumentException( String.Format("Bad \"depth\": {0}", depth.ToString())); } // If RightLine is valid, then it points to either added/modified or unchanged line, handle them the same way bool isRightSideContext = position.RightLine != null; int linenumber = isRightSideContext ? int.Parse(position.RightLine) : int.Parse(position.LeftLine); string leftFilename = position.LeftPath; string rightFilename = position.RightPath; string leftSHA = position.Refs.LeftSHA; string rightSHA = position.Refs.RightSHA; FullContextDiffProvider provider = new FullContextDiffProvider(_git); FullContextDiff context = provider.GetFullContextDiff(leftSHA, rightSHA, leftFilename, rightFilename); Debug.Assert(context.Left.Count == context.Right.Count); if (linenumber > context.Left.Count) { throw new ArgumentException( String.Format("Line number {0} is greater than total line number count, invalid \"position\": {1}", linenumber.ToString(), position.ToString())); } return(createDiffContext(linenumber, isRightSideContext, context, depth)); }
// isRightSideContext is true when linenumber corresponds to the right side (sha2). // linenumber is one-based private DiffContext createDiffContext(int linenumber, bool isRightSideContext, FullContextDiff context, ContextDepth depth) { int startLineNumber = Math.Max(1, linenumber - depth.Up); int endLineNumber = linenumber + depth.Down; SparsedListIterator <string> itLeft = context.Left.Begin(); SparsedListIterator <string> itRight = context.Right.Begin(); if (isRightSideContext) { itRight = SparsedListUtils.FindNth(itRight, startLineNumber - 1); itLeft = SparsedListUtils.Advance(itLeft, itRight.Position); } else { itLeft = SparsedListUtils.FindNth(itLeft, startLineNumber - 1); itRight = SparsedListUtils.Advance(itRight, itLeft.Position); } DiffContext diffContext = new DiffContext { Lines = new List <DiffContext.Line>() }; int iContextLine = 0; while (true) { int?leftLineNumber = itLeft.LineNumber != null ? itLeft.LineNumber + 1 : null; int?rightLineNumber = itRight.LineNumber != null ? itRight.LineNumber + 1 : null; DiffContext.Line line = getLineContext(leftLineNumber, rightLineNumber, itLeft.Current, itRight.Current); diffContext.Lines.Add(line); if ((leftLineNumber.HasValue && !isRightSideContext && leftLineNumber == linenumber) || (rightLineNumber.HasValue && isRightSideContext && rightLineNumber == linenumber)) { // zero-based index of a selected line in DiffContext.Lines diffContext.SelectedIndex = iContextLine; } if ((leftLineNumber.HasValue && !isRightSideContext && leftLineNumber >= endLineNumber) || (rightLineNumber.HasValue && isRightSideContext && rightLineNumber >= endLineNumber)) { // we've just reached a line that should not be included in the context break; } if (!itLeft.Next() || !itRight.Next()) { // we've just reached the end break; } ++iContextLine; } return(diffContext); }
/// <summary> /// Throws ArgumentException, ContextMakingException. /// </summary> public DiffContext GetContext(DiffPosition position, ContextDepth depth) { if (!Context.Helpers.IsValidPosition(position)) { throw new ArgumentException( String.Format("Bad \"position\": {0}", position.ToString())); } if (!Context.Helpers.IsValidContextDepth(depth)) { throw new ArgumentException( String.Format("Bad \"depth\": {0}", depth.ToString())); } GitDiffAnalyzer analyzer = new GitDiffAnalyzer(_git, position.Refs.LeftSHA, position.Refs.RightSHA, position.LeftPath, position.RightPath); // If RightLine is valid, then it points to either added/modified or unchanged line, handle them the same way bool isRightSideContext = position.RightLine != null; int linenumber = isRightSideContext ? int.Parse(position.RightLine) : int.Parse(position.LeftLine); string filename = isRightSideContext ? position.RightPath : position.LeftPath; string sha = isRightSideContext ? position.Refs.RightSHA : position.Refs.LeftSHA; GitShowRevisionArguments arguments = new GitShowRevisionArguments(filename, sha); IEnumerable <string> contents; try { contents = _git?.ShowRevision(arguments); } catch (GitNotAvailableDataException ex) { throw new ContextMakingException("Cannot obtain git revision", ex); } if (contents == null) { throw new ContextMakingException("Cannot obtain git revision", null); } if (linenumber > contents.Count()) { throw new ArgumentException( String.Format("Line number {0} is greater than total line number count, invalid \"position\": {1}", linenumber.ToString(), position.ToString())); } return(createDiffContext(linenumber, isRightSideContext, contents, analyzer, depth)); }
/// <summary> /// Throws ArgumentException, ContextMakingException. /// </summary> public DiffContext GetContext(DiffPosition position, ContextDepth depth, UnchangedLinePolicy unchangedLinePolicy) { if (!Context.Helpers.IsValidPosition(position)) { throw new ArgumentException( String.Format("Bad \"position\": {0}", position.ToString())); } if (!Context.Helpers.IsValidContextDepth(depth)) { throw new ArgumentException( String.Format("Bad \"depth\": {0}", depth.ToString())); } bool isRightSideContext = Helpers.IsRightSidePosition(position, unchangedLinePolicy); int linenumber = isRightSideContext ? Helpers.GetRightLineNumber(position) : Helpers.GetLeftLineNumber(position); string filename = isRightSideContext ? position.RightPath : position.LeftPath; string sha = isRightSideContext ? position.Refs.RightSHA : position.Refs.LeftSHA; GitShowRevisionArguments arguments = new GitShowRevisionArguments(filename, sha); IEnumerable <string> contents; try { contents = _git?.ShowRevision(arguments); } catch (GitNotAvailableDataException ex) { throw new ContextMakingException("Cannot obtain git revision", ex); } if (contents == null) { throw new ContextMakingException("Cannot obtain git revision", null); } if (linenumber > contents.Count()) { throw new ArgumentException( String.Format("Line number {0} is greater than total line number count, invalid \"position\": {1}", linenumber.ToString(), position.ToString())); } return(createDiffContext(linenumber, contents, isRightSideContext, depth)); }
// isRightSideContext is true when linenumber and sha correspond to the right side // linenumber is one-based private DiffContext createDiffContext(int linenumber, bool isRightSideContext, IEnumerable <string> contents, ContextDepth depth, DiffPosition position) { List <DiffContext.Line> lines = new List <DiffContext.Line>(); int startLineNumber = Math.Max(1, linenumber - depth.Up); for (int iContextLine = 0; iContextLine < depth.Size + 1; ++iContextLine) { if (startLineNumber + iContextLine == contents.Count() + 1) { // we have just reached the end break; } lines.Add(getLineContext(startLineNumber + iContextLine, isRightSideContext, contents, position)); } // zero-based index of a selected line in DiffContext.Lines return(new DiffContext(lines, linenumber - startLineNumber)); }
// isRightSideContext is true when linenumber and sha correspond to the right side // linenumber is one-based private DiffContext createDiffContext(int linenumber, IEnumerable <string> contents, bool isRightSideContext, ContextDepth depth) { int startLineNumber = Math.Max(1, linenumber - depth.Up); int selectedIndex = linenumber - startLineNumber; List <DiffContext.Line> lines = new List <DiffContext.Line>(); IEnumerable <string> shiftedContents = contents.Skip(startLineNumber - 1); foreach (string text in shiftedContents) { lines.Add(getContextLine(startLineNumber + lines.Count, isRightSideContext, text)); if (lines.Count == depth.Size + 1) { break; } } // zero-based index of a selected line in DiffContext.Lines return(new DiffContext(lines, selectedIndex)); }
/// <summary> /// Throws ArgumentException, ContextMakingException. /// </summary> public DiffContext GetContext(DiffPosition position, ContextDepth depth, UnchangedLinePolicy unchangedLinePolicy) { if (!Context.Helpers.IsValidPosition(position)) { throw new ArgumentException( String.Format("Bad \"position\": {0}", position.ToString())); } if (!Context.Helpers.IsValidContextDepth(depth)) { throw new ArgumentException( String.Format("Bad \"depth\": {0}", depth.ToString())); } bool isRightSideContext = Helpers.IsRightSidePosition(position, unchangedLinePolicy); int linenumber = isRightSideContext ? Helpers.GetRightLineNumber(position) : Helpers.GetLeftLineNumber(position); string leftFilename = position.LeftPath; string rightFilename = position.RightPath; string leftSHA = position.Refs.LeftSHA; string rightSHA = position.Refs.RightSHA; try { FullContextDiff context = _git.FullContextDiffProvider.GetFullContextDiff( leftSHA, rightSHA, leftFilename, rightFilename); Debug.Assert(context.Left.Count == context.Right.Count); if (linenumber > context.Left.Count) { throw new ArgumentException( String.Format("Line number {0} is greater than total line number count, invalid \"position\": {1}", linenumber.ToString(), position.ToString())); } return(createDiffContext(linenumber, isRightSideContext, context, depth)); } catch (FullContextDiffProviderException ex) { throw new ContextMakingException("Cannot obtain full context diff", ex); } }
public static bool IsValidContextDepth(ContextDepth depth) { return(depth.Up <= depth.Down && Math.Min(depth.Up, depth.Down) >= 0); }