Esempio n. 1
0
 private void BuildEditList()
 {
     _editList = DiffAlgorithm.GetAlgorithm(DiffAlgorithm.SupportedAlgorithm.HISTOGRAM).Diff(
         IgnoreWhitespace
         ? DiffViewer.Text.Comparator.WS_IGNORE_ALL
         : DiffViewer.Text.Comparator.DEFAULT,
         _leftText,
         _rightText
         );
 }
Esempio n. 2
0
 /// <param name="local"></param>
 /// <param name="inCore"></param>
 protected internal ResolveMerger(Repository local, bool inCore) : base(local)
 {
     DiffAlgorithm.SupportedAlgorithm diffAlg = local.GetConfig().GetEnum(ConfigConstants
                                                                          .CONFIG_DIFF_SECTION, null, ConfigConstants.CONFIG_KEY_ALGORITHM, DiffAlgorithm.SupportedAlgorithm
                                                                          .HISTOGRAM);
     mergeAlgorithm = new MergeAlgorithm(DiffAlgorithm.GetAlgorithm(diffAlg));
     commitNames    = new string[] { "BASE", "OURS", "THEIRS" };
     this.inCore    = inCore;
     if (inCore)
     {
         dircache = DirCache.NewInCore();
     }
 }
        private static void WriteUnifiedDiffBetweenJson(StringBuilder sb, JToken expectedJToken, JToken actualJToken)
        {
            NormalizeForTextDiffJson(expectedJToken);
            NormalizeForTextDiffJson(actualJToken);
            var expectedJson  = JsonConvert.SerializeObject(expectedJToken, Formatting.Indented).Replace("\r\n", "\n");
            var actualJson    = JsonConvert.SerializeObject(actualJToken, Formatting.Indented).Replace("\r\n", "\n");
            var expectedText  = new RawText(Encoding.UTF8.GetBytes(expectedJson));
            var actualText    = new RawText(Encoding.UTF8.GetBytes(actualJson));
            var diffAlgorithm = DiffAlgorithm.GetAlgorithm(DiffAlgorithm.SupportedAlgorithm.HISTOGRAM);
            var differences   = diffAlgorithm.Diff(RawTextComparator.WS_IGNORE_ALL, expectedText, actualText);

            if (differences.Count == 0)
            {
                return;
            }

            var expectedLines = expectedJson.Split('\n');
            var actualLines   = actualJson.Split('\n');

            var a          = 0;
            var b          = 0;
            var i          = 0;
            var difference = differences[i];

            while (a < expectedLines.Length && b < actualLines.Length)
            {
                if (difference != null && a >= difference.GetBeginA())
                {
                    while (a < difference.GetEndA())
                    {
                        sb.AppendLine("-" + expectedLines[a]);
                        a++;
                    }

                    while (b < difference.GetEndB())
                    {
                        sb.AppendLine("+" + actualLines[b]);
                        b++;
                    }

                    i++;
                    difference = i < differences.Count ? differences[i] : null;
                }
                else
                {
                    sb.AppendLine(" " + actualLines[b]);
                    b++;
                    a++;
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Executes the
        /// <code>Diff</code>
        /// command with all the options and parameters
        /// collected by the setter methods (e.g.
        /// <see cref="SetCached(bool)">SetCached(bool)</see>
        /// of this
        /// class. Each instance of this class should only be used for one invocation
        /// of the command. Don't call this method twice on an instance.
        /// </summary>
        /// <returns>a DiffEntry for each path which is different</returns>
        /// <exception cref="NGit.Api.Errors.GitAPIException"></exception>
        public override IList <DiffEntry> Call()
        {
            DiffFormatter diffFmt;

            if (@out != null && !showNameAndStatusOnly)
            {
                diffFmt = new DiffFormatter(new BufferedOutputStream(@out));
            }
            else
            {
                diffFmt = new DiffFormatter(NullOutputStream.INSTANCE);
            }

            try {
                diffFmt.SetRepository(repo);
            } catch (ArgumentException) {
                // This means diff algorithm is not supported.
            } finally {
                diffFmt.SetDiffAlgorithm(DiffAlgorithm.GetAlgorithm(DiffAlgorithm.SupportedAlgorithm.MYERS));
            }

            diffFmt.SetProgressMonitor(monitor);
            try
            {
                if (cached)
                {
                    if (oldTree == null)
                    {
                        ObjectId head = repo.Resolve(Constants.HEAD + "^{tree}");
                        if (head == null)
                        {
                            throw new NoHeadException(JGitText.Get().cannotReadTree);
                        }
                        CanonicalTreeParser p      = new CanonicalTreeParser();
                        ObjectReader        reader = repo.NewObjectReader();
                        try
                        {
                            p.Reset(reader, head);
                        }
                        finally
                        {
                            reader.Release();
                        }
                        oldTree = p;
                    }
                    newTree = new DirCacheIterator(repo.ReadDirCache());
                }
                else
                {
                    if (oldTree == null)
                    {
                        oldTree = new DirCacheIterator(repo.ReadDirCache());
                    }
                    if (newTree == null)
                    {
                        newTree = new FileTreeIterator(repo);
                    }
                }
                diffFmt.SetPathFilter(pathFilter);
                IList <DiffEntry> result = diffFmt.Scan(oldTree, newTree);
                if (showNameAndStatusOnly)
                {
                    return(result);
                }
                else
                {
                    if (contextLines >= 0)
                    {
                        diffFmt.SetContext(contextLines);
                    }
                    if (destinationPrefix != null)
                    {
                        diffFmt.SetNewPrefix(destinationPrefix);
                    }
                    if (sourcePrefix != null)
                    {
                        diffFmt.SetOldPrefix(sourcePrefix);
                    }
                    diffFmt.Format(result);
                    diffFmt.Flush();
                    return(result);
                }
            }
            catch (IOException e)
            {
                throw new JGitInternalException(e.Message, e);
            }
            finally
            {
                diffFmt.Release();
            }
        }