Пример #1
0
        private IChangeBlob CreateNewBlob(LineChangeType type, ChangeSection section)
        {
            IChangeBlob blob;

            if (type == LineChangeType.Normal)
            {
                blob = new NormalBlob();
            }
            else
            {
                blob = new AddRemoveBlob();
            }
            section.changeBlobs.Add(blob);
            return(blob);
        }
Пример #2
0
        private void BuildChangeSections(Commit commit)
        {
            string[]     lines             = null;
            Blob         headBlob          = null;
            Blob         indexBlob         = null;
            Stream       indexFileContent  = null;
            StreamReader indexFileReader   = null;
            int          lastIndexFileLine = 0;
            TreeEntry    headFile          = commit != null ? commit[path] : GitManager.Repository.Head[path];

            if (headFile == null)
            {
                return;
            }
            headBlob = ((Blob)headFile.Target);

            if (headBlob.IsBinary)
            {
                isBinary = true;
                return;
            }

            try
            {
                var indexFile = GitManager.Repository.Index[path];
                if (indexFile != null)
                {
                    indexBlob        = (Blob)GitManager.Repository.Lookup(indexFile.Id);
                    indexFileContent = indexBlob.GetContentStream();
                    indexFileReader  = new StreamReader(indexFileContent);
                }

                if (headBlob != null && indexBlob != null)
                {
                    var compareOptions = new CompareOptions();
                    compareOptions.ContextLines = 0;
                    var contentChanges = GitManager.Repository.Diff.Compare(headBlob, indexBlob, compareOptions);
                    lines = contentChanges.Patch.Split('\n');
                }

                if (lines == null || indexFileContent == null)
                {
                    return;
                }

                changeSections = new List <ChangeSection>();
                ChangeSection currentSection = null;
                IChangeBlob   currentBlob    = null;

                LineChangeType lastLineChangeType = LineChangeType.Normal;

                for (int i = 0; i < lines.Length - 1; i++)
                {
                    if (lines[i].StartsWith("@@"))
                    {
                        var newSection = CreateSection(lines[i]);
                        lastLineChangeType = LineChangeType.Normal;

                        if (currentSection == null)
                        {
                            var prevNormalSection = new ChangeSection(false);
                            prevNormalSection.addedStartLine   = 1;
                            prevNormalSection.removedStartLine = 1;
                            var b = new NormalBlob();

                            for (int j = 0; j < newSection.addedStartLine - 1; j++)
                            {
                                b.lines.Add(ColorizeLine(indexFileReader.ReadLine()));
                                lastIndexFileLine++;
                            }
                            prevNormalSection.changeBlobs.Add(b);
                            changeSections.Add(prevNormalSection);
                        }
                        else
                        {
                            var prevNormalSection = new ChangeSection(false);
                            var b = new NormalBlob();

                            int start = (currentSection.addedStartLine + currentSection.addedLineCount) - 1;
                            int count = (newSection.addedStartLine - (currentSection.addedStartLine + currentSection.addedLineCount));

                            prevNormalSection.addedStartLine   = currentSection.addedStartLine + currentSection.addedLineCount;
                            prevNormalSection.removedStartLine = currentSection.removedStartLine + currentSection.removedLineCount;

                            while (lastIndexFileLine < start)
                            {
                                indexFileReader.ReadLine();
                                lastIndexFileLine++;
                            }

                            for (int j = 0; j < count; j++)
                            {
                                b.lines.Add(ColorizeLine(indexFileReader.ReadLine()));
                                lastIndexFileLine++;
                            }

                            prevNormalSection.addedLineCount   = prevNormalSection.changeBlobs.Count;
                            prevNormalSection.removedLineCount = prevNormalSection.changeBlobs.Count;

                            prevNormalSection.changeBlobs.Add(b);
                            changeSections.Add(prevNormalSection);
                        }

                        changeSections.Add(newSection);
                        currentSection = newSection;
                    }
                    else if (currentSection != null)
                    {
                        LineChangeType lineChangeType = LineChangeType.Normal;
                        if (lines[i].StartsWith("+"))
                        {
                            lineChangeType = LineChangeType.Added;
                        }
                        else if (lines[i].StartsWith("-"))
                        {
                            lineChangeType = LineChangeType.Removed;
                        }

                        if (lastLineChangeType == LineChangeType.Normal && lineChangeType != LineChangeType.Normal)
                        {
                            if (currentBlob != null)
                            {
                                currentBlob.Finish();
                            }
                            currentBlob = CreateNewBlob(lineChangeType, currentSection);
                        }
                        else if (lastLineChangeType != LineChangeType.Normal && lineChangeType == LineChangeType.Normal)
                        {
                            if (currentBlob != null)
                            {
                                currentBlob.Finish();
                            }
                            currentBlob = CreateNewBlob(lineChangeType, currentSection);
                        }

                        if (currentBlob != null)
                        {
                            currentBlob.AddLine(lineChangeType, ColorizeLine(lines[i]));
                        }

                        lastLineChangeType = lineChangeType;
                    }
                }

                if (currentBlob != null)
                {
                    currentBlob.Finish();
                }

                if (currentSection != null)
                {
                    var lastNormalBlob = new NormalBlob();
                    while (lastIndexFileLine < currentSection.addedStartLine + currentSection.addedLineCount - 1)
                    {
                        indexFileReader.ReadLine();
                        lastIndexFileLine++;
                    }

                    while (!indexFileReader.EndOfStream)
                    {
                        lastNormalBlob.lines.Add(ColorizeLine(indexFileReader.ReadLine()));
                        lastIndexFileLine++;
                    }
                    currentSection.changeBlobs.Add(lastNormalBlob);

                    maxLines         = changeSections.Sum(s => s.changeBlobs.Sum(b => b.Lines));
                    totalLinesHeight = maxLines * EditorGUIUtility.singleLineHeight;
                    foreach (var changeSection in changeSections)
                    {
                        foreach (var blob in changeSection.changeBlobs)
                        {
                            if (blob is NormalBlob)
                            {
                                var normalBlob = ((NormalBlob)blob);
                                foreach (var line in normalBlob.lines)
                                {
                                    maxLineWidth = Mathf.Max(maxLineWidth, GetLineWidth(line));
                                }
                            }
                            else if (blob is AddRemoveBlob)
                            {
                                var addRemoveBlob = ((AddRemoveBlob)blob);
                                if (addRemoveBlob.addedLines != null)
                                {
                                    foreach (var addedLine in addRemoveBlob.addedLines)
                                    {
                                        maxLineWidth = Mathf.Max(maxLineWidth, GetLineWidth(addedLine));
                                    }
                                }
                                if (addRemoveBlob.removedLines != null)
                                {
                                    foreach (var removedLine in addRemoveBlob.removedLines)
                                    {
                                        maxLineWidth = Mathf.Max(maxLineWidth, GetLineWidth(removedLine));
                                    }
                                }
                            }
                        }
                    }
                    maxLineNumWidth = changeSections.Max(s => Mathf.Max(styles.LineNum.CalcSize(new GUIContent(s.addedStartLine.ToString())).x, styles.LineNum.CalcSize(new GUIContent(s.removedStartLine.ToString())).x));
                }
            }
            catch (Exception e)
            {
                Debug.LogError("There was a problem while loading changes");
                Debug.LogException(e);
            }
            finally
            {
                if (indexFileContent != null)
                {
                    indexFileContent.Dispose();
                }
                if (indexFileReader != null)
                {
                    indexFileReader.Dispose();
                }
            }
        }
