Exemple #1
0
        /// <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));
        }
Exemple #2
0
        /// <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));
        }
        /// <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));
        }
        /// <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);
            }
        }