A parsed collection of FileHeader s from a unified diff patch file
Example #1
0
 public virtual void TestError_CcTruncatedOld()
 {
     NGit.Patch.Patch p = ParseTestPatchFile();
     NUnit.Framework.Assert.AreEqual(1, p.GetFiles().Count);
     NUnit.Framework.Assert.AreEqual(3, p.GetErrors().Count);
     {
         FormatError e = p.GetErrors()[0];
         NUnit.Framework.Assert.AreEqual(FormatError.Severity.ERROR, e.GetSeverity());
         NUnit.Framework.Assert.AreEqual(MessageFormat.Format(JGitText.Get().truncatedHunkLinesMissingForAncestor
                                                              , Sharpen.Extensions.ValueOf(1), Sharpen.Extensions.ValueOf(1)), e.GetMessage());
         NUnit.Framework.Assert.AreEqual(346, e.GetOffset());
         NUnit.Framework.Assert.IsTrue(e.GetLineText().StartsWith("@@@ -55,12 -163,13 +163,15 @@@ public "
                                                                  ));
     }
     {
         FormatError e = p.GetErrors()[1];
         NUnit.Framework.Assert.AreEqual(FormatError.Severity.ERROR, e.GetSeverity());
         NUnit.Framework.Assert.AreEqual(MessageFormat.Format(JGitText.Get().truncatedHunkLinesMissingForAncestor
                                                              , Sharpen.Extensions.ValueOf(2), Sharpen.Extensions.ValueOf(2)), e.GetMessage());
         NUnit.Framework.Assert.AreEqual(346, e.GetOffset());
         NUnit.Framework.Assert.IsTrue(e.GetLineText().StartsWith("@@@ -55,12 -163,13 +163,15 @@@ public "
                                                                  ));
     }
     {
         FormatError e = p.GetErrors()[2];
         NUnit.Framework.Assert.AreEqual(FormatError.Severity.ERROR, e.GetSeverity());
         NUnit.Framework.Assert.AreEqual("Truncated hunk, at least 3 new lines is missing"
                                         , e.GetMessage());
         NUnit.Framework.Assert.AreEqual(346, e.GetOffset());
         NUnit.Framework.Assert.IsTrue(e.GetLineText().StartsWith("@@@ -55,12 -163,13 +163,15 @@@ public "
                                                                  ));
     }
 }
        public virtual void TestParse_GitBinaryDelta()
        {
            NGit.Patch.Patch p = ParseTestPatchFile();
            NUnit.Framework.Assert.AreEqual(1, p.GetFiles().Count);
            NUnit.Framework.Assert.IsTrue(p.GetErrors().IsEmpty());
            FileHeader fh = p.GetFiles()[0];

            NUnit.Framework.Assert.IsTrue(fh.GetNewPath().StartsWith("zero.bin"));
            NUnit.Framework.Assert.AreEqual(DiffEntry.ChangeType.MODIFY, fh.GetChangeType());
            NUnit.Framework.Assert.AreEqual(FileHeader.PatchType.GIT_BINARY, fh.GetPatchType(
                                                ));
            NUnit.Framework.Assert.AreSame(FileMode.REGULAR_FILE, fh.GetNewMode());
            NUnit.Framework.Assert.IsNotNull(fh.GetOldId());
            NUnit.Framework.Assert.IsNotNull(fh.GetNewId());
            NUnit.Framework.Assert.AreEqual("08e7df176454f3ee5eeda13efa0adaa54828dfd8", fh.GetOldId
                                                ().Name);
            NUnit.Framework.Assert.AreEqual("d70d8710b6d32ff844af0ee7c247e4b4b051867f", fh.GetNewId
                                                ().Name);
            NUnit.Framework.Assert.IsTrue(fh.GetHunks().IsEmpty());
            NUnit.Framework.Assert.IsFalse(fh.HasMetaDataChanges());
            BinaryHunk fwd = fh.GetForwardBinaryHunk();
            BinaryHunk rev = fh.GetReverseBinaryHunk();

            NUnit.Framework.Assert.IsNotNull(fwd);
            NUnit.Framework.Assert.IsNotNull(rev);
            NUnit.Framework.Assert.AreEqual(12, fwd.GetSize());
            NUnit.Framework.Assert.AreEqual(11, rev.GetSize());
            NUnit.Framework.Assert.AreSame(fh, fwd.GetFileHeader());
            NUnit.Framework.Assert.AreSame(fh, rev.GetFileHeader());
            NUnit.Framework.Assert.AreEqual(BinaryHunk.Type.DELTA_DEFLATED, fwd.GetType());
            NUnit.Framework.Assert.AreEqual(BinaryHunk.Type.DELTA_DEFLATED, rev.GetType());
            NUnit.Framework.Assert.AreEqual(496, fh.endOffset);
        }
