protected static GitSharp.Patch.Patch ParseTestPatchFile(string patchFile) { try { using (var inStream = new FileStream(patchFile, System.IO.FileMode.Open)) { var p = new GitSharp.Patch.Patch(); p.parse(inStream); return p; } } catch(IOException) { Assert.Fail("No " + patchFile + " test vector"); return null; // Never happens } }
public void testEmpty() { GitSharp.Patch.Patch p = new GitSharp.Patch.Patch(); Assert.IsTrue(p.getFiles().Count == 0); Assert.IsTrue(p.getErrors().Count == 0); }
public virtual int parseBody(Patch script, int end) { byte[] buf = _file.Buffer; int c = RawParseUtils.nextLF(buf, _startOffset), last = c; _oldImage.LinesDeleted = 0; _oldImage.LinesAdded = 0; for (; c < end; last = c, c = RawParseUtils.nextLF(buf, c)) { bool breakScan; switch (buf[c]) { case (byte)' ': case (byte)'\n': LinesContext++; continue; case (byte)'-': _oldImage.LinesDeleted++; continue; case (byte)'+': _oldImage.LinesAdded++; continue; case (byte)'\\': // Matches "\ No newline at end of file" continue; default: breakScan = true; break; } if (breakScan) { break; } } if (last < end && LinesContext + _oldImage.LinesDeleted - 1 == _oldImage.LineCount && LinesContext + _oldImage.LinesAdded == NewLineCount && RawParseUtils.match(buf, last, Patch.SigFooter) >= 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. // _oldImage.LinesDeleted--; return last; } if (LinesContext + _oldImage.LinesDeleted < _oldImage.LineCount) { int missingCount = _oldImage.LineCount - (LinesContext + _oldImage.LinesDeleted); script.error(buf, _startOffset, "Truncated hunk, at least " + missingCount + " old lines is missing"); } else if (LinesContext + _oldImage.LinesAdded < NewLineCount) { int missingCount = NewLineCount - (LinesContext + _oldImage.LinesAdded); script.error(buf, _startOffset, "Truncated hunk, at least " + missingCount + " new lines is missing"); } else if (LinesContext + _oldImage.LinesDeleted > _oldImage.LineCount || LinesContext + _oldImage.LinesAdded > NewLineCount) { string oldcnt = _oldImage.LineCount + ":" + NewLineCount; string newcnt = (LinesContext + _oldImage.LinesDeleted) + ":" + (LinesContext + _oldImage.LinesAdded); script.warn(buf, _startOffset, "Hunk header " + oldcnt + " does not match body line count of " + newcnt); } return c; }
public int parseBody(Patch script, int end) { byte[] buf = file.buf; int c = RawParseUtils.nextLF(buf, startOffset), last = c; old.nDeleted = 0; old.nAdded = 0; bool break_scan = false; for (; c < end; last = c, c = RawParseUtils.nextLF(buf, c)) { switch (buf[c]) { case (byte)' ': case (byte)'\n': nContext++; continue; case (byte)'-': old.nDeleted++; continue; case (byte)'+': old.nAdded++; continue; case (byte)'\\': // Matches "\ No newline at end of file" continue; default: break_scan = true; break; } if (break_scan) break; } if (last < end && nContext + old.nDeleted - 1 == old.lineCount && nContext + old.nAdded == newLineCount && RawParseUtils.match(buf, last, 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, "Truncated hunk, at least " + missingCount + " old lines is missing"); } else if (nContext + old.nAdded < newLineCount) { int missingCount = newLineCount - (nContext + old.nAdded); script.error(buf, startOffset, "Truncated hunk, at least " + missingCount + " new lines is missing"); } 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, "Hunk header " + oldcnt + " does not match body line count of " + newcnt); } return c; }
public override int parseBody(Patch script, int end) { byte[] buf = File.Buffer; int c = RawParseUtils.nextLF(buf, StartOffset); _old.ForEach(coi => { coi.LinesAdded = 0; coi.LinesDeleted = 0; coi.LinesContext = 0; }); LinesContext = 0; int nAdded = 0; for (int eol; c < end; c = eol) { eol = RawParseUtils.nextLF(buf, c); if (eol - c < _old.Count + 1) { // Line isn't long enough to mention the state of each // ancestor. It must be the end of the hunk. break; } bool break_scan = false; 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. // break_scan = true; break; } if (break_scan) break; int localcontext = 0; for (int ancestor = 0; ancestor < _old.Count; ancestor++) { switch (buf[c + ancestor]) { case (byte)' ': localcontext++; _old[ancestor].LinesContext++; continue; case (byte)'-': _old[ancestor].LinesDeleted++; continue; case (byte)'+': _old[ancestor].LinesAdded++; nAdded++; continue; default: break_scan = true; break; } if (break_scan) break; } if (break_scan) break; if (localcontext == _old.Count) { LinesContext++; } } for (int ancestor = 0; ancestor < _old.Count; ancestor++) { CombinedOldImage o = _old[ancestor]; int cmp = o.LinesContext + o.LinesDeleted; if (cmp < o.LineCount) { int missingCnt = o.LineCount - cmp; script.error(buf, StartOffset, "Truncated hunk, at least " + missingCnt + " lines is missing for ancestor " + (ancestor + 1)); } } if (LinesContext + nAdded < NewLineCount) { int missingCount = NewLineCount - (LinesContext + nAdded); script.error(buf, StartOffset, "Truncated hunk, at least " + missingCount + " new lines is missing"); } return c; }