2 * Copyright (C) 2008, Google Inc.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org
.spearce
.jgit
.patch
;
40 import static org
.spearce
.jgit
.lib
.Constants
.encodeASCII
;
41 import static org
.spearce
.jgit
.patch
.FileHeader
.isHunkHdr
;
42 import static org
.spearce
.jgit
.patch
.FileHeader
.NEW_NAME
;
43 import static org
.spearce
.jgit
.patch
.FileHeader
.OLD_NAME
;
44 import static org
.spearce
.jgit
.util
.RawParseUtils
.match
;
45 import static org
.spearce
.jgit
.util
.RawParseUtils
.nextLF
;
47 import java
.io
.IOException
;
48 import java
.io
.InputStream
;
49 import java
.util
.ArrayList
;
50 import java
.util
.List
;
52 import org
.spearce
.jgit
.util
.TemporaryBuffer
;
54 /** A parsed collection of {@link FileHeader}s from a unified diff patch file */
56 private static final byte[] DIFF_GIT
= encodeASCII("diff --git ");
58 private static final byte[] DIFF_CC
= encodeASCII("diff --cc ");
60 private static final byte[] DIFF_COMBINED
= encodeASCII("diff --combined ");
62 private static final byte[][] BIN_HEADERS
= new byte[][] {
63 encodeASCII("Binary files "), encodeASCII("Files "), };
65 private static final byte[] BIN_TRAILER
= encodeASCII(" differ\n");
67 private static final byte[] GIT_BINARY
= encodeASCII("GIT binary patch\n");
69 static final byte[] SIG_FOOTER
= encodeASCII("-- \n");
71 /** The files, in the order they were parsed out of the input. */
72 private final List
<FileHeader
> files
;
74 /** Formatting errors, if any were identified. */
75 private final List
<FormatError
> errors
;
77 /** Create an empty patch. */
79 files
= new ArrayList
<FileHeader
>();
80 errors
= new ArrayList
<FormatError
>(0);
84 * Add a single file to this patch.
86 * Typically files should be added by parsing the text through one of this
87 * class's parse methods.
90 * the header of the file.
92 public void addFile(final FileHeader fh
) {
96 /** @return list of files described in the patch, in occurrence order. */
97 public List
<?
extends FileHeader
> getFiles() {
102 * Add a formatting error to this patch script.
105 * the error description.
107 public void addError(final FormatError err
) {
111 /** @return collection of formatting errors, if any. */
112 public List
<FormatError
> getErrors() {
117 * Parse a patch received from an InputStream.
119 * Multiple parse calls on the same instance will concatenate the patch
120 * data, but each parse input must start with a valid file header (don't
121 * split a single file across parse calls).
124 * the stream to read the patch data from. The stream is read
125 * until EOF is reached.
126 * @throws IOException
127 * there was an error reading from the input stream.
129 public void parse(final InputStream is
) throws IOException
{
130 final byte[] buf
= readFully(is
);
131 parse(buf
, 0, buf
.length
);
134 private static byte[] readFully(final InputStream is
) throws IOException
{
135 final TemporaryBuffer b
= new TemporaryBuffer();
139 return b
.toByteArray();
146 * Parse a patch stored in a byte[].
148 * Multiple parse calls on the same instance will concatenate the patch
149 * data, but each parse input must start with a valid file header (don't
150 * split a single file across parse calls).
153 * the buffer to parse.
155 * starting position to parse from.
157 * 1 past the last position to end parsing. The total length to
158 * be parsed is <code>end - ptr</code>.
160 public void parse(final byte[] buf
, int ptr
, final int end
) {
162 ptr
= parseFile(buf
, ptr
, end
);
165 private int parseFile(final byte[] buf
, int c
, final int end
) {
167 if (isHunkHdr(buf
, c
, end
) >= 1) {
168 // If we find a disconnected hunk header we might
169 // have missed a file header previously. The hunk
170 // isn't valid without knowing where it comes from.
172 error(buf
, c
, "Hunk disconnected from file");
177 // Valid git style patch?
179 if (match(buf
, c
, DIFF_GIT
) >= 0)
180 return parseDiffGit(buf
, c
, end
);
181 if (match(buf
, c
, DIFF_CC
) >= 0)
182 return parseDiffCombined(DIFF_CC
, buf
, c
, end
);
183 if (match(buf
, c
, DIFF_COMBINED
) >= 0)
184 return parseDiffCombined(DIFF_COMBINED
, buf
, c
, end
);
186 // Junk between files? Leading junk? Traditional
187 // (non-git generated) patch?
189 final int n
= nextLF(buf
, c
);
191 // Patches cannot be only one line long. This must be
192 // trailing junk that we should ignore.
198 // A valid header must be at least 6 bytes on the
199 // first line, e.g. "--- a/b\n".
205 if (match(buf
, c
, OLD_NAME
) >= 0 && match(buf
, n
, NEW_NAME
) >= 0) {
206 // Probably a traditional patch. Ensure we have at least
207 // a "@@ -0,0" smelling line next. We only check the "@@ -".
209 final int f
= nextLF(buf
, n
);
212 if (isHunkHdr(buf
, f
, end
) == 1)
213 return parseTraditionalPatch(buf
, c
, end
);
221 private int parseDiffGit(final byte[] buf
, final int start
, final int end
) {
222 final FileHeader fh
= new FileHeader(buf
, start
);
223 int ptr
= fh
.parseGitFileName(start
+ DIFF_GIT
.length
, end
);
225 return skipFile(buf
, start
, end
);
227 ptr
= fh
.parseGitHeaders(ptr
, end
);
228 ptr
= parseHunks(fh
, ptr
, end
);
234 private int parseDiffCombined(final byte[] hdr
, final byte[] buf
,
235 final int start
, final int end
) {
236 final CombinedFileHeader fh
= new CombinedFileHeader(buf
, start
);
237 int ptr
= fh
.parseGitFileName(start
+ hdr
.length
, end
);
239 return skipFile(buf
, start
, end
);
241 ptr
= fh
.parseGitHeaders(ptr
, end
);
242 ptr
= parseHunks(fh
, ptr
, end
);
248 private int parseTraditionalPatch(final byte[] buf
, final int start
,
250 final FileHeader fh
= new FileHeader(buf
, start
);
251 int ptr
= fh
.parseTraditionalHeaders(start
, end
);
252 ptr
= parseHunks(fh
, ptr
, end
);
258 private static int skipFile(final byte[] buf
, int ptr
, final int end
) {
259 ptr
= nextLF(buf
, ptr
);
260 if (match(buf
, ptr
, OLD_NAME
) >= 0)
261 ptr
= nextLF(buf
, ptr
);
265 private int parseHunks(final FileHeader fh
, int c
, final int end
) {
266 final byte[] buf
= fh
.buf
;
268 // If we see a file header at this point, we have all of the
269 // hunks for our current file. We should stop and report back
270 // with this position so it can be parsed again later.
272 if (match(buf
, c
, DIFF_GIT
) >= 0)
274 if (match(buf
, c
, DIFF_CC
) >= 0)
276 if (match(buf
, c
, DIFF_COMBINED
) >= 0)
278 if (match(buf
, c
, OLD_NAME
) >= 0)
280 if (match(buf
, c
, NEW_NAME
) >= 0)
283 if (isHunkHdr(buf
, c
, end
) == fh
.getParentCount()) {
284 final HunkHeader h
= fh
.newHunkHeader(c
);
286 c
= h
.parseBody(this, end
);
296 if (match(buf
, c
, SIG_FOOTER
) < 0)
297 warn(buf
, c
, "Unexpected hunk trailer");
303 final int eol
= nextLF(buf
, c
);
304 if (fh
.getHunks().isEmpty() && match(buf
, c
, GIT_BINARY
) >= 0) {
305 fh
.patchType
= FileHeader
.PatchType
.GIT_BINARY
;
306 return parseGitBinary(fh
, eol
, end
);
309 if (fh
.getHunks().isEmpty() && BIN_TRAILER
.length
< eol
- c
310 && match(buf
, eol
- BIN_TRAILER
.length
, BIN_TRAILER
) >= 0
311 && matchAny(buf
, c
, BIN_HEADERS
)) {
312 // The patch is a binary file diff, with no deltas.
314 fh
.patchType
= FileHeader
.PatchType
.BINARY
;
318 // Skip this line and move to the next. Its probably garbage
319 // after the last hunk of a file.
324 if (fh
.getHunks().isEmpty()
325 && fh
.getPatchType() == FileHeader
.PatchType
.UNIFIED
326 && !fh
.hasMetaDataChanges()) {
327 // Hmm, an empty patch? If there is no metadata here we
328 // really have a binary patch that we didn't notice above.
330 fh
.patchType
= FileHeader
.PatchType
.BINARY
;
336 private int parseGitBinary(final FileHeader fh
, int c
, final int end
) {
337 final BinaryHunk postImage
= new BinaryHunk(fh
, c
);
338 final int nEnd
= postImage
.parseHunk(c
, end
);
340 // Not a binary hunk.
342 error(fh
.buf
, c
, "Missing forward-image in GIT binary patch");
346 postImage
.endOffset
= c
;
347 fh
.forwardBinaryHunk
= postImage
;
349 final BinaryHunk preImage
= new BinaryHunk(fh
, c
);
350 final int oEnd
= preImage
.parseHunk(c
, end
);
353 preImage
.endOffset
= c
;
354 fh
.reverseBinaryHunk
= preImage
;
360 void warn(final byte[] buf
, final int ptr
, final String msg
) {
361 addError(new FormatError(buf
, ptr
, FormatError
.Severity
.WARNING
, msg
));
364 void error(final byte[] buf
, final int ptr
, final String msg
) {
365 addError(new FormatError(buf
, ptr
, FormatError
.Severity
.ERROR
, msg
));
368 private static boolean matchAny(final byte[] buf
, final int c
,
369 final byte[][] srcs
) {
370 for (final byte[] s
: srcs
) {
371 if (match(buf
, c
, s
) >= 0)