Example #3
0
        public virtual void TestError_GitBinaryNoForwardHunk()
        {
            NGit.Patch.Patch p = ParseTestPatchFile();
            NUnit.Framework.Assert.AreEqual(2, p.GetFiles().Count);
            {
                FileHeader fh = p.GetFiles()[0];
                NUnit.Framework.Assert.AreEqual("org.spearce.egit.ui/icons/toolbar/fetchd.png", fh
                                                .GetNewPath());
                NUnit.Framework.Assert.AreEqual(FileHeader.PatchType.GIT_BINARY, fh.GetPatchType(
                                                    ));
                NUnit.Framework.Assert.IsTrue(fh.GetHunks().IsEmpty());
                NUnit.Framework.Assert.IsNull(fh.GetForwardBinaryHunk());
            }
            {
                FileHeader fh = p.GetFiles()[1];
                NUnit.Framework.Assert.AreEqual("org.spearce.egit.ui/icons/toolbar/fetche.png", fh
                                                .GetNewPath());
                NUnit.Framework.Assert.AreEqual(FileHeader.PatchType.UNIFIED, fh.GetPatchType());
                NUnit.Framework.Assert.IsTrue(fh.GetHunks().IsEmpty());
                NUnit.Framework.Assert.IsNull(fh.GetForwardBinaryHunk());
            }
            NUnit.Framework.Assert.AreEqual(1, p.GetErrors().Count);
            FormatError e = p.GetErrors()[0];

            NUnit.Framework.Assert.AreEqual(FormatError.Severity.ERROR, e.GetSeverity());
            NUnit.Framework.Assert.AreEqual("Missing forward-image in GIT binary patch", e.GetMessage
                                                ());
            NUnit.Framework.Assert.AreEqual(297, e.GetOffset());
            NUnit.Framework.Assert.AreEqual("\n", e.GetLineText());
        }
        public virtual void TestParse_AddNoNewline()
        {
            NGit.Patch.Patch p = ParseTestPatchFile();
            NUnit.Framework.Assert.AreEqual(1, p.GetFiles().Count);
            NUnit.Framework.Assert.IsTrue(p.GetErrors().IsEmpty());
            FileHeader f = p.GetFiles()[0];

            NUnit.Framework.Assert.AreEqual("a", f.GetNewPath());
            NUnit.Framework.Assert.AreEqual(256, f.startOffset);
            NUnit.Framework.Assert.AreEqual("f2ad6c7", f.GetOldId().Name);
            NUnit.Framework.Assert.AreEqual("c59d9b6", f.GetNewId().Name);
            NUnit.Framework.Assert.AreEqual(FileHeader.PatchType.UNIFIED, f.GetPatchType());
            NUnit.Framework.Assert.AreSame(FileMode.REGULAR_FILE, f.GetOldMode());
            NUnit.Framework.Assert.AreSame(FileMode.REGULAR_FILE, f.GetNewMode());
            NUnit.Framework.Assert.AreEqual(1, f.GetHunks().Count);
            {
                HunkHeader h = f.GetHunks()[0];
                NUnit.Framework.Assert.AreSame(f, h.GetFileHeader());
                NUnit.Framework.Assert.AreEqual(321, h.startOffset);
                NUnit.Framework.Assert.AreEqual(1, h.GetOldImage().GetStartLine());
                NUnit.Framework.Assert.AreEqual(1, h.GetOldImage().GetLineCount());
                NUnit.Framework.Assert.AreEqual(1, h.GetNewStartLine());
                NUnit.Framework.Assert.AreEqual(1, h.GetNewLineCount());
                NUnit.Framework.Assert.AreEqual(0, h.GetLinesContext());
                NUnit.Framework.Assert.AreEqual(1, h.GetOldImage().GetLinesAdded());
                NUnit.Framework.Assert.AreEqual(1, h.GetOldImage().GetLinesDeleted());
                NUnit.Framework.Assert.AreSame(f.GetOldId(), h.GetOldImage().GetId());
                NUnit.Framework.Assert.AreEqual(367, h.endOffset);
            }
        }
Example #5
0
 /// <exception cref="System.IO.IOException"></exception>
 private NGit.Patch.Patch ParseTestPatchFile()
 {
     using (var @in = GetResourceStream())
     {
         NGit.Patch.Patch p = new NGit.Patch.Patch();
         p.Parse(@in);
         return(p);
     }
 }
Example #6
0
        public virtual void TestFileHeader()
        {
            NGit.Patch.Patch p  = ParseTestPatchFile("testGetText_BothISO88591.patch");
            FileHeader       fh = p.GetFiles()[0];
            EditList         e  = fh.ToEditList();

            NUnit.Framework.Assert.AreEqual(2, e.Count);
            NUnit.Framework.Assert.AreEqual(new Edit(4 - 1, 5 - 1, 4 - 1, 5 - 1), e[0]);
            NUnit.Framework.Assert.AreEqual(new Edit(16 - 1, 17 - 1, 16 - 1, 17 - 1), e[1]);
        }
Example #7
0
        public virtual void TestTypes()
        {
            NGit.Patch.Patch p  = ParseTestPatchFile("testEditList_Types.patch");
            FileHeader       fh = p.GetFiles()[0];
            EditList         e  = fh.ToEditList();

            NUnit.Framework.Assert.AreEqual(3, e.Count);
            NUnit.Framework.Assert.AreEqual(new Edit(3 - 1, 3 - 1, 3 - 1, 4 - 1), e[0]);
            NUnit.Framework.Assert.AreEqual(new Edit(17 - 1, 19 - 1, 18 - 1, 18 - 1), e[1]);
            NUnit.Framework.Assert.AreEqual(new Edit(23 - 1, 25 - 1, 22 - 1, 28 - 1), e[2]);
        }
Example #8
0
        public virtual void TestGetText_NoBinary()
        {
            Encoding cs = Sharpen.Extensions.GetEncoding("ISO-8859-1");

            NGit.Patch.Patch p = ParseTestPatchFile();
            NUnit.Framework.Assert.IsTrue(p.GetErrors().IsEmpty());
            NUnit.Framework.Assert.AreEqual(1, p.GetFiles().Count);
            FileHeader fh = p.GetFiles()[0];

            NUnit.Framework.Assert.AreEqual(0, fh.GetHunks().Count);
            NUnit.Framework.Assert.AreEqual(ReadTestPatchFile(cs), fh.GetScriptText(cs, cs));
        }
Example #9
0
        public virtual void TestError_BodyTooLong()
        {
            NGit.Patch.Patch p = ParseTestPatchFile();
            NUnit.Framework.Assert.AreEqual(1, p.GetFiles().Count);
            NUnit.Framework.Assert.AreEqual(1, p.GetErrors().Count);
            FormatError e = p.GetErrors()[0];

            NUnit.Framework.Assert.AreEqual(FormatError.Severity.WARNING, e.GetSeverity());
            NUnit.Framework.Assert.AreEqual("Hunk header 4:11 does not match body line count of 4:12"
                                            , e.GetMessage());
            NUnit.Framework.Assert.AreEqual(349, e.GetOffset());
            NUnit.Framework.Assert.IsTrue(e.GetLineText().StartsWith("@@ -109,4 +109,11 @@ assert"
                                                                     ));
        }
Example #10
0
        public virtual void TestHunkHeader()
        {
            NGit.Patch.Patch p     = ParseTestPatchFile("testGetText_BothISO88591.patch");
            FileHeader       fh    = p.GetFiles()[0];
            EditList         list0 = fh.GetHunks()[0].ToEditList();

            NUnit.Framework.Assert.AreEqual(1, list0.Count);
            NUnit.Framework.Assert.AreEqual(new Edit(4 - 1, 5 - 1, 4 - 1, 5 - 1), list0[0]);
            EditList list1 = fh.GetHunks()[1].ToEditList();

            NUnit.Framework.Assert.AreEqual(1, list1.Count);
            NUnit.Framework.Assert.AreEqual(new Edit(16 - 1, 17 - 1, 16 - 1, 17 - 1), list1[0
                                            ]);
        }
