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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. /** 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,
  166. Integer.valueOf(missingCnt),
  167. Integer.valueOf(ancestor + 1)));
  168. }
  169. }
  170. if (nContext + nAdded < newLineCount) {
  171. final int missingCount = newLineCount - (nContext + nAdded);
  172. script.error(buf, startOffset, MessageFormat.format(
  173. JGitText.get().truncatedHunkNewLinesMissing,
  174. Integer.valueOf(missingCount)));
  175. }
  176. return c;
  177. }
  178. @Override
  179. void extractFileLines(final OutputStream[] out) throws IOException {
  180. final byte[] buf = file.buf;
  181. int ptr = startOffset;
  182. int eol = nextLF(buf, ptr);
  183. if (endOffset <= eol)
  184. return;
  185. // Treat the hunk header as though it were from the ancestor,
  186. // as it may have a function header appearing after it which
  187. // was copied out of the ancestor file.
  188. //
  189. out[0].write(buf, ptr, eol - ptr);
  190. SCAN: for (ptr = eol; ptr < endOffset; ptr = eol) {
  191. eol = nextLF(buf, ptr);
  192. if (eol - ptr < old.length + 1) {
  193. // Line isn't long enough to mention the state of each
  194. // ancestor. It must be the end of the hunk.
  195. break SCAN;
  196. }
  197. switch (buf[ptr]) {
  198. case ' ':
  199. case '-':
  200. case '+':
  201. break;
  202. default:
  203. // Line can't possibly be part of this hunk; the first
  204. // ancestor information isn't recognizable.
  205. //
  206. break SCAN;
  207. }
  208. int delcnt = 0;
  209. for (int ancestor = 0; ancestor < old.length; ancestor++) {
  210. switch (buf[ptr + ancestor]) {
  211. case '-':
  212. delcnt++;
  213. out[ancestor].write(buf, ptr, eol - ptr);
  214. continue;
  215. case ' ':
  216. out[ancestor].write(buf, ptr, eol - ptr);
  217. continue;
  218. case '+':
  219. continue;
  220. default:
  221. break SCAN;
  222. }
  223. }
  224. if (delcnt < old.length) {
  225. // This line appears in the new file if it wasn't deleted
  226. // relative to all ancestors.
  227. //
  228. out[old.length].write(buf, ptr, eol - ptr);
  229. }
  230. }
  231. }
  232. void extractFileLines(final StringBuilder sb, final String[] text,
  233. final int[] offsets) {
  234. final byte[] buf = file.buf;
  235. int ptr = startOffset;
  236. int eol = nextLF(buf, ptr);
  237. if (endOffset <= eol)
  238. return;
  239. copyLine(sb, text, offsets, 0);
  240. SCAN: for (ptr = eol; ptr < endOffset; ptr = eol) {
  241. eol = nextLF(buf, ptr);
  242. if (eol - ptr < old.length + 1) {
  243. // Line isn't long enough to mention the state of each
  244. // ancestor. It must be the end of the hunk.
  245. break SCAN;
  246. }
  247. switch (buf[ptr]) {
  248. case ' ':
  249. case '-':
  250. case '+':
  251. break;
  252. default:
  253. // Line can't possibly be part of this hunk; the first
  254. // ancestor information isn't recognizable.
  255. //
  256. break SCAN;
  257. }
  258. boolean copied = false;
  259. for (int ancestor = 0; ancestor < old.length; ancestor++) {
  260. switch (buf[ptr + ancestor]) {
  261. case ' ':
  262. case '-':
  263. if (copied)
  264. skipLine(text, offsets, ancestor);
  265. else {
  266. copyLine(sb, text, offsets, ancestor);
  267. copied = true;
  268. }
  269. continue;
  270. case '+':
  271. continue;
  272. default:
  273. break SCAN;
  274. }
  275. }
  276. if (!copied) {
  277. // If none of the ancestors caused the copy then this line
  278. // must be new across the board, so it only appears in the
  279. // text of the new file.
  280. //
  281. copyLine(sb, text, offsets, old.length);
  282. }
  283. }
  284. }
  285. }