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.

TreeFormatter.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * Copyright (C) 2010, 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.lib;
  44. import static org.eclipse.jgit.lib.Constants.OBJECT_ID_LENGTH;
  45. import static org.eclipse.jgit.lib.Constants.OBJ_TREE;
  46. import static org.eclipse.jgit.lib.Constants.encode;
  47. import static org.eclipse.jgit.lib.FileMode.GITLINK;
  48. import static org.eclipse.jgit.lib.FileMode.REGULAR_FILE;
  49. import static org.eclipse.jgit.lib.FileMode.TREE;
  50. import java.io.IOException;
  51. import org.eclipse.jgit.errors.CorruptObjectException;
  52. import org.eclipse.jgit.internal.JGitText;
  53. import org.eclipse.jgit.revwalk.RevBlob;
  54. import org.eclipse.jgit.revwalk.RevCommit;
  55. import org.eclipse.jgit.revwalk.RevTree;
  56. import org.eclipse.jgit.treewalk.CanonicalTreeParser;
  57. import org.eclipse.jgit.util.TemporaryBuffer;
  58. /**
  59. * Mutable formatter to construct a single tree object.
  60. *
  61. * This formatter does not process subtrees. Callers must handle creating each
  62. * subtree on their own.
  63. *
  64. * To maintain good performance for bulk operations, this formatter does not
  65. * validate its input. Callers are responsible for ensuring the resulting tree
  66. * object is correctly well formed by writing entries in the correct order.
  67. */
  68. public class TreeFormatter {
  69. /**
  70. * Compute the size of a tree entry record.
  71. *
  72. * This method can be used to estimate the correct size of a tree prior to
  73. * allocating a formatter. Getting the size correct at allocation time
  74. * ensures the internal buffer is sized correctly, reducing copying.
  75. *
  76. * @param mode
  77. * the mode the entry will have.
  78. * @param nameLen
  79. * the length of the name, in bytes.
  80. * @return the length of the record.
  81. */
  82. public static int entrySize(FileMode mode, int nameLen) {
  83. return mode.copyToLength() + nameLen + OBJECT_ID_LENGTH + 2;
  84. }
  85. private byte[] buf;
  86. private int ptr;
  87. private TemporaryBuffer.Heap overflowBuffer;
  88. /**
  89. * Create an empty formatter with a default buffer size.
  90. */
  91. public TreeFormatter() {
  92. this(8192);
  93. }
  94. /**
  95. * Create an empty formatter with the specified buffer size.
  96. *
  97. * @param size
  98. * estimated size of the tree, in bytes. Callers can use
  99. * {@link #entrySize(FileMode, int)} to estimate the size of each
  100. * entry in advance of allocating the formatter.
  101. */
  102. public TreeFormatter(int size) {
  103. buf = new byte[size];
  104. }
  105. /**
  106. * Add a link to a submodule commit, mode is {@link org.eclipse.jgit.lib.FileMode#GITLINK}.
  107. *
  108. * @param name
  109. * name of the entry.
  110. * @param commit
  111. * the ObjectId to store in this entry.
  112. */
  113. public void append(String name, RevCommit commit) {
  114. append(name, GITLINK, commit);
  115. }
  116. /**
  117. * Add a subtree, mode is {@link org.eclipse.jgit.lib.FileMode#TREE}.
  118. *
  119. * @param name
  120. * name of the entry.
  121. * @param tree
  122. * the ObjectId to store in this entry.
  123. */
  124. public void append(String name, RevTree tree) {
  125. append(name, TREE, tree);
  126. }
  127. /**
  128. * Add a regular file, mode is {@link org.eclipse.jgit.lib.FileMode#REGULAR_FILE}.
  129. *
  130. * @param name
  131. * name of the entry.
  132. * @param blob
  133. * the ObjectId to store in this entry.
  134. */
  135. public void append(String name, RevBlob blob) {
  136. append(name, REGULAR_FILE, blob);
  137. }
  138. /**
  139. * Append any entry to the tree.
  140. *
  141. * @param name
  142. * name of the entry.
  143. * @param mode
  144. * mode describing the treatment of {@code id}.
  145. * @param id
  146. * the ObjectId to store in this entry.
  147. */
  148. public void append(String name, FileMode mode, AnyObjectId id) {
  149. append(encode(name), mode, id);
  150. }
  151. /**
  152. * Append any entry to the tree.
  153. *
  154. * @param name
  155. * name of the entry. The name should be UTF-8 encoded, but file
  156. * name encoding is not a well defined concept in Git.
  157. * @param mode
  158. * mode describing the treatment of {@code id}.
  159. * @param id
  160. * the ObjectId to store in this entry.
  161. */
  162. public void append(byte[] name, FileMode mode, AnyObjectId id) {
  163. append(name, 0, name.length, mode, id);
  164. }
  165. /**
  166. * Append any entry to the tree.
  167. *
  168. * @param nameBuf
  169. * buffer holding the name of the entry. The name should be UTF-8
  170. * encoded, but file name encoding is not a well defined concept
  171. * in Git.
  172. * @param namePos
  173. * first position within {@code nameBuf} of the name data.
  174. * @param nameLen
  175. * number of bytes from {@code nameBuf} to use as the name.
  176. * @param mode
  177. * mode describing the treatment of {@code id}.
  178. * @param id
  179. * the ObjectId to store in this entry.
  180. */
  181. public void append(byte[] nameBuf, int namePos, int nameLen, FileMode mode,
  182. AnyObjectId id) {
  183. append(nameBuf, namePos, nameLen, mode, id, false);
  184. }
  185. /**
  186. * Append any entry to the tree.
  187. *
  188. * @param nameBuf
  189. * buffer holding the name of the entry. The name should be UTF-8
  190. * encoded, but file name encoding is not a well defined concept
  191. * in Git.
  192. * @param namePos
  193. * first position within {@code nameBuf} of the name data.
  194. * @param nameLen
  195. * number of bytes from {@code nameBuf} to use as the name.
  196. * @param mode
  197. * mode describing the treatment of {@code id}.
  198. * @param id
  199. * the ObjectId to store in this entry.
  200. * @param allowEmptyName
  201. * allow an empty filename (creating a corrupt tree)
  202. * @since 4.6
  203. */
  204. public void append(byte[] nameBuf, int namePos, int nameLen, FileMode mode,
  205. AnyObjectId id, boolean allowEmptyName) {
  206. if (nameLen == 0 && !allowEmptyName) {
  207. throw new IllegalArgumentException(
  208. JGitText.get().invalidTreeZeroLengthName);
  209. }
  210. if (fmtBuf(nameBuf, namePos, nameLen, mode)) {
  211. id.copyRawTo(buf, ptr);
  212. ptr += OBJECT_ID_LENGTH;
  213. } else {
  214. try {
  215. fmtOverflowBuffer(nameBuf, namePos, nameLen, mode);
  216. id.copyRawTo(overflowBuffer);
  217. } catch (IOException badBuffer) {
  218. // This should never occur.
  219. throw new RuntimeException(badBuffer);
  220. }
  221. }
  222. }
  223. /**
  224. * Append any entry to the tree.
  225. *
  226. * @param nameBuf
  227. * buffer holding the name of the entry. The name should be UTF-8
  228. * encoded, but file name encoding is not a well defined concept
  229. * in Git.
  230. * @param namePos
  231. * first position within {@code nameBuf} of the name data.
  232. * @param nameLen
  233. * number of bytes from {@code nameBuf} to use as the name.
  234. * @param mode
  235. * mode describing the treatment of {@code id}.
  236. * @param idBuf
  237. * buffer holding the raw ObjectId of the entry.
  238. * @param idPos
  239. * first position within {@code idBuf} to copy the id from.
  240. */
  241. public void append(byte[] nameBuf, int namePos, int nameLen, FileMode mode,
  242. byte[] idBuf, int idPos) {
  243. if (fmtBuf(nameBuf, namePos, nameLen, mode)) {
  244. System.arraycopy(idBuf, idPos, buf, ptr, OBJECT_ID_LENGTH);
  245. ptr += OBJECT_ID_LENGTH;
  246. } else {
  247. try {
  248. fmtOverflowBuffer(nameBuf, namePos, nameLen, mode);
  249. overflowBuffer.write(idBuf, idPos, OBJECT_ID_LENGTH);
  250. } catch (IOException badBuffer) {
  251. // This should never occur.
  252. throw new RuntimeException(badBuffer);
  253. }
  254. }
  255. }
  256. private boolean fmtBuf(byte[] nameBuf, int namePos, int nameLen,
  257. FileMode mode) {
  258. if (buf == null || buf.length < ptr + entrySize(mode, nameLen))
  259. return false;
  260. mode.copyTo(buf, ptr);
  261. ptr += mode.copyToLength();
  262. buf[ptr++] = ' ';
  263. System.arraycopy(nameBuf, namePos, buf, ptr, nameLen);
  264. ptr += nameLen;
  265. buf[ptr++] = 0;
  266. return true;
  267. }
  268. private void fmtOverflowBuffer(byte[] nameBuf, int namePos, int nameLen,
  269. FileMode mode) throws IOException {
  270. if (buf != null) {
  271. overflowBuffer = new TemporaryBuffer.Heap(Integer.MAX_VALUE);
  272. overflowBuffer.write(buf, 0, ptr);
  273. buf = null;
  274. }
  275. mode.copyTo(overflowBuffer);
  276. overflowBuffer.write((byte) ' ');
  277. overflowBuffer.write(nameBuf, namePos, nameLen);
  278. overflowBuffer.write((byte) 0);
  279. }
  280. /**
  281. * Insert this tree and obtain its ObjectId.
  282. *
  283. * @param ins
  284. * the inserter to store the tree.
  285. * @return computed ObjectId of the tree
  286. * @throws java.io.IOException
  287. * the tree could not be stored.
  288. */
  289. public ObjectId insertTo(ObjectInserter ins) throws IOException {
  290. if (buf != null)
  291. return ins.insert(OBJ_TREE, buf, 0, ptr);
  292. final long len = overflowBuffer.length();
  293. return ins.insert(OBJ_TREE, len, overflowBuffer.openInputStream());
  294. }
  295. /**
  296. * Compute the ObjectId for this tree
  297. *
  298. * @param ins a {@link org.eclipse.jgit.lib.ObjectInserter} object.
  299. * @return ObjectId for this tree
  300. */
  301. public ObjectId computeId(ObjectInserter ins) {
  302. if (buf != null)
  303. return ins.idFor(OBJ_TREE, buf, 0, ptr);
  304. final long len = overflowBuffer.length();
  305. try {
  306. return ins.idFor(OBJ_TREE, len, overflowBuffer.openInputStream());
  307. } catch (IOException e) {
  308. // this should never happen
  309. throw new RuntimeException(e);
  310. }
  311. }
  312. /**
  313. * Copy this formatter's buffer into a byte array.
  314. *
  315. * This method is not efficient, as it needs to create a copy of the
  316. * internal buffer in order to supply an array of the correct size to the
  317. * caller. If the buffer is just to pass to an ObjectInserter, consider
  318. * using {@link org.eclipse.jgit.lib.ObjectInserter#insert(TreeFormatter)}
  319. * instead.
  320. *
  321. * @return a copy of this formatter's buffer.
  322. */
  323. public byte[] toByteArray() {
  324. if (buf != null) {
  325. byte[] r = new byte[ptr];
  326. System.arraycopy(buf, 0, r, 0, ptr);
  327. return r;
  328. }
  329. try {
  330. return overflowBuffer.toByteArray();
  331. } catch (IOException err) {
  332. // This should never happen, its read failure on a byte array.
  333. throw new RuntimeException(err);
  334. }
  335. }
  336. /** {@inheritDoc} */
  337. @SuppressWarnings("nls")
  338. @Override
  339. public String toString() {
  340. byte[] raw = toByteArray();
  341. CanonicalTreeParser p = new CanonicalTreeParser();
  342. p.reset(raw);
  343. StringBuilder r = new StringBuilder();
  344. r.append("Tree={");
  345. if (!p.eof()) {
  346. r.append('\n');
  347. try {
  348. new ObjectChecker().checkTree(raw);
  349. } catch (CorruptObjectException error) {
  350. r.append("*** ERROR: ").append(error.getMessage()).append("\n");
  351. r.append('\n');
  352. }
  353. }
  354. while (!p.eof()) {
  355. final FileMode mode = p.getEntryFileMode();
  356. r.append(mode);
  357. r.append(' ');
  358. r.append(Constants.typeString(mode.getObjectType()));
  359. r.append(' ');
  360. r.append(p.getEntryObjectId().name());
  361. r.append(' ');
  362. r.append(p.getEntryPathString());
  363. r.append('\n');
  364. p.next();
  365. }
  366. r.append("}");
  367. return r.toString();
  368. }
  369. }