Example #11
0
        public virtual void TestError_TruncatedNew()
        {
            NGit.Patch.Patch p = ParseTestPatchFile();
            NUnit.Framework.Assert.AreEqual(1, p.GetFiles().Count);
            NUnit.Framework.Assert.AreEqual(1, p.GetErrors().Count);
            FormatError e = p.GetErrors()[0];

            NUnit.Framework.Assert.AreEqual(FormatError.Severity.ERROR, e.GetSeverity());
            NUnit.Framework.Assert.AreEqual("Truncated hunk, at least 1 new lines is missing"
                                            , e.GetMessage());
            NUnit.Framework.Assert.AreEqual(313, e.GetOffset());
            NUnit.Framework.Assert.IsTrue(e.GetLineText().StartsWith("@@ -236,9 +236,9 @@ protected "
                                                                     ));
        }
        public virtual void TestParse_GitBinaryLiteral()
        {
            NGit.Patch.Patch p        = ParseTestPatchFile();
            int[]            binsizes = new int[] { 359, 393, 372, 404 };
            NUnit.Framework.Assert.AreEqual(5, p.GetFiles().Count);
            NUnit.Framework.Assert.IsTrue(p.GetErrors().IsEmpty());
            for (int i = 0; i < 4; i++)
            {
                FileHeader fh = p.GetFiles()[i];
                NUnit.Framework.Assert.AreEqual(DiffEntry.ChangeType.ADD, fh.GetChangeType());
                NUnit.Framework.Assert.IsNotNull(fh.GetOldId());
                NUnit.Framework.Assert.IsNotNull(fh.GetNewId());
                NUnit.Framework.Assert.AreEqual(ObjectId.ZeroId.Name, fh.GetOldId().Name);
                NUnit.Framework.Assert.AreSame(FileMode.REGULAR_FILE, fh.GetNewMode());
                NUnit.Framework.Assert.IsTrue(fh.GetNewPath().StartsWith("org.spearce.egit.ui/icons/toolbar/"
                                                                         ));
                NUnit.Framework.Assert.AreEqual(FileHeader.PatchType.GIT_BINARY, fh.GetPatchType(
                                                    ));
                NUnit.Framework.Assert.IsTrue(fh.GetHunks().IsEmpty());
                NUnit.Framework.Assert.IsTrue(fh.HasMetaDataChanges());
                BinaryHunk fwd = fh.GetForwardBinaryHunk();
                BinaryHunk rev = fh.GetReverseBinaryHunk();
                NUnit.Framework.Assert.IsNotNull(fwd);
                NUnit.Framework.Assert.IsNotNull(rev);
                NUnit.Framework.Assert.AreEqual(binsizes[i], fwd.GetSize());
                NUnit.Framework.Assert.AreEqual(0, rev.GetSize());
                NUnit.Framework.Assert.AreSame(fh, fwd.GetFileHeader());
                NUnit.Framework.Assert.AreSame(fh, rev.GetFileHeader());
                NUnit.Framework.Assert.AreEqual(BinaryHunk.Type.LITERAL_DEFLATED, fwd.GetType());
                NUnit.Framework.Assert.AreEqual(BinaryHunk.Type.LITERAL_DEFLATED, rev.GetType());
            }
            FileHeader fh_1 = p.GetFiles()[4];

            NUnit.Framework.Assert.AreEqual("org.spearce.egit.ui/plugin.xml", fh_1.GetNewPath
                                                ());
            NUnit.Framework.Assert.AreEqual(DiffEntry.ChangeType.MODIFY, fh_1.GetChangeType()
                                            );
            NUnit.Framework.Assert.AreEqual(FileHeader.PatchType.UNIFIED, fh_1.GetPatchType()
                                            );
            NUnit.Framework.Assert.IsFalse(fh_1.HasMetaDataChanges());
            NUnit.Framework.Assert.AreEqual("ee8a5a0", fh_1.GetNewId().Name);
            NUnit.Framework.Assert.IsNull(fh_1.GetForwardBinaryHunk());
            NUnit.Framework.Assert.IsNull(fh_1.GetReverseBinaryHunk());
            NUnit.Framework.Assert.AreEqual(1, fh_1.GetHunks().Count);
            NUnit.Framework.Assert.AreEqual(272, fh_1.GetHunks()[0].GetOldImage().GetStartLine
                                                ());
        }
