public static IntList GetLines(RawText rawText) { if (_linesField == null) _linesField = typeof(RawText).GetField("lines", BindingFlags.NonPublic | BindingFlags.Instance); return (IntList)_linesField.GetValue(rawText); }
public virtual void TestEqualsWithWhitespace() { RawText a = new RawText(Constants.EncodeASCII("foo-a\n \n a b c\na \n foo\na b c\n" )); RawText b = new RawText(Constants.EncodeASCII("foo-a b\n\nab c\na\nfoo\na b c \n" )); // "foo-a" != "foo-a b" NUnit.Framework.Assert.IsFalse(cmp.Equals(a, 0, b, 0)); NUnit.Framework.Assert.IsFalse(cmp.Equals(b, 0, a, 0)); // " " == "" NUnit.Framework.Assert.IsTrue(cmp.Equals(a, 1, b, 1)); NUnit.Framework.Assert.IsTrue(cmp.Equals(b, 1, a, 1)); // " a b c" != "ab c" NUnit.Framework.Assert.IsFalse(cmp.Equals(a, 2, b, 2)); NUnit.Framework.Assert.IsFalse(cmp.Equals(b, 2, a, 2)); // "a " == "a" NUnit.Framework.Assert.IsTrue(cmp.Equals(a, 3, b, 3)); NUnit.Framework.Assert.IsTrue(cmp.Equals(b, 3, a, 3)); // " foo" != "foo" NUnit.Framework.Assert.IsFalse(cmp.Equals(a, 4, b, 4)); NUnit.Framework.Assert.IsFalse(cmp.Equals(b, 4, a, 4)); // "a b c" == "a b c " NUnit.Framework.Assert.IsTrue(cmp.Equals(a, 5, b, 5)); NUnit.Framework.Assert.IsTrue(cmp.Equals(b, 5, a, 5)); }
public override bool Equals(RawText a, int ai, RawText b, int bi) { ai++; bi++; int @as = a.lines.Get(ai); int bs = b.lines.Get(bi); int ae = a.lines.Get(ai + 1); int be = b.lines.Get(bi + 1); ae = RawCharUtil.TrimTrailingWhitespace(a.content, @as, ae); be = RawCharUtil.TrimTrailingWhitespace(b.content, bs, be); while (@as < ae && bs < be) { byte ac = a.content[@as]; byte bc = b.content[bs]; while (@as < ae - 1 && RawCharUtil.IsWhitespace(ac)) { @as++; ac = a.content[@as]; } while (bs < be - 1 && RawCharUtil.IsWhitespace(bc)) { bs++; bc = b.content[bs]; } if (ac != bc) { return(false); } @as++; bs++; } return(@as == ae && bs == be); }
public virtual void LineDelimiterStartingWithCharacterReturn() { var rt = new RawText(Constants.EncodeASCII("\nfoo")); NUnit.Framework.Assert.AreEqual("\n", rt.GetLineDelimiter()); NUnit.Framework.Assert.IsTrue(rt.IsMissingNewlineAtEnd()); }
public virtual void TestLineDelimiter() { RawText rt = new RawText(Constants.EncodeASCII("foo\n")); NUnit.Framework.Assert.AreEqual("\n", rt.GetLineDelimiter()); NUnit.Framework.Assert.IsFalse(rt.IsMissingNewlineAtEnd()); rt = new RawText(Constants.EncodeASCII("foo\r\n")); NUnit.Framework.Assert.AreEqual("\r\n", rt.GetLineDelimiter()); NUnit.Framework.Assert.IsFalse(rt.IsMissingNewlineAtEnd()); rt = new RawText(Constants.EncodeASCII("foo\nbar")); NUnit.Framework.Assert.AreEqual("\n", rt.GetLineDelimiter()); NUnit.Framework.Assert.IsTrue(rt.IsMissingNewlineAtEnd()); rt = new RawText(Constants.EncodeASCII("foo\r\nbar")); NUnit.Framework.Assert.AreEqual("\r\n", rt.GetLineDelimiter()); NUnit.Framework.Assert.IsTrue(rt.IsMissingNewlineAtEnd()); rt = new RawText(Constants.EncodeASCII("foo\nbar\r\n")); NUnit.Framework.Assert.AreEqual("\n", rt.GetLineDelimiter()); NUnit.Framework.Assert.IsFalse(rt.IsMissingNewlineAtEnd()); rt = new RawText(Constants.EncodeASCII("foo\r\nbar\n")); NUnit.Framework.Assert.AreEqual("\r\n", rt.GetLineDelimiter()); NUnit.Framework.Assert.IsFalse(rt.IsMissingNewlineAtEnd()); rt = new RawText(Constants.EncodeASCII("foo")); NUnit.Framework.Assert.IsNull(rt.GetLineDelimiter()); NUnit.Framework.Assert.IsTrue(rt.IsMissingNewlineAtEnd()); rt = new RawText(Constants.EncodeASCII(string.Empty)); NUnit.Framework.Assert.IsNull(rt.GetLineDelimiter()); NUnit.Framework.Assert.IsTrue(rt.IsMissingNewlineAtEnd()); rt = new RawText(Constants.EncodeASCII("\n")); NUnit.Framework.Assert.AreEqual("\n", rt.GetLineDelimiter()); NUnit.Framework.Assert.IsFalse(rt.IsMissingNewlineAtEnd()); rt = new RawText(Constants.EncodeASCII("\r\n")); NUnit.Framework.Assert.AreEqual("\r\n", rt.GetLineDelimiter()); NUnit.Framework.Assert.IsFalse(rt.IsMissingNewlineAtEnd()); }
public virtual void TestEqualsWithWhitespace() { RawText a = new RawText(Constants.EncodeASCII("foo-a\n \n a b c\na \n foo\na b c\n" )); RawText b = new RawText(Constants.EncodeASCII("foo-a b\n\nab c\na\nfoo\na b c \n" )); // "foo-a" != "foo-a b" NUnit.Framework.Assert.IsFalse(cmp.Equals(a, 0, b, 0)); NUnit.Framework.Assert.IsFalse(cmp.Equals(b, 0, a, 0)); // " " == "" NUnit.Framework.Assert.IsTrue(cmp.Equals(a, 1, b, 1)); NUnit.Framework.Assert.IsTrue(cmp.Equals(b, 1, a, 1)); // " a b c" != "ab c" NUnit.Framework.Assert.IsFalse(cmp.Equals(a, 2, b, 2)); NUnit.Framework.Assert.IsFalse(cmp.Equals(b, 2, a, 2)); // "a " == "a" NUnit.Framework.Assert.IsTrue(cmp.Equals(a, 3, b, 3)); NUnit.Framework.Assert.IsTrue(cmp.Equals(b, 3, a, 3)); // " foo" != "foo" NUnit.Framework.Assert.IsFalse(cmp.Equals(a, 4, b, 4)); NUnit.Framework.Assert.IsFalse(cmp.Equals(b, 4, a, 4)); // "a b c" == "a b c " NUnit.Framework.Assert.IsTrue(cmp.Equals(a, 5, b, 5)); NUnit.Framework.Assert.IsTrue(cmp.Equals(b, 5, a, 5)); }
public virtual void TestComparatorReduceCommonStartEnd() { RawTextComparator c = RawTextComparator.DEFAULT; Edit e; e = c.ReduceCommonStartEnd(T(string.Empty), T(string.Empty), new Edit(0, 0, 0, 0) ); NUnit.Framework.Assert.AreEqual(new Edit(0, 0, 0, 0), e); e = c.ReduceCommonStartEnd(T("a"), T("b"), new Edit(0, 1, 0, 1)); NUnit.Framework.Assert.AreEqual(new Edit(0, 1, 0, 1), e); e = c.ReduceCommonStartEnd(T("a"), T("a"), new Edit(0, 1, 0, 1)); NUnit.Framework.Assert.AreEqual(new Edit(1, 1, 1, 1), e); e = c.ReduceCommonStartEnd(T("axB"), T("axC"), new Edit(0, 3, 0, 3)); NUnit.Framework.Assert.AreEqual(new Edit(2, 3, 2, 3), e); e = c.ReduceCommonStartEnd(T("Bxy"), T("Cxy"), new Edit(0, 3, 0, 3)); NUnit.Framework.Assert.AreEqual(new Edit(0, 1, 0, 1), e); e = c.ReduceCommonStartEnd(T("bc"), T("Abc"), new Edit(0, 2, 0, 3)); NUnit.Framework.Assert.AreEqual(new Edit(0, 0, 0, 1), e); e = new Edit(0, 5, 0, 5); e = c.ReduceCommonStartEnd(T("abQxy"), T("abRxy"), e); NUnit.Framework.Assert.AreEqual(new Edit(2, 3, 2, 3), e); RawText a = new RawText(Sharpen.Runtime.GetBytesForString("p\na b\nQ\nc d\n", "UTF-8" )); RawText b = new RawText(Sharpen.Runtime.GetBytesForString("p\na b \nR\n c d \n" , "UTF-8")); e = new Edit(0, 4, 0, 4); e = RawTextComparator.WS_IGNORE_ALL.ReduceCommonStartEnd(a, b, e); NUnit.Framework.Assert.AreEqual(new Edit(2, 3, 2, 3), e); }
public virtual void TestComparatorReduceCommonStartEnd() { RawTextComparator c = RawTextComparator.DEFAULT; Edit e; e = c.ReduceCommonStartEnd(T(string.Empty), T(string.Empty), new Edit(0, 0, 0, 0) ); NUnit.Framework.Assert.AreEqual(new Edit(0, 0, 0, 0), e); e = c.ReduceCommonStartEnd(T("a"), T("b"), new Edit(0, 1, 0, 1)); NUnit.Framework.Assert.AreEqual(new Edit(0, 1, 0, 1), e); e = c.ReduceCommonStartEnd(T("a"), T("a"), new Edit(0, 1, 0, 1)); NUnit.Framework.Assert.AreEqual(new Edit(1, 1, 1, 1), e); e = c.ReduceCommonStartEnd(T("axB"), T("axC"), new Edit(0, 3, 0, 3)); NUnit.Framework.Assert.AreEqual(new Edit(2, 3, 2, 3), e); e = c.ReduceCommonStartEnd(T("Bxy"), T("Cxy"), new Edit(0, 3, 0, 3)); NUnit.Framework.Assert.AreEqual(new Edit(0, 1, 0, 1), e); e = c.ReduceCommonStartEnd(T("bc"), T("Abc"), new Edit(0, 2, 0, 3)); NUnit.Framework.Assert.AreEqual(new Edit(0, 0, 0, 1), e); e = new Edit(0, 5, 0, 5); e = c.ReduceCommonStartEnd(T("abQxy"), T("abRxy"), e); NUnit.Framework.Assert.AreEqual(new Edit(2, 3, 2, 3), e); RawText a = new RawText(Sharpen.Runtime.GetBytesForString("p\na b\nQ\nc d\n", "UTF-8" )); RawText b = new RawText(Sharpen.Runtime.GetBytesForString("p\na b \nR\n c d \n" , "UTF-8")); e = new Edit(0, 4, 0, 4); e = RawTextComparator.WS_IGNORE_ALL.ReduceCommonStartEnd(a, b, e); NUnit.Framework.Assert.AreEqual(new Edit(2, 3, 2, 3), e); }
public static byte[] GetContent(RawText rawText) { if (_contentField == null) _contentField = typeof(RawText).GetField("content", BindingFlags.NonPublic | BindingFlags.Instance); return (byte[])_contentField.GetValue(rawText); }
/// <exception cref="System.IO.IOException"></exception> /// <exception cref="NGit.Errors.CorruptObjectException"></exception> /// <exception cref="NGit.Errors.MissingObjectException"></exception> private DiffFormatter.FormatResult CreateFormatResult(DiffEntry ent) { DiffFormatter.FormatResult res = new DiffFormatter.FormatResult(); ByteArrayOutputStream buf = new ByteArrayOutputStream(); EditList editList; FileHeader.PatchType type; FormatHeader(buf, ent); if (ent.GetOldMode() == FileMode.GITLINK || ent.GetNewMode() == FileMode.GITLINK) { FormatOldNewPaths(buf, ent); WriteGitLinkDiffText(buf, ent); editList = new EditList(); type = FileHeader.PatchType.UNIFIED; } else { AssertHaveRepository(); byte[] aRaw = Open(DiffEntry.Side.OLD, ent); byte[] bRaw = Open(DiffEntry.Side.NEW, ent); if (aRaw == BINARY || bRaw == BINARY || RawText.IsBinary(aRaw) || RawText.IsBinary (bRaw)) { // FormatOldNewPaths(buf, ent); buf.Write(Constants.EncodeASCII("Binary files differ\n")); editList = new EditList(); type = FileHeader.PatchType.BINARY; } else { res.a = new RawText(aRaw); res.b = new RawText(bRaw); editList = Diff(res.a, res.b); type = FileHeader.PatchType.UNIFIED; switch (ent.GetChangeType()) { case DiffEntry.ChangeType.RENAME: case DiffEntry.ChangeType.COPY: { if (!editList.IsEmpty()) { FormatOldNewPaths(buf, ent); } break; } default: { FormatOldNewPaths(buf, ent); break; break; } } } } res.header = new FileHeader(buf.ToByteArray(), editList, type); return(res); }
public virtual void TestWriteLine1() { RawText a = new RawText(Constants.EncodeASCII("foo-a\nfoo-b\n")); ByteArrayOutputStream o = new ByteArrayOutputStream(); a.WriteLine(o, 0); byte[] r = o.ToByteArray(); NUnit.Framework.Assert.AreEqual("foo-a", RawParseUtils.Decode(r)); }
public virtual void TestWriteLine2() { RawText a = new RawText(Constants.EncodeASCII("foo-a\nfoo-b")); ByteArrayOutputStream o = new ByteArrayOutputStream(); a.WriteLine(o, 1); byte[] r = o.ToByteArray(); NUnit.Framework.Assert.AreEqual("foo-b", RawParseUtils.Decode(r)); }
public virtual void TestWriteLine3() { RawText a = new RawText(Constants.EncodeASCII("a\n\nb\n")); ByteArrayOutputStream o = new ByteArrayOutputStream(); a.WriteLineInternal(o, 1); byte[] r = o.ToByteArray(); NUnit.Framework.Assert.AreEqual(string.Empty, RawParseUtils.Decode(r)); }
/// <summary>Formats a list of edits in unified diff format</summary> /// <param name="edits">some differences which have been calculated between A and B</param> /// <param name="a">the text A which was compared</param> /// <param name="b">the text B which was compared</param> /// <exception cref="System.IO.IOException">System.IO.IOException</exception> public virtual void Format(EditList edits, RawText a, RawText b) { for (int curIdx = 0; curIdx < edits.Count;) { Edit curEdit = edits[curIdx]; int endIdx = FindCombinedEnd(edits, curIdx); Edit endEdit = edits[endIdx]; int aCur = Math.Max(0, curEdit.GetBeginA() - context); int bCur = Math.Max(0, curEdit.GetBeginB() - context); int aEnd = Math.Min(a.Size(), endEdit.GetEndA() + context); int bEnd = Math.Min(b.Size(), endEdit.GetEndB() + context); WriteHunkHeader(aCur, aEnd, bCur, bEnd); while (aCur < aEnd || bCur < bEnd) { if (aCur < curEdit.GetBeginA() || endIdx + 1 < curIdx) { WriteContextLine(a, aCur); if (IsEndOfLineMissing(a, aCur)) { @out.Write(noNewLine); } aCur++; bCur++; } else { if (aCur < curEdit.GetEndA()) { WriteRemovedLine(a, aCur); if (IsEndOfLineMissing(a, aCur)) { @out.Write(noNewLine); } aCur++; } else { if (bCur < curEdit.GetEndB()) { WriteAddedLine(b, bCur); if (IsEndOfLineMissing(b, bCur)) { @out.Write(noNewLine); } bCur++; } } } if (End(curEdit, aCur, bCur) && ++curIdx < edits.Count) { curEdit = edits[curIdx]; } } } }
public virtual void TestComparatorReduceCommonStartButLastLineNoEol() { RawText a; RawText b; Edit e; a = new RawText(Sharpen.Runtime.GetBytesForString("start", "UTF-8")); b = new RawText(Sharpen.Runtime.GetBytesForString("start of line", "UTF-8")); e = new Edit(0, 1, 0, 1); e = RawTextComparator.DEFAULT.ReduceCommonStartEnd(a, b, e); NUnit.Framework.Assert.AreEqual(new Edit(0, 1, 0, 1), e); }
public virtual void TestComparatorReduceCommonStartButLastLineNoEol_2() { RawText a; RawText b; Edit e; a = new RawText(Sharpen.Runtime.GetBytesForString("start", "UTF-8")); b = new RawText(Sharpen.Runtime.GetBytesForString("start of\nlastline", "UTF-8")); e = new Edit(0, 1, 0, 2); e = RawTextComparator.DEFAULT.ReduceCommonStartEnd(a, b, e); NUnit.Framework.Assert.AreEqual(new Edit(0, 1, 0, 2), e); }
public virtual void TestEquals() { RawText a = new RawText(Constants.EncodeASCII("foo-a\nfoo-b\n")); RawText b = new RawText(Constants.EncodeASCII("foo-b\nfoo-c\n")); RawTextComparator cmp = RawTextComparator.DEFAULT; NUnit.Framework.Assert.AreEqual(2, a.Size()); NUnit.Framework.Assert.AreEqual(2, b.Size()); // foo-a != foo-b NUnit.Framework.Assert.IsFalse(cmp.Equals(a, 0, b, 0)); NUnit.Framework.Assert.IsFalse(cmp.Equals(b, 0, a, 0)); // foo-b == foo-b NUnit.Framework.Assert.IsTrue(cmp.Equals(a, 1, b, 0)); NUnit.Framework.Assert.IsTrue(cmp.Equals(b, 0, a, 1)); }
public virtual void TestEquals() { RawText a = new RawText(Constants.EncodeASCII("foo-a\nfoo-b\n")); RawText b = new RawText(Constants.EncodeASCII("foo-b\nfoo-c\n")); RawTextComparator cmp = RawTextComparator.DEFAULT; NUnit.Framework.Assert.AreEqual(2, a.Size()); NUnit.Framework.Assert.AreEqual(2, b.Size()); // foo-a != foo-b NUnit.Framework.Assert.IsFalse(cmp.Equals(a, 0, b, 0)); NUnit.Framework.Assert.IsFalse(cmp.Equals(b, 0, a, 0)); // foo-b == foo-b NUnit.Framework.Assert.IsTrue(cmp.Equals(a, 1, b, 0)); NUnit.Framework.Assert.IsTrue(cmp.Equals(b, 0, a, 1)); }
public HunkRangeInfo(ITextSnapshot snapshot, Edit edit, RawText originalText, RawText workingText) { if (snapshot == null) throw new ArgumentNullException("snapshot"); if (edit == null) throw new ArgumentNullException("edit"); if (originalText == null) throw new ArgumentNullException("originalText"); if (workingText == null) throw new ArgumentNullException("workingText"); _snapshot = snapshot; _edit = edit; _originalText = originalText.GetString(edit.GetBeginA(), edit.GetEndA(), true).Split('\n').Select(i => i.TrimEnd('\r')).ToList(); _canRollback = true; }
public virtual void TestEqualsWithoutWhitespace() { RawText a = new RawText(Constants.EncodeASCII("foo-a\nfoo-b\nfoo\n")); RawText b = new RawText(Constants.EncodeASCII("foo-b\nfoo-c\nf\n")); NUnit.Framework.Assert.AreEqual(3, a.Size()); NUnit.Framework.Assert.AreEqual(3, b.Size()); // foo-a != foo-b NUnit.Framework.Assert.IsFalse(cmp.Equals(a, 0, b, 0)); NUnit.Framework.Assert.IsFalse(cmp.Equals(b, 0, a, 0)); // foo-b == foo-b NUnit.Framework.Assert.IsTrue(cmp.Equals(a, 1, b, 0)); NUnit.Framework.Assert.IsTrue(cmp.Equals(b, 0, a, 1)); // foo != f NUnit.Framework.Assert.IsFalse(cmp.Equals(a, 2, b, 2)); NUnit.Framework.Assert.IsFalse(cmp.Equals(b, 2, a, 2)); }
public virtual void TestComparatorReduceCommonStartEnd_EmptyLine() { RawText a; RawText b; Edit e; a = new RawText(Sharpen.Runtime.GetBytesForString("R\n y\n", "UTF-8")); b = new RawText(Sharpen.Runtime.GetBytesForString("S\n\n y\n", "UTF-8")); e = new Edit(0, 2, 0, 3); e = RawTextComparator.DEFAULT.ReduceCommonStartEnd(a, b, e); NUnit.Framework.Assert.AreEqual(new Edit(0, 1, 0, 2), e); a = new RawText(Sharpen.Runtime.GetBytesForString("S\n\n y\n", "UTF-8")); b = new RawText(Sharpen.Runtime.GetBytesForString("R\n y\n", "UTF-8")); e = new Edit(0, 3, 0, 2); e = RawTextComparator.DEFAULT.ReduceCommonStartEnd(a, b, e); NUnit.Framework.Assert.AreEqual(new Edit(0, 2, 0, 1), e); }
public virtual void TestEqualsWithoutWhitespace() { RawText a = new RawText(Constants.EncodeASCII("foo-a\nfoo-b\nfoo\n")); RawText b = new RawText(Constants.EncodeASCII("foo-b\nfoo-c\nf\n")); NUnit.Framework.Assert.AreEqual(3, a.Size()); NUnit.Framework.Assert.AreEqual(3, b.Size()); // foo-a != foo-b NUnit.Framework.Assert.IsFalse(cmp.Equals(a, 0, b, 0)); NUnit.Framework.Assert.IsFalse(cmp.Equals(b, 0, a, 0)); // foo-b == foo-b NUnit.Framework.Assert.IsTrue(cmp.Equals(a, 1, b, 0)); NUnit.Framework.Assert.IsTrue(cmp.Equals(b, 0, a, 1)); // foo != f NUnit.Framework.Assert.IsFalse(cmp.Equals(a, 2, b, 2)); NUnit.Framework.Assert.IsFalse(cmp.Equals(b, 2, a, 2)); }
/// <exception cref="System.Exception"></exception> private ApplyResult Init(string name, bool preExists, bool postExists) { Git git = new Git(db); if (preExists) { a = new RawText(ReadFile(name + "_PreImage")); Write(new FilePath(db.Directory.GetParent(), name), a.GetString(0, a.Size(), false )); git.Add().AddFilepattern(name).Call(); git.Commit().SetMessage("PreImage").Call(); } if (postExists) { b = new RawText(ReadFile(name + "_PostImage")); } return git.Apply().SetPatch(typeof(DiffFormatterReflowTest).GetResourceAsStream(name + ".patch")).Call(); }
/// <param name="args">two filenames specifying the contents to be diffed</param> public static void Main(string[] args) { if (args.Length != 2) { System.Console.Error.WriteLine(JGitText.Get().need2Arguments); System.Environment.Exit(1); } try { RawText a = new RawText(new FilePath(args[0])); RawText b = new RawText(new FilePath(args[1])); EditList r = INSTANCE.Diff(RawTextComparator.DEFAULT, a, b); System.Console.Out.WriteLine(r.ToString()); } catch (Exception e) { Sharpen.Runtime.PrintStackTrace(e); } }
public virtual void TestFallbackToMyersDiff() { HistogramDiff hd = new HistogramDiff(); hd.SetMaxChainLength(4); RawTextComparator cmp = RawTextComparator.DEFAULT; RawText ac = T("bbbbb"); RawText bc = T("AbCbDbEFbZ"); EditList r; // Without fallback our results are limited due to collisions. hd.SetFallbackAlgorithm(null); r = hd.Diff(cmp, ac, bc); NUnit.Framework.Assert.AreEqual(1, r.Count); // Results go up when we add a fallback for the high collision regions. hd.SetFallbackAlgorithm(MyersDiff <Sequence> .INSTANCE); r = hd.Diff(cmp, ac, bc); NUnit.Framework.Assert.AreEqual(5, r.Count); }
/// <summary>Format a patch script, reusing a previously parsed FileHeader.</summary> /// <remarks> /// Format a patch script, reusing a previously parsed FileHeader. /// <p> /// This formatter is primarily useful for editing an existing patch script /// to increase or reduce the number of lines of context within the script. /// All header lines are reused as-is from the supplied FileHeader. /// </remarks> /// <param name="head">existing file header containing the header lines to copy.</param> /// <param name="a"> /// text source for the pre-image version of the content. This /// must match the content of /// <see cref="DiffEntry.GetOldId()">DiffEntry.GetOldId()</see> /// . /// </param> /// <param name="b"> /// text source for the post-image version of the content. This /// must match the content of /// <see cref="DiffEntry.GetNewId()">DiffEntry.GetNewId()</see> /// . /// </param> /// <exception cref="System.IO.IOException">writing to the supplied stream failed.</exception> public virtual void Format(FileHeader head, RawText a, RawText b) { // Reuse the existing FileHeader as-is by blindly copying its // header lines, but avoiding its hunks. Instead we recreate // the hunks from the text instances we have been supplied. // int start = head.GetStartOffset(); int end = head.GetEndOffset(); if (!head.GetHunks().IsEmpty()) { end = head.GetHunks()[0].GetStartOffset(); } @out.Write(head.GetBuffer(), start, end - start); if (head.GetPatchType() == FileHeader.PatchType.UNIFIED) { Format(head.ToEditList(), a, b); } }
public override bool Equals(RawText a, int ai, RawText b, int bi) { ai++; bi++; int @as = a.lines.Get(ai); int bs = b.lines.Get(bi); int ae = a.lines.Get(ai + 1); int be = b.lines.Get(bi + 1); if (ae - @as != be - bs) { return(false); } while (@as < ae) { if (a.content[@as++] != b.content[bs++]) { return(false); } } return(true); }
public override bool Equals(RawText a, int ai, RawText b, int bi) { ai++; bi++; IntList a_lines = RawTextAccessor.GetLines(a); IntList b_lines = RawTextAccessor.GetLines(b); byte[] a_content = RawTextAccessor.GetContent(a); byte[] b_content = RawTextAccessor.GetContent(b); int @as = a_lines.Get(ai); int bs = b_lines.Get(bi); int ae = a_lines.Get(ai + 1); int be = b_lines.Get(bi + 1); ae = TrimTrailingSpace(a_content, @as, ae); be = TrimTrailingSpace(b_content, bs, be); while (@as < ae && bs < be) { byte ac = a_content[@as]; byte bc = b_content[bs]; while (@as < ae - 1 && ac == ' ') { @as++; ac = a_content[@as]; } while (bs < be - 1 && bc == ' ') { bs++; bc = b_content[bs]; } if (ac != bc) { return false; } @as++; bs++; } return @as == ae && bs == be; }
public override bool Equals(RawText a, int ai, RawText b, int bi) { ai++; bi++; int @as = a.lines.Get(ai); int bs = b.lines.Get(bi); int ae = a.lines.Get(ai + 1); int be = b.lines.Get(bi + 1); ae = RawCharUtil.TrimTrailingWhitespace(a.content, @as, ae); be = RawCharUtil.TrimTrailingWhitespace(b.content, bs, be); if (ae - @as != be - bs) { return(false); } while (@as < ae) { if (a.content[@as++] != b.content[bs++]) { return(false); } } return(true); }
static int SetBlameLines (NGit.Repository repo, RevCommit[] lines, RevCommit commit, RawText curText, RawText ancestorText) { int lineCount = 0; IEnumerable<Hunk> diffHunks = GetDiffHunks (curText, ancestorText); foreach (Hunk e in diffHunks) { int basePosition = e.InsertStart - 1; for (int i = 0; i < e.Inserted; i++) { int lineNum = basePosition + i; if (lines [lineNum] == null) { lines [lineNum] = commit; lineCount++; } } } return lineCount; }
public virtual void TestEmpty() { RawText r = new RawText(new byte[0]); NUnit.Framework.Assert.AreEqual(0, r.Size()); }
public virtual EditList Diff(RawText a, RawText b) { return(Algorithm().Diff(RawTextComparator.DEFAULT, a, b)); }
/// <summary> /// Throw away the /// <see cref="GetResultContents()">GetResultContents()</see> /// . /// </summary> public virtual void DiscardResultContents() { resultContents = null; }
/// <exception cref="System.IO.IOException"></exception> private void Init(string name) { a = new RawText(ReadFile(name + "_PreImage")); b = new RawText(ReadFile(name + "_PostImage")); file = ParseTestPatchFile(name + ".patch").GetFiles()[0]; }
static int SetBlameLines (NGit.Repository repo, RevCommit[] lines, RevCommit commit, RawText curText, RawText ancestorText) { int lineCount = 0; var differ = MyersDiff<RawText>.INSTANCE; foreach (Edit e in differ.Diff (RawTextComparator.DEFAULT, ancestorText, curText)) { for (int n = e.GetBeginB (); n < e.GetEndB (); n++) { if (lines [n] == null) { lines [n] = commit; lineCount ++; } } } return lineCount; }
public virtual void LineDelimiterStartingWithCharacterReturn() { var rt = new RawText(Constants.EncodeASCII("\nfoo")); NUnit.Framework.Assert.AreEqual("\n", rt.GetLineDelimiter()); NUnit.Framework.Assert.IsTrue(rt.IsMissingNewlineAtEnd()); }
static IEnumerable<Hunk> GetDiffHunks (RawText curText, RawText ancestorText) { Dictionary<string, int> codeDictionary = new Dictionary<string, int> (); int codeCounter = 0; int[] ancestorDiffCodes = GetDiffCodes (ref codeCounter, codeDictionary, ancestorText); int[] currentDiffCodes = GetDiffCodes (ref codeCounter, codeDictionary, curText); return Diff.GetDiff<int> (ancestorDiffCodes, currentDiffCodes); }
static string[] GetLineStrings (RawText text) { int lineCount = text.Size (); string[] lines = new string[lineCount]; for (int i = 0; i < lineCount; i++) { lines [i] = text.GetString (i); } return lines; }
private EditList Diff(RawText a, RawText b) { return(diffAlgorithm.Diff(comparator, a, b)); }
/// <exception cref="System.IO.IOException"></exception> private void Init(string name) { a = new RawText(ReadFile(name + "_PreImage")); b = new RawText(ReadFile(name + "_PostImage")); file = ParseTestPatchFile(name + ".patch").GetFiles()[0]; }
/// <summary> /// Diff working file with last commit /// </summary> /// <param name="fileName">Expect relative path</param> /// <returns>diff file in temp folder</returns> public string DiffFile(string fileName) { try { if (!this.HasGitRepository) return ""; var tmpFileName = Path.ChangeExtension(Path.GetTempFileName(), ".diff"); var status = GetFileStatus(fileName); if (head == null || status == GitFileStatus.New || status == GitFileStatus.Added) { tmpFileName = Path.ChangeExtension(tmpFileName, Path.GetExtension(fileName)); File.Copy(GetFullPath(fileName), tmpFileName); return tmpFileName; } if (GitBash.Exists) { var fileNameRel = GetRelativeFileName(fileName); GitBash.RunCmd(string.Format("diff HEAD -- \"{0}\" > \"{1}\"", fileNameRel, tmpFileName), this.GitWorkingDirectory); } else { HistogramDiff hd = new HistogramDiff(); hd.SetFallbackAlgorithm(null); var fullName = GetFullPath(fileName); RawText b = new RawText(File.Exists(GetFullPath(fileName)) ? File.ReadAllBytes(fullName) : new byte[0]); RawText a = new RawText(GetFileContent(fileName) ?? new byte[0]); var list = hd.Diff(RawTextComparator.DEFAULT, a, b); using (Stream stream = File.Create(tmpFileName)) { DiffFormatter df = new DiffFormatter(stream); df.Format(list, a, b); df.Flush(); } //using (Stream mstream = new MemoryStream(), // stream = new BufferedStream(mstream)) //{ // DiffFormatter df = new DiffFormatter(stream); // df.Format(list, a, b); // df.Flush(); // stream.Seek(0, SeekOrigin.Begin); // var ret = new StreamReader(stream).ReadToEnd(); // File.WriteAllText(tmpFileName, ret); //} } return tmpFileName; } catch (Exception ex) { Log.WriteLine("Refresh: {0}\r\n{1}", this.initFolder, ex.ToString()); return ""; } }
public static RevCommit[] Blame (NGit.Repository repo, RevCommit c, string file) { TreeWalk tw = TreeWalk.ForPath (repo, ToGitPath (repo, file), c.Tree); if (tw == null) return new RevCommit [0]; ObjectId id = tw.GetObjectId (0); byte[] data = repo.ObjectDatabase.Open (id).GetBytes (); int lineCount = NGit.Util.RawParseUtils.LineMap (data, 0, data.Length).Size (); RevCommit[] lines = new RevCommit [lineCount]; var curText = new RawText (data); RevCommit prevAncestor = c; ObjectId prevObjectId = null; RevCommit prevCommit = null; int emptyLines = lineCount; RevWalk rw = new RevWalk (repo); foreach (ObjectId ancestorId in c.Parents) { RevCommit ancestor = rw.ParseCommit (ancestorId); tw = TreeWalk.ForPath (repo, ToGitPath (repo, file), ancestor.Tree); if (prevCommit != null && (tw == null || tw.GetObjectId (0) != prevObjectId)) { if (prevObjectId == null) break; byte[] prevData = repo.ObjectDatabase.Open (prevObjectId).GetBytes (); var prevText = new RawText (prevData); var differ = MyersDiff<RawText>.INSTANCE; foreach (Edit e in differ.Diff (RawTextComparator.DEFAULT, prevText, curText)) { for (int n = e.GetBeginB (); n < e.GetEndB (); n++) { if (lines [n] == null) { lines [n] = prevCommit; emptyLines--; } } } if (tw == null || emptyLines <= 0) break; } prevCommit = ancestor; prevObjectId = tw != null ? tw.GetObjectId (0) : null; } for (int n=0; n<lines.Length; n++) if (lines [n] == null) lines [n] = prevAncestor; return lines; }
public virtual void TestWriteLine3() { RawText a = new RawText(Constants.EncodeASCII("a\n\nb\n")); ByteArrayOutputStream o = new ByteArrayOutputStream(); a.WriteLine(o, 1); byte[] r = o.ToByteArray(); NUnit.Framework.Assert.AreEqual(string.Empty, RawParseUtils.Decode(r)); }
static int[] GetDiffCodes (ref int codeCounter, Dictionary<string, int> codeDictionary, RawText text) { int lineCount = text.Size (); int[] result = new int[lineCount]; string[] lines = GetLineStrings (text); for (int i = 0; i < lineCount; i++) { string lineText = lines [i]; int curCode; if (!codeDictionary.TryGetValue (lineText, out curCode)) { codeDictionary [lineText] = curCode = ++codeCounter; } result [i] = curCode; } return result; }
public virtual void TestLineDelimiter() { RawText rt = new RawText(Constants.EncodeASCII("foo\n")); NUnit.Framework.Assert.AreEqual("\n", rt.GetLineDelimiter()); NUnit.Framework.Assert.IsFalse(rt.IsMissingNewlineAtEnd()); rt = new RawText(Constants.EncodeASCII("foo\r\n")); NUnit.Framework.Assert.AreEqual("\r\n", rt.GetLineDelimiter()); NUnit.Framework.Assert.IsFalse(rt.IsMissingNewlineAtEnd()); rt = new RawText(Constants.EncodeASCII("foo\nbar")); NUnit.Framework.Assert.AreEqual("\n", rt.GetLineDelimiter()); NUnit.Framework.Assert.IsTrue(rt.IsMissingNewlineAtEnd()); rt = new RawText(Constants.EncodeASCII("foo\r\nbar")); NUnit.Framework.Assert.AreEqual("\r\n", rt.GetLineDelimiter()); NUnit.Framework.Assert.IsTrue(rt.IsMissingNewlineAtEnd()); rt = new RawText(Constants.EncodeASCII("foo\nbar\r\n")); NUnit.Framework.Assert.AreEqual("\n", rt.GetLineDelimiter()); NUnit.Framework.Assert.IsFalse(rt.IsMissingNewlineAtEnd()); rt = new RawText(Constants.EncodeASCII("foo\r\nbar\n")); NUnit.Framework.Assert.AreEqual("\r\n", rt.GetLineDelimiter()); NUnit.Framework.Assert.IsFalse(rt.IsMissingNewlineAtEnd()); rt = new RawText(Constants.EncodeASCII("foo")); NUnit.Framework.Assert.IsNull(rt.GetLineDelimiter()); NUnit.Framework.Assert.IsTrue(rt.IsMissingNewlineAtEnd()); rt = new RawText(Constants.EncodeASCII(string.Empty)); NUnit.Framework.Assert.IsNull(rt.GetLineDelimiter()); NUnit.Framework.Assert.IsTrue(rt.IsMissingNewlineAtEnd()); rt = new RawText(Constants.EncodeASCII("\n")); NUnit.Framework.Assert.AreEqual("\n", rt.GetLineDelimiter()); NUnit.Framework.Assert.IsFalse(rt.IsMissingNewlineAtEnd()); rt = new RawText(Constants.EncodeASCII("\r\n")); NUnit.Framework.Assert.AreEqual("\r\n", rt.GetLineDelimiter()); NUnit.Framework.Assert.IsFalse(rt.IsMissingNewlineAtEnd()); }
/// <summary> /// Diff working file with last commit /// </summary> /// <param name="fileName">Expect relative path</param> /// <returns>diff file in temp folder</returns> public string DiffFile(string fileName) { var tmpFileName = Path.ChangeExtension(Path.GetTempFileName(), ".diff"); try { if (!this.HasGitRepository) return ""; var status = GetFileStatus(fileName); if (head == null || status == GitFileStatus.New || status == GitFileStatus.Added) { tmpFileName = Path.ChangeExtension(tmpFileName, Path.GetExtension(fileName)); File.Copy(GetFullPath(fileName), tmpFileName); if (IsBinaryFile(tmpFileName)) { File.Delete(tmpFileName); File.WriteAllText(tmpFileName, "Binary file: " + fileName); } return tmpFileName; } if (GitBash.Exists) { var fileNameRel = GetRelativeFileName(fileName); GitBash.RunCmd(string.Format("diff HEAD -- \"{0}\" > \"{1}\"", fileNameRel, tmpFileName), this.GitWorkingDirectory); } else { HistogramDiff hd = new HistogramDiff(); hd.SetFallbackAlgorithm(null); var fullName = GetFullPath(fileName); RawText b = new RawText(File.Exists(GetFullPath(fileName)) ? File.ReadAllBytes(fullName) : new byte[0]); RawText a = new RawText(GetFileContent(fileName) ?? new byte[0]); var list = hd.Diff(RawTextComparator.DEFAULT, a, b); using (Stream stream = File.Create(tmpFileName)) { DiffFormatter df = new DiffFormatter(stream); df.Format(list, a, b); df.Flush(); } } // normalize the line endings of the diff so VS doesn't ask to do it for us string text = File.ReadAllText(tmpFileName).Replace("\r\n", "\n").Replace('\r', '\n'); // normalize to Environment.NewLine since this is only used for display in the IDE // and we want users to be able to copy from the diff and paste in a document without // changing line endings File.WriteAllText(tmpFileName, text.Replace("\n", Environment.NewLine)); } catch (Exception ex) { Log.WriteLine("DiffFile: {0}\r\n{1}", this.initFolder, ex.ToString()); //File.WriteAllText(tmpFileName, ex.ToString()); } return tmpFileName; }
/// <summary>Output a line of context (unmodified line).</summary> /// <remarks>Output a line of context (unmodified line).</remarks> /// <param name="text">RawText for accessing raw data</param> /// <param name="line">the line number within text</param> /// <exception cref="System.IO.IOException">System.IO.IOException</exception> protected internal virtual void WriteContextLine(RawText text, int line) { WriteLine(' ', text, line); }
public virtual void TestComparatorReduceCommonStartEnd_EmptyLine() { RawText a; RawText b; Edit e; a = new RawText(Sharpen.Runtime.GetBytesForString("R\n y\n", "UTF-8")); b = new RawText(Sharpen.Runtime.GetBytesForString("S\n\n y\n", "UTF-8")); e = new Edit(0, 2, 0, 3); e = RawTextComparator.DEFAULT.ReduceCommonStartEnd(a, b, e); NUnit.Framework.Assert.AreEqual(new Edit(0, 1, 0, 2), e); a = new RawText(Sharpen.Runtime.GetBytesForString("S\n\n y\n", "UTF-8")); b = new RawText(Sharpen.Runtime.GetBytesForString("R\n y\n", "UTF-8")); e = new Edit(0, 3, 0, 2); e = RawTextComparator.DEFAULT.ReduceCommonStartEnd(a, b, e); NUnit.Framework.Assert.AreEqual(new Edit(0, 2, 0, 1), e); }
private bool IsEndOfLineMissing(RawText text, int line) { return(line + 1 == text.Size() && text.IsMissingNewlineAtEnd()); }
static string GenerateDiff (byte[] data1, byte[] data2) { if (RawText.IsBinary (data1) || RawText.IsBinary (data2)) { if (data1.Length != data2.Length) return GettextCatalog.GetString (" Binary files differ"); if (data1.Length == data2.Length) { for (int n=0; n<data1.Length; n++) { if (data1[n] != data2[n]) return GettextCatalog.GetString (" Binary files differ"); } } return string.Empty; } var text1 = new RawText (data1); var text2 = new RawText (data2); var edits = DiffAlgorithm.GetAlgorithm (DiffAlgorithm.SupportedAlgorithm.MYERS) .Diff (RawTextComparator.DEFAULT, text1, text2); MemoryStream s = new MemoryStream (); var formatter = new DiffFormatter (s); formatter.Format (edits, text1, text2); return Encoding.UTF8.GetString (s.ToArray ()); }
/// <summary>Output an added line.</summary> /// <remarks>Output an added line.</remarks> /// <param name="text">RawText for accessing raw data</param> /// <param name="line">the line number within text</param> /// <exception cref="System.IO.IOException">System.IO.IOException</exception> protected internal virtual void WriteAddedLine(RawText text, int line) { WriteLine('+', text, line); }
internal BlameResult(BlameGenerator bg, string path, RawText text) { generator = bg; resultPath = path; resultContents = text; int cnt = text.Size(); sourceCommits = new RevCommit[cnt]; sourceAuthors = new PersonIdent[cnt]; sourceCommitters = new PersonIdent[cnt]; sourceLines = new int[cnt]; sourcePaths = new string[cnt]; }
/// <summary>Output a removed line</summary> /// <param name="text">RawText for accessing raw data</param> /// <param name="line">the line number within text</param> /// <exception cref="System.IO.IOException">System.IO.IOException</exception> protected internal virtual void WriteRemovedLine(RawText text, int line) { WriteLine('-', text, line); }
/// <summary>Write a standard patch script line.</summary> /// <remarks>Write a standard patch script line.</remarks> /// <param name="prefix">prefix before the line, typically '-', '+', ' '.</param> /// <param name="text">the text object to obtain the line from.</param> /// <param name="cur">line number to output.</param> /// <exception cref="System.IO.IOException">the stream threw an exception while writing to it. /// </exception> protected internal virtual void WriteLine(char prefix, RawText text, int cur) { @out.Write(prefix); text.WriteLine(@out, cur); @out.Write('\n'); }
public virtual void TestEmpty() { RawText r = new RawText(new byte[0]); NUnit.Framework.Assert.AreEqual(0, r.Size()); }
string GenerateDiff (byte[] data1, byte[] data2) { if (RawText.IsBinary (data1) || RawText.IsBinary (data2)) { if (data1.Length != data2.Length) return GettextCatalog.GetString (" Binary files differ"); if (data1.Length == data2.Length) { for (int n=0; n<data1.Length; n++) { if (data1[n] != data2[n]) return GettextCatalog.GetString (" Binary files differ"); } } return string.Empty; } var text1 = new RawText (data1); var text2 = new RawText (data2); var edits = MyersDiff<RawText>.INSTANCE.Diff(RawTextComparator.DEFAULT, text1, text2); MemoryStream s = new MemoryStream (); var formatter = new NGit.Diff.DiffFormatter (s); formatter.Format (edits, text1, text2); return Encoding.UTF8.GetString (s.ToArray ()); }
/// <exception cref="System.IO.IOException"></exception> internal virtual void LoadText(ObjectReader reader) { ObjectLoader ldr = reader.Open(sourceBlob, Constants.OBJ_BLOB); sourceText = new RawText(ldr.GetCachedBytes(int.MaxValue)); }