/// <param name="f"></param> /// <param name="fh"></param> /// <exception cref="System.IO.IOException">System.IO.IOException</exception> /// <exception cref="NGit.Api.Errors.PatchApplyException">NGit.Api.Errors.PatchApplyException /// </exception> private void Apply(FilePath f, FileHeader fh) { RawText rt = new RawText(f); IList <string> oldLines = new AList <string>(rt.Size()); for (int i = 0; i < rt.Size(); i++) { oldLines.AddItem(rt.GetString(i)); } IList <string> newLines = new AList <string>(oldLines); foreach (HunkHeader hh in fh.GetHunks()) { StringBuilder hunk = new StringBuilder(); for (int j = hh.GetStartOffset(); j < hh.GetEndOffset(); j++) { hunk.Append((char)hh.GetBuffer()[j]); } RawText hrt = new RawText(Sharpen.Runtime.GetBytesForString(hunk.ToString())); IList <string> hunkLines = new AList <string>(hrt.Size()); for (int i_1 = 0; i_1 < hrt.Size(); i_1++) { hunkLines.AddItem(hrt.GetString(i_1)); } int pos = 0; for (int j_1 = 1; j_1 < hunkLines.Count; j_1++) { string hunkLine = hunkLines[j_1]; switch (hunkLine[0]) { case ' ': { if (!newLines[hh.GetNewStartLine() - 1 + pos].Equals(Sharpen.Runtime.Substring(hunkLine , 1))) { throw new PatchApplyException(MessageFormat.Format(JGitText.Get().patchApplyException , hh)); } pos++; break; } case '-': { if (!newLines[hh.GetNewStartLine() - 1 + pos].Equals(Sharpen.Runtime.Substring(hunkLine , 1))) { throw new PatchApplyException(MessageFormat.Format(JGitText.Get().patchApplyException , hh)); } newLines.Remove(hh.GetNewStartLine() - 1 + pos); break; } case '+': { newLines.Add(hh.GetNewStartLine() - 1 + pos, Sharpen.Runtime.Substring(hunkLine, 1)); pos++; break; } } } } if (!IsNoNewlineAtEndOfFile(fh)) { newLines.AddItem(string.Empty); } if (!rt.IsMissingNewlineAtEnd()) { oldLines.AddItem(string.Empty); } if (!IsChanged(oldLines, newLines)) { return; } // don't touch the file StringBuilder sb = new StringBuilder(); string eol = rt.Size() == 0 || (rt.Size() == 1 && rt.IsMissingNewlineAtEnd()) ? "\n" : rt.GetLineDelimiter(); foreach (string l in newLines) { sb.Append(l); if (eol != null) { sb.Append(eol); } } Sharpen.Runtime.DeleteCharAt(sb, sb.Length - 1); FileWriter fw = new FileWriter(f); fw.Write(sb.ToString()); fw.Close(); }
/// <param name="f"></param> /// <param name="fh"></param> /// <exception cref="System.IO.IOException">System.IO.IOException</exception> /// <exception cref="NGit.Api.Errors.PatchApplyException">NGit.Api.Errors.PatchApplyException /// </exception> private void Apply(FilePath f, FileHeader fh) { RawText rt = new RawText(f); IList <string> oldLines = new AList <string>(rt.Size()); for (int i = 0; i < rt.Size(); i++) { oldLines.AddItem(rt.GetString(i)); } IList <string> newLines = new AList <string>(oldLines); RawText hrt = null; var containedCRLF = false; foreach (HunkHeader hh in fh.GetHunks()) { var buffer = Sharpen.Runtime.GetStringForBytes(hh.GetBuffer(), hh.GetStartOffset(), hh.GetEndOffset() - hh.GetStartOffset()); if (!containedCRLF) { containedCRLF |= buffer.Contains("\r\n"); } hrt = new RawText(Sharpen.Runtime.GetBytesForString(buffer)); IList <string> hunkLines = new AList <string>(hrt.Size()); for (int i_1 = 0; i_1 < hrt.Size(); i_1++) { hunkLines.AddItem(hrt.GetString(i_1)); } int pos = 0; for (int j_1 = 1; j_1 < hunkLines.Count; j_1++) { string hunkLine = hunkLines[j_1]; switch (hunkLine[0]) { case ' ': { if (!newLines[hh.GetNewStartLine() - 1 + pos].Equals(Sharpen.Runtime.Substring(hunkLine , 1))) { throw new PatchApplyException(MessageFormat.Format(JGitText.Get().patchApplyException , hh), new PatchApplyModifiedException(f.GetAbsolutePath(), buffer, hunkLine, newLines[hh.GetNewStartLine() - 1 + pos])); } pos++; break; } case '-': { var index = Math.Max(hh.GetNewStartLine() - 1 + pos, 0); if (!newLines[index].Equals(Sharpen.Runtime.Substring(hunkLine , 1))) { throw new PatchApplyException(MessageFormat.Format(JGitText.Get().patchApplyException , hh), new PatchApplyModifiedException(f.GetAbsolutePath(), buffer, hunkLine, newLines[index])); } newLines.Remove(index); break; } case '+': { newLines.Add(hh.GetNewStartLine() - 1 + pos, Sharpen.Runtime.Substring(hunkLine, 1)); pos++; break; } } } } if (!IsNoNewlineAtEndOfFile(hrt)) { newLines.AddItem(string.Empty); } if (!rt.IsMissingNewlineAtEnd()) { oldLines.AddItem(string.Empty); } if (!IsChanged(oldLines, newLines)) { return; } // don't touch the file StringBuilder sb = new StringBuilder(); string eol = rt.Size() == 0 || (rt.Size() == 1 && rt.IsMissingNewlineAtEnd()) ? (containedCRLF ? "\r\n" : "\n") : rt.GetLineDelimiter(); for (int index = 0; index < newLines.Count; index++) { sb.Append(newLines[index]); var moreLinesToCome = index < newLines.Count - 1; if (moreLinesToCome && eol != null) { sb.Append(eol); } } FileWriter fw = new FileWriter(f); fw.Write(sb.ToString()); fw.Close(); }