Example #13
0
        public virtual void TestParse_OneFileCc()
        {
            NGit.Patch.Patch p = ParseTestPatchFile();
            NUnit.Framework.Assert.AreEqual(1, p.GetFiles().Count);
            NUnit.Framework.Assert.IsTrue(p.GetErrors().IsEmpty());
            CombinedFileHeader cfh = (CombinedFileHeader)p.GetFiles()[0];

            NUnit.Framework.Assert.AreEqual("org.spearce.egit.ui/src/org/spearce/egit/ui/UIText.java"
                                            , cfh.GetNewPath());
            NUnit.Framework.Assert.AreEqual(cfh.GetNewPath(), cfh.GetOldPath());
            NUnit.Framework.Assert.AreEqual(98, cfh.startOffset);
            NUnit.Framework.Assert.AreEqual(2, cfh.GetParentCount());
            NUnit.Framework.Assert.AreSame(cfh.GetOldId(0), cfh.GetOldId());
            NUnit.Framework.Assert.AreEqual("169356b", cfh.GetOldId(0).Name);
            NUnit.Framework.Assert.AreEqual("dd8c317", cfh.GetOldId(1).Name);
            NUnit.Framework.Assert.AreEqual("fd85931", cfh.GetNewId().Name);
            NUnit.Framework.Assert.AreSame(cfh.GetOldMode(0), cfh.GetOldMode());
            NUnit.Framework.Assert.AreSame(FileMode.REGULAR_FILE, cfh.GetOldMode(0));
            NUnit.Framework.Assert.AreSame(FileMode.REGULAR_FILE, cfh.GetOldMode(1));
            NUnit.Framework.Assert.AreSame(FileMode.EXECUTABLE_FILE, cfh.GetNewMode());
            NUnit.Framework.Assert.AreEqual(DiffEntry.ChangeType.MODIFY, cfh.GetChangeType());
            NUnit.Framework.Assert.AreEqual(FileHeader.PatchType.UNIFIED, cfh.GetPatchType());
            NUnit.Framework.Assert.AreEqual(1, ((IList <HunkHeader>)cfh.GetHunks()).Count
                                            );
            {
                CombinedHunkHeader h = (CombinedHunkHeader)(cfh.GetHunks()[0]);
                NUnit.Framework.Assert.AreSame(cfh, ((CombinedFileHeader)h.GetFileHeader()));
                NUnit.Framework.Assert.AreEqual(346, h.startOffset);
                NUnit.Framework.Assert.AreEqual(764, h.endOffset);
                NUnit.Framework.Assert.AreSame(h.GetOldImage(0), h.GetOldImage());
                NUnit.Framework.Assert.AreSame(cfh.GetOldId(0), h.GetOldImage(0).GetId());
                NUnit.Framework.Assert.AreSame(cfh.GetOldId(1), h.GetOldImage(1).GetId());
                NUnit.Framework.Assert.AreEqual(55, h.GetOldImage(0).GetStartLine());
                NUnit.Framework.Assert.AreEqual(12, h.GetOldImage(0).GetLineCount());
                NUnit.Framework.Assert.AreEqual(3, h.GetOldImage(0).GetLinesAdded());
                NUnit.Framework.Assert.AreEqual(0, h.GetOldImage(0).GetLinesDeleted());
                NUnit.Framework.Assert.AreEqual(163, h.GetOldImage(1).GetStartLine());
                NUnit.Framework.Assert.AreEqual(13, h.GetOldImage(1).GetLineCount());
                NUnit.Framework.Assert.AreEqual(2, h.GetOldImage(1).GetLinesAdded());
                NUnit.Framework.Assert.AreEqual(0, h.GetOldImage(1).GetLinesDeleted());
                NUnit.Framework.Assert.AreEqual(163, h.GetNewStartLine());
                NUnit.Framework.Assert.AreEqual(15, h.GetNewLineCount());
                NUnit.Framework.Assert.AreEqual(10, h.GetLinesContext());
            }
        }
Example #14
0
        public virtual void TestParse_CcNewFile()
        {
            NGit.Patch.Patch p = ParseTestPatchFile();
            NUnit.Framework.Assert.AreEqual(1, p.GetFiles().Count);
            NUnit.Framework.Assert.IsTrue(p.GetErrors().IsEmpty());
            CombinedFileHeader cfh = (CombinedFileHeader)p.GetFiles()[0];

            NUnit.Framework.Assert.AreSame(DiffEntry.DEV_NULL, cfh.GetOldPath());
            NUnit.Framework.Assert.AreEqual("d", cfh.GetNewPath());
            NUnit.Framework.Assert.AreEqual(187, cfh.startOffset);
            NUnit.Framework.Assert.AreEqual(2, cfh.GetParentCount());
            NUnit.Framework.Assert.AreSame(cfh.GetOldId(0), cfh.GetOldId());
            NUnit.Framework.Assert.AreEqual("0000000", cfh.GetOldId(0).Name);
            NUnit.Framework.Assert.AreEqual("0000000", cfh.GetOldId(1).Name);
            NUnit.Framework.Assert.AreEqual("4bcfe98", cfh.GetNewId().Name);
            NUnit.Framework.Assert.AreSame(cfh.GetOldMode(0), cfh.GetOldMode());
            NUnit.Framework.Assert.AreSame(FileMode.MISSING, cfh.GetOldMode(0));
            NUnit.Framework.Assert.AreSame(FileMode.MISSING, cfh.GetOldMode(1));
            NUnit.Framework.Assert.AreSame(FileMode.REGULAR_FILE, cfh.GetNewMode());
            NUnit.Framework.Assert.AreEqual(DiffEntry.ChangeType.ADD, cfh.GetChangeType());
            NUnit.Framework.Assert.AreEqual(FileHeader.PatchType.UNIFIED, cfh.GetPatchType());
            NUnit.Framework.Assert.AreEqual(1, ((IList <HunkHeader>)cfh.GetHunks()).Count
                                            );
            {
                CombinedHunkHeader h = (CombinedHunkHeader)(cfh.GetHunks()[0]);
                NUnit.Framework.Assert.AreSame(cfh, ((CombinedFileHeader)h.GetFileHeader()));
                NUnit.Framework.Assert.AreEqual(273, h.startOffset);
                NUnit.Framework.Assert.AreEqual(300, h.endOffset);
                NUnit.Framework.Assert.AreSame(h.GetOldImage(0), h.GetOldImage());
                NUnit.Framework.Assert.AreSame(cfh.GetOldId(0), h.GetOldImage(0).GetId());
                NUnit.Framework.Assert.AreSame(cfh.GetOldId(1), h.GetOldImage(1).GetId());
                NUnit.Framework.Assert.AreEqual(1, h.GetOldImage(0).GetStartLine());
                NUnit.Framework.Assert.AreEqual(0, h.GetOldImage(0).GetLineCount());
                NUnit.Framework.Assert.AreEqual(1, h.GetOldImage(0).GetLinesAdded());
                NUnit.Framework.Assert.AreEqual(0, h.GetOldImage(0).GetLinesDeleted());
                NUnit.Framework.Assert.AreEqual(1, h.GetOldImage(1).GetStartLine());
                NUnit.Framework.Assert.AreEqual(0, h.GetOldImage(1).GetLineCount());
                NUnit.Framework.Assert.AreEqual(1, h.GetOldImage(1).GetLinesAdded());
                NUnit.Framework.Assert.AreEqual(0, h.GetOldImage(1).GetLinesDeleted());
                NUnit.Framework.Assert.AreEqual(1, h.GetNewStartLine());
                NUnit.Framework.Assert.AreEqual(1, h.GetNewLineCount());
                NUnit.Framework.Assert.AreEqual(0, h.GetLinesContext());
            }
        }