Пример #3
0
        private void ProcessChanges(string[] lines, StreamReader indexFileReader, ref int lastIndexFileLine)
        {
            changeSections = new List <ChangeSection>();
            ChangeSection currentSection = null;
            IChangeBlob   currentBlob    = null;

            LineChangeType lastLineChangeType = LineChangeType.Normal;

            for (int i = 0; i < lines.Length - 1; i++)
            {
                if (lines[i].StartsWith("@@"))
                {
                    var newSection = CreateSection(lines[i]);
                    lastLineChangeType = LineChangeType.Normal;

                    if (currentSection == null)
                    {
                        //the first normal section before any changes
                        var prevNormalSection = new ChangeSection(false);
                        prevNormalSection.addedStartLine   = 1;
                        prevNormalSection.removedStartLine = 1;
                        var b = new NormalBlob();

                        for (int j = 0; j < newSection.addedStartLine - 1; j++)
                        {
                            b.lines.Add(ColorizeLine(indexFileReader.ReadLine()));
                            lastIndexFileLine++;
                        }
                        prevNormalSection.changeBlobs.Add(b);
                        changeSections.Add(prevNormalSection);
                    }
                    else
                    {
                        var nextNormalSection = new ChangeSection(false);
                        var b = new NormalBlob();

                        int start = (currentSection.addedStartLine + currentSection.addedLineCount) - 1;
                        int count = (newSection.addedStartLine - (currentSection.addedStartLine + currentSection.addedLineCount));

                        nextNormalSection.addedStartLine   = currentSection.addedStartLine + currentSection.addedLineCount;
                        nextNormalSection.removedStartLine = currentSection.removedStartLine + currentSection.removedLineCount;

                        while (lastIndexFileLine < start)
                        {
                            indexFileReader.ReadLine();
                            lastIndexFileLine++;
                        }

                        for (int j = 0; j < count; j++)
                        {
                            b.lines.Add(ColorizeLine(indexFileReader.ReadLine()));
                            lastIndexFileLine++;
                        }

                        nextNormalSection.changeBlobs.Add(b);
                        changeSections.Add(nextNormalSection);
                    }

                    changeSections.Add(newSection);
                    currentSection = newSection;
                }
                else if (currentSection != null)
                {
                    LineChangeType lineChangeType = LineChangeType.Normal;
                    if (lines[i].StartsWith("+"))
                    {
                        lineChangeType = LineChangeType.Added;
                    }
                    else if (lines[i].StartsWith("-"))
                    {
                        lineChangeType = LineChangeType.Removed;
                    }

                    if (lastLineChangeType == LineChangeType.Normal && lineChangeType != LineChangeType.Normal)
                    {
                        if (currentBlob != null)
                        {
                            currentBlob.Finish();
                        }
                        currentBlob = CreateNewBlob(lineChangeType, currentSection);
                    }
                    else if (lastLineChangeType != LineChangeType.Normal && lineChangeType == LineChangeType.Normal)
                    {
                        if (currentBlob != null)
                        {
                            currentBlob.Finish();
                        }
                        currentBlob = CreateNewBlob(lineChangeType, currentSection);
                    }

                    if (currentBlob != null)
                    {
                        currentBlob.AddLine(lineChangeType, ColorizeLine(lines[i]));
                    }

                    lastLineChangeType = lineChangeType;
                }
            }

            if (currentBlob != null)
            {
                currentBlob.Finish();
            }

            if (currentSection != null)
            {
                var lastSection    = new ChangeSection(false);
                var lastNormalBlob = new NormalBlob();

                lastSection.addedStartLine   = currentSection.addedStartLine + currentSection.addedLineCount;
                lastSection.removedStartLine = currentSection.removedStartLine + currentSection.removedLineCount;

                int start = (currentSection.addedStartLine + currentSection.addedLineCount) - 1;

                while (lastIndexFileLine < start)
                {
                    indexFileReader.ReadLine();
                    lastIndexFileLine++;
                }

                while (!indexFileReader.EndOfStream)
                {
                    lastNormalBlob.lines.Add(ColorizeLine(indexFileReader.ReadLine()));
                    lastIndexFileLine++;
                }
                lastSection.changeBlobs.Add(lastNormalBlob);

                changeSections.Add(lastSection);

                maxLines         = changeSections.Sum(s => s.changeBlobs.Sum(b => b.Lines));
                totalLinesHeight = maxLines * EditorGUIUtility.singleLineHeight;
                foreach (var changeSection in changeSections)
                {
                    foreach (var blob in changeSection.changeBlobs)
                    {
                        if (blob is NormalBlob)
                        {
                            var normalBlob = ((NormalBlob)blob);
                            foreach (var line in normalBlob.lines)
                            {
                                maxLineWidth = Mathf.Max(maxLineWidth, GetLineWidth(line));
                            }
                        }
                        else if (blob is AddRemoveBlob)
                        {
                            var addRemoveBlob = ((AddRemoveBlob)blob);
                            if (addRemoveBlob.addedLines != null)
                            {
                                foreach (var addedLine in addRemoveBlob.addedLines)
                                {
                                    maxLineWidth = Mathf.Max(maxLineWidth, GetLineWidth(addedLine));
                                }
                            }
                            if (addRemoveBlob.removedLines != null)
                            {
                                foreach (var removedLine in addRemoveBlob.removedLines)
                                {
                                    maxLineWidth = Mathf.Max(maxLineWidth, GetLineWidth(removedLine));
                                }
                            }
                        }
                    }
                }
                maxLineNumWidth = styles.LineNum.CalcSize(GitGUI.GetTempContent(maxLines.ToString())).x;
            }
        }