You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Patch.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.patch;
  44. import static org.eclipse.jgit.lib.Constants.encodeASCII;
  45. import static org.eclipse.jgit.patch.FileHeader.NEW_NAME;
  46. import static org.eclipse.jgit.patch.FileHeader.OLD_NAME;
  47. import static org.eclipse.jgit.patch.FileHeader.isHunkHdr;
  48. import static org.eclipse.jgit.util.RawParseUtils.match;
  49. import static org.eclipse.jgit.util.RawParseUtils.nextLF;
  50. import java.io.IOException;
  51. import java.io.InputStream;
  52. import java.util.ArrayList;
  53. import java.util.List;
  54. import org.eclipse.jgit.internal.JGitText;
  55. import org.eclipse.jgit.util.TemporaryBuffer;
  56. /** A parsed collection of {@link FileHeader}s from a unified diff patch file */
  57. public class Patch {
  58. static final byte[] DIFF_GIT = encodeASCII("diff --git "); //$NON-NLS-1$
  59. private static final byte[] DIFF_CC = encodeASCII("diff --cc "); //$NON-NLS-1$
  60. private static final byte[] DIFF_COMBINED = encodeASCII("diff --combined "); //$NON-NLS-1$
  61. private static final byte[][] BIN_HEADERS = new byte[][] {
  62. encodeASCII("Binary files "), encodeASCII("Files "), }; //$NON-NLS-1$ //$NON-NLS-2$
  63. private static final byte[] BIN_TRAILER = encodeASCII(" differ\n"); //$NON-NLS-1$
  64. private static final byte[] GIT_BINARY = encodeASCII("GIT binary patch\n"); //$NON-NLS-1$
  65. static final byte[] SIG_FOOTER = encodeASCII("-- \n"); //$NON-NLS-1$
  66. /** The files, in the order they were parsed out of the input. */
  67. private final List<FileHeader> files;
  68. /** Formatting errors, if any were identified. */
  69. private final List<FormatError> errors;
  70. /** Create an empty patch. */
  71. public Patch() {
  72. files = new ArrayList<FileHeader>();
  73. errors = new ArrayList<FormatError>(0);
  74. }
  75. /**
  76. * Add a single file to this patch.
  77. * <p>
  78. * Typically files should be added by parsing the text through one of this
  79. * class's parse methods.
  80. *
  81. * @param fh
  82. * the header of the file.
  83. */
  84. public void addFile(final FileHeader fh) {
  85. files.add(fh);
  86. }
  87. /** @return list of files described in the patch, in occurrence order. */
  88. public List<? extends FileHeader> getFiles() {
  89. return files;
  90. }
  91. /**
  92. * Add a formatting error to this patch script.
  93. *
  94. * @param err
  95. * the error description.
  96. */
  97. public void addError(final FormatError err) {
  98. errors.add(err);
  99. }
  100. /** @return collection of formatting errors, if any. */
  101. public List<FormatError> getErrors() {
  102. return errors;
  103. }
  104. /**
  105. * Parse a patch received from an InputStream.
  106. * <p>
  107. * Multiple parse calls on the same instance will concatenate the patch
  108. * data, but each parse input must start with a valid file header (don't
  109. * split a single file across parse calls).
  110. *
  111. * @param is
  112. * the stream to read the patch data from. The stream is read
  113. * until EOF is reached.
  114. * @throws IOException
  115. * there was an error reading from the input stream.
  116. */
  117. public void parse(final InputStream is) throws IOException {
  118. final byte[] buf = readFully(is);
  119. parse(buf, 0, buf.length);
  120. }
  121. private static byte[] readFully(final InputStream is) throws IOException {
  122. TemporaryBuffer b = new TemporaryBuffer.Heap(Integer.MAX_VALUE);
  123. b.copy(is);
  124. b.close();
  125. return b.toByteArray();
  126. }
  127. /**
  128. * Parse a patch stored in a byte[].
  129. * <p>
  130. * Multiple parse calls on the same instance will concatenate the patch
  131. * data, but each parse input must start with a valid file header (don't
  132. * split a single file across parse calls).
  133. *
  134. * @param buf
  135. * the buffer to parse.
  136. * @param ptr
  137. * starting position to parse from.
  138. * @param end
  139. * 1 past the last position to end parsing. The total length to
  140. * be parsed is <code>end - ptr</code>.
  141. */
  142. public void parse(final byte[] buf, int ptr, final int end) {
  143. while (ptr < end)
  144. ptr = parseFile(buf, ptr, end);
  145. }
  146. private int parseFile(final byte[] buf, int c, final int end) {
  147. while (c < end) {
  148. if (isHunkHdr(buf, c, end) >= 1) {
  149. // If we find a disconnected hunk header we might
  150. // have missed a file header previously. The hunk
  151. // isn't valid without knowing where it comes from.
  152. //
  153. error(buf, c, JGitText.get().hunkDisconnectedFromFile);
  154. c = nextLF(buf, c);
  155. continue;
  156. }
  157. // Valid git style patch?
  158. //
  159. if (match(buf, c, DIFF_GIT) >= 0)
  160. return parseDiffGit(buf, c, end);
  161. if (match(buf, c, DIFF_CC) >= 0)
  162. return parseDiffCombined(DIFF_CC, buf, c, end);
  163. if (match(buf, c, DIFF_COMBINED) >= 0)
  164. return parseDiffCombined(DIFF_COMBINED, buf, c, end);
  165. // Junk between files? Leading junk? Traditional
  166. // (non-git generated) patch?
  167. //
  168. final int n = nextLF(buf, c);
  169. if (n >= end) {
  170. // Patches cannot be only one line long. This must be
  171. // trailing junk that we should ignore.
  172. //
  173. return end;
  174. }
  175. if (n - c < 6) {
  176. // A valid header must be at least 6 bytes on the
  177. // first line, e.g. "--- a/b\n".
  178. //
  179. c = n;
  180. continue;
  181. }
  182. if (match(buf, c, OLD_NAME) >= 0 && match(buf, n, NEW_NAME) >= 0) {
  183. // Probably a traditional patch. Ensure we have at least
  184. // a "@@ -0,0" smelling line next. We only check the "@@ -".
  185. //
  186. final int f = nextLF(buf, n);
  187. if (f >= end)
  188. return end;
  189. if (isHunkHdr(buf, f, end) == 1)
  190. return parseTraditionalPatch(buf, c, end);
  191. }
  192. c = n;
  193. }
  194. return c;
  195. }
  196. private int parseDiffGit(final byte[] buf, final int start, final int end) {
  197. final FileHeader fh = new FileHeader(buf, start);
  198. int ptr = fh.parseGitFileName(start + DIFF_GIT.length, end);
  199. if (ptr < 0)
  200. return skipFile(buf, start);
  201. ptr = fh.parseGitHeaders(ptr, end);
  202. ptr = parseHunks(fh, ptr, end);
  203. fh.endOffset = ptr;
  204. addFile(fh);
  205. return ptr;
  206. }
  207. private int parseDiffCombined(final byte[] hdr, final byte[] buf,
  208. final int start, final int end) {
  209. final CombinedFileHeader fh = new CombinedFileHeader(buf, start);
  210. int ptr = fh.parseGitFileName(start + hdr.length, end);
  211. if (ptr < 0)
  212. return skipFile(buf, start);
  213. ptr = fh.parseGitHeaders(ptr, end);
  214. ptr = parseHunks(fh, ptr, end);
  215. fh.endOffset = ptr;
  216. addFile(fh);
  217. return ptr;
  218. }
  219. private int parseTraditionalPatch(final byte[] buf, final int start,
  220. final int end) {
  221. final FileHeader fh = new FileHeader(buf, start);
  222. int ptr = fh.parseTraditionalHeaders(start, end);
  223. ptr = parseHunks(fh, ptr, end);
  224. fh.endOffset = ptr;
  225. addFile(fh);
  226. return ptr;
  227. }
  228. private static int skipFile(final byte[] buf, int ptr) {
  229. ptr = nextLF(buf, ptr);
  230. if (match(buf, ptr, OLD_NAME) >= 0)
  231. ptr = nextLF(buf, ptr);
  232. return ptr;
  233. }
  234. private int parseHunks(final FileHeader fh, int c, final int end) {
  235. final byte[] buf = fh.buf;
  236. while (c < end) {
  237. // If we see a file header at this point, we have all of the
  238. // hunks for our current file. We should stop and report back
  239. // with this position so it can be parsed again later.
  240. //
  241. if (match(buf, c, DIFF_GIT) >= 0)
  242. break;
  243. if (match(buf, c, DIFF_CC) >= 0)
  244. break;
  245. if (match(buf, c, DIFF_COMBINED) >= 0)
  246. break;
  247. if (match(buf, c, OLD_NAME) >= 0)
  248. break;
  249. if (match(buf, c, NEW_NAME) >= 0)
  250. break;
  251. if (isHunkHdr(buf, c, end) == fh.getParentCount()) {
  252. final HunkHeader h = fh.newHunkHeader(c);
  253. h.parseHeader();
  254. c = h.parseBody(this, end);
  255. h.endOffset = c;
  256. fh.addHunk(h);
  257. if (c < end) {
  258. switch (buf[c]) {
  259. case '@':
  260. case 'd':
  261. case '\n':
  262. break;
  263. default:
  264. if (match(buf, c, SIG_FOOTER) < 0)
  265. warn(buf, c, JGitText.get().unexpectedHunkTrailer);
  266. }
  267. }
  268. continue;
  269. }
  270. final int eol = nextLF(buf, c);
  271. if (fh.getHunks().isEmpty() && match(buf, c, GIT_BINARY) >= 0) {
  272. fh.patchType = FileHeader.PatchType.GIT_BINARY;
  273. return parseGitBinary(fh, eol, end);
  274. }
  275. if (fh.getHunks().isEmpty() && BIN_TRAILER.length < eol - c
  276. && match(buf, eol - BIN_TRAILER.length, BIN_TRAILER) >= 0
  277. && matchAny(buf, c, BIN_HEADERS)) {
  278. // The patch is a binary file diff, with no deltas.
  279. //
  280. fh.patchType = FileHeader.PatchType.BINARY;
  281. return eol;
  282. }
  283. // Skip this line and move to the next. Its probably garbage
  284. // after the last hunk of a file.
  285. //
  286. c = eol;
  287. }
  288. if (fh.getHunks().isEmpty()
  289. && fh.getPatchType() == FileHeader.PatchType.UNIFIED
  290. && !fh.hasMetaDataChanges()) {
  291. // Hmm, an empty patch? If there is no metadata here we
  292. // really have a binary patch that we didn't notice above.
  293. //
  294. fh.patchType = FileHeader.PatchType.BINARY;
  295. }
  296. return c;
  297. }
  298. private int parseGitBinary(final FileHeader fh, int c, final int end) {
  299. final BinaryHunk postImage = new BinaryHunk(fh, c);
  300. final int nEnd = postImage.parseHunk(c, end);
  301. if (nEnd < 0) {
  302. // Not a binary hunk.
  303. //
  304. error(fh.buf, c, JGitText.get().missingForwardImageInGITBinaryPatch);
  305. return c;
  306. }
  307. c = nEnd;
  308. postImage.endOffset = c;
  309. fh.forwardBinaryHunk = postImage;
  310. final BinaryHunk preImage = new BinaryHunk(fh, c);
  311. final int oEnd = preImage.parseHunk(c, end);
  312. if (oEnd >= 0) {
  313. c = oEnd;
  314. preImage.endOffset = c;
  315. fh.reverseBinaryHunk = preImage;
  316. }
  317. return c;
  318. }
  319. void warn(final byte[] buf, final int ptr, final String msg) {
  320. addError(new FormatError(buf, ptr, FormatError.Severity.WARNING, msg));
  321. }
  322. void error(final byte[] buf, final int ptr, final String msg) {
  323. addError(new FormatError(buf, ptr, FormatError.Severity.ERROR, msg));
  324. }
  325. private static boolean matchAny(final byte[] buf, final int c,
  326. final byte[][] srcs) {
  327. for (final byte[] s : srcs) {
  328. if (match(buf, c, s) >= 0)
  329. return true;
  330. }
  331. return false;
  332. }
  333. }