Example #15
0
        public virtual void TestError_DisconnectedHunk()
        {
            NGit.Patch.Patch p = ParseTestPatchFile();
            NUnit.Framework.Assert.AreEqual(1, p.GetFiles().Count);
            {
                FileHeader fh = p.GetFiles()[0];
                NUnit.Framework.Assert.AreEqual("org.eclipse.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java"
                                                , fh.GetNewPath());
                NUnit.Framework.Assert.AreEqual(1, fh.GetHunks().Count);
            }
            NUnit.Framework.Assert.AreEqual(1, p.GetErrors().Count);
            FormatError e = p.GetErrors()[0];

            NUnit.Framework.Assert.AreEqual(FormatError.Severity.ERROR, e.GetSeverity());
            NUnit.Framework.Assert.AreEqual("Hunk disconnected from file", e.GetMessage());
            NUnit.Framework.Assert.AreEqual(18, e.GetOffset());
            NUnit.Framework.Assert.IsTrue(e.GetLineText().StartsWith("@@ -109,4 +109,11 @@ assert"
                                                                     ));
        }
Example #16
0
        public virtual void TestGetText_Convert()
        {
            Encoding csOld = Sharpen.Extensions.GetEncoding("ISO-8859-1");
            Encoding csNew = Sharpen.Extensions.GetEncoding("UTF-8");

            NGit.Patch.Patch p = ParseTestPatchFile();
            NUnit.Framework.Assert.IsTrue(p.GetErrors().IsEmpty());
            NUnit.Framework.Assert.AreEqual(1, p.GetFiles().Count);
            FileHeader fh = p.GetFiles()[0];

            NUnit.Framework.Assert.AreEqual(2, fh.GetHunks().Count);
            // Read the original file as ISO-8859-1 and fix up the one place
            // where we changed the character encoding. That makes the exp
            // string match what we really expect to get back.
            //
            string exp = ReadTestPatchFile(csOld);

            exp = exp.Replace("\x12f\xcdngstr\x12f\x10am", "\u00c5ngstr\u00f6m");
            NUnit.Framework.Assert.AreEqual(exp, fh.GetScriptText(csOld, csNew));
        }
Example #17
0
        /// <exception cref="System.IO.IOException"></exception>
        private NGit.Patch.Patch ParseTestPatchFile(string patchFile)
        {
            InputStream @in = GetType().GetResourceAsStream(patchFile);

            if (@in == null)
            {
                NUnit.Framework.Assert.Fail("No " + patchFile + " test vector");
                return(null);
            }
            // Never happens
            try
            {
                NGit.Patch.Patch p = new NGit.Patch.Patch();
                p.Parse(@in);
                return(p);
            }
            finally
            {
                @in.Close();
            }
        }
        public virtual void TestParse_NoBinary()
        {
            NGit.Patch.Patch p = ParseTestPatchFile();
            NUnit.Framework.Assert.AreEqual(5, p.GetFiles().Count);
            NUnit.Framework.Assert.IsTrue(p.GetErrors().IsEmpty());
            for (int i = 0; i < 4; i++)
            {
                FileHeader fh = p.GetFiles()[i];
                NUnit.Framework.Assert.AreEqual(DiffEntry.ChangeType.ADD, fh.GetChangeType());
                NUnit.Framework.Assert.IsNotNull(fh.GetOldId());
                NUnit.Framework.Assert.IsNotNull(fh.GetNewId());
                NUnit.Framework.Assert.AreEqual("0000000", fh.GetOldId().Name);
                NUnit.Framework.Assert.AreSame(FileMode.MISSING, fh.GetOldMode());
                NUnit.Framework.Assert.AreSame(FileMode.REGULAR_FILE, fh.GetNewMode());
                NUnit.Framework.Assert.IsTrue(fh.GetNewPath().StartsWith("org.spearce.egit.ui/icons/toolbar/"
                                                                         ));
                NUnit.Framework.Assert.AreEqual(FileHeader.PatchType.BINARY, fh.GetPatchType());
                NUnit.Framework.Assert.IsTrue(fh.GetHunks().IsEmpty());
                NUnit.Framework.Assert.IsTrue(fh.HasMetaDataChanges());
                NUnit.Framework.Assert.IsNull(fh.GetForwardBinaryHunk());
                NUnit.Framework.Assert.IsNull(fh.GetReverseBinaryHunk());
            }
            FileHeader fh_1 = p.GetFiles()[4];

            NUnit.Framework.Assert.AreEqual("org.spearce.egit.ui/plugin.xml", fh_1.GetNewPath
                                                ());
            NUnit.Framework.Assert.AreEqual(DiffEntry.ChangeType.MODIFY, fh_1.GetChangeType()
                                            );
            NUnit.Framework.Assert.AreEqual(FileHeader.PatchType.UNIFIED, fh_1.GetPatchType()
                                            );
            NUnit.Framework.Assert.IsFalse(fh_1.HasMetaDataChanges());
            NUnit.Framework.Assert.AreEqual("ee8a5a0", fh_1.GetNewId().Name);
            NUnit.Framework.Assert.IsNull(fh_1.GetForwardBinaryHunk());
            NUnit.Framework.Assert.IsNull(fh_1.GetReverseBinaryHunk());
            NUnit.Framework.Assert.AreEqual(1, fh_1.GetHunks().Count);
            NUnit.Framework.Assert.AreEqual(272, fh_1.GetHunks()[0].GetOldImage().GetStartLine
                                                ());
        }
Example #19
0
        public virtual void TestError_GarbageBetweenFiles()
        {
            NGit.Patch.Patch p = ParseTestPatchFile();
            NUnit.Framework.Assert.AreEqual(2, p.GetFiles().Count);
            {
                FileHeader fh = p.GetFiles()[0];
                NUnit.Framework.Assert.AreEqual("org.eclipse.jgit.test/tst/org/spearce/jgit/lib/RepositoryConfigTest.java"
                                                , fh.GetNewPath());
                NUnit.Framework.Assert.AreEqual(1, fh.GetHunks().Count);
            }
            {
                FileHeader fh = p.GetFiles()[1];
                NUnit.Framework.Assert.AreEqual("org.eclipse.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java"
                                                , fh.GetNewPath());
                NUnit.Framework.Assert.AreEqual(1, fh.GetHunks().Count);
            }
            NUnit.Framework.Assert.AreEqual(1, p.GetErrors().Count);
            FormatError e = p.GetErrors()[0];

            NUnit.Framework.Assert.AreEqual(FormatError.Severity.WARNING, e.GetSeverity());
            NUnit.Framework.Assert.AreEqual("Unexpected hunk trailer", e.GetMessage());
            NUnit.Framework.Assert.AreEqual(926, e.GetOffset());
            NUnit.Framework.Assert.AreEqual("I AM NOT HERE\n", e.GetLineText());
        }
