Exemplo n.º 1
0
        private MatchResult matchFileName(MergeRequestKey mrk, Core.Matching.DiffRefs refs,
                                          string originalLeftFileName, string originalRightFileName, bool isLeftSideLine,
                                          out string leftFileName, out string rightFileName)
        {
            leftFileName = rightFileName = null;
            FileNameMatcher fileNameMatcher = getFileNameMatcher(_git, mrk);

            try
            {
                if (!fileNameMatcher.Match(refs, originalLeftFileName, originalRightFileName, isLeftSideLine,
                                           out leftFileName, out rightFileName))
                {
                    return(MatchResult.Cancelled);
                }
                return(MatchResult.Success);
            }
            catch (ArgumentException ex)
            {
                ExceptionHandlers.Handle("Cannot create DiffPosition", ex);
            }
            catch (MatchingException ex)
            {
                ExceptionHandlers.Handle("Cannot create DiffPosition", ex);
            }
            return(MatchResult.Error);
        }
Exemplo n.º 2
0
        private MatchResult doFullMatch(MergeRequestKey mrk, Core.Matching.DiffRefs refs,
                                        MatchInfo matchInfo, out DiffPosition position)
        {
            if (!matchInfo.IsValid())
            {
                position = null;
                return(MatchResult.Error);
            }

            MatchResult fileMatchResult = matchFileName(mrk, refs, matchInfo.LeftFileName, matchInfo.RightFileName,
                                                        matchInfo.IsLeftSideLineNumber, out string leftFileName, out string rightFileName);

            if (fileMatchResult != MatchResult.Success)
            {
                position = null;
                return(fileMatchResult);
            }

            MatchResult lineMatchResult = matchLineNumber(leftFileName, rightFileName, refs, matchInfo.LineNumber,
                                                          matchInfo.IsLeftSideLineNumber, out string leftLineNumber, out string rightLineNumber);

            if (lineMatchResult != MatchResult.Success)
            {
                position = null;
                return(fileMatchResult);
            }

            position = new DiffPosition(leftFileName, rightFileName, leftLineNumber, rightLineNumber, refs);
            return(MatchResult.Success);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Throws GitOperationException in case of problems with git.
        /// </summary>
        public bool Process(DiffToolInfo source, Core.Matching.DiffRefs refs, out DiffToolInfo dest)
        {
            string currentName;
            string anotherName;
            bool   moved;
            bool   renamed;

            try
            {
                renamed = checkForRenamedFile(refs, source, out currentName, out anotherName, out moved);
            }
            catch (GitOperationException)
            {
                throw; // fatal error
            }

            if (renamed)
            {
                Trace.TraceInformation("Detected file {0}. Git repository path: {1}. DiffRefs: {2}\nDiffToolInfo: {3}",
                                       (moved ? "move" : "rename"), _gitRepository.Path, refs.ToString(), source.ToString());
            }

            dest = source;
            return(!renamed || handleFileRename(source, currentName, anotherName, moved, out dest));
        }
Exemplo n.º 4
0
        // Collect discussions started for lines within DiffContextDepth range near `position`
        private IEnumerable <ReportedDiscussionNote> getRelatedDiscussions(
            MergeRequestKey mrk, ReportedDiscussionNoteKey?keyOpt, DiffPosition position)
        {
            // Obtain a context for a passed position.
            DiffContext ctx = getDiffContext <CombinedContextMaker>(position, UnchangedLinePolicy.TakeFromRight);

            if (!ctx.IsValid())
            {
                return(Array.Empty <ReportedDiscussionNote>());
            }

            string leftFileName  = position.LeftPath;
            string rightFileName = position.RightPath;

            Core.Matching.DiffRefs refs = position.Refs;
            List <DiffPosition>    neighborPositions = new List <DiffPosition>();

            // CombinedContextMaker provides a context where line numbers are matched so no need to
            // match them manually here.
            foreach (DiffContext.Line line in ctx.Lines)
            {
                Debug.Assert(line.Left.HasValue || line.Right.HasValue);
                string leftLineNumber  = null;
                string rightLineNumber = null;
                if (line.Left.HasValue)
                {
                    leftLineNumber = line.Left.Value.Number.ToString();
                }
                if (line.Right.HasValue)
                {
                    rightLineNumber = line.Right.Value.Number.ToString();
                }
                neighborPositions.Add(new DiffPosition(leftFileName, rightFileName, leftLineNumber, rightLineNumber, refs));
            }

            // Find discussions that reported on each line from the diff context.
            List <ReportedDiscussionNote> relatedNotes = new List <ReportedDiscussionNote>();

            foreach (Discussion discussion in _getDiscussions(mrk))
            {
                DiscussionNote firstNote         = discussion.Notes.First();
                DiffPosition   firstNotePosition = PositionConverter.Convert(firstNote.Position);
                if (firstNotePosition != null)
                {
                    foreach (DiffPosition neighbor in neighborPositions)
                    {
                        if (doPositionsReferenceTheSameLine(neighbor, firstNotePosition) &&
                            (!keyOpt.HasValue || keyOpt.Value.Id != firstNote.Id))
                        {
                            ReportedDiscussionNote note = new ReportedDiscussionNote(firstNote.Id, discussion.Id,
                                                                                     firstNotePosition, firstNote.Body, firstNote.Author.Name, firstNote.Created_At);
                            relatedNotes.Add(note);
                        }
                    }
                }
            }
            return(relatedNotes.GroupBy(note => note.Key.Id).Select(c => c.First()));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Throws GitOperationException and GitObjectException in case of problems with git.
        /// </summary>
        private bool checkForRenamedFile(Core.Matching.DiffRefs refs, DiffToolInfo diffToolInfo,
                                         out string currentName, out string anotherName, out bool moved)
        {
            GitRenameDetector renameChecker = new GitRenameDetector(_gitRepository);

            if (!diffToolInfo.Left.HasValue)
            {
                Debug.Assert(diffToolInfo.Right.HasValue);
                currentName = diffToolInfo.Right.Value.FileName;
                anotherName = renameChecker.IsRenamed(
                    refs.LeftSHA,
                    refs.RightSHA,
                    diffToolInfo.Right.Value.FileName,
                    false, out moved);
                if (anotherName == diffToolInfo.Right.Value.FileName)
                {
                    // it is not a renamed but removed file
                    return(false);
                }
            }
            else if (!diffToolInfo.Right.HasValue)
            {
                Debug.Assert(diffToolInfo.Left.HasValue);
                currentName = diffToolInfo.Left.Value.FileName;
                anotherName = renameChecker.IsRenamed(
                    refs.LeftSHA,
                    refs.RightSHA,
                    diffToolInfo.Left.Value.FileName,
                    true, out moved);
                if (anotherName == diffToolInfo.Left.Value.FileName)
                {
                    // it is not a renamed but added file
                    return(false);
                }
            }
            else
            {
                // If even two names are given, we need to check here because use might selected manually two
                // versions of a moved file
                bool isLeftSide = diffToolInfo.IsLeftSideCurrent;
                currentName = isLeftSide ? diffToolInfo.Left.Value.FileName : diffToolInfo.Right.Value.FileName;
                anotherName = renameChecker.IsRenamed(
                    refs.LeftSHA,
                    refs.RightSHA,
                    currentName,
                    isLeftSide, out moved);
                return(moved);
            }
            return(true);
        }
Exemplo n.º 6
0
        async private Task submitDiscussionAsync(MergeRequestKey mrk,
                                                 MatchInfo matchInfo, Core.Matching.DiffRefs diffRefs, DiffPosition position, string body, bool includeContext)
        {
            if (body.Length == 0)
            {
                MessageBox.Show("Discussion text cannot be empty", "Warning",
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation,
                                MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
                return;
            }

            NewDiscussionParameters parameters = new NewDiscussionParameters(
                body, includeContext ? createPositionParameters(position) : new PositionParameters?());

            IDiscussionCreator creator = _shortcuts.GetDiscussionCreator(mrk, _currentUser);

            try
            {
                await creator.CreateDiscussionAsync(parameters, true);
            }
            catch (DiscussionCreatorException ex)
            {
                Trace.TraceInformation(
                    "Additional information about exception:\n" +
                    "Position: {0}\n" +
                    "Include context: {1}\n" +
                    "Snapshot refs: {2}\n" +
                    "MatchInfo: {3}\n" +
                    "Body:\n{4}",
                    position.ToString(),
                    includeContext.ToString(),
                    diffRefs.ToString(),
                    matchInfo.ToString(),
                    body);

                if (!ex.Handled)
                {
                    throw;
                }
            }
        }
Exemplo n.º 7
0
        private MatchResult matchLineNumber(string leftPath, string rightPath, Core.Matching.DiffRefs refs,
                                            int lineNumber, bool isLeftSideLine, out string leftLineNumber, out string rightLineNumber)
        {
            leftLineNumber = rightLineNumber = null;
            LineNumberMatcher matcher = new LineNumberMatcher(_git);

            try
            {
                matcher.Match(refs, leftPath, rightPath, lineNumber, isLeftSideLine, out leftLineNumber, out rightLineNumber);
                return(MatchResult.Success);
            }
            catch (ArgumentException ex)
            {
                ExceptionHandlers.Handle("Cannot create DiffPosition", ex);
            }
            catch (MatchingException ex)
            {
                ExceptionHandlers.Handle("Cannot create DiffPosition", ex);
            }
            return(MatchResult.Error);
        }
Exemplo n.º 8
0
        DiffPosition scrollPosition(DiffPosition position, bool scrollUp, bool isLeftSideLine)
        {
            if (!Core.Context.Helpers.IsValidPosition(position))
            {
                return(position);
            }

            int lineNumber = isLeftSideLine
               ? Core.Context.Helpers.GetLeftLineNumber(position)
               : Core.Context.Helpers.GetRightLineNumber(position);

            lineNumber += (scrollUp ? -1 : 1);

            string leftPath  = position.LeftPath;
            string rightPath = position.RightPath;

            Core.Matching.DiffRefs refs = position.Refs;
            if (matchLineNumber(leftPath, rightPath, refs, lineNumber, isLeftSideLine,
                                out string leftLineNumber, out string rightLineNumber) == MatchResult.Success)
            {
                return(new DiffPosition(leftPath, rightPath, leftLineNumber, rightLineNumber, refs));
            }
            return(null);
        }