/// <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]; }
private int ParseHunks(FileHeader fh, int c, int end) { byte[] buf = fh.buf; while (c < end) { // If we see a file header at this point, we have all of the // hunks for our current file. We should stop and report back // with this position so it can be parsed again later. // if (RawParseUtils.Match(buf, c, DIFF_GIT) >= 0) { break; } if (RawParseUtils.Match(buf, c, DIFF_CC) >= 0) { break; } if (RawParseUtils.Match(buf, c, DIFF_COMBINED) >= 0) { break; } if (RawParseUtils.Match(buf, c, FileHeader.OLD_NAME) >= 0) { break; } if (RawParseUtils.Match(buf, c, FileHeader.NEW_NAME) >= 0) { break; } if (FileHeader.IsHunkHdr(buf, c, end) == fh.GetParentCount()) { HunkHeader h = fh.NewHunkHeader(c); h.ParseHeader(); c = h.ParseBody(this, end); h.endOffset = c; fh.AddHunk(h); if (c < end) { switch (buf[c]) { case (byte)('@'): case (byte)('d'): case (byte)('\n'): { break; } default: { if (RawParseUtils.Match(buf, c, SIG_FOOTER) < 0 && RawParseUtils.Match(buf, c, SIG_FOOTER_WINDOWS) < 0) { Warn(buf, c, JGitText.Get().unexpectedHunkTrailer); } break; } } } continue; } int eol = RawParseUtils.NextLF(buf, c); if (fh.GetHunks().IsEmpty() && (RawParseUtils.Match(buf, c, GIT_BINARY) >= 0 || RawParseUtils.Match(buf, c, GIT_BINARY_WINDOWS) >= 0)) { fh.patchType = FileHeader.PatchType.GIT_BINARY; return ParseGitBinary(fh, eol, end); } if (fh.GetHunks().IsEmpty() && ((BIN_TRAILER.Length < eol - c && RawParseUtils.Match(buf, eol - BIN_TRAILER.Length, BIN_TRAILER) >= 0 ) || (BIN_TRAILER_WINDOWS.Length < eol - c && RawParseUtils.Match(buf, eol - BIN_TRAILER_WINDOWS.Length, BIN_TRAILER_WINDOWS) >= 0)) && MatchAny(buf, c, BIN_HEADERS )) { // The patch is a binary file diff, with no deltas. // fh.patchType = FileHeader.PatchType.BINARY; return eol; } // Skip this line and move to the next. Its probably garbage // after the last hunk of a file. // c = eol; } if (fh.GetHunks().IsEmpty() && fh.GetPatchType() == FileHeader.PatchType.UNIFIED && !fh.HasMetaDataChanges()) { // Hmm, an empty patch? If there is no metadata here we // really have a binary patch that we didn't notice above. // fh.patchType = FileHeader.PatchType.BINARY; } return c; }
private int ParseTraditionalPatch(byte[] buf, int start, int end) { FileHeader fh = new FileHeader(buf, start); int ptr = fh.ParseTraditionalHeaders(start, end); ptr = ParseHunks(fh, ptr, end); fh.endOffset = ptr; AddFile(fh); return ptr; }
public _OldImage_122(FileHeader fh) { this.fh = fh; }
private int ParseGitBinary(FileHeader fh, int c, int end) { BinaryHunk postImage = new BinaryHunk(fh, c); int nEnd = postImage.ParseHunk(c, end); if (nEnd < 0) { // Not a binary hunk. // Error(fh.buf, c, JGitText.Get().missingForwardImageInGITBinaryPatch); return c; } c = nEnd; postImage.endOffset = c; fh.forwardBinaryHunk = postImage; BinaryHunk preImage = new BinaryHunk(fh, c); int oEnd = preImage.ParseHunk(c, end); if (oEnd >= 0) { c = oEnd; preImage.endOffset = c; fh.reverseBinaryHunk = preImage; } return c; }
/// <summary>Constructs a new FileHeader</summary> /// <param name="headerLines">buffer holding the diff header for this file</param> /// <param name="edits">the edits for this file</param> /// <param name="type">the type of patch used to modify this file</param> public FileHeader(byte[] headerLines, EditList edits, FileHeader.PatchType type) : this(headerLines, 0) { endOffset = headerLines.Length; int ptr = ParseGitFileName(NGit.Patch.Patch.DIFF_GIT.Length, headerLines.Length); ParseGitHeaders(ptr, headerLines.Length); this.patchType = type; AddHunk(new HunkHeader(this, edits)); }
private int ParseFile(byte[] buf, int c, int end) { while (c < end) { if (FileHeader.IsHunkHdr(buf, c, end) >= 1) { // If we find a disconnected hunk header we might // have missed a file header previously. The hunk // isn't valid without knowing where it comes from. // Error(buf, c, JGitText.Get().hunkDisconnectedFromFile); c = RawParseUtils.NextLF(buf, c); continue; } // Valid git style patch? // if (RawParseUtils.Match(buf, c, DIFF_GIT) >= 0) { return(ParseDiffGit(buf, c, end)); } if (RawParseUtils.Match(buf, c, DIFF_CC) >= 0) { return(ParseDiffCombined(DIFF_CC, buf, c, end)); } if (RawParseUtils.Match(buf, c, DIFF_COMBINED) >= 0) { return(ParseDiffCombined(DIFF_COMBINED, buf, c, end)); } // Junk between files? Leading junk? Traditional // (non-git generated) patch? // int n = RawParseUtils.NextLF(buf, c); if (n >= end) { // Patches cannot be only one line long. This must be // trailing junk that we should ignore. // return(end); } if (n - c < 6) { // A valid header must be at least 6 bytes on the // first line, e.g. "--- a/b\n". // c = n; continue; } if (RawParseUtils.Match(buf, c, FileHeader.OLD_NAME) >= 0 && RawParseUtils.Match( buf, n, FileHeader.NEW_NAME) >= 0) { // Probably a traditional patch. Ensure we have at least // a "@@ -0,0" smelling line next. We only check the "@@ -". // int f = RawParseUtils.NextLF(buf, n); if (f >= end) { return(end); } if (FileHeader.IsHunkHdr(buf, f, end) == 1) { return(ParseTraditionalPatch(buf, c, end)); } } c = n; } return(c); }
private int ParseHunks(FileHeader fh, int c, int end) { byte[] buf = fh.buf; while (c < end) { // If we see a file header at this point, we have all of the // hunks for our current file. We should stop and report back // with this position so it can be parsed again later. // if (RawParseUtils.Match(buf, c, DIFF_GIT) >= 0) { break; } if (RawParseUtils.Match(buf, c, DIFF_CC) >= 0) { break; } if (RawParseUtils.Match(buf, c, DIFF_COMBINED) >= 0) { break; } if (RawParseUtils.Match(buf, c, FileHeader.OLD_NAME) >= 0) { break; } if (RawParseUtils.Match(buf, c, FileHeader.NEW_NAME) >= 0) { break; } if (FileHeader.IsHunkHdr(buf, c, end) == fh.GetParentCount()) { HunkHeader h = fh.NewHunkHeader(c); h.ParseHeader(); c = h.ParseBody(this, end); h.endOffset = c; fh.AddHunk(h); if (c < end) { switch (buf[c]) { case (byte)('@'): case (byte)('d'): case (byte)('\n'): { break; } default: { if (RawParseUtils.Match(buf, c, SIG_FOOTER) < 0) { Warn(buf, c, JGitText.Get().unexpectedHunkTrailer); } break; } } } continue; } int eol = RawParseUtils.NextLF(buf, c); if (fh.GetHunks().IsEmpty() && RawParseUtils.Match(buf, c, GIT_BINARY) >= 0) { fh.patchType = FileHeader.PatchType.GIT_BINARY; return(ParseGitBinary(fh, eol, end)); } if (fh.GetHunks().IsEmpty() && BIN_TRAILER.Length < eol - c && RawParseUtils.Match (buf, eol - BIN_TRAILER.Length, BIN_TRAILER) >= 0 && MatchAny(buf, c, BIN_HEADERS )) { // The patch is a binary file diff, with no deltas. // fh.patchType = FileHeader.PatchType.BINARY; return(eol); } // Skip this line and move to the next. Its probably garbage // after the last hunk of a file. // c = eol; } if (fh.GetHunks().IsEmpty() && fh.GetPatchType() == FileHeader.PatchType.UNIFIED && !fh.HasMetaDataChanges()) { // Hmm, an empty patch? If there is no metadata here we // really have a binary patch that we didn't notice above. // fh.patchType = FileHeader.PatchType.BINARY; } return(c); }
public virtual void TestParse_ConfigCaseInsensitive() { NGit.Patch.Patch p = ParseTestPatchFile(); NUnit.Framework.Assert.AreEqual(2, p.GetFiles().Count); NUnit.Framework.Assert.That(p.GetErrors(), Is.Empty); 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(m_Crlf ? 586 : 572, fRepositoryConfigTest.startOffset); NUnit.Framework.Assert.AreEqual(m_Crlf ? 1520 : 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(m_Crlf ? 939 : 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(m_Crlf ? 1520 : 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(m_Crlf ? 1837 : 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(m_Crlf ? 2480 : 2434, h.endOffset); } { HunkHeader h = fRepositoryConfig.GetHunks()[1]; NUnit.Framework.Assert.AreEqual(m_Crlf ? 2480 : 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(m_Crlf ? 2871 : 2816, h.endOffset); } { HunkHeader h = fRepositoryConfig.GetHunks()[2]; NUnit.Framework.Assert.AreEqual(m_Crlf ? 2871 : 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(m_Crlf ? 3099 : 3035, h.endOffset); } }
/// <summary>Add a single file to this patch.</summary> /// <remarks> /// Add a single file to this patch. /// <p> /// Typically files should be added by parsing the text through one of this /// class's parse methods. /// </remarks> /// <param name="fh">the header of the file.</param> public virtual void AddFile(FileHeader fh) { files.AddItem(fh); }
public virtual void TestParseGitFileName_EmptyHeader() { FileHeader fh = Data("\n\n"); NUnit.Framework.Assert.AreEqual(1, fh.ParseGitFileName(0, fh.buf.Length)); }
public virtual void TestParseGitFileName_NoSecondLine() { FileHeader fh = Data("\n"); NUnit.Framework.Assert.AreEqual(-1, fh.ParseGitFileName(0, fh.buf.Length)); }
internal HunkHeader(FileHeader fh, int offset, HunkHeader.OldImage oi) { file = fh; startOffset = offset; old = oi; }
internal BinaryHunk(FileHeader fh, int offset) { file = fh; startOffset = offset; }
internal HunkHeader(FileHeader fh, EditList editList) : this(fh, fh.buf.Length) { this.editList = editList; endOffset = startOffset; nContext = 0; if (editList.IsEmpty()) { newStartLine = 0; newLineCount = 0; } else { newStartLine = editList[0].GetBeginB(); Edit last = editList[editList.Count - 1]; newLineCount = last.GetEndB() - newStartLine; } }
internal HunkHeader(FileHeader fh, int offset) : this(fh, offset, new _OldImage_122 (fh)) { }
private int ParseDiffGit(byte[] buf, int start, int end) { FileHeader fh = new FileHeader(buf, start); int ptr = fh.ParseGitFileName(start + DIFF_GIT.Length, end); if (ptr < 0) { return SkipFile(buf, start); } ptr = fh.ParseGitHeaders(ptr, end); ptr = ParseHunks(fh, ptr, end); fh.endOffset = ptr; AddFile(fh); return ptr; }
private static void AssertParse(FileHeader fh) { int ptr = fh.ParseGitFileName(0, fh.buf.Length); NUnit.Framework.Assert.IsTrue(ptr > 0); ptr = fh.ParseGitHeaders(ptr, fh.buf.Length); NUnit.Framework.Assert.IsTrue(ptr > 0); }