Example #20
0
        public virtual void TestParse_CcDeleteFile()
        {
            NGit.Patch.Patch p = ParseTestPatchFile();
            NUnit.Framework.Assert.AreEqual(1, p.GetFiles().Count);
            NUnit.Framework.Assert.IsTrue(p.GetErrors().IsEmpty());
            CombinedFileHeader cfh = (CombinedFileHeader)p.GetFiles()[0];

            NUnit.Framework.Assert.AreEqual("a", cfh.GetOldPath());
            NUnit.Framework.Assert.AreSame(DiffEntry.DEV_NULL, cfh.GetNewPath());
            NUnit.Framework.Assert.AreEqual(187, cfh.startOffset);
            NUnit.Framework.Assert.AreEqual(2, cfh.GetParentCount());
            NUnit.Framework.Assert.AreSame(cfh.GetOldId(0), cfh.GetOldId());
            NUnit.Framework.Assert.AreEqual("7898192", cfh.GetOldId(0).Name);
            NUnit.Framework.Assert.AreEqual("2e65efe", cfh.GetOldId(1).Name);
            NUnit.Framework.Assert.AreEqual("0000000", cfh.GetNewId().Name);
            NUnit.Framework.Assert.AreSame(cfh.GetOldMode(0), cfh.GetOldMode());
            NUnit.Framework.Assert.AreSame(FileMode.REGULAR_FILE, cfh.GetOldMode(0));
            NUnit.Framework.Assert.AreSame(FileMode.REGULAR_FILE, cfh.GetOldMode(1));
            NUnit.Framework.Assert.AreSame(FileMode.MISSING, cfh.GetNewMode());
            NUnit.Framework.Assert.AreEqual(DiffEntry.ChangeType.DELETE, cfh.GetChangeType());
            NUnit.Framework.Assert.AreEqual(FileHeader.PatchType.UNIFIED, cfh.GetPatchType());
            NUnit.Framework.Assert.IsTrue((cfh.GetHunks()).IsEmpty
                                              ());
        }
Example #21
0
		public virtual void TestEmpty()
		{
			NGit.Patch.Patch p = new NGit.Patch.Patch();
			NUnit.Framework.Assert.IsTrue(p.GetFiles().IsEmpty());
			NUnit.Framework.Assert.IsTrue(p.GetErrors().IsEmpty());
		}
Example #22
0
		/// <exception cref="System.IO.IOException"></exception>
		private NGit.Patch.Patch ParseTestPatchFile(string patchFile)
		{
			InputStream @in = GetType().GetResourceAsStream(patchFile);
			if (@in == null)
			{
				NUnit.Framework.Assert.Fail("No " + patchFile + " test vector");
				return null;
			}
			// Never happens
			try
			{
				NGit.Patch.Patch p = new NGit.Patch.Patch();
				p.Parse(@in);
				return p;
			}
			finally
			{
				@in.Close();
			}
		}
        internal override int ParseBody(NGit.Patch.Patch script, int end)
        {
            byte[] buf = file.buf;
            int    c   = RawParseUtils.NextLF(buf, startOffset);

            foreach (CombinedHunkHeader.CombinedOldImage o in old)
            {
                o.nDeleted = 0;
                o.nAdded   = 0;
                o.nContext = 0;
            }
            nContext = 0;
            int nAdded = 0;

            for (int eol; c < end; c = eol)
            {
                eol = RawParseUtils.NextLF(buf, c);
                if (eol - c < old.Length + 1)
                {
                    // Line isn't long enough to mention the state of each
                    // ancestor. It must be the end of the hunk.
                    goto SCAN_break;
                }
                switch (buf[c])
                {
                case (byte)(' '):
                case (byte)('-'):
                case (byte)('+'):
                {
                    break;
                }

                default:
                {
                    // Line can't possibly be part of this hunk; the first
                    // ancestor information isn't recognizable.
                    //
                    goto SCAN_break;
                    break;
                }
                }
                int localcontext = 0;
                for (int ancestor = 0; ancestor < old.Length; ancestor++)
                {
                    switch (buf[c + ancestor])
                    {
                    case (byte)(' '):
                    {
                        localcontext++;
                        old[ancestor].nContext++;
                        continue;
                        goto case (byte)('-');
                    }

                    case (byte)('-'):
                    {
                        old[ancestor].nDeleted++;
                        continue;
                        goto case (byte)('+');
                    }

                    case (byte)('+'):
                    {
                        old[ancestor].nAdded++;
                        nAdded++;
                        continue;
                        goto default;
                    }

                    default:
                    {
                        goto SCAN_break;
                        break;
                    }
                    }
                }
                if (localcontext == old.Length)
                {
                    nContext++;
                }
                SCAN_continue :;
            }
            SCAN_break :;
            for (int ancestor_1 = 0; ancestor_1 < old.Length; ancestor_1++)
            {
                CombinedHunkHeader.CombinedOldImage o_1 = old[ancestor_1];
                int cmp = o_1.nContext + o_1.nDeleted;
                if (cmp < o_1.lineCount)
                {
                    int missingCnt = o_1.lineCount - cmp;
                    script.Error(buf, startOffset, MessageFormat.Format(JGitText.Get().truncatedHunkLinesMissingForAncestor
                                                                        , Sharpen.Extensions.ValueOf(missingCnt), Sharpen.Extensions.ValueOf(ancestor_1
                                                                                                                                             + 1)));
                }
            }
            if (nContext + nAdded < newLineCount)
            {
                int missingCount = newLineCount - (nContext + nAdded);
                script.Error(buf, startOffset, MessageFormat.Format(JGitText.Get().truncatedHunkNewLinesMissing
                                                                    , Sharpen.Extensions.ValueOf(missingCount)));
            }
            return(c);
        }
