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.

CombinedHunkHeader.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Copyright (C) 2008, 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.util.RawParseUtils.nextLF;
  45. import static org.eclipse.jgit.util.RawParseUtils.parseBase10;
  46. import java.io.IOException;
  47. import java.io.OutputStream;
  48. import java.text.MessageFormat;
  49. import org.eclipse.jgit.JGitText;
  50. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  51. import org.eclipse.jgit.util.MutableInteger;
  52. /** Hunk header for a hunk appearing in a "diff --cc" style patch. */
  53. public class CombinedHunkHeader extends HunkHeader {
  54. private static abstract class CombinedOldImage extends OldImage {
  55. int nContext;
  56. }
  57. private CombinedOldImage[] old;
  58. CombinedHunkHeader(final CombinedFileHeader fh, final int offset) {
  59. super(fh, offset, null);
  60. old = new CombinedOldImage[fh.getParentCount()];
  61. for (int i = 0; i < old.length; i++) {
  62. final int imagePos = i;
  63. old[i] = new CombinedOldImage() {
  64. @Override
  65. public AbbreviatedObjectId getId() {
  66. return fh.getOldId(imagePos);
  67. }
  68. };
  69. }
  70. }
  71. @Override
  72. public CombinedFileHeader getFileHeader() {
  73. return (CombinedFileHeader) super.getFileHeader();
  74. }
  75. @Override
  76. public OldImage getOldImage() {
  77. return getOldImage(0);
  78. }
  79. /**
  80. * Get the OldImage data related to the nth ancestor
  81. *
  82. * @param nthParent
  83. * the ancestor to get the old image data of
  84. * @return image data of the requested ancestor.
  85. */
  86. public OldImage getOldImage(final int nthParent) {
  87. return old[nthParent];
  88. }
  89. @Override
  90. void parseHeader() {
  91. // Parse "@@@ -55,12 -163,13 +163,15 @@@ protected boolean"
  92. //
  93. final byte[] buf = file.buf;
  94. final MutableInteger ptr = new MutableInteger();
  95. ptr.value = nextLF(buf, startOffset, ' ');
  96. for (int n = 0; n < old.length; n++) {
  97. old[n].startLine = -parseBase10(buf, ptr.value, ptr);
  98. if (buf[ptr.value] == ',')
  99. old[n].lineCount = parseBase10(buf, ptr.value + 1, ptr);
  100. else
  101. old[n].lineCount = 1;
  102. }
  103. newStartLine = parseBase10(buf, ptr.value + 1, ptr);
  104. if (buf[ptr.value] == ',')
  105. newLineCount = parseBase10(buf, ptr.value + 1, ptr);
  106. else
  107. newLineCount = 1;
  108. }
  109. @Override
  110. int parseBody(final Patch script, final int end) {
  111. final byte[] buf = file.buf;
  112. int c = nextLF(buf, startOffset);
  113. for (final CombinedOldImage o : old) {
  114. o.nDeleted = 0;
  115. o.nAdded = 0;
  116. o.nContext = 0;
  117. }
  118. nContext = 0;
  119. int nAdded = 0;
  120. SCAN: for (int eol; c < end; c = eol) {
  121. eol = nextLF(buf, c);
  122. if (eol - c < old.length + 1) {
  123. // Line isn't long enough to mention the state of each
  124. // ancestor. It must be the end of the hunk.
  125. break SCAN;
  126. }
  127. switch (buf[c]) {
  128. case ' ':
  129. case '-':
  130. case '+':
  131. break;
  132. default:
  133. // Line can't possibly be part of this hunk; the first
  134. // ancestor information isn't recognizable.
  135. //
  136. break SCAN;
  137. }
  138. int localcontext = 0;
  139. for (int ancestor = 0; ancestor < old.length; ancestor++) {
  140. switch (buf[c + ancestor]) {
  141. case ' ':
  142. localcontext++;
  143. old[ancestor].nContext++;
  144. continue;
  145. case '-':
  146. old[ancestor].nDeleted++;
  147. continue;
  148. case '+':
  149. old[ancestor].nAdded++;
  150. nAdded++;
  151. continue;
  152. default:
  153. break SCAN;
  154. }
  155. }
  156. if (localcontext == old.length)
  157. nContext++;
  158. }
  159. for (int ancestor = 0; ancestor < old.length; ancestor++) {
  160. final CombinedOldImage o = old[ancestor];
  161. final int cmp = o.nContext + o.nDeleted;
  162. if (cmp < o.lineCount) {
  163. final int missingCnt = o.lineCount - cmp;
  164. script.error(buf, startOffset, MessageFormat.format(
  165. JGitText.get().truncatedHunkLinesMissingForAncestor, missingCnt, (ancestor + 1)));
  166. }
  167. }
  168. if (nContext + nAdded < newLineCount) {
  169. final int missingCount = newLineCount - (nContext + nAdded);
  170. script.error(buf, startOffset, MessageFormat.format(JGitText.get().truncatedHunkNewLinesMissing, missingCount));
  171. }
  172. return c;
  173. }
  174. @Override
  175. void extractFileLines(final OutputStream[] out) throws IOException {
  176. final byte[] buf = file.buf;
  177. int ptr = startOffset;
  178. int eol = nextLF(buf, ptr);
  179. if (endOffset <= eol)
  180. return;
  181. // Treat the hunk header as though it were from the ancestor,
  182. // as it may have a function header appearing after it which
  183. // was copied out of the ancestor file.
  184. //
  185. out[0].write(buf, ptr, eol - ptr);
  186. SCAN: for (ptr = eol; ptr < endOffset; ptr = eol) {
  187. eol = nextLF(buf, ptr);
  188. if (eol - ptr < old.length + 1) {
  189. // Line isn't long enough to mention the state of each
  190. // ancestor. It must be the end of the hunk.
  191. break SCAN;
  192. }
  193. switch (buf[ptr]) {
  194. case ' ':
  195. case '-':
  196. case '+':
  197. break;
  198. default:
  199. // Line can't possibly be part of this hunk; the first
  200. // ancestor information isn't recognizable.
  201. //
  202. break SCAN;
  203. }
  204. int delcnt = 0;
  205. for (int ancestor = 0; ancestor < old.length; ancestor++) {
  206. switch (buf[ptr + ancestor]) {
  207. case '-':
  208. delcnt++;
  209. out[ancestor].write(buf, ptr, eol - ptr);
  210. continue;
  211. case ' ':
  212. out[ancestor].write(buf, ptr, eol - ptr);
  213. continue;
  214. case '+':
  215. continue;
  216. default:
  217. break SCAN;
  218. }
  219. }
  220. if (delcnt < old.length) {
  221. // This line appears in the new file if it wasn't deleted
  222. // relative to all ancestors.
  223. //
  224. out[old.length].write(buf, ptr, eol - ptr);
  225. }
  226. }
  227. }
  228. void extractFileLines(final StringBuilder sb, final String[] text,
  229. final int[] offsets) {
  230. final byte[] buf = file.buf;
  231. int ptr = startOffset;
  232. int eol = nextLF(buf, ptr);
  233. if (endOffset <= eol)
  234. return;
  235. copyLine(sb, text, offsets, 0);
  236. SCAN: for (ptr = eol; ptr < endOffset; ptr = eol) {
  237. eol = nextLF(buf, ptr);
  238. if (eol - ptr < old.length + 1) {
  239. // Line isn't long enough to mention the state of each
  240. // ancestor. It must be the end of the hunk.
  241. break SCAN;
  242. }
  243. switch (buf[ptr]) {
  244. case ' ':
  245. case '-':
  246. case '+':
  247. break;
  248. default:
  249. // Line can't possibly be part of this hunk; the first
  250. // ancestor information isn't recognizable.
  251. //
  252. break SCAN;
  253. }
  254. boolean copied = false;
  255. for (int ancestor = 0; ancestor < old.length; ancestor++) {
  256. switch (buf[ptr + ancestor]) {
  257. case ' ':
  258. case '-':
  259. if (copied)
  260. skipLine(text, offsets, ancestor);
  261. else {
  262. copyLine(sb, text, offsets, ancestor);
  263. copied = true;
  264. }
  265. continue;
  266. case '+':
  267. continue;
  268. default:
  269. break SCAN;
  270. }
  271. }
  272. if (!copied) {
  273. // If none of the ancestors caused the copy then this line
  274. // must be new across the board, so it only appears in the
  275. // text of the new file.
  276. //
  277. copyLine(sb, text, offsets, old.length);
  278. }
  279. }
  280. }
  281. }