public static bool IsLeftSidePosition(DiffPosition position,
                                       UnchangedLinePolicy unchangedLinePolicy = UnchangedLinePolicy.TakeFromLeft)
 {
     if (position.RightLine != null && position.LeftLine != null)
     {
         return(unchangedLinePolicy == UnchangedLinePolicy.TakeFromLeft);
     }
     return(position.LeftLine != null);
 }
        /// <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()));
            }

            // If RightLine is valid, then it points to either added/modified or unchanged line, handle them the same way
            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, isRightSideContext, contents, depth, position));
        }
        /// <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);
            }
        }
        private DiffContext getDiffContext <T>(DiffPosition position, UnchangedLinePolicy policy) where T : IContextMaker
        {
            if (position == null || !Core.Context.Helpers.IsValidPosition(position))
            {
                return(DiffContext.InvalidContext);
            }

            try
            {
                T maker = (T)Activator.CreateInstance(typeof(T), _git);
                return(maker.GetContext(position, DiffContextDepth, policy));
            }
            catch (ArgumentException ex)
            {
                ExceptionHandlers.Handle("Failed to obtain DiffContext", ex);
                return(DiffContext.InvalidContext);
            }
            catch (ContextMakingException ex)
            {
                ExceptionHandlers.Handle("Failed to obtain DiffContext", ex);
                return(DiffContext.InvalidContext);
            }
        }