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.

CombinedFileHeader.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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.lib.Constants.encodeASCII;
  45. import static org.eclipse.jgit.util.RawParseUtils.match;
  46. import static org.eclipse.jgit.util.RawParseUtils.nextLF;
  47. import java.nio.charset.Charset;
  48. import java.util.ArrayList;
  49. import java.util.Arrays;
  50. import java.util.List;
  51. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  52. import org.eclipse.jgit.lib.FileMode;
  53. /**
  54. * A file in the Git "diff --cc" or "diff --combined" format.
  55. * <p>
  56. * A combined diff shows an n-way comparison between two or more ancestors and
  57. * the final revision. Its primary function is to perform code reviews on a
  58. * merge which introduces changes not in any ancestor.
  59. */
  60. public class CombinedFileHeader extends FileHeader {
  61. private static final byte[] MODE = encodeASCII("mode "); //$NON-NLS-1$
  62. private AbbreviatedObjectId[] oldIds;
  63. private FileMode[] oldModes;
  64. CombinedFileHeader(byte[] b, int offset) {
  65. super(b, offset);
  66. }
  67. /** {@inheritDoc} */
  68. @Override
  69. @SuppressWarnings("unchecked")
  70. public List<? extends CombinedHunkHeader> getHunks() {
  71. return (List<CombinedHunkHeader>) super.getHunks();
  72. }
  73. /**
  74. * {@inheritDoc}
  75. * <p>
  76. *
  77. * @return number of ancestor revisions mentioned in this diff.
  78. */
  79. @Override
  80. public int getParentCount() {
  81. return oldIds.length;
  82. }
  83. /**
  84. * {@inheritDoc}
  85. * <p>
  86. * @return get the file mode of the first parent.
  87. */
  88. @Override
  89. public FileMode getOldMode() {
  90. return getOldMode(0);
  91. }
  92. /**
  93. * Get the file mode of the nth ancestor
  94. *
  95. * @param nthParent
  96. * the ancestor to get the mode of
  97. * @return the mode of the requested ancestor.
  98. */
  99. public FileMode getOldMode(int nthParent) {
  100. return oldModes[nthParent];
  101. }
  102. /**
  103. * {@inheritDoc}
  104. * <p>
  105. *
  106. * @return get the object id of the first parent.
  107. */
  108. @Override
  109. public AbbreviatedObjectId getOldId() {
  110. return getOldId(0);
  111. }
  112. /**
  113. * Get the ObjectId of the nth ancestor
  114. *
  115. * @param nthParent
  116. * the ancestor to get the object id of
  117. * @return the id of the requested ancestor.
  118. */
  119. public AbbreviatedObjectId getOldId(int nthParent) {
  120. return oldIds[nthParent];
  121. }
  122. /** {@inheritDoc} */
  123. @Override
  124. public String getScriptText(Charset ocs, Charset ncs) {
  125. final Charset[] cs = new Charset[getParentCount() + 1];
  126. Arrays.fill(cs, ocs);
  127. cs[getParentCount()] = ncs;
  128. return getScriptText(cs);
  129. }
  130. /**
  131. * {@inheritDoc}
  132. * <p>
  133. * Convert the patch script for this file into a string.
  134. */
  135. @Override
  136. public String getScriptText(Charset[] charsetGuess) {
  137. return super.getScriptText(charsetGuess);
  138. }
  139. @Override
  140. int parseGitHeaders(int ptr, int end) {
  141. while (ptr < end) {
  142. final int eol = nextLF(buf, ptr);
  143. if (isHunkHdr(buf, ptr, end) >= 1) {
  144. // First hunk header; break out and parse them later.
  145. break;
  146. } else if (match(buf, ptr, OLD_NAME) >= 0) {
  147. parseOldName(ptr, eol);
  148. } else if (match(buf, ptr, NEW_NAME) >= 0) {
  149. parseNewName(ptr, eol);
  150. } else if (match(buf, ptr, INDEX) >= 0) {
  151. parseIndexLine(ptr + INDEX.length, eol);
  152. } else if (match(buf, ptr, MODE) >= 0) {
  153. parseModeLine(ptr + MODE.length, eol);
  154. } else if (match(buf, ptr, NEW_FILE_MODE) >= 0) {
  155. parseNewFileMode(ptr, eol);
  156. } else if (match(buf, ptr, DELETED_FILE_MODE) >= 0) {
  157. parseDeletedFileMode(ptr + DELETED_FILE_MODE.length, eol);
  158. } else {
  159. // Probably an empty patch (stat dirty).
  160. break;
  161. }
  162. ptr = eol;
  163. }
  164. return ptr;
  165. }
  166. /** {@inheritDoc} */
  167. @Override
  168. protected void parseIndexLine(int ptr, int eol) {
  169. // "index $asha1,$bsha1..$csha1"
  170. //
  171. final List<AbbreviatedObjectId> ids = new ArrayList<>();
  172. while (ptr < eol) {
  173. final int comma = nextLF(buf, ptr, ',');
  174. if (eol <= comma)
  175. break;
  176. ids.add(AbbreviatedObjectId.fromString(buf, ptr, comma - 1));
  177. ptr = comma;
  178. }
  179. oldIds = new AbbreviatedObjectId[ids.size() + 1];
  180. ids.toArray(oldIds);
  181. final int dot2 = nextLF(buf, ptr, '.');
  182. oldIds[ids.size()] = AbbreviatedObjectId.fromString(buf, ptr, dot2 - 1);
  183. newId = AbbreviatedObjectId.fromString(buf, dot2 + 1, eol - 1);
  184. oldModes = new FileMode[oldIds.length];
  185. }
  186. /** {@inheritDoc} */
  187. @Override
  188. protected void parseNewFileMode(int ptr, int eol) {
  189. for (int i = 0; i < oldModes.length; i++)
  190. oldModes[i] = FileMode.MISSING;
  191. super.parseNewFileMode(ptr, eol);
  192. }
  193. @Override
  194. HunkHeader newHunkHeader(int offset) {
  195. return new CombinedHunkHeader(this, offset);
  196. }
  197. private void parseModeLine(int ptr, int eol) {
  198. // "mode $amode,$bmode..$cmode"
  199. //
  200. int n = 0;
  201. while (ptr < eol) {
  202. final int comma = nextLF(buf, ptr, ',');
  203. if (eol <= comma)
  204. break;
  205. oldModes[n++] = parseFileMode(ptr, comma);
  206. ptr = comma;
  207. }
  208. final int dot2 = nextLF(buf, ptr, '.');
  209. oldModes[n] = parseFileMode(ptr, dot2);
  210. newMode = parseFileMode(dot2 + 1, eol);
  211. }
  212. private void parseDeletedFileMode(int ptr, int eol) {
  213. // "deleted file mode $amode,$bmode"
  214. //
  215. changeType = ChangeType.DELETE;
  216. int n = 0;
  217. while (ptr < eol) {
  218. final int comma = nextLF(buf, ptr, ',');
  219. if (eol <= comma)
  220. break;
  221. oldModes[n++] = parseFileMode(ptr, comma);
  222. ptr = comma;
  223. }
  224. oldModes[n] = parseFileMode(ptr, eol);
  225. newMode = FileMode.MISSING;
  226. }
  227. }