/// <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 TestCreateFileHeader_Add() { ObjectId adId = Blob("a\nd\n"); DiffEntry ent = DiffEntry.Add("FOO", adId); FileHeader fh = df.ToFileHeader(ent); string diffHeader = "diff --git a/FOO b/FOO\n" + "new file mode " + REGULAR_FILE + "\n" + "index " + ObjectId.ZeroId.Abbreviate(8).Name + ".." + adId.Abbreviate( 8).Name + "\n" + "--- /dev/null\n" + "+++ b/FOO\n"; // // // NUnit.Framework.Assert.AreEqual(diffHeader, RawParseUtils.Decode(fh.GetBuffer())); NUnit.Framework.Assert.AreEqual(0, fh.GetStartOffset()); NUnit.Framework.Assert.AreEqual(fh.GetBuffer().Length, fh.GetEndOffset()); NUnit.Framework.Assert.AreEqual(FileHeader.PatchType.UNIFIED, fh.GetPatchType()); NUnit.Framework.Assert.AreEqual(1, fh.GetHunks().Count); HunkHeader hh = fh.GetHunks()[0]; NUnit.Framework.Assert.AreEqual(1, hh.ToEditList().Count); EditList el = hh.ToEditList(); NUnit.Framework.Assert.AreEqual(1, el.Count); Edit e = el[0]; NUnit.Framework.Assert.AreEqual(0, e.GetBeginA()); NUnit.Framework.Assert.AreEqual(0, e.GetEndA()); NUnit.Framework.Assert.AreEqual(0, e.GetBeginB()); NUnit.Framework.Assert.AreEqual(2, e.GetEndB()); NUnit.Framework.Assert.AreEqual(Edit.Type.INSERT, e.GetType()); }
public virtual void TestCreateFileHeader_Modify() { ObjectId adId = Blob("a\nd\n"); ObjectId abcdId = Blob("a\nb\nc\nd\n"); string diffHeader = MakeDiffHeader(PATH_A, PATH_A, adId, abcdId); DiffEntry ad = DiffEntry.Delete(PATH_A, adId); DiffEntry abcd = DiffEntry.Add(PATH_A, abcdId); DiffEntry mod = DiffEntry.Pair(DiffEntry.ChangeType.MODIFY, ad, abcd, 0); FileHeader fh = df.ToFileHeader(mod); NUnit.Framework.Assert.AreEqual(diffHeader, RawParseUtils.Decode(fh.GetBuffer())); NUnit.Framework.Assert.AreEqual(0, fh.GetStartOffset()); NUnit.Framework.Assert.AreEqual(fh.GetBuffer().Length, fh.GetEndOffset()); NUnit.Framework.Assert.AreEqual(FileHeader.PatchType.UNIFIED, fh.GetPatchType()); NUnit.Framework.Assert.AreEqual(1, fh.GetHunks().Count); HunkHeader hh = fh.GetHunks()[0]; NUnit.Framework.Assert.AreEqual(1, hh.ToEditList().Count); EditList el = hh.ToEditList(); NUnit.Framework.Assert.AreEqual(1, el.Count); Edit e = el[0]; NUnit.Framework.Assert.AreEqual(1, e.GetBeginA()); NUnit.Framework.Assert.AreEqual(1, e.GetEndA()); NUnit.Framework.Assert.AreEqual(1, e.GetBeginB()); NUnit.Framework.Assert.AreEqual(3, e.GetEndB()); NUnit.Framework.Assert.AreEqual(Edit.Type.INSERT, e.GetType()); }
public virtual void TestCreate() { Edit e = new Edit(1, 2, 3, 4); NUnit.Framework.Assert.AreEqual(1, e.GetBeginA()); NUnit.Framework.Assert.AreEqual(2, e.GetEndA()); NUnit.Framework.Assert.AreEqual(3, e.GetBeginB()); NUnit.Framework.Assert.AreEqual(4, e.GetEndB()); }
public virtual void TestCreateEmpty() { Edit e = new Edit(1, 3); NUnit.Framework.Assert.AreEqual(1, e.GetBeginA()); NUnit.Framework.Assert.AreEqual(1, e.GetEndA()); NUnit.Framework.Assert.AreEqual(3, e.GetBeginB()); NUnit.Framework.Assert.AreEqual(3, e.GetEndB()); NUnit.Framework.Assert.IsTrue(e.IsEmpty(), "is empty"); NUnit.Framework.Assert.AreEqual(Edit.Type.EMPTY, e.GetType()); }
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; }