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
.util
.RawParseUtils
.match
;
41 import static org
.spearce
.jgit
.util
.RawParseUtils
.nextLF
;
42 import static org
.spearce
.jgit
.util
.RawParseUtils
.parseBase10
;
44 import org
.spearce
.jgit
.util
.MutableInteger
;
46 /** Hunk header describing the layout of a single block of lines */
47 public class HunkHeader
{
48 private final FileHeader file
;
50 /** Offset within {@link #file}.buf to the "@@ -" line. */
51 final int startOffset
;
53 /** Position 1 past the end of this hunk within {@link #file}'s buf. */
56 /** First line number in the pre-image file where the hunk starts */
59 /** Total number of pre-image lines this hunk covers (context + deleted) */
62 /** First line number in the post-image file where the hunk starts */
65 /** Total number of post-image lines this hunk covers (context + inserted) */
68 /** Total number of lines of context appearing in this hunk */
71 /** Number of lines removed by this hunk */
74 /** Number of lines added by this hunk */
77 HunkHeader(final FileHeader fh
, final int offset
) {
82 /** @return header for the file this hunk applies to */
83 public FileHeader
getFileHeader() {
87 /** @return first line number in the pre-image file where the hunk starts */
88 public int getOldStartLine() {
92 /** @return total number of pre-image lines this hunk covers */
93 public int getOldLineCount() {
97 /** @return first line number in the post-image file where the hunk starts */
98 public int getNewStartLine() {
102 /** @return Total number of post-image lines this hunk covers */
103 public int getNewLineCount() {
107 /** @return total number of lines of context appearing in this hunk */
108 public int getLinesContext() {
112 /** @return number of lines removed by this hunk */
113 public int getLinesDeleted() {
117 /** @return number of lines added by this hunk */
118 public int getLinesAdded() {
123 // Parse "@@ -236,9 +236,9 @@ protected boolean"
125 final byte[] buf
= file
.buf
;
126 final MutableInteger ptr
= new MutableInteger();
127 ptr
.value
= nextLF(buf
, startOffset
, ' ');
128 oldStartLine
= -parseBase10(buf
, ptr
.value
, ptr
);
129 oldLineCount
= parseBase10(buf
, ptr
.value
+ 1, ptr
);
131 newStartLine
= parseBase10(buf
, ptr
.value
+ 1, ptr
);
132 newLineCount
= parseBase10(buf
, ptr
.value
+ 1, ptr
);
136 final byte[] buf
= file
.buf
;
137 final int sz
= buf
.length
;
138 int c
= nextLF(buf
, startOffset
), last
= c
;
143 SCAN
: for (; c
< sz
; last
= c
, c
= nextLF(buf
, c
)) {
158 case '\\': // Matches "\ No newline at end of file"
166 if (last
< sz
&& nContext
+ nDeleted
- 1 == oldLineCount
167 && nContext
+ nAdded
== newLineCount
168 && match(buf
, last
, Patch
.SIG_FOOTER
) >= 0) {
169 // This is an extremely common occurrence of "corruption".
170 // Users add footers with their signatures after this mark,
171 // and git diff adds the git executable version number.
172 // Let it slide; the hunk otherwise looked sound.
178 if (nContext
+ nDeleted
!= oldLineCount
179 || nContext
+ nAdded
!= newLineCount
) {
180 // TODO report on truncated hunk