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
.util
.RawParseUtils
.decode
;
42 import static org
.spearce
.jgit
.util
.RawParseUtils
.match
;
43 import static org
.spearce
.jgit
.util
.RawParseUtils
.nextLF
;
44 import static org
.spearce
.jgit
.util
.RawParseUtils
.parseBase10
;
46 import java
.util
.ArrayList
;
47 import java
.util
.Collections
;
48 import java
.util
.List
;
50 import org
.spearce
.jgit
.lib
.AbbreviatedObjectId
;
51 import org
.spearce
.jgit
.lib
.Constants
;
52 import org
.spearce
.jgit
.lib
.FileMode
;
53 import org
.spearce
.jgit
.util
.QuotedString
;
55 /** Patch header describing an action for a single file path. */
56 public class FileHeader
{
57 /** Magical file name used for file adds or deletes. */
58 public static final String DEV_NULL
= "/dev/null";
60 private static final byte[] OLD_MODE
= encodeASCII("old mode ");
62 private static final byte[] NEW_MODE
= encodeASCII("new mode ");
64 private static final byte[] DELETED_FILE_MODE
= encodeASCII("deleted file mode ");
66 private static final byte[] NEW_FILE_MODE
= encodeASCII("new file mode ");
68 private static final byte[] COPY_FROM
= encodeASCII("copy from ");
70 private static final byte[] COPY_TO
= encodeASCII("copy to ");
72 private static final byte[] RENAME_OLD
= encodeASCII("rename old ");
74 private static final byte[] RENAME_NEW
= encodeASCII("rename new ");
76 private static final byte[] RENAME_FROM
= encodeASCII("rename from ");
78 private static final byte[] RENAME_TO
= encodeASCII("rename to ");
80 private static final byte[] SIMILARITY_INDEX
= encodeASCII("similarity index ");
82 private static final byte[] DISSIMILARITY_INDEX
= encodeASCII("dissimilarity index ");
84 private static final byte[] INDEX
= encodeASCII("index ");
86 static final byte[] OLD_NAME
= encodeASCII("--- ");
88 static final byte[] NEW_NAME
= encodeASCII("+++ ");
90 /** General type of change a single file-level patch describes. */
91 public static enum ChangeType
{
92 /** Add a new file to the project */
95 /** Modify an existing file in the project (content and/or mode) */
98 /** Delete an existing file from the project */
101 /** Rename an existing file to a new location */
104 /** Copy an existing file to a new location, keeping the original */
108 /** Type of patch used by this file. */
109 public static enum PatchType
{
110 /** A traditional unified diff style patch of a text file. */
113 /** An empty patch with a message "Binary files ... differ" */
116 /** A Git binary patch, holding pre and post image deltas */
120 /** Buffer holding the patch data for this file. */
123 /** Offset within {@link #buf} to the "diff ..." line. */
124 final int startOffset
;
126 /** Position 1 past the end of this file within {@link #buf}. */
129 /** File name of the old (pre-image). */
130 private String oldName
;
132 /** File name of the new (post-image). */
133 private String newName
;
135 /** Old mode of the file, if described by the patch, else null. */
136 private FileMode oldMode
;
138 /** New mode of the file, if described by the patch, else null. */
139 private FileMode newMode
;
141 /** General type of change indicated by the patch. */
142 private ChangeType changeType
;
144 /** Similarity score if {@link #changeType} is a copy or rename. */
147 /** ObjectId listed on the index line for the old (pre-image) */
148 private AbbreviatedObjectId oldId
;
150 /** ObjectId listed on the index line for the new (post-image) */
151 private AbbreviatedObjectId newId
;
153 /** Type of patch used to modify this file */
156 /** The hunks of this file */
157 private List
<HunkHeader
> hunks
;
159 /** If {@link #patchType} is {@link PatchType#GIT_BINARY}, the new image */
160 BinaryHunk forwardBinaryHunk
;
162 /** If {@link #patchType} is {@link PatchType#GIT_BINARY}, the old image */
163 BinaryHunk reverseBinaryHunk
;
165 FileHeader(final byte[] b
, final int offset
) {
167 startOffset
= offset
;
168 changeType
= ChangeType
.MODIFY
; // unless otherwise designated
169 patchType
= PatchType
.UNIFIED
;
172 int getParentCount() {
177 * Get the old name associated with this file.
179 * The meaning of the old name can differ depending on the semantic meaning
182 * <li><i>file add</i>: always <code>/dev/null</code></li>
183 * <li><i>file modify</i>: always {@link #getNewName()}</li>
184 * <li><i>file delete</i>: always the file being deleted</li>
185 * <li><i>file copy</i>: source file the copy originates from</li>
186 * <li><i>file rename</i>: source file the rename originates from</li>
189 * @return old name for this file.
191 public String
getOldName() {
196 * Get the new name associated with this file.
198 * The meaning of the new name can differ depending on the semantic meaning
201 * <li><i>file add</i>: always the file being created</li>
202 * <li><i>file modify</i>: always {@link #getOldName()}</li>
203 * <li><i>file delete</i>: always <code>/dev/null</code></li>
204 * <li><i>file copy</i>: destination file the copy ends up at</li>
205 * <li><i>file rename</i>: destination file the rename ends up at/li>
208 * @return new name for this file.
210 public String
getNewName() {
214 /** @return the old file mode, if described in the patch */
215 public FileMode
getOldMode() {
219 /** @return the new file mode, if described in the patch */
220 public FileMode
getNewMode() {
224 /** @return the type of change this patch makes on {@link #getNewName()} */
225 public ChangeType
getChangeType() {
230 * @return similarity score between {@link #getOldName()} and
231 * {@link #getNewName()} if {@link #getChangeType()} is
232 * {@link ChangeType#COPY} or {@link ChangeType#RENAME}.
234 public int getScore() {
239 * Get the old object id from the <code>index</code>.
241 * @return the object id; null if there is no index line
243 public AbbreviatedObjectId
getOldId() {
248 * Get the new object id from the <code>index</code>.
250 * @return the object id; null if there is no index line
252 public AbbreviatedObjectId
getNewId() {
256 /** @return style of patch used to modify this file */
257 public PatchType
getPatchType() {
261 /** @return true if this patch modifies metadata about a file */
262 public boolean hasMetaDataChanges() {
263 return changeType
!= ChangeType
.MODIFY
|| newMode
!= oldMode
;
266 /** @return hunks altering this file; in order of appearance in patch */
267 public List
<HunkHeader
> getHunks() {
269 return Collections
.emptyList();
273 void addHunk(final HunkHeader h
) {
274 if (h
.getFileHeader() != this)
275 throw new IllegalArgumentException("Hunk belongs to another file");
277 hunks
= new ArrayList
<HunkHeader
>();
281 HunkHeader
newHunkHeader(final int offset
) {
282 return new HunkHeader(this, offset
);
285 /** @return if a {@link PatchType#GIT_BINARY}, the new-image delta/literal */
286 public BinaryHunk
getForwardBinaryHunk() {
287 return forwardBinaryHunk
;
290 /** @return if a {@link PatchType#GIT_BINARY}, the old-image delta/literal */
291 public BinaryHunk
getReverseBinaryHunk() {
292 return reverseBinaryHunk
;
296 * Parse a "diff --git" or "diff --cc" line.
299 * first character after the "diff --git " or "diff --cc " part.
301 * one past the last position to parse.
302 * @return first character after the LF at the end of the line; -1 on error.
304 int parseGitFileName(int ptr
, final int end
) {
305 final int eol
= nextLF(buf
, ptr
);
311 // buffer[ptr..eol] looks like "a/foo b/foo\n". After the first
312 // A regex to match this is "^[^/]+/(.*?) [^/+]+/\1\n$". There
313 // is only one way to split the line such that text to the left
314 // of the space matches the text to the right, excluding the part
315 // before the first slash.
318 final int aStart
= nextLF(buf
, ptr
, '/');
323 final int sp
= nextLF(buf
, ptr
, ' ');
325 // We can't split the header, it isn't valid.
326 // This may be OK if this is a rename patch.
330 final int bStart
= nextLF(buf
, sp
, '/');
334 // If buffer[aStart..sp - 1] = buffer[bStart..eol - 1]
335 // we have a valid split.
337 if (eq(aStart
, sp
- 1, bStart
, eol
- 1)) {
338 if (buf
[bol
] == '"') {
339 // We're a double quoted name. The region better end
340 // in a double quote too, and we need to decode the
341 // characters before reading the name.
343 if (buf
[sp
- 2] != '"') {
346 oldName
= QuotedString
.GIT_PATH
.dequote(buf
, bol
, sp
- 1);
347 oldName
= p1(oldName
);
349 oldName
= decode(Constants
.CHARSET
, buf
, aStart
, sp
- 1);
355 // This split wasn't correct. Move past the space and try
356 // another split as the space must be part of the file name.
364 int parseGitHeaders(int ptr
, final int end
) {
366 final int eol
= nextLF(buf
, ptr
);
367 if (isHunkHdr(buf
, ptr
, eol
) >= 1) {
368 // First hunk header; break out and parse them later.
371 } else if (match(buf
, ptr
, OLD_NAME
) >= 0) {
372 oldName
= p1(parseName(oldName
, ptr
+ OLD_NAME
.length
, eol
));
373 if (oldName
== DEV_NULL
)
374 changeType
= ChangeType
.ADD
;
376 } else if (match(buf
, ptr
, NEW_NAME
) >= 0) {
377 newName
= p1(parseName(newName
, ptr
+ NEW_NAME
.length
, eol
));
378 if (newName
== DEV_NULL
)
379 changeType
= ChangeType
.DELETE
;
381 } else if (match(buf
, ptr
, OLD_MODE
) >= 0) {
382 oldMode
= parseFileMode(ptr
+ OLD_MODE
.length
, eol
);
384 } else if (match(buf
, ptr
, NEW_MODE
) >= 0) {
385 newMode
= parseFileMode(ptr
+ NEW_MODE
.length
, eol
);
387 } else if (match(buf
, ptr
, DELETED_FILE_MODE
) >= 0) {
388 oldMode
= parseFileMode(ptr
+ DELETED_FILE_MODE
.length
, eol
);
389 changeType
= ChangeType
.DELETE
;
391 } else if (match(buf
, ptr
, NEW_FILE_MODE
) >= 0) {
392 newMode
= parseFileMode(ptr
+ NEW_FILE_MODE
.length
, eol
);
393 changeType
= ChangeType
.ADD
;
395 } else if (match(buf
, ptr
, COPY_FROM
) >= 0) {
396 oldName
= parseName(oldName
, ptr
+ COPY_FROM
.length
, eol
);
397 changeType
= ChangeType
.COPY
;
399 } else if (match(buf
, ptr
, COPY_TO
) >= 0) {
400 newName
= parseName(newName
, ptr
+ COPY_TO
.length
, eol
);
401 changeType
= ChangeType
.COPY
;
403 } else if (match(buf
, ptr
, RENAME_OLD
) >= 0) {
404 oldName
= parseName(oldName
, ptr
+ RENAME_OLD
.length
, eol
);
405 changeType
= ChangeType
.RENAME
;
407 } else if (match(buf
, ptr
, RENAME_NEW
) >= 0) {
408 newName
= parseName(newName
, ptr
+ RENAME_NEW
.length
, eol
);
409 changeType
= ChangeType
.RENAME
;
411 } else if (match(buf
, ptr
, RENAME_FROM
) >= 0) {
412 oldName
= parseName(oldName
, ptr
+ RENAME_FROM
.length
, eol
);
413 changeType
= ChangeType
.RENAME
;
415 } else if (match(buf
, ptr
, RENAME_TO
) >= 0) {
416 newName
= parseName(newName
, ptr
+ RENAME_TO
.length
, eol
);
417 changeType
= ChangeType
.RENAME
;
419 } else if (match(buf
, ptr
, SIMILARITY_INDEX
) >= 0) {
420 score
= parseBase10(buf
, ptr
+ SIMILARITY_INDEX
.length
, null);
422 } else if (match(buf
, ptr
, DISSIMILARITY_INDEX
) >= 0) {
423 score
= parseBase10(buf
, ptr
+ DISSIMILARITY_INDEX
.length
, null);
425 } else if (match(buf
, ptr
, INDEX
) >= 0) {
426 parseIndexLine(ptr
+ INDEX
.length
, eol
);
429 // Probably an empty patch (stat dirty).
438 int parseTraditionalHeaders(int ptr
, final int end
) {
440 final int eol
= nextLF(buf
, ptr
);
441 if (isHunkHdr(buf
, ptr
, eol
) >= 1) {
442 // First hunk header; break out and parse them later.
445 } else if (match(buf
, ptr
, OLD_NAME
) >= 0) {
446 oldName
= p1(parseName(oldName
, ptr
+ OLD_NAME
.length
, eol
));
447 if (oldName
== DEV_NULL
)
448 changeType
= ChangeType
.ADD
;
450 } else if (match(buf
, ptr
, NEW_NAME
) >= 0) {
451 newName
= p1(parseName(newName
, ptr
+ NEW_NAME
.length
, eol
));
452 if (newName
== DEV_NULL
)
453 changeType
= ChangeType
.DELETE
;
456 // Possibly an empty patch.
465 private String
parseName(final String expect
, int ptr
, final int end
) {
470 if (buf
[ptr
] == '"') {
471 // New style GNU diff format
473 r
= QuotedString
.GIT_PATH
.dequote(buf
, ptr
, end
- 1);
475 // Older style GNU diff format, an optional tab ends the name.
478 while (ptr
< tab
&& buf
[tab
- 1] != '\t')
482 r
= decode(Constants
.CHARSET
, buf
, ptr
, tab
- 1);
485 if (r
.equals(DEV_NULL
))
490 private static String
p1(final String r
) {
491 final int s
= r
.indexOf('/');
492 return s
> 0 ? r
.substring(s
+ 1) : r
;
495 private FileMode
parseFileMode(int ptr
, final int end
) {
497 while (ptr
< end
- 1) {
499 tmp
+= buf
[ptr
++] - '0';
501 return FileMode
.fromBits(tmp
);
504 private void parseIndexLine(int ptr
, final int end
) {
505 // "index $asha1..$bsha1[ $mode]" where $asha1 and $bsha1
506 // can be unique abbreviations
508 final int dot2
= nextLF(buf
, ptr
, '.');
509 final int mode
= nextLF(buf
, dot2
, ' ');
511 oldId
= AbbreviatedObjectId
.fromString(buf
, ptr
, dot2
- 1);
512 newId
= AbbreviatedObjectId
.fromString(buf
, dot2
+ 1, mode
- 1);
515 newMode
= oldMode
= parseFileMode(mode
, end
);
518 private boolean eq(int aPtr
, int aEnd
, int bPtr
, int bEnd
) {
519 if (aEnd
- aPtr
!= bEnd
- bPtr
) {
522 while (aPtr
< aEnd
) {
523 if (buf
[aPtr
++] != buf
[bPtr
++])
530 * Determine if this is a patch hunk header.
535 * first position in the buffer to evaluate
537 * last position to consider; usually the end of the buffer (
538 * <code>buf.length</code>) or the first position on the next
539 * line. This is only used to avoid very long runs of '@' from
540 * killing the scan loop.
541 * @return the number of "ancestor revisions" in the hunk header. A
542 * traditional two-way diff ("@@ -...") returns 1; a combined diff
543 * for a 3 way-merge returns 3. If this is not a hunk header, 0 is
546 static int isHunkHdr(final byte[] buf
, final int start
, final int end
) {
548 while (ptr
< end
&& buf
[ptr
] == '@')
552 if (ptr
== end
|| buf
[ptr
++] != ' ')
554 if (ptr
== end
|| buf
[ptr
++] != '-')
556 return (ptr
- 3) - start
;