Example #24
0
        internal virtual int ParseBody(NGit.Patch.Patch script, int end)
        {
            byte[] buf  = file.buf;
            int    c    = RawParseUtils.NextLF(buf, startOffset);
            int    last = c;

            old.nDeleted = 0;
            old.nAdded   = 0;
            for (; c < end; last = c, c = RawParseUtils.NextLF(buf, c))
            {
                switch (buf[c])
                {
                case (byte)(' '):
                case (byte)('\n'):
                {
                    nContext++;
                    continue;
                    goto case (byte)('-');
                }

                case (byte)('-'):
                {
                    old.nDeleted++;
                    continue;
                    goto case (byte)('+');
                }

                case (byte)('+'):
                {
                    old.nAdded++;
                    continue;
                    goto case (byte)('\\');
                }

                case (byte)('\\'):
                {
                    // Matches "\ No newline at end of file"
                    continue;
                    goto default;
                }

                default:
                {
                    goto SCAN_break;
                    break;
                }
                }
                SCAN_continue :;
            }
            SCAN_break :;
            if (last < end && nContext + old.nDeleted - 1 == old.lineCount && nContext + old.
                nAdded == newLineCount && RawParseUtils.Match(buf, last, NGit.Patch.Patch.SIG_FOOTER
                                                              ) >= 0)
            {
                // This is an extremely common occurrence of "corruption".
                // Users add footers with their signatures after this mark,
                // and git diff adds the git executable version number.
                // Let it slide; the hunk otherwise looked sound.
                //
                old.nDeleted--;
                return(last);
            }
            if (nContext + old.nDeleted < old.lineCount)
            {
                int missingCount = old.lineCount - (nContext + old.nDeleted);
                script.Error(buf, startOffset, MessageFormat.Format(JGitText.Get().truncatedHunkOldLinesMissing
                                                                    , missingCount));
            }
            else
            {
                if (nContext + old.nAdded < newLineCount)
                {
                    int missingCount = newLineCount - (nContext + old.nAdded);
                    script.Error(buf, startOffset, MessageFormat.Format(JGitText.Get().truncatedHunkNewLinesMissing
                                                                        , missingCount));
                }
                else
                {
                    if (nContext + old.nDeleted > old.lineCount || nContext + old.nAdded > newLineCount)
                    {
                        string oldcnt = old.lineCount + ":" + newLineCount;
                        string newcnt = (nContext + old.nDeleted) + ":" + (nContext + old.nAdded);
                        script.Warn(buf, startOffset, MessageFormat.Format(JGitText.Get().hunkHeaderDoesNotMatchBodyLineCountOf
                                                                           , oldcnt, newcnt));
                    }
                }
            }
            return(c);
        }
