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

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