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
.decodeNoFallback
;
43 import static org
.spearce
.jgit
.util
.RawParseUtils
.extractBinaryString
;
44 import static org
.spearce
.jgit
.util
.RawParseUtils
.match
;
45 import static org
.spearce
.jgit
.util
.RawParseUtils
.nextLF
;
46 import static org
.spearce
.jgit
.util
.RawParseUtils
.parseBase10
;
48 import java
.io
.IOException
;
49 import java
.nio
.charset
.CharacterCodingException
;
50 import java
.nio
.charset
.Charset
;
51 import java
.util
.ArrayList
;
52 import java
.util
.Collections
;
53 import java
.util
.List
;
55 import org
.spearce
.jgit
.lib
.AbbreviatedObjectId
;
56 import org
.spearce
.jgit
.lib
.Constants
;
57 import org
.spearce
.jgit
.lib
.FileMode
;
58 import org
.spearce
.jgit
.util
.QuotedString
;
59 import org
.spearce
.jgit
.util
.RawParseUtils
;
60 import org
.spearce
.jgit
.util
.TemporaryBuffer
;
62 /** Patch header describing an action for a single file path. */
63 public class FileHeader
{
64 /** Magical file name used for file adds or deletes. */
65 public static final String DEV_NULL
= "/dev/null";
67 private static final byte[] OLD_MODE
= encodeASCII("old mode ");
69 private static final byte[] NEW_MODE
= encodeASCII("new mode ");
71 static final byte[] DELETED_FILE_MODE
= encodeASCII("deleted file mode ");
73 static final byte[] NEW_FILE_MODE
= encodeASCII("new file mode ");
75 private static final byte[] COPY_FROM
= encodeASCII("copy from ");
77 private static final byte[] COPY_TO
= encodeASCII("copy to ");
79 private static final byte[] RENAME_OLD
= encodeASCII("rename old ");
81 private static final byte[] RENAME_NEW
= encodeASCII("rename new ");
83 private static final byte[] RENAME_FROM
= encodeASCII("rename from ");
85 private static final byte[] RENAME_TO
= encodeASCII("rename to ");
87 private static final byte[] SIMILARITY_INDEX
= encodeASCII("similarity index ");
89 private static final byte[] DISSIMILARITY_INDEX
= encodeASCII("dissimilarity index ");
91 static final byte[] INDEX
= encodeASCII("index ");
93 static final byte[] OLD_NAME
= encodeASCII("--- ");
95 static final byte[] NEW_NAME
= encodeASCII("+++ ");
97 /** General type of change a single file-level patch describes. */
98 public static enum ChangeType
{
99 /** Add a new file to the project */
102 /** Modify an existing file in the project (content and/or mode) */
105 /** Delete an existing file from the project */
108 /** Rename an existing file to a new location */
111 /** Copy an existing file to a new location, keeping the original */
115 /** Type of patch used by this file. */
116 public static enum PatchType
{
117 /** A traditional unified diff style patch of a text file. */
120 /** An empty patch with a message "Binary files ... differ" */
123 /** A Git binary patch, holding pre and post image deltas */
127 /** Buffer holding the patch data for this file. */
130 /** Offset within {@link #buf} to the "diff ..." line. */
131 final int startOffset
;
133 /** Position 1 past the end of this file within {@link #buf}. */
136 /** File name of the old (pre-image). */
137 private String oldName
;
139 /** File name of the new (post-image). */
140 private String newName
;
142 /** Old mode of the file, if described by the patch, else null. */
143 private FileMode oldMode
;
145 /** New mode of the file, if described by the patch, else null. */
146 protected FileMode newMode
;
148 /** General type of change indicated by the patch. */
149 protected ChangeType changeType
;
151 /** Similarity score if {@link #changeType} is a copy or rename. */
154 /** ObjectId listed on the index line for the old (pre-image) */
155 private AbbreviatedObjectId oldId
;
157 /** ObjectId listed on the index line for the new (post-image) */
158 protected AbbreviatedObjectId newId
;
160 /** Type of patch used to modify this file */
163 /** The hunks of this file */
164 private List
<HunkHeader
> hunks
;
166 /** If {@link #patchType} is {@link PatchType#GIT_BINARY}, the new image */
167 BinaryHunk forwardBinaryHunk
;
169 /** If {@link #patchType} is {@link PatchType#GIT_BINARY}, the old image */
170 BinaryHunk reverseBinaryHunk
;
172 FileHeader(final byte[] b
, final int offset
) {
174 startOffset
= offset
;
175 changeType
= ChangeType
.MODIFY
; // unless otherwise designated
176 patchType
= PatchType
.UNIFIED
;
179 int getParentCount() {
183 /** @return the byte array holding this file's patch script. */
184 public byte[] getBuffer() {
188 /** @return offset the start of this file's script in {@link #getBuffer()}. */
189 public int getStartOffset() {
193 /** @return offset one past the end of the file script. */
194 public int getEndOffset() {
199 * Convert the patch script for this file into a string.
201 * The default character encoding ({@link Constants#CHARSET}) is assumed for
202 * both the old and new files.
204 * @return the patch script, as a Unicode string.
206 public String
getScriptText() {
207 return getScriptText(null, null);
211 * Convert the patch script for this file into a string.
214 * hint character set to decode the old lines with.
216 * hint character set to decode the new lines with.
217 * @return the patch script, as a Unicode string.
219 public String
getScriptText(Charset oldCharset
, Charset newCharset
) {
220 return getScriptText(new Charset
[] { oldCharset
, newCharset
});
223 String
getScriptText(Charset
[] charsetGuess
) {
224 if (getHunks().isEmpty()) {
225 // If we have no hunks then we can safely assume the entire
226 // patch is a binary style patch, or a meta-data only style
227 // patch. Either way the encoding of the headers should be
228 // strictly 7-bit US-ASCII and the body is either 7-bit ASCII
229 // (due to the base 85 encoding used for a BinaryHunk) or is
230 // arbitrary noise we have chosen to ignore and not understand
231 // (e.g. the message "Binary files ... differ").
233 return extractBinaryString(buf
, startOffset
, endOffset
);
236 if (charsetGuess
!= null && charsetGuess
.length
!= getParentCount() + 1)
237 throw new IllegalArgumentException("Expected "
238 + (getParentCount() + 1) + " character encoding guesses");
240 if (trySimpleConversion(charsetGuess
)) {
241 Charset cs
= charsetGuess
!= null ? charsetGuess
[0] : null;
243 cs
= Constants
.CHARSET
;
245 return decodeNoFallback(cs
, buf
, startOffset
, endOffset
);
246 } catch (CharacterCodingException cee
) {
247 // Try the much slower, more-memory intensive version which
248 // can handle a character set conversion patch.
252 final StringBuilder r
= new StringBuilder(endOffset
- startOffset
);
254 // Always treat the headers as US-ASCII; Git file names are encoded
255 // in a C style escape if any character has the high-bit set.
257 final int hdrEnd
= getHunks().get(0).getStartOffset();
258 for (int ptr
= startOffset
; ptr
< hdrEnd
;) {
259 final int eol
= Math
.min(hdrEnd
, nextLF(buf
, ptr
));
260 r
.append(extractBinaryString(buf
, ptr
, eol
));
264 final String
[] files
= extractFileLines(charsetGuess
);
265 final int[] offsets
= new int[files
.length
];
266 for (final HunkHeader h
: getHunks())
267 h
.extractFileLines(r
, files
, offsets
);
271 private static boolean trySimpleConversion(final Charset
[] charsetGuess
) {
272 if (charsetGuess
== null)
274 for (int i
= 1; i
< charsetGuess
.length
; i
++) {
275 if (charsetGuess
[i
] != charsetGuess
[0])
281 private String
[] extractFileLines(final Charset
[] csGuess
) {
282 final TemporaryBuffer
[] tmp
= new TemporaryBuffer
[getParentCount() + 1];
284 for (int i
= 0; i
< tmp
.length
; i
++)
285 tmp
[i
] = new TemporaryBuffer();
286 for (final HunkHeader h
: getHunks())
287 h
.extractFileLines(tmp
);
289 final String
[] r
= new String
[tmp
.length
];
290 for (int i
= 0; i
< tmp
.length
; i
++) {
291 Charset cs
= csGuess
!= null ? csGuess
[i
] : null;
293 cs
= Constants
.CHARSET
;
294 r
[i
] = RawParseUtils
.decode(cs
, tmp
[i
].toByteArray());
297 } catch (IOException ioe
) {
298 throw new RuntimeException("Cannot convert script to text", ioe
);
300 for (final TemporaryBuffer b
: tmp
) {
308 * Get the old name associated with this file.
310 * The meaning of the old name can differ depending on the semantic meaning
313 * <li><i>file add</i>: always <code>/dev/null</code></li>
314 * <li><i>file modify</i>: always {@link #getNewName()}</li>
315 * <li><i>file delete</i>: always the file being deleted</li>
316 * <li><i>file copy</i>: source file the copy originates from</li>
317 * <li><i>file rename</i>: source file the rename originates from</li>
320 * @return old name for this file.
322 public String
getOldName() {
327 * Get the new name associated with this file.
329 * The meaning of the new name can differ depending on the semantic meaning
332 * <li><i>file add</i>: always the file being created</li>
333 * <li><i>file modify</i>: always {@link #getOldName()}</li>
334 * <li><i>file delete</i>: always <code>/dev/null</code></li>
335 * <li><i>file copy</i>: destination file the copy ends up at</li>
336 * <li><i>file rename</i>: destination file the rename ends up at/li>
339 * @return new name for this file.
341 public String
getNewName() {
345 /** @return the old file mode, if described in the patch */
346 public FileMode
getOldMode() {
350 /** @return the new file mode, if described in the patch */
351 public FileMode
getNewMode() {
355 /** @return the type of change this patch makes on {@link #getNewName()} */
356 public ChangeType
getChangeType() {
361 * @return similarity score between {@link #getOldName()} and
362 * {@link #getNewName()} if {@link #getChangeType()} is
363 * {@link ChangeType#COPY} or {@link ChangeType#RENAME}.
365 public int getScore() {
370 * Get the old object id from the <code>index</code>.
372 * @return the object id; null if there is no index line
374 public AbbreviatedObjectId
getOldId() {
379 * Get the new object id from the <code>index</code>.
381 * @return the object id; null if there is no index line
383 public AbbreviatedObjectId
getNewId() {
387 /** @return style of patch used to modify this file */
388 public PatchType
getPatchType() {
392 /** @return true if this patch modifies metadata about a file */
393 public boolean hasMetaDataChanges() {
394 return changeType
!= ChangeType
.MODIFY
|| newMode
!= oldMode
;
397 /** @return hunks altering this file; in order of appearance in patch */
398 public List
<?
extends HunkHeader
> getHunks() {
400 return Collections
.emptyList();
404 void addHunk(final HunkHeader h
) {
405 if (h
.getFileHeader() != this)
406 throw new IllegalArgumentException("Hunk belongs to another file");
408 hunks
= new ArrayList
<HunkHeader
>();
412 HunkHeader
newHunkHeader(final int offset
) {
413 return new HunkHeader(this, offset
);
416 /** @return if a {@link PatchType#GIT_BINARY}, the new-image delta/literal */
417 public BinaryHunk
getForwardBinaryHunk() {
418 return forwardBinaryHunk
;
421 /** @return if a {@link PatchType#GIT_BINARY}, the old-image delta/literal */
422 public BinaryHunk
getReverseBinaryHunk() {
423 return reverseBinaryHunk
;
427 * Parse a "diff --git" or "diff --cc" line.
430 * first character after the "diff --git " or "diff --cc " part.
432 * one past the last position to parse.
433 * @return first character after the LF at the end of the line; -1 on error.
435 int parseGitFileName(int ptr
, final int end
) {
436 final int eol
= nextLF(buf
, ptr
);
442 // buffer[ptr..eol] looks like "a/foo b/foo\n". After the first
443 // A regex to match this is "^[^/]+/(.*?) [^/+]+/\1\n$". There
444 // is only one way to split the line such that text to the left
445 // of the space matches the text to the right, excluding the part
446 // before the first slash.
449 final int aStart
= nextLF(buf
, ptr
, '/');
454 final int sp
= nextLF(buf
, ptr
, ' ');
456 // We can't split the header, it isn't valid.
457 // This may be OK if this is a rename patch.
461 final int bStart
= nextLF(buf
, sp
, '/');
465 // If buffer[aStart..sp - 1] = buffer[bStart..eol - 1]
466 // we have a valid split.
468 if (eq(aStart
, sp
- 1, bStart
, eol
- 1)) {
469 if (buf
[bol
] == '"') {
470 // We're a double quoted name. The region better end
471 // in a double quote too, and we need to decode the
472 // characters before reading the name.
474 if (buf
[sp
- 2] != '"') {
477 oldName
= QuotedString
.GIT_PATH
.dequote(buf
, bol
, sp
- 1);
478 oldName
= p1(oldName
);
480 oldName
= decode(Constants
.CHARSET
, buf
, aStart
, sp
- 1);
486 // This split wasn't correct. Move past the space and try
487 // another split as the space must be part of the file name.
495 int parseGitHeaders(int ptr
, final int end
) {
497 final int eol
= nextLF(buf
, ptr
);
498 if (isHunkHdr(buf
, ptr
, eol
) >= 1) {
499 // First hunk header; break out and parse them later.
502 } else if (match(buf
, ptr
, OLD_NAME
) >= 0) {
503 parseOldName(ptr
, eol
);
505 } else if (match(buf
, ptr
, NEW_NAME
) >= 0) {
506 parseNewName(ptr
, eol
);
508 } else if (match(buf
, ptr
, OLD_MODE
) >= 0) {
509 oldMode
= parseFileMode(ptr
+ OLD_MODE
.length
, eol
);
511 } else if (match(buf
, ptr
, NEW_MODE
) >= 0) {
512 newMode
= parseFileMode(ptr
+ NEW_MODE
.length
, eol
);
514 } else if (match(buf
, ptr
, DELETED_FILE_MODE
) >= 0) {
515 oldMode
= parseFileMode(ptr
+ DELETED_FILE_MODE
.length
, eol
);
516 newMode
= FileMode
.MISSING
;
517 changeType
= ChangeType
.DELETE
;
519 } else if (match(buf
, ptr
, NEW_FILE_MODE
) >= 0) {
520 parseNewFileMode(ptr
, eol
);
522 } else if (match(buf
, ptr
, COPY_FROM
) >= 0) {
523 oldName
= parseName(oldName
, ptr
+ COPY_FROM
.length
, eol
);
524 changeType
= ChangeType
.COPY
;
526 } else if (match(buf
, ptr
, COPY_TO
) >= 0) {
527 newName
= parseName(newName
, ptr
+ COPY_TO
.length
, eol
);
528 changeType
= ChangeType
.COPY
;
530 } else if (match(buf
, ptr
, RENAME_OLD
) >= 0) {
531 oldName
= parseName(oldName
, ptr
+ RENAME_OLD
.length
, eol
);
532 changeType
= ChangeType
.RENAME
;
534 } else if (match(buf
, ptr
, RENAME_NEW
) >= 0) {
535 newName
= parseName(newName
, ptr
+ RENAME_NEW
.length
, eol
);
536 changeType
= ChangeType
.RENAME
;
538 } else if (match(buf
, ptr
, RENAME_FROM
) >= 0) {
539 oldName
= parseName(oldName
, ptr
+ RENAME_FROM
.length
, eol
);
540 changeType
= ChangeType
.RENAME
;
542 } else if (match(buf
, ptr
, RENAME_TO
) >= 0) {
543 newName
= parseName(newName
, ptr
+ RENAME_TO
.length
, eol
);
544 changeType
= ChangeType
.RENAME
;
546 } else if (match(buf
, ptr
, SIMILARITY_INDEX
) >= 0) {
547 score
= parseBase10(buf
, ptr
+ SIMILARITY_INDEX
.length
, null);
549 } else if (match(buf
, ptr
, DISSIMILARITY_INDEX
) >= 0) {
550 score
= parseBase10(buf
, ptr
+ DISSIMILARITY_INDEX
.length
, null);
552 } else if (match(buf
, ptr
, INDEX
) >= 0) {
553 parseIndexLine(ptr
+ INDEX
.length
, eol
);
556 // Probably an empty patch (stat dirty).
565 void parseOldName(int ptr
, final int eol
) {
566 oldName
= p1(parseName(oldName
, ptr
+ OLD_NAME
.length
, eol
));
567 if (oldName
== DEV_NULL
)
568 changeType
= ChangeType
.ADD
;
571 void parseNewName(int ptr
, final int eol
) {
572 newName
= p1(parseName(newName
, ptr
+ NEW_NAME
.length
, eol
));
573 if (newName
== DEV_NULL
)
574 changeType
= ChangeType
.DELETE
;
577 void parseNewFileMode(int ptr
, final int eol
) {
578 oldMode
= FileMode
.MISSING
;
579 newMode
= parseFileMode(ptr
+ NEW_FILE_MODE
.length
, eol
);
580 changeType
= ChangeType
.ADD
;
583 int parseTraditionalHeaders(int ptr
, final int end
) {
585 final int eol
= nextLF(buf
, ptr
);
586 if (isHunkHdr(buf
, ptr
, eol
) >= 1) {
587 // First hunk header; break out and parse them later.
590 } else if (match(buf
, ptr
, OLD_NAME
) >= 0) {
591 parseOldName(ptr
, eol
);
593 } else if (match(buf
, ptr
, NEW_NAME
) >= 0) {
594 parseNewName(ptr
, eol
);
597 // Possibly an empty patch.
606 private String
parseName(final String expect
, int ptr
, final int end
) {
611 if (buf
[ptr
] == '"') {
612 // New style GNU diff format
614 r
= QuotedString
.GIT_PATH
.dequote(buf
, ptr
, end
- 1);
616 // Older style GNU diff format, an optional tab ends the name.
619 while (ptr
< tab
&& buf
[tab
- 1] != '\t')
623 r
= decode(Constants
.CHARSET
, buf
, ptr
, tab
- 1);
626 if (r
.equals(DEV_NULL
))
631 private static String
p1(final String r
) {
632 final int s
= r
.indexOf('/');
633 return s
> 0 ? r
.substring(s
+ 1) : r
;
636 FileMode
parseFileMode(int ptr
, final int end
) {
638 while (ptr
< end
- 1) {
640 tmp
+= buf
[ptr
++] - '0';
642 return FileMode
.fromBits(tmp
);
645 void parseIndexLine(int ptr
, final int end
) {
646 // "index $asha1..$bsha1[ $mode]" where $asha1 and $bsha1
647 // can be unique abbreviations
649 final int dot2
= nextLF(buf
, ptr
, '.');
650 final int mode
= nextLF(buf
, dot2
, ' ');
652 oldId
= AbbreviatedObjectId
.fromString(buf
, ptr
, dot2
- 1);
653 newId
= AbbreviatedObjectId
.fromString(buf
, dot2
+ 1, mode
- 1);
656 newMode
= oldMode
= parseFileMode(mode
, end
);
659 private boolean eq(int aPtr
, int aEnd
, int bPtr
, int bEnd
) {
660 if (aEnd
- aPtr
!= bEnd
- bPtr
) {
663 while (aPtr
< aEnd
) {
664 if (buf
[aPtr
++] != buf
[bPtr
++])
671 * Determine if this is a patch hunk header.
676 * first position in the buffer to evaluate
678 * last position to consider; usually the end of the buffer (
679 * <code>buf.length</code>) or the first position on the next
680 * line. This is only used to avoid very long runs of '@' from
681 * killing the scan loop.
682 * @return the number of "ancestor revisions" in the hunk header. A
683 * traditional two-way diff ("@@ -...") returns 1; a combined diff
684 * for a 3 way-merge returns 3. If this is not a hunk header, 0 is
687 static int isHunkHdr(final byte[] buf
, final int start
, final int end
) {
689 while (ptr
< end
&& buf
[ptr
] == '@')
693 if (ptr
== end
|| buf
[ptr
++] != ' ')
695 if (ptr
== end
|| buf
[ptr
++] != '-')
697 return (ptr
- 3) - start
;