Example #25
0
        /// <summary>
        /// Executes the
        /// <code>ApplyCommand</code>
        /// command with all the options and
        /// parameters collected by the setter methods (e.g.
        /// <see cref="SetPatch(Sharpen.InputStream)">SetPatch(Sharpen.InputStream)</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>
        /// an
        /// <see cref="ApplyResult">ApplyResult</see>
        /// object representing the command result
        /// </returns>
        /// <exception cref="NGit.Api.Errors.GitAPIException">NGit.Api.Errors.GitAPIException
        ///     </exception>
        /// <exception cref="NGit.Api.Errors.PatchFormatException">NGit.Api.Errors.PatchFormatException
        ///     </exception>
        /// <exception cref="NGit.Api.Errors.PatchApplyException">NGit.Api.Errors.PatchApplyException
        ///     </exception>
        public override ApplyResult Call()
        {
            CheckCallable();
            ApplyResult r = new ApplyResult();

            try
            {
                NGit.Patch.Patch p = new NGit.Patch.Patch();
                try
                {
                    p.Parse(@in);
                }
                finally
                {
                    @in.Close();
                }
                if (!p.GetErrors().IsEmpty())
                {
                    throw new PatchFormatException(p.GetErrors());
                }
                foreach (FileHeader fh in p.GetFiles())
                {
                    DiffEntry.ChangeType type = fh.GetChangeType();
                    FilePath             f    = null;
                    switch (type)
                    {
                    case DiffEntry.ChangeType.ADD:
                    {
                        f = GetFile(fh.GetNewPath(), true);
                        Apply(f, fh);
                        break;
                    }

                    case DiffEntry.ChangeType.MODIFY:
                    {
                        f = GetFile(fh.GetOldPath(), false);
                        Apply(f, fh);
                        break;
                    }

                    case DiffEntry.ChangeType.DELETE:
                    {
                        f = GetFile(fh.GetOldPath(), false);
                        if (!f.Delete())
                        {
                            throw new PatchApplyException(MessageFormat.Format(JGitText.Get().cannotDeleteFile
                                                                               , f));
                        }
                        break;
                    }

                    case DiffEntry.ChangeType.RENAME:
                    {
                        f = GetFile(fh.GetOldPath(), false);
                        FilePath dest = GetFile(fh.GetNewPath(), false);
                        if (!f.RenameTo(dest))
                        {
                            throw new PatchApplyException(MessageFormat.Format(JGitText.Get().renameFileFailed
                                                                               , f, dest));
                        }
                        break;
                    }

                    case DiffEntry.ChangeType.COPY:
                    {
                        f = GetFile(fh.GetOldPath(), false);
                        byte[]     bs = IOUtil.ReadFully(f);
                        FileWriter fw = new FileWriter(GetFile(fh.GetNewPath(), true));
                        fw.Write(Sharpen.Runtime.GetStringForBytes(bs));
                        fw.Close();
                        break;
                    }
                    }
                    r.AddUpdatedFile(f);
                }
            }
            catch (IOException e)
            {
                throw new PatchApplyException(MessageFormat.Format(JGitText.Get().patchApplyException
                                                                   , e.Message), e);
            }
            SetCallable(false);
            return(r);
        }
        public virtual void TestParse_ConfigCaseInsensitive()
        {
            NGit.Patch.Patch p = ParseTestPatchFile();
            NUnit.Framework.Assert.AreEqual(2, p.GetFiles().Count);
            NUnit.Framework.Assert.IsTrue(p.GetErrors().IsEmpty());
            FileHeader fRepositoryConfigTest = p.GetFiles()[0];
            FileHeader fRepositoryConfig     = p.GetFiles()[1];

            NUnit.Framework.Assert.AreEqual("org.eclipse.jgit.test/tst/org/spearce/jgit/lib/RepositoryConfigTest.java"
                                            , fRepositoryConfigTest.GetNewPath());
            NUnit.Framework.Assert.AreEqual("org.eclipse.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java"
                                            , fRepositoryConfig.GetNewPath());
            NUnit.Framework.Assert.AreEqual(572, fRepositoryConfigTest.startOffset);
            NUnit.Framework.Assert.AreEqual(1490, fRepositoryConfig.startOffset);
            NUnit.Framework.Assert.AreEqual("da7e704", fRepositoryConfigTest.GetOldId().Name);
            NUnit.Framework.Assert.AreEqual("34ce04a", fRepositoryConfigTest.GetNewId().Name);
            NUnit.Framework.Assert.AreEqual(FileHeader.PatchType.UNIFIED, fRepositoryConfigTest
                                            .GetPatchType());
            NUnit.Framework.Assert.AreSame(FileMode.REGULAR_FILE, fRepositoryConfigTest.GetOldMode
                                               ());
            NUnit.Framework.Assert.AreSame(FileMode.REGULAR_FILE, fRepositoryConfigTest.GetNewMode
                                               ());
            NUnit.Framework.Assert.AreEqual(1, fRepositoryConfigTest.GetHunks().Count);
            {
                HunkHeader h = fRepositoryConfigTest.GetHunks()[0];
                NUnit.Framework.Assert.AreSame(fRepositoryConfigTest, h.GetFileHeader());
                NUnit.Framework.Assert.AreEqual(921, h.startOffset);
                NUnit.Framework.Assert.AreEqual(109, h.GetOldImage().GetStartLine());
                NUnit.Framework.Assert.AreEqual(4, h.GetOldImage().GetLineCount());
                NUnit.Framework.Assert.AreEqual(109, h.GetNewStartLine());
                NUnit.Framework.Assert.AreEqual(11, h.GetNewLineCount());
                NUnit.Framework.Assert.AreEqual(4, h.GetLinesContext());
                NUnit.Framework.Assert.AreEqual(7, h.GetOldImage().GetLinesAdded());
                NUnit.Framework.Assert.AreEqual(0, h.GetOldImage().GetLinesDeleted());
                NUnit.Framework.Assert.AreSame(fRepositoryConfigTest.GetOldId(), h.GetOldImage().
                                               GetId());
                NUnit.Framework.Assert.AreEqual(1490, h.endOffset);
            }
            NUnit.Framework.Assert.AreEqual("45c2f8a", fRepositoryConfig.GetOldId().Name);
            NUnit.Framework.Assert.AreEqual("3291bba", fRepositoryConfig.GetNewId().Name);
            NUnit.Framework.Assert.AreEqual(FileHeader.PatchType.UNIFIED, fRepositoryConfig.GetPatchType
                                                ());
            NUnit.Framework.Assert.AreSame(FileMode.REGULAR_FILE, fRepositoryConfig.GetOldMode
                                               ());
            NUnit.Framework.Assert.AreSame(FileMode.REGULAR_FILE, fRepositoryConfig.GetNewMode
                                               ());
            NUnit.Framework.Assert.AreEqual(3, fRepositoryConfig.GetHunks().Count);
            {
                HunkHeader h = fRepositoryConfig.GetHunks()[0];
                NUnit.Framework.Assert.AreSame(fRepositoryConfig, h.GetFileHeader());
                NUnit.Framework.Assert.AreEqual(1803, h.startOffset);
                NUnit.Framework.Assert.AreEqual(236, h.GetOldImage().GetStartLine());
                NUnit.Framework.Assert.AreEqual(9, h.GetOldImage().GetLineCount());
                NUnit.Framework.Assert.AreEqual(236, h.GetNewStartLine());
                NUnit.Framework.Assert.AreEqual(9, h.GetNewLineCount());
                NUnit.Framework.Assert.AreEqual(7, h.GetLinesContext());
                NUnit.Framework.Assert.AreEqual(2, h.GetOldImage().GetLinesAdded());
                NUnit.Framework.Assert.AreEqual(2, h.GetOldImage().GetLinesDeleted());
                NUnit.Framework.Assert.AreSame(fRepositoryConfig.GetOldId(), h.GetOldImage().GetId
                                                   ());
                NUnit.Framework.Assert.AreEqual(2434, h.endOffset);
            }
            {
                HunkHeader h = fRepositoryConfig.GetHunks()[1];
                NUnit.Framework.Assert.AreEqual(2434, h.startOffset);
                NUnit.Framework.Assert.AreEqual(300, h.GetOldImage().GetStartLine());
                NUnit.Framework.Assert.AreEqual(7, h.GetOldImage().GetLineCount());
                NUnit.Framework.Assert.AreEqual(300, h.GetNewStartLine());
                NUnit.Framework.Assert.AreEqual(7, h.GetNewLineCount());
                NUnit.Framework.Assert.AreEqual(6, h.GetLinesContext());
                NUnit.Framework.Assert.AreEqual(1, h.GetOldImage().GetLinesAdded());
                NUnit.Framework.Assert.AreEqual(1, h.GetOldImage().GetLinesDeleted());
                NUnit.Framework.Assert.AreEqual(2816, h.endOffset);
            }
            {
                HunkHeader h = fRepositoryConfig.GetHunks()[2];
                NUnit.Framework.Assert.AreEqual(2816, h.startOffset);
                NUnit.Framework.Assert.AreEqual(954, h.GetOldImage().GetStartLine());
                NUnit.Framework.Assert.AreEqual(7, h.GetOldImage().GetLineCount());
                NUnit.Framework.Assert.AreEqual(954, h.GetNewStartLine());
                NUnit.Framework.Assert.AreEqual(7, h.GetNewLineCount());
                NUnit.Framework.Assert.AreEqual(6, h.GetLinesContext());
                NUnit.Framework.Assert.AreEqual(1, h.GetOldImage().GetLinesAdded());
                NUnit.Framework.Assert.AreEqual(1, h.GetOldImage().GetLinesDeleted());
                NUnit.Framework.Assert.AreEqual(3035, h.endOffset);
            }
        }
 public virtual void TestEmpty()
 {
     NGit.Patch.Patch p = new NGit.Patch.Patch();
     NUnit.Framework.Assert.IsTrue(p.GetFiles().IsEmpty());
     NUnit.Framework.Assert.IsTrue(p.GetErrors().IsEmpty());
 }
Example #28
0
 private static Patch Parse(InputStream crlfStream)
 {
     NGit.Patch.Patch p = new Patch();
     p.Parse(crlfStream);
     return(p);
 }